472,119 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

How to find owner process ID?

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, ProcInfogetOpenWindows()
{
Dictionary<HWND, ProcInfodictWindows = 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

Dec 9 '06 #1
4 21036
"Nayan" <cn****@gmail.comwrote:
>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.

--
Lucian
Dec 9 '06 #2
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
On Dec 9, 3:36 am, Lucian Wischik <l...@wischik.comwrote:
"Nayan" <cna...@gmail.comwrote:
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.

--
Lucian
Dec 9 '06 #3
"Nayan" <cn****@gmail.comwrote:
>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);

--
Lucian
Dec 9 '06 #4
Ya, I got the solution at the same time :)

Thanks a million for your help, Lucian!

Regards,
Nayan

On Dec 9, 3:51 am, Lucian Wischik <l...@wischik.comwrote:
"Nayan" <cna...@gmail.comwrote:
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);

--
Lucian
Dec 9 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by neo | last post: by
reply views Thread by Christoph Kunz | last post: by
1 post views Thread by Jan Jeitziner | last post: by
1 post views Thread by coroto | last post: by
1 post views Thread by M D | last post: by
reply views Thread by leo001 | 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.