Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CreateProcess strange behaviour?!

CreateProcess strange behaviour?!

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Enis
    wrote on last edited by
    #1

    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 and WaitForSingleObject. 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 use CreateProcess 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 use CreateProcess 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)

    A 1 Reply Last reply
    0
    • E Enis

      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 and WaitForSingleObject. 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 use CreateProcess 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 use CreateProcess 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)

      A Offline
      A Offline
      Alois Kraus
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups