In my program I use class Process to start another application. This application starts fullscreen. My purpose is to handle mouse click from that application in my program. What WinApi functions should I use?
By "WinApi", I assume that you mean "Win32".
In order to handle messages of another process, you need to install a Win32 hook. See this article for more details. You need the WH_MOUSE hook and the SetWindowsHookEx Win32 API.
A hook function needs to be in a DLL, so that it can be injected in any process. You will need to filter the messages you get for the process that you started.
A hook function needs to be a global function so you must code it in C++. You will also need to use some inter-process communication to well, communicate with your main program, if needed.
Related
There is a program which I can't modify it's code having been opened. I want to create a "watcher" to hook the closing event of that program in order to make the main window of the program hidden rather than being closed when users try to close that program.
I have googled it and someone says hook the API named OpenProcess would work. I tried using a C# lib called "EasyHook" and succesfully inject a remote process and hook MessageBeep API. Then I tried hooking OpenProcess and TerminateProcess, but the hooked methods of those two methods were never called.
So what is the proper method should I hook or is there any other way can realize my purpose? If there is no way to prevent the process being terminated with task manager, is there any tricks like hooking the close button of the window etc.?
PS. I don't know kernel programing of Windows and driver programing, so I want to realize it in user mode if it is possible.
I want to create a "watcher" to hook the closing event of that program in order to make the main window of the program hidden rather than being closed when users try to close that program.
There is no process close event that you can hook. However, for GUI programs, there are WM_CLOSE and WM_SYSCOMMAND|SC_CLOSE window messages that you can intercept with SetWindowsHookEx().
I have googled it and someone says hook the API named OpenProcess would work.
Whoever said that hooking OpenProcess() is the solution to this was either mistaken, or you misread what that hook was actually being used for.
I tried using a C# lib called "EasyHook" and succesfully inject a remote process and hook MessageBeep API. Then I tried hooking OpenProcess and TerminateProcess, but the hooked methods of those two methods were never called.
Of course, because you tried to hook them in the process that is being terminated, but that is not where they are called from, they are called in the process that is doing the terminating (ie, in Task Manager itself).
If there is no way to prevent the process being terminated with task manager
If the brute force "End Process (Tree)" option is used, then there is no option to catch/block that. The "End Task" option on the "Application" tab tries to perform a graceful termination using window messages before it resorts to brute force.
is there any tricks like hooking the close button of the window etc.?
See my first comment above.
What I'm trying to do: receive messages of other process window (Spy++).
For example: I opened new notepad window: http://i.stack.imgur.com/vNg6h.png
And then I moved the mouse over the notepad window, the windows will send this message to the notepad window (or to the main window child window/s):
WM_MOUSEMOVE xPos=100,yPos=200
I want to receive this message exactly when then mouse event is happening (by event or while loop) (probably while loop with Application.doEvents() and Threading.Thread.Sleep(1)).
What I have tried:
create messages listener by the ManagedSpyLib (if you will import this library you can't compile your project... and if you will succeed to import this library it's not built for this mission)
I also searched about it and this is the most relevant result: http://www.codeproject.com/Articles/3923/InterSpy-An-integrated-Windows-message-trace-and-f
but this is a c++ project and I'm trying to do it in C# or VB.NET.
Good example is better than explanation for me. :)
The hooks that you need for this are WH_CALLWNDPROC and WH_GETMESSAGE. And these require DLL injection. You cannot inject managed code and so you will need to use unmanaged code for the hooking. You can then get your unmanaged injected DLL to communicate back to your C# application, but you will have to concede defeat on your attempts to write the hooking code in C#.
You need to look at the HOOK. That's how SPY++ works.
However, .Net is not very suitable for this, because you have to use a lot of WIN32 API functions.
In my c# application I need to detect when a user clicks one of two buttons in a different, third party application.
I am able to get the Handle of the application but the MainWindowHandle returns 0.
I tried WndProc but for some reason the event will not fire in my application.
How can I get/intercept the button click event from that application into mine?
Global system hooks allow an application to intercept Windows messages intended for other applications. This has always been difficult to implement in C#. This project on Codeplex attempts to implement global system hooks by creating a DLL wrapper in C++ that posts messages to the hooking application's message queue. Put simply, this lets you implement any type of global Windows hook from managed code: http://www.codeproject.com/KB/system/WilsonSystemGlobalHooks.aspx
Let's assume that we've got 2 windows processes ,
Process A is the sender, and Process B is the receiver.
Process B is running a classic Win32 API events loop
How do I generate and send mouse and keyboard events from process A to B ?
Basically via SendMessage or PostMessage. If you want to simulate input events for the whole operating system, then SendInput might be interesting.
You may want to check TestAPI in Codeplex it includes some C# classes that wrap SendMessage and PostMessage APIs (http://testapi.codeplex.com/SourceControl/changeset/view/35517#424245)
TestApi actually wraps up SendInput internally, and exposes a couple of simple classes -- Mouse and Keyboard -- to help you simulate input. SendInput provides the most general way to inject input, but is a notoriously tricky API to use -- the wrappers simplify the usage greatly.
See Link for specific usage examples.
I am trying to track visible windows from all currently running processes. My program interacts with these windows and the faster it can detect them the better. My goal is to move visible windows to a certain location on screen before they even draw in the default position if that is possible. If not I want to move them as quickly as possible after they are created.
Right now I enumerate through the visble windows using EnumWindows (p/invoked from user32.dll) in a loop with as small a delay in between iterations as I can justify.
I am looking for a method to hook into 'something' which will allow me to wait for a 'window opening up' event to fire instead of constantly polling.
Are there any methods to achieve this?
You need the SetWindowsHookEx() API function, setting a WH_SHELL hook. The callback gets a HSHELL_WINDOWCREATED notification when a new toplevel window is created.
This is a global hook, you cannot write the code for this hook in C#. It requires a DLL that can be injected in a process, the CLR cannot be initialized properly to support managed code. You'll need an unmanaged DLL to get the job done, this project offers one.