Restoring an application started with CreateProcess
Hello all, I'm new to these groups so please be gentle. My problem is
that I'm launching an external application with CreateProcess, storing
the process ID for that application and at some point in processing my
application I need to restore the launched application. The create
process and storing of the resulting process ID is fine, here's the
code.
CListCtrl * pList = (CListCtrl *)GetDlgItem(IDC_PLUGINS_LIST);
CString strQuotedFile = "";
strQuotedFile.Format("\"%s\"", strFile);
STARTUPINFO stStartupInfo;
PROCESS_INFORMATION stProcessInfo;
ZeroMemory((void *)&stStartupInfo, sizeof(STARTUPINFO));
ZeroMemory((void *)&stProcessInfo, sizeof(PROCESS_INFORMATION));
stStartupInfo.cb = sizeof(STARTUPINFO);
CString strCmdLine = "";
strCmdLine.Format("%s %s", m_pPluginFavourites->GetEditorName(),
strQuotedFile);
if (CreateProcess(NULL, (LPTSTR)(LPCTSTR)strCmdLine, 0, 0, FALSE,
NORMAL_PRIORITY_CLASS, 0, 0, &stStartupInfo, &stProcessInfo) == FALSE)
{
LPVOID lpMsgBuf;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
MessageBox((LPCTSTR)lpMsgBuf, "Error", MB_OK);
LocalFree(lpMsgBuf);
}
else
{
pList->SetItemData(m_nSelectedPluginIndex, stProcessInfo.dwProcessId);
}
Now the hard part. I use EnumWindows to find the external application
window by process ID and then attempt to restore it but I can't get the
external application to restore to it's original viewing state, here's
the relevant code.
BOOL CALLBACK CPluginsDialog::EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
CPluginsDialog * pDlg = (CPluginsDialog *)lParam;
return pDlg->EnumHelper(hwnd);
}
BOOL CPluginsDialog::EnumHelper (HWND hWnd)
{
CWindow myWindow;
myWindow.Attach(hWnd);
DWORD dwID = myWindow.GetWindowProcessID();
if (dwID == m_dwProcessID)
{
sm_hWndHostMain = hWnd;
return FALSE; // No need to continue enum (got what we wanted)
}
return TRUE;
}
Here's how I make the call to EnumWindows.
CListCtrl * pList = (CListCtrl *)GetDlgItem(IDC_PLUGINS_LIST);
DWORD dwProcessID = pList->GetItemData(m_nSelectedPluginIndex);
if (dwProcessID > 0)
{
// enumerate all open windows, looking for the process ID
m_dwProcessID = dwProcessID;
::EnumWindows(EnumWindowsProc, (LPARAM)this);
// anything found ?
if (sm_hWndHostMain)
{
// got it, restore the window
CWnd* wndMain = CWnd::FromHandle(sm_hWndHostMain);
CWnd* pTop = wndMain->GetTopLevelParent();
pTop->SendMessage(WM_ACTIVATE);
}
else
{
MessageBox("The selected plugin is already open for edit in an
external editor but Atlas failed to find the already open
editor\nPlease manuallt restore the alerady open editor", "Info",
MB_ICONINFORMATION | MB_OK);
}
}
I have tried the following variations to try and get this to work:
wndMain->ShowWindow(WM_MDIACTIVATE);
wndMain->SetForegroundWindow();
::SetForegroundWindow(sm_hWndHostMain);
wndMain->SendMessage(WM_MDIACTIVATE);
Any advice/suggestions ? I'm at my wits end as to how I can this
external application to restore. Many thanks in advance.
Chris.