Take the 2-minute tour ×

I have a C# windows application and ultimately launches a dialog from an interop component. The problem is that this dialog window sometimes appears behind the c# application's main window, forcing a user to alt-tab to find it.

I've put measures into place to find this dialog window and bring it forward...

private static extern bool SetForegroundWindow(IntPtr hWnd);

public class SearchData
    public string Wndclass;
    public string Title;
    public IntPtr hWnd;

private static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, ref SearchData data);
private delegate bool EnumWindowsProc(IntPtr hWnd, ref SearchData data);

public static bool EnumProc(IntPtr hWnd, ref SearchData data)
  //Code to determine whether the window from handle hWnd is our target window.
  //apply handle, title, class to data and halt the enumeration

...but 'finding' the dialog is problematic as the dialog's className and form title changes.

However, the dialog window's parent process (Task Manager>Go To Process) is the same as the current process. So to correctly 'find' this dialog window, my aim is to enumerate through all the windows, find the parent processID and compare against CurrentProcess.

Is there a way to obtain the overall parent process from a window handle?

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

GetWindowThreadProccessId will do the work-msdn.microsoft.com/en-us/library/windows/desktop/ms633522(v=vs.85).aspx

share|improve this answer
Excellent, thanks. This answer and this post (stackoverflow.com/questions/2281429/…) led me to the solution. –  MoSlo Sep 26 '12 at 9:33
add comment

Try hiding your application window prior to opening the dialog. That way it won't appear behind it.

share|improve this answer
Not a complete solution as the dialog can appear behind other windows as well (such as a web browser etc). Users get confused, and select the main application from the task bar, expecting the dialog to appear. Meanwhile its there, it's just buried behind other windows. This is especially a problem with multi-screen displays. –  MoSlo Sep 26 '12 at 8:37
add comment

Your Answer


