Using Hooks and Using Mouse Commands

Hello,
I have a couple of questions; first of all, is it possible to use hooks (e.g. - WH_Keyboard) in a windows forms application in VS2005? Secondly, how is it possible to move the mouse, make it click at points, drag etc...?
With Thanks,
Gal Beniamini.
[276 byte] By [GalBeniamini] at [2007-12-17]
# 1
1. It is possible to use windows hooks in .NET, an article (with C#) for this can be found here http://support.microsoft.com/default.aspx?scid=kb;EN-US;318804

2. Hooks can not be used to simulate mouse input. This has to be done by SendInput (older versions are keybd_event, mouse_event)

MartinRichter at 2007-9-9 > top of Msdn Tech,Visual C++,Visual C++ General...
# 2
Hello Martin,
Is there no way to use Windows Hooks in .Net C++...? If so then why?
Furthermore, my second question was not connected to hooks, I was just asking how was it possible to make the mouse move, click etc.
With Thanks,
Gal Beniamini.
GalBeniamini at 2007-9-9 > top of Msdn Tech,Visual C++,Visual C++ General...
# 3
Smile If you can use windows hooks from C# you can do it from managed C++ or from C++/CLI too. You just have to transfer the code sample I gave you.
I do not found/have a sample of hooks in C++.
MartinRichter at 2007-9-9 > top of Msdn Tech,Visual C++,Visual C++ General...
# 4
Hello again Martin,
I had a look at your code but I have no idea how to "transfer" the code to C++. By the way, I downloaded C# just to check how this works and it is not a global hook, it can monitor the mouse only in the form area... Is there absolutly no way the do something about this? I saw something done in Visual Studio 2003 C#:
http://www.codeproject.com/csharp/GlobalSystemHook.asp

Do you have any suggestions on how to do this is .Net 2 C++?
With Thanks,
Gal Beniamini.

GalBeniamini at 2007-9-9 > top of Msdn Tech,Visual C++,Visual C++ General...
# 5

What is your problem of transferring C# code to C++/CLI?
What kind of hook do you want when you say that it hooks only the form area?
Also the hook at codeproject shows you, what to do when you want a system wide hook!
AFAIK you didn't tell me in detail what you need.

MartinRichter at 2007-9-9 > top of Msdn Tech,Visual C++,Visual C++ General...
# 6
Hello again, Martin,
Sorry for not making my self clear but I hope this clears things up;
first of all, I just don't know how to transfer code from C# to C++...
Secondly, the hook in the given example only monitors mouse movements within the form area, but I'm looking for a system wide hook.
With Thanks,
Gal Beniamini.
GalBeniamini at 2007-9-9 > top of Msdn Tech,Visual C++,Visual C++ General...
# 7
For a systemwide mouse hook you need a a DLL that holds the MouseProc.
All this code can be written in native C++.
Because you are using C++ you can merge the native and managed code, or you can write an easy interface for the managed world.

1. What is the reason that you need a system wide mouse hook?
2. Why do you need it in the managed world?

BTW: C# is so simple an similar to C++ that it shouldn't be a problem to port some code if you use C++/CLI. As far as I understand you are using VS 2005 Beta 2.

MartinRichter at 2007-9-9 > top of Msdn Tech,Visual C++,Visual C++ General...
# 8
Hello Martin and thanks,
I still have one last question which is; which dll holds the KeyBoardProc?
I need a global hook because I want a program to have global hotkeys... and I'm doing this in the "managed world" because it's the way I like working, and I want the challenge.
With Thanks,
Gal Beniamini.
GalBeniamini at 2007-9-9 > top of Msdn Tech,Visual C++,Visual C++ General...
# 9

Your DLL holds the KeyBoardProc! You have to create your own DLL that contains the Hook! And
1. You need some kind of IPC to inform you application about the things that the hook scans!
2. Don't try to use the Framework in a hook DLL! This will load the .NET Framework into every process. This can't be a thing that you want!

So this code part must be native! The way how to inform your .NET program is free for your choice.

BUT! Why don't you use the Windows built in hotkeys? Read the docs for RegisterHotKey!

MartinRichter at 2007-9-9 > top of Msdn Tech,Visual C++,Visual C++ General...
# 10
Hello Martin,
Thanks for your help, I didn't know about RegisterHotkey, and this helps a lot.
With Thanks,
Gal Beniamini.
GalBeniamini at 2007-9-9 > top of Msdn Tech,Visual C++,Visual C++ General...