CreateProcess strange behaviour?!
-
Hello all! Here is some "strange" behaviour of CreateProcess (or winword, I don't know) I wanted to launch winword from my application and be notified when the user close the session I opened, so I used
CreateProcess
andWaitForSingleObject
. Here is the code sample from MSDN:SECURITY_ATTRIBUTES sec; sec.bInheritHandle = FALSE; sec.lpSecurityDescriptor = NULL; sec.nLength = sizeof(SECURITY_ATTRIBUTES); STARTUPINFO si; ::ZeroMemory(&si, sizeof(STARTUPINFO)); si.cb = sizeof(STARTUPINFO); PROCESS_INFORMATION pi; //notice the 2 slashes after winword.exe if(::CreateProcess(NULL, "C:\\Program Files\\Microsoft Office\\Office\\winword.exe / / C:\\STPMesagElec.doc", NULL,NULL, TRUE, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi)) { // Wait for Word to become Idle. WaitForInputIdle(pi.hProcess, 3000); WaitForSingleObject(pi.hProcess,INFINITE); } else { int err = GetLastError(); ::MessageBeep(0); ::MessageBox(NULL, "CreateProcess() failed.\nCheck Path for WinWord.Exe.", "Error", MB_SETFOREGROUND); return; } ::CloseHandle(pi.hThread); ::CloseHandle(pi.hProcess);
Now, the result: - If I useCreateProcess
as above, but without the 2 slashes, it works only when there is no other instance of Word already opened; if a session already exists,WaitForSingleObject
doesn't block anymore - If I useCreateProcess
with the two slashes, it works just as I need it. I noticed that with at least one of the slashes,CreateProcess
creates a new instance of winword.exe, else it opens a new document in the already existing process. Also, with other applications (eg Notepad or even Excel), it works fine without the slashes. Do you think that this is abnormal behaviour? Why it needs the slashes? (i discovered this by pure luck, it isn't documented anywhere) Enis Arif ----------- "I am enough of an artist to draw freely upon my imagination. Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world." (Albert Einstein) -
Hello all! Here is some "strange" behaviour of CreateProcess (or winword, I don't know) I wanted to launch winword from my application and be notified when the user close the session I opened, so I used
CreateProcess
andWaitForSingleObject
. Here is the code sample from MSDN:SECURITY_ATTRIBUTES sec; sec.bInheritHandle = FALSE; sec.lpSecurityDescriptor = NULL; sec.nLength = sizeof(SECURITY_ATTRIBUTES); STARTUPINFO si; ::ZeroMemory(&si, sizeof(STARTUPINFO)); si.cb = sizeof(STARTUPINFO); PROCESS_INFORMATION pi; //notice the 2 slashes after winword.exe if(::CreateProcess(NULL, "C:\\Program Files\\Microsoft Office\\Office\\winword.exe / / C:\\STPMesagElec.doc", NULL,NULL, TRUE, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi)) { // Wait for Word to become Idle. WaitForInputIdle(pi.hProcess, 3000); WaitForSingleObject(pi.hProcess,INFINITE); } else { int err = GetLastError(); ::MessageBeep(0); ::MessageBox(NULL, "CreateProcess() failed.\nCheck Path for WinWord.Exe.", "Error", MB_SETFOREGROUND); return; } ::CloseHandle(pi.hThread); ::CloseHandle(pi.hProcess);
Now, the result: - If I useCreateProcess
as above, but without the 2 slashes, it works only when there is no other instance of Word already opened; if a session already exists,WaitForSingleObject
doesn't block anymore - If I useCreateProcess
with the two slashes, it works just as I need it. I noticed that with at least one of the slashes,CreateProcess
creates a new instance of winword.exe, else it opens a new document in the already existing process. Also, with other applications (eg Notepad or even Excel), it works fine without the slashes. Do you think that this is abnormal behaviour? Why it needs the slashes? (i discovered this by pure luck, it isn't documented anywhere) Enis Arif ----------- "I am enough of an artist to draw freely upon my imagination. Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world." (Albert Einstein)Winword does check if it is already running. It registers in the ROT as an automation server. If You create a second instance only a new document is created because winword knows about its predecessor. For the second slash I dont know but this can fool the command line parser of winword so it creates always a new instance regardless of previously existing ones. But there should also be a documented switch there (/automation ???)