Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > .NET Languages
You last visited: Today at 00:13

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[C#] Getting PID from window title

Discussion on [C#] Getting PID from window title within the .NET Languages forum part of the Coders Den category.

Reply
 
Old   #1
 
iCraziE's Avatar
 
elite*gold: 0
Join Date: Jan 2010
Posts: 456
Received Thanks: 218
[C#] Getting PID from window title

I searched everywhere and can not find anything useful on this topic. Is this even possible?

I have multiple process. Each have the same process name, but different window titles.

I want to use the window title to find out the process ID.

Any suggestions? Thank you.
iCraziE is offline  
Old 01/09/2013, 12:43   #2
 
Kraizy​'s Avatar
 
elite*gold: 0
The Black Market: 471/0/0
Join Date: Apr 2010
Posts: 9,696
Received Thanks: 1,811
PHP Code:
for each p as process in process.getprocessesbyname("notepad")
   
msgbox(p.mainwindowtitle " - " p.id)
next 
Kraizy​ is offline  
Old 01/09/2013, 13:14   #3


 
Lawliet's Avatar
 
elite*gold: 2
Join Date: Jul 2009
Posts: 14,456
Received Thanks: 4,685
In C#^^

Code:
public int GetPidBy(string name)
{
      forech(var process in process.GetProcessesByName(name){
             return process.id;
      }
      return 0;
}
Lawliet is offline  
Old 01/09/2013, 13:28   #4
 
Kraizy​'s Avatar
 
elite*gold: 0
The Black Market: 471/0/0
Join Date: Apr 2010
Posts: 9,696
Received Thanks: 1,811
Ups nicht gesehen, dass im Titel C# stand..naja sollte trzdm nicht allzu schwer sein, ne kleine Schleife zu übersetzen.
Kraizy​ is offline  
Old 01/09/2013, 13:33   #5
 
elite*gold: 1000
Join Date: Apr 2012
Posts: 1,003
Received Thanks: 208
Quote:
Originally Posted by Lawliet! View Post
In C#^^

Code:
public int GetPidBy(string name)
{
      fore[COLOR="Red"]a[/COLOR]ch(Process proc in [COLOR="Red"]P[/COLOR]rocess.GetProcessesByName(name){
             return proc.id;
      }
      return 0;
}
Wenn dann richtig
qkuh is offline  
Old 01/10/2013, 00:05   #6
 
»jD«'s Avatar
 
elite*gold: 237
Join Date: Sep 2010
Posts: 1,152
Received Thanks: 4,910
Thats not what he is asking. He's asking for the window title, not the process name which is just the EXE File name without the extension. Again you have you use a Win API and the one you are looking for is FindWindow. Kraizy​'s post is the right way to do it but there is arguably a faster way to do it if you have lots of processes running!

You should first use FindWindow then GetWindowThreadProcessId. Example follows:

Code:
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll", SetLastError=true)]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

IntPtr hWnd = FindWindow(null, "Title of the Window!");
int processID = 0;
GetWindowThreadProcessId(hWnd, out processID);

MessageBox.Show("Process ID for Window is " + processID);
-jD
»jD« is offline  
Thanks
1 User
Old 01/10/2013, 00:32   #7
 
Kraizy​'s Avatar
 
elite*gold: 0
The Black Market: 471/0/0
Join Date: Apr 2010
Posts: 9,696
Received Thanks: 1,811
well, again vb.net but u should be able to translate it to c#:
PHP Code:
for each x in p as process in process.getprocessesbyname("notepad"where p.mainwindowtitle "Untitled - Notepad"
   
msgbox(x.id)
next 
Not sure if its 100% correct, I'm on my phone atm..also, if u r sure that there is only one same window title, u dont need to use a loop

@jD ur method is kinda overkill
Kraizy​ is offline  
Old 01/10/2013, 01:35   #8
 
»jD«'s Avatar
 
elite*gold: 237
Join Date: Sep 2010
Posts: 1,152
Received Thanks: 4,910
Quote:
Originally Posted by Kraizy​ View Post
well, again vb.net but u should be able to translate it to c#:
PHP Code:
for each x in p as process in process.getprocessesbyname("notepad"where p.mainwindowtitle "Untitled - Notepad"
   
msgbox(x.id)
next 
Not sure if its 100% correct, I'm on my phone atm..also, if u r sure that there is only one same window title, u dont need to use a loop

@jD ur method is kinda overkill
You would need to use a loop. getprocessesbyname returns all processes with the friendly exe name. For example, I could have chrome.exe running and the "name" that I would use is getprocessesbyname("chrome"). However, I would need to loop through them all to find the one out of possible 100 processes that are running to find the single website title I was looking for, hence why you would need to loop.

EDIT: Sorry didn't see the call to "where". Still, chrome uses multiple child processes for each tab and each has the same main window title handle applied to them. FindWindow finds only the parent process to each child process.

ALSO, callig GetProcessesByName in a foreach or with a linq call on the end of it causes GetProcessesByName to be called each iteration of the loop. You are better storing it in a variable before using it in a foreach or a loop.

I just did some tests, the P/Invoke is almost 5x faster ^.^

-jD
»jD« is offline  
Old 01/10/2013, 01:41   #9
 
iCraziE's Avatar
 
elite*gold: 0
Join Date: Jan 2010
Posts: 456
Received Thanks: 218
Hmm, that's not quite what i was looking for. But, i've come up with this now. How can i specify the process ID it shows in the message box now, in my script. Lets say for example I want to say getprocessbyid and use the value it got from my code here.

Code:
private void function1()
        {
            Process[] processlist = Process.GetProcessesByName("my process");

            string title = "title";
            string procID;
            
            foreach (Process process in processlist)
            {
                if (String.Equals(process.MainWindowTitle, title))
                {
                    procID = process.Id.ToString();
                    MessageBox.Show("This is the process ID: " + process.Id);
                    MessageBox.Show(procID);
                }
                else
                {
                    MessageBox.Show("Window not found");
                }                
            }
        }
iCraziE is offline  
Old 01/10/2013, 03:10   #10
 
»jD«'s Avatar
 
elite*gold: 237
Join Date: Sep 2010
Posts: 1,152
Received Thanks: 4,910
Sorry, I don't exactly get what you are asking?

-jD
»jD« is offline  
Old 01/10/2013, 04:55   #11
 
iCraziE's Avatar
 
elite*gold: 0
Join Date: Jan 2010
Posts: 456
Received Thanks: 218
Hmm, I think the code you posted before might work for me. I am going to try it right now. I didn't see it when I made my last post. But it looks good. Exactly what I was looking for I think.
iCraziE is offline  
Old 01/10/2013, 05:24   #12
 
»jD«'s Avatar
 
elite*gold: 237
Join Date: Sep 2010
Posts: 1,152
Received Thanks: 4,910
Alright!

-jD
»jD« is offline  
Reply


Similar Threads Similar Threads
How to change window title
01/05/2013 - Flyff Private Server - 1 Replies
how to change the window title of all program .exes from accountserver.exe to worldserver.exe in source???
Window Title
04/22/2008 - Perfect World - 3 Replies
anybody knows how to change the window title of pwo? maybe with olly or smth... ? thx in advance!
Rename CO2 window title (any window actually)
05/13/2006 - CO2 Exploits, Hacks & Tools - 4 Replies
I have seen a few posts requesting this. It is a simple autohotkey macro nothing fancy here. You will notice also that the UPX packer detection is not triggered. If you use autohotkey and want to know how to remove the packer PM me it is rather simple. To use this little tool thingie it is very simple: Execute the code. Focus your window. Press Hotkey CTRL+WIN+w Change the Title. Apply
Hex editing the Conquer2.0 window title..
11/22/2005 - Conquer Online 2 - 2 Replies
Okay, me and a friend of mine want to change the name of the window title (). Since I know a bit about hex editing, we figure I could change the title fairly easy, except I can't find where to change it!! I'm using Hex Workshop4.2, and i'm looking in the conquer.exe file, as well as the play.exe file. Can someone please show me how to do this? Much much appreciated.



All times are GMT +1. The time now is 00:14.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.