Click here to Skip to main content
15,665,942 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is it possible to get MainWindowHandle of running process.
For example i have a running process. and i get it by using

Process[] process = Process.GetProcessesByName("MRMS");

process[0].MainWindowHandle returns 0.

Now i want to get its MainWindowHandle value. If it is Possible? then please explain. Code example will be very helpful.
Updated 10-Aug-12 10:58am
[no name] 10-Aug-12 17:13pm    
You might already be. MainWindowHandle returns an IntPtr so you may not be getting the value correctly.
Sergey Alexandrovich Kryukov 10-Aug-12 19:10pm    
Could be just the timing thing; please see my answer.
Sergey Alexandrovich Kryukov 10-Aug-12 19:10pm    
It looks like .NET and C#. Please tag the platform and language.
xavior885 11-Aug-12 2:02am    
Ok let me try it. I can loop for mainwindowhandle until it gets intptr value greater than zero or use a time interval to wait a handle to be created first.

You already do it correctly. One possible problem could be that you examine the handle at the moment of time when the process is already started but the handle is not yet created. It could regularly happen if you create that process by the same application. In this case, read this property again some time later.

Share this answer
Assuming that you want this for a particular application you can use Spy++[^] to determine the class of the main window. Generally determining exactly which window is the main window for an application isn't possible - even if it usually is the first window created by the apllication.

Best regards
Espen Harlinn
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900