I'ts been a long time. How are you CornedBee?
I have been programming in C# for a while now and now am forced to make a C++ dll to set up a global keyboard hook for a clipboard program I am making.
My code never enters DllMain!! Is that ok?
It works but how do I notify my C# app that the callback was called and get which key was pressed?Code:#include <windows.h> #include "Global Hook.h" #include <stdio.h> #pragma data_seg(".shared") #pragma data_seg() //extern "C" tells compiler not to change the name of the //function so that it can be called directly from another program. extern "C" _declspec(dllexport) HHOOK SetHook(); extern "C" _declspec(dllexport) void Unhook(); _declspec(dllexport) int _WEP (int bSystemExit); HHOOK hKeyhook=0; HINSTANCE hDll = LoadLibrary((LPCTSTR)"WinHook.dll"); //--------------------------- BOOL APIENTRY DllMain ( HINSTANCE hInst /* Library instance handle. */ , DWORD reason /* Reason this function is being called. */ , LPVOID reserved /* Not used. */ ) { if ( reason == DLL_PROCESS_ATTACH) hDll = hInst; MessageBox(NULL, TEXT("ENTERED DLLMAIN"), TEXT(""), MB_OK); //return (int)(DisableThreadLibraryCalls (hDll)); /* Returns TRUE on success, FALSE on failure */ return TRUE; } //--------------------------- LRESULT CALLBACK KeyBoardProc(int nCode, WPARAM wParam, LPARAM lParam) { // This is where you want to decide what to do when receiving keyboard parameters. MessageBox(NULL, TEXT("In CallBack"), TEXT("KeyPress"), MB_OK); return CallNextHookEx(hKeyhook, nCode, wParam, lParam); //CallNextHookEx returns long value. } //--------------------------- extern "C" _declspec(dllexport) HHOOK SetHook() { if(hKeyhook == NULL) { hKeyhook = SetWindowsHookEx(WH_KEYBOARD, KeyBoardProc, hDll, 0); if (hKeyhook == NULL) MessageBox(NULL, TEXT("Failed"), TEXT("Failed"), MB_OK); else MessageBox(NULL, TEXT("Hooked!!!"), TEXT("Success!!!"), MB_OK); } return hKeyhook; }




Reply With Quote