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);