Thread: DLL variables scope

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    35

    DLL variables scope

    I'm writing a DLL in C. This is the dllmain.c code:
    Code:
    #include "dll.h"
    
    char* szClassName = "hidden window class name";
    char* szWndName   = "hidden window window name";
    
    HINSTANCE g_hinstance  = NULL;
    HWND      g_hWnd       = NULL;
    DWORD     g_dwThreadId;
    
    DWORD WINAPI ThreadProc (LPVOID lpParameter);
    LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
    
    DLLIMPORT void HelloWorld()
    {
        CreateThread
        (
            NULL, 
            0,
            ThreadProc,
            NULL,
            0,
            &g_dwThreadId
        );
    
    }
    
    DWORD WINAPI ThreadProc(LPVOID lpParameter)
    {
        WNDCLASSEX wincl;
        MSG        messages;
        
        wincl.hInstance = g_hinstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProcedure;
        wincl.style = CS_HREDRAW | CS_VREDRAW;
        wincl.cbSize = sizeof (WNDCLASSEX);
        
        wincl.hIcon = NULL;
        wincl.hIconSm = NULL;
        wincl.hCursor = NULL;
        wincl.lpszMenuName = NULL;
        wincl.cbClsExtra = 0;
        wincl.cbWndExtra = 0;
        wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
        
        if (!RegisterClassEx (&wincl)) return 0;
        
        g_hWnd = CreateWindowEx
        (
            0,
            szClassName,
            szWndName,
            WS_OVERLAPPEDWINDOW,
            0,
            0,
            100,
            100,
            NULL,
            NULL,
            g_hinstance,
            NULL 
        );
        
        ShowWindow(g_hWnd, SW_HIDE);
        
        while (GetMessage (&messages, NULL, 0, 0)) {
            TranslateMessage(&messages);
            DispatchMessage(&messages);
        }        
    
        UnregisterClass(szClassName, g_hinstance);
        
        return 0;    
    }
    
    LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)
        {
            case WM_CREATE:
            {
                break;
            }
            /* other messages processing */
            case WM_DESTROY:
            {
                PostQuitMessage(0);
                break;
            }
            default:
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
        return 0;
    }
    
    
    BOOL APIENTRY DllMain (HINSTANCE hInst, DWORD reason, LPVOID reserved)
    {
        switch (reason)
        {
            case DLL_PROCESS_ATTACH:
            {
                g_hinstance = (HINSTANCE)hInst;
                break;
            }    
            case DLL_PROCESS_DETACH:
                break;
            case DLL_THREAD_ATTACH:
                break;
            case DLL_THREAD_DETACH:
                break;
        }
        return TRUE;
    }
    This is the dll.h code:

    Code:
    #ifndef _DLL_H_
    #define _DLL_H_
    
    #if BUILDING_DLL
    # define DLLIMPORT __declspec (dllexport)
    #else /* Not BUILDING_DLL */
    # define DLLIMPORT __declspec (dllimport)
    #endif /* Not BUILDING_DLL */
    
    
    DLLIMPORT void DoSomething (void);
    
    #endif /* _DLL_H_ */
    If I check the value of the global variable called g_hWnd in the WindowProcedure, when the WM_CREATE occurs, I always get NULL. What is problem? Whar is the real scope of the global variables? Can't WindowsProcedure access to the global variables because it simply can't or just because the window has been created in a different thread?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    CreateWindow[Ex]() will dispatch several messages to the window procedure before returning from the call.......which is when g_hwnd is assigned.

    gg

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    you should initialize g_hwnd in the WM_CREATE message handler.

    Code:
            case WM_CREATE:
            {
                g_hwnd = hwnd;
                break;
            }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  3. How to Free a Load-Time DLL?
    By Ktulu in forum Windows Programming
    Replies: 7
    Last Post: 11-16-2006, 05:43 PM
  4. Error C2664 - Trying to call an external Dll
    By jamez05 in forum C++ Programming
    Replies: 3
    Last Post: 08-08-2006, 06:07 AM
  5. Calling a VB DLL w/ forms from C++ DLL
    By UW_COOP in forum C++ Programming
    Replies: 8
    Last Post: 06-30-2003, 08:04 AM