Reputation: 51
How can i get hWnd of a hidden window? With my configuration it returns 0.
This is what i have tried:
ProcessStartInfo startInfo = new ProcessStartInfo(path);
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process process = Process.Start(startInfo);
process.WaitForInputIdle();
MessageBox.Show(process.MainWindowHandle.ToString());
Upvotes: 0
Views: 1609
Reputation: 51
This worked for me:
delegate bool EnumThreadDelegate(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll")]
static extern bool EnumThreadWindows(int dwThreadId, EnumThreadDelegate lpfn,
IntPtr lParam);
static IEnumerable<IntPtr> EnumerateProcessWindowHandles(int processId)
{
var handles = new List<IntPtr>();
foreach (ProcessThread thread in Process.GetProcessById(processId).Threads)
EnumThreadWindows(thread.Id,
(hWnd, lParam) => { handles.Add(hWnd); return true; }, IntPtr.Zero);
return handles;
}
Usage:
ProcessStartInfo startInfo = new ProcessStartInfo(path);
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process process = Process.Start(startInfo);
process.WaitForInputIdle();
IntPtr WindowHandle = EnumerateProcessWindowHandles(process.Id).First();
Upvotes: 3