Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the Current Windows Application

Tags:

c#

windows

I'm trying to write what I hope is a simple application tracker. That is, whenever a new application starts, or a running application becomes current, I want to note the event and start timing it's time "on top".

I have code that lists all the current applications that are running, and some code that tells me the top window (always my test console app, naturally).

What I think I'm missing is the Windows event stream to monitor or something.

I'm using .NET (C# preferred).

Any hints, tips or cheats available?

Thanks - Jonathan

like image 852
jdharley Avatar asked Dec 07 '22 08:12

jdharley


1 Answers

I think the best way to do this would be using windows "hooks" (i.e. SetWindowsHookEx). These allow you to hook in to windows core functionality, specifically there is one called WH_CALLWNDPROC which calls a user function any time any window in the system receives a message.

You could use this to globally listen for messages that bring a window to the foreground, and/or messages for user interaction (mouse, keyboard).

However, this is a raw Windows API function primarily meant for use in a C/C++ windows DLL. You can implement it in C#, but that is a can of worms you may not want to open. But opening it would probably be the best way of doing what you're asking.

like image 63
SoapBox Avatar answered Dec 28 '22 02:12

SoapBox