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
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