Im not sure if you can hook CallWndProc... in fact I'm pretty sure that a lot of programs use that including the SetWindowsHook funchtion, but I'm not sure o_O... doesn't seem like a good idea.
After seeing BlackAngels little snippet on a simple keyboard hook I wanted to go a little more in depth with hooks so instead of a keyboard hook I tryed something else and it's been nothing but torture.
Compiles fine. Everytime I run it it gives me an access violation error and every single program closes and explorer.exe closes. Slow moving trial and error.Code:#include <iostream> #include <windows.h> using namespace std; HHOOK Hook; HMODULE Mod; LRESULT CALLBACK CallWndProc(int code, WPARAM wParam, LPARAM lParam) { if(code == HC_ACTION) { PCWPSTRUCT info = (PCWPSTRUCT)lParam; return CallNextHookEx(Hook,code,wParam,lParam); } } int main() { MSG msg; Mod = GetModuleHandle(NULL); if(!Mod) { return 0; } Hook = SetWindowsHookEx(WH_CALLWNDPROC,CallWndProc,Mod,0); if(!Hook) { return 0; } GetMessage(&msg,0,0,0); cin.get(); }
Not sure if i'm going in the right direction here but, I was trying to edit any messages that were being sent so that they could do something else. I'm in no hurry, this is all just for practice.
Any help is appreciated, thanks.
Im not sure if you can hook CallWndProc... in fact I'm pretty sure that a lot of programs use that including the SetWindowsHook funchtion, but I'm not sure o_O... doesn't seem like a good idea.
"Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."- Dwight D. Eisenhower
Code:HHOOK SetWindowsHookEx( int idHook, HOOKPROC lpfn, HINSTANCE hMod, DWORD dwThreadId );If I wasn't able to do that, why would MSDN include that?Code:idHook [in] Specifies the type of hook procedure to be installed. This parameter can be one of the following values. WH_CALLWNDPROC Installs a hook procedure that monitors messages before the system sends them to the destination window procedure. For more information, see the CallWndProc hook procedure.
MSDN lied to me..
SetWindowsHookEx Function()
Oh. my bad... hmmmm.. wonder what the problem. Maybe I can figure it out with zero understanding of Hooking =/
"Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."- Dwight D. Eisenhower
add a 'return 0' at the very end of your callback.
Also:
The CallWndProc hook procedure can examine the message, but it cannot modify it. After the hook procedure returns control to the system, the message is passed to the window procedure.
Tryed that already. Outcome stays the same.
Thanks, I think I found what i'm looking for but I'm still having trouble.The CallWndProc hook procedure can examine the message, but it cannot modify it. After the hook procedure returns control to the system, the message is passed to the window procedure.
GetMsgProc Function()
Not sure if that's right but, GetMsgProc is called whenever GetMessage catches a message and the callback should output "Test" everytime it catches something but nothing happens. I'm testing by pressing any button on the keyboard, I think it should be sending WM_KEYDOWN or WM_SYSKEYDOWN. Correct me if i'm wrong.Code:#include <iostream> #include <windows.h> using namespace std; HHOOK Hook; HMODULE Mod; MSG *msg; LRESULT CALLBACK Proc(int code, WPARAM wParam, LPARAM lParam) { msg = (MSG*)lParam; if(msg) { cout <<"Test"; } return CallNextHookEx(Hook,code,wParam,lParam); } int main() { Mod = GetModuleHandle(NULL); if(!Mod) { return 0; } Hook = SetWindowsHookEx(WH_GETMESSAGE,(HOOKPROC)Proc,Mod,0); if(!Hook) { return 0; } GetMessage(msg,0,0,0); cin.get(); }
Try using PeekMessage() instead of GetMessage(). GetMessage will freeze up until a message is sent. Also it requires you to do something with that message. So I imagine your freezing all the messages running through every application running on Windows.
EDIT: Oh nvm.. guess you needed GetMsgProc... my bad. >_>
Last edited by why06; 11-24-2009 at 04:11 PM.
"Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."- Dwight D. Eisenhower