You need to write a global hook on the keyboard and mouse with shared memory. I use Visual Studio 2015. After reading and using a large number of manuals - not one did not work. Currently created a dll project. It added two files main.cpp main.def

dll main.cpp

__declspec(dllexport) LRESULT CALLBACK keyboardHook(int code, WPARAM wParam, LPARAM lParam) { MessageBox(NULL, TEXT("Heey"), TEXT("qwe"), MB_OK); return CallNextHookEx(0, code, wParam, lParam); } 

dll main.def

 LIBRARY "hookDll" EXPORTS keyboardHook 

exe main.cpp

 #include <iostream> #include <Windows.h> using namespace std; int main() { HHOOK hHook; HINSTANCE hInstDll; HOOKPROC hookProc; hInstDll = LoadLibrary(TEXT("moreDll.dll")); hookProc = (HOOKPROC)GetProcAddress(hInstDll, "keyboardHook"); SetWindowsHookEx(WH_KEYBOARD, hookProc, hInstDll, 0); getchar(); getchar(); UnhookWindowsHookEx(hHook); FreeLibrary(hInstDll); cout << endl << endl; return 0; } 

The dll is loading, the fact is that the dll project was named MoreDll, and when building, I get MoreDll.dll. But I'm in GetProcAddress() I get error 127 - this procedure could not be found. I read about mangling, but in my opinion in my case it should not be.

Below is the code from the dll that I would like to use, but I donโ€™t understand how to interact with it from the .exe, and how to correctly compile it into a dll.

 // // some data will be shared across all // instances of the DLL // #pragma comment(linker, "/SECTION:.SHARED,RWS") #pragma data_seg(".SHARED") int iKeyCount = 0; HHOOK hKeyboardHook = 0; HHOOK hMouseHook = 0; #pragma data_seg() // // instance specific data // HMODULE hInstance = 0; // // DLL load/unload entry point // BOOL APIENTRY DllMain(HANDLE hModule, DWORD dwReason, LPVOID lpReserved) { switch (dwReason) { case DLL_PROCESS_ATTACH : hInstance = (HINSTANCE) hModule; break; case DLL_THREAD_ATTACH : break; case DLL_THREAD_DETACH : break; case DLL_PROCESS_DETACH : break; } return TRUE; } // // keyboard hook // LRESULT CALLBACK KeyboardProc(int code, // hook code WPARAM wParam, // virtual-key code LPARAM lParam) // keystroke-message information { if ((lParam & 0x80000000) != 0) { ++iKeyCount; } return CallNextHookEx(hKeyboardHook, code, wParam, lParam); } // // mouse hook // LRESULT CALLBACK MouseProc(int code, // hook code WPARAM wParam, // message identifier LPARAM lParam) // mouse coordinates { switch (wParam) { case WM_LBUTTONDOWN : case WM_MBUTTONDOWN : case WM_RBUTTONDOWN : case WM_LBUTTONDBLCLK : case WM_MBUTTONDBLCLK : case WM_RBUTTONDBLCLK : ++iKeyCount; break; } return CallNextHookEx(hMouseHook, code, wParam, lParam); } // // install keyboard/mouse hooks // void KBM_API InstallHooks(void) { hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, hInstance, 0); hMouseHook = SetWindowsHookEx(WH_MOUSE, MouseProc, hInstance, 0); } // // remove keyboard/mouse hooks // void KBM_API RemoveHooks(void) { UnhookWindowsHookEx(hKeyboardHook); UnhookWindowsHookEx(hMouseHook); hKeyboardHook = hMouseHook = 0; } // // retrieve number of keystrokes // int KBM_API FetchKeyCount(bool bClear) { int kc = iKeyCount; if (bClear) iKeyCount = 0; return kc; } 
  • 2
    Using Dependency Walker, look at the name of the function exported from the DLL. - ฿Š฿š฿ค฿˜
  • 2
    It seems to me that in the definition of functions you need to use extern "C" - Mikalai Ramanovich
  • @Arhad Thanks, help, DependencyWalker showed that the name is _keyboardHook@12 . Added extern "C" and function without extern "C". _keyboardHook@12 where the C language is, and for another C ++ language such a name ?procName@@YAHHH@Z I also showed that I could not find some modules, but the hook worked) - Nikita
  • _keyboardHook@12 is the decoration of the stdcall calling convention names inherent in the Windows API. The corresponding keyword __stdcall declared in the CALLBACK macro. - ฿Š฿š฿ค฿˜
  • And yes, try removing __declspec(dllexport) . It may have a higher priority than undeclared ads in a .def file. - ฿Š฿š฿ค฿˜

0