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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CreateProcess problem

CreateProcess problem

Scheduled Pinned Locked Moved C / C++ / MFC
jsonperformancehelpquestion
7 Posts 3 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.
  • P Offline
    P Offline
    poda
    wrote on last edited by
    #1

    Hi Friends, I need to invoke a .exe file and pass a message to that file. Using CreateProcess,I invoked(called) the exe file, FindWindow API to get the invoked exe and SendMessage to send the message. The problem is, there is a time delay in CreateProcess that the FindWindow API cannot find it,if WaitForSingleObject() function is not used. CreateProcess("C:\\Bin\\wfp.exe",NULL,0,0,FALSE,CREATE_NEW_CONSOLE,0,0,&si,&pi); WaitForSingleObject(pi.hThread,500);//delay time 500ms CloseHandle(pi.hProcess); hExeWnd=FindWindow(NULL,"WFP"); if(hExeWnd) { ::SendMessage(hExeWnd,WM_PLOTWAVE,0,0); ShowWindow(hExeWnd,SW_SHOW); } The delay time 500ms may vary from machine to machine in accordance with its performance.If I give it as INFINITE,the program stops there UNTILL the wfp.exe window is CLOSED,which is unacceptable. So how can I make the program to wait untill the CreateProcess() completes its task in executing the exe. NOTE:I tried using ShellExecute API,but there too Sleep() function is needed for the time delay.

    M N 2 Replies Last reply
    0
    • P poda

      Hi Friends, I need to invoke a .exe file and pass a message to that file. Using CreateProcess,I invoked(called) the exe file, FindWindow API to get the invoked exe and SendMessage to send the message. The problem is, there is a time delay in CreateProcess that the FindWindow API cannot find it,if WaitForSingleObject() function is not used. CreateProcess("C:\\Bin\\wfp.exe",NULL,0,0,FALSE,CREATE_NEW_CONSOLE,0,0,&si,&pi); WaitForSingleObject(pi.hThread,500);//delay time 500ms CloseHandle(pi.hProcess); hExeWnd=FindWindow(NULL,"WFP"); if(hExeWnd) { ::SendMessage(hExeWnd,WM_PLOTWAVE,0,0); ShowWindow(hExeWnd,SW_SHOW); } The delay time 500ms may vary from machine to machine in accordance with its performance.If I give it as INFINITE,the program stops there UNTILL the wfp.exe window is CLOSED,which is unacceptable. So how can I make the program to wait untill the CreateProcess() completes its task in executing the exe. NOTE:I tried using ShellExecute API,but there too Sleep() function is needed for the time delay.

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      Check out WaitForInputIdle()

      --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

      P 1 Reply Last reply
      0
      • P poda

        Hi Friends, I need to invoke a .exe file and pass a message to that file. Using CreateProcess,I invoked(called) the exe file, FindWindow API to get the invoked exe and SendMessage to send the message. The problem is, there is a time delay in CreateProcess that the FindWindow API cannot find it,if WaitForSingleObject() function is not used. CreateProcess("C:\\Bin\\wfp.exe",NULL,0,0,FALSE,CREATE_NEW_CONSOLE,0,0,&si,&pi); WaitForSingleObject(pi.hThread,500);//delay time 500ms CloseHandle(pi.hProcess); hExeWnd=FindWindow(NULL,"WFP"); if(hExeWnd) { ::SendMessage(hExeWnd,WM_PLOTWAVE,0,0); ShowWindow(hExeWnd,SW_SHOW); } The delay time 500ms may vary from machine to machine in accordance with its performance.If I give it as INFINITE,the program stops there UNTILL the wfp.exe window is CLOSED,which is unacceptable. So how can I make the program to wait untill the CreateProcess() completes its task in executing the exe. NOTE:I tried using ShellExecute API,but there too Sleep() function is needed for the time delay.

        N Offline
        N Offline
        Naveen
        wrote on last edited by
        #3

        if both parent application and child is urs, u can implement another technique. Create a named event in both application. The parent application, after calling the createprocess should wait for this event. The child process may set the event to signalled state after initialized.

        nave

        P 1 Reply Last reply
        0
        • N Naveen

          if both parent application and child is urs, u can implement another technique. Create a named event in both application. The parent application, after calling the createprocess should wait for this event. The child process may set the event to signalled state after initialized.

          nave

          P Offline
          P Offline
          poda
          wrote on last edited by
          #4

          Thanks friends! I cannot use WaitForInputIdle() since it also requires the time delay. I tried using Naveen's idea of creating events. It worked! In the Parent application,I wrote as HANDLE hEvent; hEvent=CreateEvent(NULL,FALSE,FALSE,"WFP_EVENT");//Named Event CreateProcess("C:\\Bin\\wfp.exe",NULL,0,0,FALSE,CREATE_NEW_CONSOLE,0,0,&si,&pi); //Wait till the Event is Signaled from the called child application WaitForSingleObject(hEvent,INFINITE); CloseHandle(pi.hProcess); CloseHandle(hEvent); In the child application(i.e the called Exe) BOOL CWFPDlg::OnInitDialog() { ... HANDLE hEvent; hEvent=CreateEvent(NULL,FALSE,FALSE,"WFP_EVENT"); if(GetLastError()==ERROR_ALREADY_EXISTS) { hEvent=OpenEvent(EVENT_MODIFY_STATE,FALSE,"WFP_EVENT"); SetEvent(hEvent);//Signal the event } ... }

          N 1 Reply Last reply
          0
          • P poda

            Thanks friends! I cannot use WaitForInputIdle() since it also requires the time delay. I tried using Naveen's idea of creating events. It worked! In the Parent application,I wrote as HANDLE hEvent; hEvent=CreateEvent(NULL,FALSE,FALSE,"WFP_EVENT");//Named Event CreateProcess("C:\\Bin\\wfp.exe",NULL,0,0,FALSE,CREATE_NEW_CONSOLE,0,0,&si,&pi); //Wait till the Event is Signaled from the called child application WaitForSingleObject(hEvent,INFINITE); CloseHandle(pi.hProcess); CloseHandle(hEvent); In the child application(i.e the called Exe) BOOL CWFPDlg::OnInitDialog() { ... HANDLE hEvent; hEvent=CreateEvent(NULL,FALSE,FALSE,"WFP_EVENT"); if(GetLastError()==ERROR_ALREADY_EXISTS) { hEvent=OpenEvent(EVENT_MODIFY_STATE,FALSE,"WFP_EVENT"); SetEvent(hEvent);//Signal the event } ... }

            N Offline
            N Offline
            Naveen
            wrote on last edited by
            #5

            poda123 wrote:

            if(GetLastError()==ERROR_ALREADY_EXISTS) { hEvent=OpenEvent(EVENT_MODIFY_STATE,FALSE,"WFP_EVENT");

            why r u openeing the event again. u already have the handle of the event?I think u can skip that line..

            nave

            P 1 Reply Last reply
            0
            • M Michael Dunn

              Check out WaitForInputIdle()

              --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

              P Offline
              P Offline
              poda
              wrote on last edited by
              #6

              Hey... WaitForInputIdle() also works,if I specify the delay time as INFINITE. Great! Thanks a lot. Thank you!

              1 Reply Last reply
              0
              • N Naveen

                poda123 wrote:

                if(GetLastError()==ERROR_ALREADY_EXISTS) { hEvent=OpenEvent(EVENT_MODIFY_STATE,FALSE,"WFP_EVENT");

                why r u openeing the event again. u already have the handle of the event?I think u can skip that line..

                nave

                P Offline
                P Offline
                poda
                wrote on last edited by
                #7

                That's right! Thank you!

                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