471,344 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,344 software developers and data experts.

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
Jul 21 '05 #1
2 7965
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" <lo***@globalweb.net> wrote in message
news:uk**************@TK2MSFTNGP14.phx.gbl...
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

Jul 21 '05 #2
>I need to get the executable name for window under the cursor.

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

I feel like
my code is pretty close but the i never find a process.Handle that matches
the handle under my mouse.


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.
Jul 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Jay Haslup | last post: by
3 posts views Thread by NeverLift | last post: by
2 posts views Thread by er441298 | last post: by
11 posts views Thread by Bit byte | last post: by
4 posts views Thread by drodrig | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.