[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Monitoring application launch

Posted on 2009-02-22
11
Medium Priority
?
1,073 Views
Last Modified: 2012-05-06
How do I detect when any application is launched in Windows using C#?
0
Comment
Question by:casualkid
9 Comments
 
LVL 29

Expert Comment

by:fosiul01
ID: 23709211
HI good moring

is this linux base question or Windows base question ??

in linux you can use

top command to see what programm or service is running on the system
0
 
LVL 16

Expert Comment

by:ai_ja_nai
ID: 23709771
This question should be moved in C#.NET and Windows sections.

Anyway, the response is:
the normal Windows way to detect a program starting is via a hook (see the MSDNs entry for SetWindowsHookEx() for details). A Shell hook or a CBT hook would do exactly what youre looking for. This Knowledge Base article discusses the use of hooks in C#:
http://support.microsoft.com/kb/318804
However, as this article itself mentions, you cant install a global hook with .NET. This means the Shell and CBT hooks are out of your grasp! If you want to use the "normal" detection method, youll need to code a native DLL with a little function in it that sends a message back to your C# app. This is the only 100% reliable way to detect an application startup.
If this is not an option for you, there are a few other ways. On some OSes you can scour the registry to see what applications are running, but this isnt very reliable and is a big pain anyway.
So your best bet may be to poll: periodically enumerate all the top-level windows and see if any new ones have shown up since the last time you polled. This is hardly infallible, since a single app may spawn many top-level windows. And if your polling period is too long, a top-level window such as a MessageBox might show up and go away before your program ever notices it! But itll work for most situations most of the time.
To enumerate running windows, youll need to invoke the EnumWindows() Win32 function. This code snippet happens to do just that (search the code for "public class Windows"): http://www.codeproject.com/csharp/windowhider.asp
Good luck!
Oh, and note that Windows Vista will make it much harder to do any of these things. Vistas new security system means that some applications can be in a higher "security clearance" than your own program. Those higher-clearance programs simply wont be detectable by your app -- not by hooks, not by EnumWindows()... nothing. So some types of utility programs just wont be possible under Vista! This sucks. But its not my fault, I didnt do it. I thought I should warn you.
(ref: http://askville.amazon.com/detect-application-launched-C%23/AnswerViewer.do?requestId=677973)
0
 

Author Comment

by:casualkid
ID: 23720602
Thanks for your reply.

But I have already come across that solution. I need something better, like global hooks through C#.

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 16

Expert Comment

by:ai_ja_nai
ID: 23720820
there is a function that gives you the number of processes Process.GetProcesses() and EnumWindows() and the name of a process, by PID. You can make a loop cycle that scans all the open apps and lookup the matching name.
0
 

Author Comment

by:casualkid
ID: 23741825
Thanks ai ja nai.. But I want to be able to hook through this event and detect it.
0
 
LVL 16

Expert Comment

by:ai_ja_nai
ID: 23751461
I'm sorry, I belive there are no other solutions :)
0
 
LVL 16

Expert Comment

by:ai_ja_nai
ID: 23845623
"No proper answer" means that no solution came.
Instead, not only it has been provided more than one, but it has been given the advice that no better than this can ben made.
And this makes it eligible for awarding points.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1500 total points
ID: 23847136
I would think that a WMI process event watcher would get the job done.
0
 

Author Comment

by:casualkid
ID: 23848718
thanks to thelearnedone..

was able to watch for InstanceCreationEvent from Win32_Process.. :)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
Most folks would know the basics of how Dropbox works, so that’s not the purpose of this article. Security is what it’s all about, so here I’ll share how I choose to secure my Dropbox Account and the Data it contains.
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question