createprocessasuser and bring it to front

I have an application that calls createprocessasuser (e.g. notepad.exe) which works fine.

The problem is that the window is being created behind the currently focused window. I have tried messing with the process start-up flags etc. but was unable to find a suitable configuration to get it to work.

Now I am thinking I need to simulate a click on it? I have code to locate a window (using window name) and click on a button which works but I want click on the frame of the created process as I am not sure what the name may be (no control over what process name they type in).

Thanks!
You need to grab the HWND of the application you have launched and then align the thread for this application with the thread of your application so that you can call to SetForeGroundWindow on the third party app:

1
2
3
4
5
6
7
8
9
10
11
12
13
HWND hOtherWnd = FindWindow( ... );
if (hOtherWnd)
{
    DWORD hMyThread = GetWindowThreadProcessId(m_hWnd, NULL);
    DWORD hOtherThread = GetWindowThreadProcessId(hOtherWnd, NULL);
    AttachThreadInput(hMyThread, hOtherThread, TRUE);
    SetForegroundWindow(hOtherWnd);
    AttachThreadInput(hMyThread, hOtherThread,FALSE);
    if IsIconic(hOtherWnd)
        ShowWindow( hOtherWnd,SW_RESTORE );
    elses
        ShowWindow( hOtherWnd,SW_SHOW );
}


Hope that helps, let us know.
Last edited on
Worked perfectly thanks.
Registered users can post here. Sign in or register to post.