API basics

This is a discussion on API basics within the Windows Programming forums, part of the Platform Specific Boards category; I understand that there is: Code: int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) which is the main ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    8

    API basics

    I understand that there is:

    Code:
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    which is the main function for the window.

    Then there is:
    Code:
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    which does the main processing for the window and that the WinMain function calls this function in the "DispatchMessage(&Msg);" call but how do I create a variable in the main function so that it can be accessed in the WndProc function?

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>how do I create a variable in the main function so that it can be accessed in the WndProc function?<<

    1. Quick and dirty: use a global variable. Sometimes it might be more convenient to declare static variables in your window procedure.

    2. Using c++ would require that you wrap up the functionality of your wndproc by making it a class static function - search this board as there's been lots of discussion about this before. Then you would declare/instantiate an object of your 'window' or 'application' c++ class within WinMain; windows will see to it that your messages are sent to the window procedure and you can use c++ class scope variables to your heart's content - once you have obtained a ptr to your class object in your (static) window procedure.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    169
    In WinMain() use the window handle and post your self a message using PostMessage(), then process the message in WndProc(). The following code shows the basic structure.
    Code:
    #define WM_MY_MESSAGE1 WM_USER +1
    #define WM_MY_MESSAGE2 WM_USER +2
    #define WM_MY_MESSAGE3 WM_USER +3
    struct My_Struct_A
    {
         int a;     int b;     int c;
    };
    
    int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
        ...
         ...
         hWnd = CreateWindow(...); 
         ...
         ...
         char strHello[] = "hello";
         PostMessage( hWnd, WM_MY_MESSAGE1 ,0, (LPARAM )strHello );
    
         int iValue = 12345; 
         PostMessage( hWnd, WM_MY_MESSAGE2 ,0, (LPARAM )iValue );
    
         struct My_Struct_A *pMySA = (struct My_Struct_A *) malloc(sizeof(struct My_Struct_A));
         ...
         pMySA->a = 1234;
         ...
         PostMessage( hWnd, WM_MY_MESSAGE2 ,0, (LPARAM )pMySA );
    
         // Main message loop:
         while (GetMessage(&msg, NULL, 0, 0)) 
         {
              if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
              {
                   TranslateMessage(&msg);
                   DispatchMessage(&msg);
              }
         }
    
         return msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
         ...
         ...
         int iNumber;
         char *strText;
         struct My_Struct_A *pMySA;
    
         switch (message) 
         {
              case WM_MY_MESSAGE1:
                   strText = (char*) lParam;
                   break;
    
              case WM_MY_MESSAGE2:
                   iNumber = (int) lParam;
                   break;
    
              case WM_MY_MESSAGE3:
                   pMySA = (struct My_Struct_A *) lParam;
                   break;
                   ...
                   ...
                   ...
                   ...
    
              default:
                   return DefWindowProc(hWnd, message, wParam, lParam);
       }
       return 0;
    }

  4. #4
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    169
    OR there's another way. You could utilize the window long to store a pointer to allocated memory. This pointer will always be availiable from the main window procedure or from where the window handle is availiable. Allocate memory then set the pointer using SetWindowLong(), then get the pointer using GetWindowLong(), remembering to free the memory when the window's destroyed. The following code shows the basic code struture.

    Code:
    #define WM_MY_MESSAGE4 WM_USER +4
    int APIENTRY WinMain(...)
    {
         ...
         ...
         struct My_Struct_A *pMySA_2 = (struct My_Struct_A *) malloc(sizeof(struct My_Struct_A));
    
         pMySA_2->a = 1234;
         ...  
         SetWindowLong( hWnd, GWL_USERDATA, (long)pMySA_2);
    
         PostMessage( hWnd, WM_MY_MESSAGE4 ,0, 0 );     
         ...
         ...
         ...     
    }
    
    LRESULT CALLBACK WndProc(...)
    {
         struct My_Struct_A *pMySA;
    
         switch (message) 
         {
              case WM_MY_MESSAGE4:
                   pMySA = (struct My_Struct_A *) GetWindowLong(hWnd, GWL_USERDATA);
                   ccc = pMySA->a;
                   break;
              ...
              ...                    
    
              case WM_DESTROY:
                   pMySA = (struct My_Struct_A *) GetWindowLong(hWnd, GWL_USERDATA);
                   if(pMySA)
                       free(pMySA);
                   break;
              ...
              ...
         } 
    }
    Last edited by Scarlet7; 03-16-2003 at 01:57 PM.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    8
    Thanks heaps Scarlet7. That did the trick. Why are structures used? I have done some c programming but have done mostly c++ and java. Why don't u use classes?

  6. #6
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    169
    >Why are structures used?
    It's up to the programmer, you can point to what ever you like, including a class. I just used the structure as an example.
    Code:
    MyClass *my_class;
    
    my_class = new MyClass();
    
    SetWindowLong( hWnd, GWL_USERDATA, (long)my_class);
    
    ...
    
    my_class = (MyClass *) GetWindowLong(hWnd, GWL_USERDATA);
    Cheers...
    Last edited by Scarlet7; 03-17-2003 at 03:13 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Want to learn Windows API for Game Programming
    By George M. in forum Windows Programming
    Replies: 15
    Last Post: 09-28-2008, 11:26 AM
  2. Win Api Basics...
    By Devil Panther in forum Windows Programming
    Replies: 19
    Last Post: 09-09-2004, 12:28 PM
  3. OpenSSL and Win32 SSL API :: SSL/TLS
    By kuphryn in forum Networking/Device Communication
    Replies: 0
    Last Post: 03-10-2004, 07:46 PM
  4. FILES in WinAPI
    By Garfield in forum Windows Programming
    Replies: 46
    Last Post: 10-02-2003, 07:51 PM
  5. pthread api vs win32 thread api
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 11-20-2001, 08:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21