【发布时间】:2011-08-02 09:51:53
【问题描述】:
我是 Windows 编程新手(我对 c 和 c++ 知之甚少)。我正在尝试创建为键盘注册 windows 挂钩的 windows dll。我正在使用 eclipse CDT 和 MinGW(因为我不想使用 Visual Studio)来创建 dll。我能够为下面的程序创建 dll(copied from here),但是当我尝试从另一个程序加载它时,它挂起并且没有任何错误消息。
#include <windows.h>
#include <iostream>
#include <stdio.h>
#include<windef.h>
#ifdef __MINGW32__
# define __in
# define __in_z
# define __in_z_opt
#endif
#define WIN32_LEAN_AND_MEAN
#include <d3d9.h>
HINSTANCE hinst;
HHOOK hhk;
LRESULT CALLBACK wireKeyboardProc(int code,WPARAM wParam,LPARAM lParam) {
FILE * fileLog = fopen("C:\\try.txt", "a+");
fprintf(fileLog,"OK");
CallNextHookEx(hhk,code,wParam,lParam);
fclose(fileLog);
return 0;
}
extern "C" __declspec(dllexport) void install() {
hhk = SetWindowsHookEx(WH_KEYBOARD, wireKeyboardProc, hinst, NULL);
}
extern "C" __declspec(dllexport) void uninstall() {
UnhookWindowsHookEx(hhk);
}
BOOL WINAPI DllMain( __in HINSTANCE hinstDLL,
__in DWORD fdwReason,
__in LPVOID lpvReserved
) {
hinst = hinstDLL;
return TRUE;
}
这是 MinGW 的问题吗?任何帮助表示赞赏。谢谢。 下面是加载dll的测试程序。
#include <iostream>
#include <stdio.h>
#include <windows.h>
#include<windef.h>
#define WIN32_LEAN_AND_MEAN
#include <d3d9.h>
int main()
{
HINSTANCE hinst = LoadLibrary("libTestHook.dll");
if (hinst == NULL)
{
printf("null hinst");
}
typedef void (*Install)();
typedef void (*Uninstall)();
Install install = (Install) GetProcAddress(hinst, "install");
Uninstall uninstall = (Uninstall) GetProcAddress(hinst, "uninstall");
install();
int foo;
std::cin >> foo;
uninstall();
return 0;
}
libTestHook.dll 是创建的dll
【问题讨论】: