Connecting Tech Pros Worldwide Help | Site Map

Get Executable name from Window Handle

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 21st, 2005, 08:45 PM
Logan
Guest
 
Posts: n/a
Default Get Executable name from Window Handle

I need to get the executable name for window under the cursor. I feel like
my code is pretty close but the i never find a process.Handle that matches
the handle under my mouse.

here is my code:
public object ParentExecutable(IntPtr hWndUnderMouse)
{
object p = null;
System.Diagnostics.Process[] allProcesses =
System.Diagnostics.Process.GetProcesses();
for(int iii = 0; iii < allProcesses.Length; iii++)
{
try
{
if(((System.Diagnostics.Process)allProcesses[iii]).Handle ==
hWndUnderMouse)
{
p = ((System.Diagnostics.Process)allProcesses[iii]);
iii = allProcesses.Length;
}
}
catch{}
}
return p;
}
Thanks in advance for any help,
~Logan



  #2  
Old July 21st, 2005, 08:45 PM
Logan
Guest
 
Posts: n/a
Default Re: Get Executable name from Window Handle

I figured out what i needed to do. here is the new code


[DllImport("user32")]
public extern static int GetWindowThreadProcessId(
int hwnd,
ref int lpdwProcessId);
public object ParentExecutable(IntPtr hWnd)
{
object p = null;
int id = 0;
int i = UnManagedMethods.GetWindowThreadProcessId(hWnd.ToI nt32(),ref id);

System.Diagnostics.Process[] allProcesses =
System.Diagnostics.Process.GetProcesses();
for(int iii = 0; iii < allProcesses.Length; iii++)
{
try
{
if(((System.Diagnostics.Process)allProcesses[iii]).Id == id)
{
p = ((System.Diagnostics.Process)allProcesses[iii]);
iii = allProcesses.Length;
}
}
catch{}
}
return p;
}


"Logan" <logan@globalweb.net> wrote in message
news:uk2JRC9AFHA.3708@TK2MSFTNGP14.phx.gbl...[color=blue]
>I need to get the executable name for window under the cursor. I feel like
>my code is pretty close but the i never find a process.Handle that matches
>the handle under my mouse.
>
> here is my code:
> public object ParentExecutable(IntPtr hWndUnderMouse)
> {
> object p = null;
> System.Diagnostics.Process[] allProcesses =
> System.Diagnostics.Process.GetProcesses();
> for(int iii = 0; iii < allProcesses.Length; iii++)
> {
> try
> {
> if(((System.Diagnostics.Process)allProcesses[iii]).Handle ==
> hWndUnderMouse)
> {
> p = ((System.Diagnostics.Process)allProcesses[iii]);
> iii = allProcesses.Length;
> }
> }
> catch{}
> }
> return p;
> }
> Thanks in advance for any help,
> ~Logan
>
>[/color]


  #3  
Old July 21st, 2005, 08:45 PM
Mattias Sjögren
Guest
 
Posts: n/a
Default Re: Get Executable name from Window Handle

>I need to get the executable name for window under the cursor.

There's a Win32 API function GetWindowModuleFileName that does this.

[color=blue]
>I feel like
>my code is pretty close but the i never find a process.Handle that matches
>the handle under my mouse.[/color]

That's because window handles and process handles are different
things.



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.