How to find owner process ID?

N

Nayan

The base process owns this thread. But the visible window is owned by
the thread.

How do I get the owner Process ID from a Thread ID?


To understand, look at this "<<--" pointer in the following code.


sample code:

using HWND = IntPtr;
public struct ProcInfo
{
public string ProcessName;
public int ProcessID;
public string ProcessFileName;
public int ProcessInstance;
public string ProcessWindowTitle;
};

public static IDictionary<HWND, ProcInfo> getOpenWindows()
{
Dictionary<HWND, ProcInfo> dictWindows = new Dictionary<HWND,
ProcInfo>();

HWND hwndShellWindow = GetShellWindow();

EnumWindows(
delegate(HWND hWnd, int lParam)
{
if (hWnd == hwndShellWindow)
return true;

if (!IsWindowVisible(hWnd))
return true;

int lengthWindowTitle = GetWindowTextLength(hWnd);
if (lengthWindowTitle == 0)
return true;


//A process who has a window is found!!!! Great!

ProcInfo tempProc = new ProcInfo();

StringBuilder strWindowTitle = new
StringBuilder(lengthWindowTitle);
GetWindowText(hWnd, strWindowTitle, lengthWindowTitle + 1);
tempProc.ProcessWindowTitle = strWindowTitle.ToString();

HWND processInstance = GetWindowLong(hWnd,
(int)Indices.GWL_HINSTANCE);
tempProc.ProcessInstance = (int)processInstance;

HWND processID = GetWindowThreadProcessId(hWnd,
IntPtr.Zero);
tempProc.ProcessID = (int)processID;

foreach (Process proc in Process.GetProcesses())
{
if (proc.Id == (int)processID) // <<-- This never
matches :((
{
tempProc.ProcessName = proc.ProcessName;
tempProc.ProcessFileName =
proc.MainModule.FileName;
break;
}
}

dictWindows[hWnd] = tempProc;

return true;
},
0
);

return dictWindows;
}

delegate bool EnumWindowsProc(HWND hWnd, int lParam);

[DllImport("USER32.DLL")]
static extern HWND GetShellWindow();
[DllImport("USER32.DLL")]
static extern bool IsWindowVisible(HWND hWnd);
[DllImport("USER32.DLL")]
static extern int GetWindowText(HWND hWnd, StringBuilder lpString, int
nMaxCount);
[DllImport("USER32.DLL")]
static extern int GetWindowTextLength(HWND hWnd);
[DllImport("USER32.DLL")]
static extern HWND GetWindowLong(HWND hWnd, int nIndex);
[DllImport("USER32.DLL")]
static extern HWND GetWindowThreadProcessId(HWND hWnd, HWND
lpdwProcessId);



Thanks,
Nayan
 
L

Lucian Wischik

Nayan said:
The base process owns this thread. But the visible window is owned by
the thread.
How do I get the owner Process ID from a Thread ID?
HWND processID = GetWindowThreadProcessId(hWnd,
IntPtr.Zero);

in c++/win32 you'd write
DWORD processId;
DWORD threadId = GetWindowThreadProcessId(hwnd,&processId);

I think you got your arguments the wrong way round. You need to pass
processId as an "out" argument.
 
N

Nayan

Yes, you are right!!

But i m not very good in C#. How can i get the value in a variable like
this by reference??

Is this correct??
HWND processID;
HWND threadID = GetWindowThreadProcessId(hWnd, processID);


Thanks a million! :)
Nayan
 
L

Lucian Wischik

Nayan said:
But i m not very good in C#. How can i get the value in a variable like
this by reference?

[DllImport("user32.dll")] static extern UInt32
GetWindowThreadProcessId(HWND hwnd, out UInt32 processId);

....
UInt32 pid;
UInt32 tid = GetWindowThreadProcessid(hwnd,out pid);
 
N

Nayan

Ya, I got the solution at the same time :)

Thanks a million for your help, Lucian!

Regards,
Nayan

Nayan said:
But i m not very good in C#. How can i get the value in a variable like
this by reference?[DllImport("user32.dll")] static extern UInt32
GetWindowThreadProcessId(HWND hwnd, out UInt32 processId);

...
UInt32 pid;
UInt32 tid = GetWindowThreadProcessid(hwnd,out pid);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top