Hello, I am trying to get the following code to work, but the HookProc is never called. Can anyone please help me, I can provide the project files or the rest of the project which I am using to call the code if absolutely necessary.
I havent done much work (infact you could say none) with .dll files however,
case DLL_PROCESS_ATTACH:
case DLL_PROCESS_DETACH:
UnhookWindowsHookEx(KeyHook);
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
UnhookWindowsHookEx(KeyHook);
break;
doesnt look right as far as i (dont) know :P
When a process or thread attaches its going to call UnhookWindowsHookEx() this doesnt seem right...
Also if the code above is right then why not go
case DLL_PROCESS_ATTACH:
case DLL_PROCESS_DETACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
UnhookWindowsHookEx(KeyHook);
break;
??
I would assume you would be wanting somthing more like
case DLL_PROCESS_ATTACH:
// call hook window here, or do nothing
break; // <-------------!!!
case DLL_PROCESS_DETACH:
UnhookWindowsHookEx(KeyHook);
break; // <-------------!!!
case DLL_THREAD_ATTACH:
// call hook window here, or do nothing
break; // <-------------!!!
case DLL_THREAD_DETACH:
UnhookWindowsHookEx(KeyHook);
break;
Hope this helps
Chazwazza
February 15th, 2007, 04:20 PM
Sorry, I've tried your suggestions but the HookProc is still never being called. There must be something wrong, I just can't see it. Hopefully someone can find my mistake.
Thanks.
UnfitElf
February 15th, 2007, 04:39 PM
Could you also post the code that shows where you are calling the InstallHook function.
Thanks
Chazwazza
February 15th, 2007, 08:15 PM
This is the related parts (it's a large project), basically I create a window, then send the Hwnd of the window to the DLL when I install the hook, so it can use it to send messages back to the original window for processing (But the HookProc never gets called so this point).
// Create a message only window
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
/* If creating the handle doesn't work, return with a failure message */
if (!hWnd)
{
return FALSE;
}
// Show the created window
ShowWindow(hWnd,SW_HIDE);
// Set the window's handle to our global variable
HwndMe = hWnd;
// Return with a success message
return TRUE;
}
/* Catch the messages sent to the window */
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
// If a keystroke message is recieved
case WM_KEYSTROKE:
// Call the function to process the keystrokes
ProcessKeys(wParam, lParam);
return TRUE;
break;
// If the program is being closed, then destroy the program's window
case WM_CLOSE:
DestroyWindow(hwnd);
break;
// When the window is destroyed, post a quit message to close the program
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
// Calls the default window procedure to provide default processing for any window messages that an application does not process
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
UnfitElf
February 16th, 2007, 12:14 AM
ok because ive never actually done a Dll myself before, ive only seen about them, i thought it was time to give it a simple try.
Ive got it calling 2 functions, InstallHook and RemoveHook. Note that both these function only display MessageBoxes, they do NOT actuaully attempt to install a hook. Ill leave that part to you.
Here is the sorce:
main.cpp (the window)
//---------------------------------------------------------------------------------
// DLL example. i.e. calling a function from a .dll
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
//HWND hwnd;
MSG messages; // Here messages to the application are saved
WNDCLASSEX wincl; // Data structure for the windowclass
// The Window structure
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szWindowClass;
wincl.lpfnWndProc = WindowProcedure; // This function is called by windows
wincl.style = CS_DBLCLKS; // Catch double-clicks
wincl.cbSize = sizeof (WNDCLASSEX);
// Use default icon and mouse-pointer
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; // No menu
wincl.cbClsExtra = 0; // No extra bytes after the window class
wincl.cbWndExtra = 0; // structure or the window instance
// Use Windows's default color as the background of the window
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
// Register the window class, and if it fails quit the program
if (!RegisterClassEx (&wincl))
return 0;
// The class is registered, let's create the program
HwndMe = CreateWindowEx (
0, // Extended possibilites for variation
szWindowClass, // Classname
szTitle, // Title Text
WS_OVERLAPPEDWINDOW, // default window
CW_USEDEFAULT, // Windows decides the position
CW_USEDEFAULT, // where the window ends up on the screen
300, // The programs width
300, // and height in pixels
HWND_DESKTOP, // The window is a child-window to desktop
NULL, // No menu
hThisInstance, // Program Instance handler
NULL // No Window Creation data
);
hInst = hThisInstance;
// Make the window visible on the screen
ShowWindow (HwndMe, nFunsterStil);
InstallHook(HwndMe);
// Run the message loop. It will run until GetMessage() returns 0
while (GetMessage (&messages, NULL, 0, 0))
{
// Translate virtual-key messages into character messages
TranslateMessage(&messages);
// Send message to WindowProcedure
DispatchMessage(&messages);
}
// The program return-value is 0 - The value that PostQuitMessage() gave
return (int)messages.wParam;
}
//------------------------------------------------------------------------------
// Windows proc
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
break;
case WM_CLOSE:
case WM_ENDSESSION:
RemoveHook();
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
If you sre static linking remeber to include the .lib file that is generated when you compile the .dll.
You should be able to work off this by throwing you hook code into the correct functions (InstallHook, RemoveHook).
Any more questions feel free to ask. :)
If the post helped please rate!
Chazwazza
February 16th, 2007, 04:42 AM
No you see, I can call the functions in the DLL without hassle, and I can display message boxes etc. But the actual problem is with the hooks, not with the DLL. The DLL seems to be working, as I can add code to call a MessageBox in the InstallHook funciton, and it works. The problem is the hook not being set properly.
Thanks.
UnfitElf
February 16th, 2007, 06:58 AM
Haha, ok, sorry i misunderstood your question. Its been a very long day and im proably not as sharp as i should be.
I do not have time to reply now, but i will post back 2moro after work.
Chazwazza
February 17th, 2007, 01:47 AM
OK thanks, I look forward to hearing from you, it's hard to continue with writing this program when I can't work out how to get one of the major features to work. :P
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
//HWND hwnd;
MSG messages; // Here messages to the application are saved
WNDCLASSEX wincl; // Data structure for the windowclass
// The Window structure
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szWindowClass;
wincl.lpfnWndProc = WindowProcedure; // This function is called by windows
wincl.style = CS_DBLCLKS; // Catch double-clicks
wincl.cbSize = sizeof (WNDCLASSEX);
// Use default icon and mouse-pointer
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; // No menu
wincl.cbClsExtra = 0; // No extra bytes after the window class
wincl.cbWndExtra = 0; // structure or the window instance
// Use Windows's default color as the background of the window
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
// Register the window class, and if it fails quit the program
if (!RegisterClassEx (&wincl))
return 0;
// The class is registered, let's create the program
HwndMe = CreateWindowEx (
0, // Extended possibilites for variation
szWindowClass, // Classname
szTitle, // Title Text
WS_OVERLAPPEDWINDOW, // default window
CW_USEDEFAULT, // Windows decides the position
CW_USEDEFAULT, // where the window ends up on the screen
300, // The programs width
300, // and height in pixels
HWND_DESKTOP, // The window is a child-window to desktop
NULL, // No menu
hThisInstance, // Program Instance handler
NULL // No Window Creation data
);
hInst = hThisInstance;
// Make the window visible on the screen
ShowWindow (HwndMe, nFunsterStil);
InstallHook(HwndMe);
// Run the message loop. It will run until GetMessage() returns 0
while (GetMessage (&messages, NULL, 0, 0))
{
// Translate virtual-key messages into character messages
TranslateMessage(&messages);
// Send message to WindowProcedure
DispatchMessage(&messages);
}
// The program return-value is 0 - The value that PostQuitMessage() gave
return (int)messages.wParam;
}
//------------------------------------------------------------------------------
// Windows proc
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_USER+755:
MessageBeep(MB_OK);
break;
case WM_CREATE:
break;
case WM_CLOSE:
case WM_ENDSESSION:
RemoveHook();
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}