Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have created a process using C# and started that Process.

While starting the EXE I have stored the Process Id in a variable.

I have to retrieve the Title Bar Text of the EXE using its Process Id.

How to get rid of this problem.
Posted

It's not quite that simple: a process can have a number of threads, which can have a number of windows each...
But, this should get you started:
C#
[DllImport("user32", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private extern static bool EnumThreadWindows(int threadId, EnumWindowsProc callback, IntPtr lParam);
[DllImport("user32", SetLastError = true, CharSet = CharSet.Auto)]
private extern static int GetWindowText(IntPtr hWnd, StringBuilder text, int maxCount);
private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);


public IEnumerable<string> GetWindowText(Process p)
    {
    List<string> titles = new List<string>();
    foreach (ProcessThread t in p.Threads)
        {
        EnumThreadWindows(t.Id, (hWnd, lParam) =>
        {
            StringBuilder text = new StringBuilder(200);
            GetWindowText(hWnd, text, 200);
            titles.Add(text.ToString());
            return true;
        }, IntPtr.Zero);
        }
    return titles;
    }
 
Share this answer
 
Comments
KUMAR619 31-Jul-14 1:05am    
Sir, Where do I need to feed my Process Id to get Title name.
OriginalGriff 31-Jul-14 2:00am    
Don't store the Process Id - store the Process object you used to start it...
KUMAR619 31-Jul-14 2:29am    
Sir, Let me explain this briefly.
I am in Medical based project. Where there are two EXE files.

One EXE is static which exists only once.
But the second one Order.EXE is not static i.e.. when we click on a patient
a Order.EXE window opens separately. But I was asked to open Multiple patients of different Id
but If I open the same patient which is already opened Its should not open.

Since every patient's details gets open on Order.Exe then how to differentiate them and I need to eliminate the process which is already opened.

Is there any way to solve this Sir. Is is possible Sir. Please reply Sir
OriginalGriff 31-Jul-14 4:46am    
So when you open the Order.exe process, keep the Process instance you used.
You can then use Process.CloseMainWindow to tell it to shutdown, and open a new instance.
If you open the instance in your code, then you have all the tools you need to deal with it provided as part of the Process instance you used to open it! You don;t need to search for it, or it's window title, or anything: use the Process class!

So keep a list of Patients with the appropriate Process you used to open it, and look there first: you can then see if you have created it and if so close / switch to / whatever the appropriate Process for the right patient ( A Dictionary of Patient, Process should do it easily enough).

See the example here:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.close(v=vs.110).aspx
KUMAR619 31-Jul-14 4:56am    
Thanks sir. Is it morning 4 AM(See commenting time) for you sir

You are really a genius answering our question event at night.
We are very thankful to Yourself and CodeProject.com for making us learn and code better

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