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; }
_keyboardHook@12. Added extern "C" and function without extern "C"._keyboardHook@12where the C language is, and for another C ++ language such a name?procName@@YAHHH@ZI also showed that I could not find some modules, but the hook worked) - Nikita_keyboardHook@12is the decoration of the stdcall calling convention names inherent in the Windows API. The corresponding keyword__stdcalldeclared in theCALLBACKmacro. - ฿฿฿ค฿__declspec(dllexport). It may have a higher priority than undeclared ads in a.deffile. - ฿฿฿ค฿