初学钩子函数;
这是dll头文件:
/************************************************************************
*DLL.h code:
************************************************************************/
#include <windows.h>
#ifdef DLL_PORT
#else
#define DLL_PORT
extern "C" _declspec(dllimport)
#endif
DLL_PORT BOOL InstallHook();
DLL_PORT BOOL UninstHook();
DLL_PORT LRESULT CALLBACK HookProc(int nCode,WPARAM wParam,LPARAM lParam);
DLL_PORT BOOL WINAPI DllMain(HANDLE hModle,DWORD reason_for_call,LPVOID reserved);
这是dll的cpp文件:
/***********************************************************************
*MyHook.cpp code:
***********************************************************************/
#include <windows.h>
#pragma data_seg("shareddata")
HHOOK hHook=NULL;
HINSTANCE hDllInstance=NULL;
#pragma data_seg()
#define DLL_PORT
extern "C" _declspec(dllexport)
#include "MyHook.h"
BOOL WINAPI DllMain(HANDLE hModle,DWORD reason_for_call,LPVOID reserved)
{
hDllInstance=(HINSTANCE)hModle;
return true;
}
LRESULT CALLBACK HookProc(int nCode,WPARAM wParam,LPARAM lParam)
{
//*******************************************************************************
//TODO: the hook proc code write here:
// ****************************************************************************
return CallNextHookEx(hHook, nCode, wParam, lParam );
}
BOOL InstallHook()
{
bool rtn=false;
hHook=SetWindowsHookEx(WH_KEYBOARD,HookProc,hDllInstance,NULL);
if(hHook!=NULL)
rtn=true;
return rtn;
}
[COLOR="red"]BOOL[/COLOR] UninstHook()
{
if(hHook!=NULL)
{
UnhookWindowsHookEx(hHook);
hHook=NULL;
}
return true;
}
这是界面程序cpp文件:
/******************************************************************
*Test.cpp code:
*****************************************************************/
#include <windows.h>
#include "resource.h"
#include "MyHook.h"
#pragma comment(lib,"MyHook.lib")
BOOL CALLBACK DialogProc(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
DialogBoxParam(hInstance,MAKEINTRESOURCE(IDD_DIALOG),NULL,DialogProc,NULL);
return 0;
}
BOOL CALLBACK DialogProc(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_COMMAND:
if(LOWORD(wParam)==IDOK)
InstallHook();
break;
case WM_INITDIALOG:
break;
case WM_CLOSE:
[COLOR="red"]UninstHook();[/COLOR]
EndDialog(hDlg,0);
break;
default:
return false;
}
return true;
}
dll能编译成功,界面程序以编译就出现如下错误:
Linking...
TheHook.obj : error LNK2001: unresolved external symbol "[COLOR="Red"]int __cdecl
UninstHook(void)" (?UninstHook@@YAHXZ)[/COLOR]
Debug/TheHook.exe : fatal error LNK1120: 1 unresolved externals
如果去掉界面程序的UninstallHook就能成功。
不知是哪里出错了?
我的dll中明明定义UninstallHook()返回值是bool类型啊,怎么错误提示返回值是int?
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!