Re: URGENT: SetParent: Using external process as child window of calling process?!

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Joseph M. Newcomer (newcomer_at_flounder.com)
Date: 03/20/04


Date: Sat, 20 Mar 2004 18:05:28 -0500

Can't be done. Don't even waste your time. The documentation is, as you point out, quite
explicit on this point. But I wouldn't trust SetParent for anything. Historically, it has
had serious bugs (they may be fixed by now, but most of us avoid it because of the
history).

You have to create the second app as an OLE Server app, and use your app as an OLE client.
                                joe

On Fri, 19 Mar 2004 18:08:12 +0100, "Erik Laatsch" <elaatsch@gmx.de> wrote:

>Hello!
>
>I want to run a Dialog-based application in a child window of my calling
>application.
>
>1. I created the process with CreateProcess
>2. Determining the main window of the process after the message queue is
>initialized
>3. Setting the main window of the called app as child of the calling app
>
>The documentation says, that SetParent can only be called when the window
>belongs to the same process. In fact it works fine most of the time! But
>sometimes it does not. The GetParent function always returns zero for the
>remote app. What am I doing wrong?
>
>Please help!
>
>Here is my code:
>
>1. Call the process an determine the main window:
>_______________________
>
>
>BOOL CALLBACK ELSystem::StartProcessEnumWindowsProc(HWND hwnd, LPARAM
>lParam)
>{
> ELSystem* pELSys = (ELSystem*)lParam;
> DWORD dwProcessID=0;
> GetWindowThreadProcessId(hwnd,&dwProcessID);
> if(dwProcessID==pELSys->m_dwFindProcID)
> {
> pELSys->m_hwndFindProcWindow=hwnd;
> return FALSE;
> }
>
> return TRUE;
>}
>
>
>BOOL ELSystem::StartProcess(CString strPath,unsigned short wShow,
>ELPROCINFO& elInfo)
>{
>
> STARTUPINFO suInfo;
> ::ZeroMemory(&suInfo,sizeof(STARTUPINFO));
> suInfo.cb=sizeof(STARTUPINFO);
> suInfo.dwFlags=STARTF_USESHOWWINDOW;
> suInfo.wShowWindow=wShow;
>
> PROCESS_INFORMATION piInfo;
> BOOL bResult=CreateProcess(strPath,"",0,0,TRUE,0,0,0,&suInfo,&piInfo);
> WaitForInputIdle(piInfo.hProcess,10000);
> m_dwFindProcID=piInfo.dwProcessId;
> m_hwndFindProcWindow=0;
> EnumWindows(ELSystem::StartProcessEnumWindowsProc, (LPARAM)this);
> elInfo.hWnd=m_hwndFindProcWindow;
> elInfo.hProcess=piInfo.hProcess;
> elInfo.dwProcessId=piInfo.dwProcessId;
> return bResult;
>}
>Setting the window as child from within my CView derived class:
>____________________________
>
>
>
>int CHookView::Bind()
>{
> OpenServer(800);
> int iResult=0;
> do
> {
> ELSystem sys;
>
>if(!sys.StartProcess(GetDocument()->GetData()->m_strPath,SW_HIDE,m_elProcInf
>o))
> {
> CString rVal;
> LPTSTR pstrError = rVal.GetBuffer(4096);
> sys.GetErrorMessage(pstrError, 4096, NULL,GetLastError());
> rVal.ReleaseBuffer();
> CString strCaption;
> strCaption.LoadString(IDS_LOADERROR);
> CString strText;
> strText.LoadString(IDS_EXELOADERROR);
>
>
>iResult=MessageBox(strText+rVal,strCaption,MB_ABORTRETRYIGNORE|MB_ICONEXCLAM
>ATION);
> }
> else
> if(m_elProcInfo.hWnd)
> {
>
> ModifyStyle(0,WS_CLIPCHILDREN);
> m_wndAttached.Attach(m_elProcInfo.hWnd);
> if(!::IsWindow(m_wndAttached))
> {
> MessageBox("Hallo","Hallo",MB_OK);
> }
> m_wndAttached.SetParent(this);
> CWnd* pWnd=m_wndAttached.GetParent();
>
> m_wndAttached.ModifyStyle(WS_CAPTION,WS_OVERLAPPED);
> CRect rect;
> GetClientRect(rect);
> m_wndAttached.MoveWindow(rect);
> Invalidate();
> m_wndAttached.Invalidate();
> m_wndAttached.ShowWindow(SW_MAXIMIZE);
> SetTimer(TIMER_CONNECTION,5000,0);
> }
> else
> {
>
>iResult=ELSystem::MessageBox(IDS_NOWINDOW,IDS_NOWINDOWCAPTION,MB_ABORTRETRYI
>GNORE|MB_ICONSTOP);
>
> }
> if(iResult==IDABORT)
> {
> GetParent()->DestroyWindow();
> }
> }
> while(iResult==IDRETRY);
>
> return iResult;
>
>}
>

Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm


Quantcast