none
为什么我写的全局钩子不起作用的? RRS feed

  • 问题

  • #include <windows.h>
    
    //声明者函数是来自 动态链接库 的
    extern  _declspec(dllimport) void SetHook(HWND hwnd);
    
    #define CLSNAME "CapitalTips"
    #define WNDNAME "超级演示"
    
    HWND hwnd;
    
    LRESULT CALLBACK WindowProc(HWND hwnd,
    							UINT uMsg,
    							WPARAM wParam,
    							LPARAM lParam
    							);
    
    int WINAPI WinMain (HINSTANCE hInstance,
    					HINSTANCE hPrevInstance,
    					PSTR szCmdLine, 
    					int iCmdShow)
    {
    	static TCHAR clsname[] = TEXT(CLSNAME);
    	WNDCLASS wndclass;
    	wndclass.style = CS_HREDRAW | CS_VREDRAW ;
    	wndclass.lpfnWndProc = WindowProc ;
    	wndclass.cbClsExtra = 0 ;
    	wndclass.cbWndExtra = 0 ;
    	wndclass.hInstance = hInstance ;
    	wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
    	wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
    	wndclass.hbrBackground = (HBRUSH) ::GetStockObject(WHITE_BRUSH);
    	wndclass.lpszMenuName = NULL ;
    	wndclass.lpszClassName = clsname ;
    
    	if(!::RegisterClass(&wndclass))
    	{
    		MessageBox ( NULL, 
    			TEXT ("This program requires Windows NT!"),
    			clsname, 
    			MB_ICONERROR) ;
    		return 0 ;
    	}
    
    	hwnd = ::CreateWindow(clsname,TEXT(WNDNAME),
    		WS_OVERLAPPEDWINDOW,
    		CW_USEDEFAULT,CW_USEDEFAULT,
    		CW_USEDEFAULT,CW_USEDEFAULT,
    		NULL,NULL,hInstance,NULL);
    
    	::ShowWindow(hwnd,SW_SHOW);
    	::UpdateWindow(hwnd);
    
    	MSG msg;
    	while(::GetMessage(&msg,NULL,0,0)){
    		::TranslateMessage(&msg);
    		::DispatchMessage(&msg);
    	}
    	return msg.wParam;
    }
    
    LRESULT CALLBACK WindowProc(HWND hwnd,
    							UINT uMsg,
    							WPARAM wParam,
    							LPARAM lParam
    							)
    {
    	HDC hdc;
    	PAINTSTRUCT ps;
    	RECT rect;
    	int cySysTaskbar;
    	short i;
    
    	switch(uMsg)
    	{
    	case WM_CREATE:
    		//启动hook
    		SetHook(hwnd);
    		return 0;
    
    	case WM_DESTROY:
    		::PostQuitMessage(0);
    		return 0;
    
    	case WM_PAINT:
    		hdc = ::BeginPaint(hwnd,&ps);
    		::EndPaint(hwnd,&ps);
    		return 0;
    	}
    	return ::DefWindowProc(hwnd,uMsg,wParam,lParam);
    }
    
    #include<windows.h>
    
    //新建一个数据集
    #pragma data_seg("mydata")
    HWND hwnddll = NULL;//必须初始化
    #pragma data_seg()
    
    //并在此处声明为read write shared
    #pragma comment(linker,"/SECTION:mydata,RWS")
    
    #define WM_USERDEF (WM_USER+1)
    HINSTANCE hInstance = NULL;
    HHOOK hHook;
    
    
    BOOL WINAPI DllMain(
        HINSTANCE hDllHandle, 
        DWORD     nReason,    
        LPVOID    Reserved    
    )
    {
    	hInstance = hDllHandle;
    	return TRUE;
    }
    
    LRESULT CALLBACK KeyboardProc(          int code,
        WPARAM wParam,
        LPARAM lParam
    )
    {
    	if(wParam == VK_F2)
    	{
    		::SendMessage(hwnddll,WM_CLOSE,0,0);
    		::UnhookWindowsHookEx(hHook);
    		return 1;
    	}
    	return ::CallNextHookEx(hHook,code,wParam,lParam);
    
    }
    
    void SetHook(HWND hwnd)
    {
    	hwnddll = hwnd;
    	hHook = ::SetWindowsHookEx(WH_KEYBOARD ,
    		KeyboardProc,hInstance,0);
    }
    

    前一个代码是在主程序的,后一个是我的dll

    我是一步一步来的呀,就是没有全局的效果,只有当前焦点窗口才能作用,然后如果转到其他的窗口就不起作用了????

    求高手解释

    2011年10月20日 11:31

答案

全部回复