Get Executable name from Window Handle

  • Thread starter Thread starter Logan
  • Start date Start date
L

Logan

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
 
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.ToInt32(),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;
}
 
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
 
Back
Top