Board index » cppbuilder » Problems with createprocess opening winword, explorer,internet explorer

Problems with createprocess opening winword, explorer,internet explorer

Hi,
i'm trying to open Doc A and Doc B using createprocess with Winword. I've
wrapped the code inside a thread and shown below.

So, on the first thread, Doc A opens in WinWord, and we WaitForSingleObject
until the process terminates. Okay, i can find all the windows using
enumwindows callback, and sendmessage WM_CLOSE, or temiante the thread -
what ever.

But, When i create a new thread to open a different document, i do not get a
new process - instead the thread executes and skips WaitForSingleObject, and
seems to use the first process as Doc A. How then can i tell if this second
document is still open (and that a user has not closed it) or indeed how can
i close this document?

The same problems occure with other software such as explorer and internet
explorer.

Help on this would be very gratefully recieved.
rgds Ang.

void __fastcall TRandomThread::Execute(){
try{
    hJobObject = CreateJobObject(0,0);
    STARTUPINFO si = {sizeof(STARTUPINFO),0};
    si.dwFlags = STARTF_FORCEONFEEDBACK ;
    si.wShowWindow= SW_SHOW;//
    char* ptrStartDir = NULL;
    AnsiString VFileName = this->FileName;
    //e.g. "C:\\Office\\WINWORD.exe";// \"C:\\DocA.doc\"";
    if(!CreateProcess(NULL, VFileName.c_str(),NULL, NULL, false,
CREATE_SUSPENDED,//FOR CREATEJOBOJBECT
        NULL, ptrStartDir,&si,&pi )) {
           //Unable to open process
           CloseHandle(pi.hProcess);
           CloseHandle(pi.hThread);
           throw Exception(SysErrorMessage(GetLastError()));//Shellexe error
        }
        else{
           if(AssignProcessToJobObject(hJobObject,pi.hProcess)==0)
             throw Exception(SysErrorMessage(GetLastError()));
           if(ResumeThread( pi.hThread )==0xFFFFFFFF)
             throw Exception(SysErrorMessage(GetLastError()));
           Application->ProcessMessages();
           DWORD ExitC;
           DWORD dwResult = WaitForSingleObject(pi.hProcess, INFINITE);
           if (dwResult == WAIT_OBJECT_0)
               GetExitCodeProcess(pi.hProcess, &ExitC);

           //**//
           fgw = GetForegroundWindow();
           CloseHandle(pi.hProcess);
           CloseHandle(pi.hThread);
    }

Quote
}catch(Exception &e){

    Application->ShowException(&e);
Quote
}
}

 

Re:Problems with createprocess opening winword, explorer,internet explorer


Such programs are typically designed to only run one physical instance of
themselves at a time, thus only one process is available.  When a second
instance is requested, it actually executes just long enough to detect the
first instance, and if found then transmits the relevant information back to
the first instance and then terminate immediately.  When the first instance
receives the information, it merely opens another window within itself to
display the new data.  Your second thread is not waiting for the second
document to close because it is working with the second process which is
only around for a very short period of time.  Everything is being handled in
the first instance.

Gambit

Quote
"Angus" <aguspick...@yahoo.com> wrote in message

news:3e3a15f4@newsgroups.borland.com...
Quote
> But, When i create a new thread to open a different document,
> i do not get a new process - instead the thread executes and skips
> WaitForSingleObject, and seems to use the first process as Doc A.
> How then can i tell if this second document is still open (and that a
> user has not closed it) or indeed how can i close this document?

> The same problems occure with other software such as explorer
> and internet explorer.

Other Threads