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?