简体   繁体   English

需要帮助创建Windows dll

[英]need help creating windows dll

I am new to windows programming(I know little bit about c and c++). 我是Windows编程的新手(我对c和c ++不太了解)。 I am trying to create windows dll which registers windows hook for keyboard.I am using eclipse CDT with MinGW(as I dont want to use Visual Studio) to create dll.I was able to create dll for below program( copied from here ), but when I try to load it from another program,it hangs with out any error message. 我正在尝试创建用于注册用于键盘的Windows钩子的Windows dll。我正在使用带有MinGW的Eclipse CDT(因为我不想使用Visual Studio)来创建dll。我能够为以下程序创建dll( 从此处复制 ),但是当我尝试从另一个程序加载它时,它挂起,没有任何错误消息。

#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;
}

Is this a problem with MinGW? 这是MinGW的问题吗? Any Help is appreciated.Thank you. 感谢您的帮助。谢谢。 Below is the test program that loads dll. 下面是加载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 is the created dll libTestHook.dll是创建的dll

Your hook dll seems to be alright (except that you must probably use the return value of CallNextHookEx). 您的钩子dll似乎还不错(除了您可能必须使用CallNextHookEx的返回值)。 However if I use this in a console application it hangs; 但是,如果我在控制台应用程序中使用它,它将挂起; if I use it in a windows application it is ok. 如果我在Windows应用程序中使用它就可以了。 This may be due to the fact that hooking is dependent on a windows message queue. 这可能是由于挂钩取决于Windows消息队列这一事实。

See also this ' C++ Console app, SetWindowsHookEx, Callback is never called ' 另请参见此C ++控制台应用程序,SetWindowsHookEx,从不调用回调

I don't think it hangs . 我不认为它挂了 What a keyboard hook generally does is process a "main event loop" until the program is closed. 键盘挂钩通常会执行“主事件循环”,直到程序关闭。 In this case, I'd assume that this is exactly what's going on. 在这种情况下,我认为这是什么回事。 Your program is running, calling the required routine from the dll, and then the dll is continually processing events. 您的程序正在运行,从dll调用所需的例程,然后dll不断处理事件。
That being said, windows seems to do something strange with terminals and mingw. 话虽如此,窗户似乎与终端和mingw做一些奇怪的事情。 More specifically, it doesn't play nice, like it does when using MSVS. 更具体地说,它的运行效果不佳,就像使用MSVS时一样。 Perhaps this might also be the cause of your problems - MinGW compiles things more or less like gcc does, however MSVS's cl puts strange declarations in to open terminals and print to them, etc. etc. 也许这也可能是您遇到问题的原因-MinGW像gcc一样或多或少地编译了东西,但是MSVS的cl在打开终端并打印到它们的过程中添加了奇怪的声明,等等。

Of course, note that at some point you need to call the install() and the uninstall() functions in your code - which I'm assuming you've already done. 当然,请注意,有时需要在代码中调用install()uninstall()函数-我假设您已经完成了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM