c++ - 需要帮助创建 Windows dll

标签 c++ c windows winapi dll

我是 Windows 编程的新手(我对 C 和 C++ 知之甚少)。我正在尝试创建为键盘注册 Windows Hook 的 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

最佳答案

你的 hook dll 似乎没问题(除了你可能必须使用 CallNextHookEx 的返回值)。但是,如果我在控制台应用程序中使用它,它会挂起;如果我在 Windows 应用程序中使用它,就可以了。这可能是由于 Hook 依赖于 Windows 消息队列。

另请参阅此“C++ Console app, SetWindowsHookEx, Callback is never called” '

关于c++ - 需要帮助创建 Windows dll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5610648/

相关文章:

c - 与字符串函数相关的代码优化

c - 任何知道如何使用 C 编程实现多相滤波器的人

windows - 为什么我无法从命令提示符运行此 exe 文件?

windows - Windows 8下读取物理内存

c++ - make_shared 和 emplace 函数

c++ - 成员函数指针 - 如何做到这一点?

c++ - 用c++绘制频谱

c - 遍历文件中的行并与上一行进行比较

windows - Ruby FTP 在 Windows XP 下非常慢

c++ - 如何在 C++ 中获取 char 的最低有效位