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. A question for the advanced programers

A question for the advanced programers

Scheduled Pinned Locked Moved C / C++ / MFC
jsontutorialquestion
17 Posts 5 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.
  • M Magius96

    Thanks for the advice, but I can't use the first suggestion, because Prog2 is actually a dos program, and several copies of it could be running at once. I also can't use the 2nd suggestion, because Prog2 is already compiled. "Don't tell me I'm smart, I might actually believe you!"

    J Offline
    J Offline
    Jorgen Sigvardsson
    wrote on last edited by
    #7

    Doh. Disregard my previous post. I thought you were referring to Takas suggestion. -- This space for rent.

    1 Reply Last reply
    0
    • M Magius96

      Your answer seems to be closer to what I'm looking for, in that it sounds like I'd be checking for Prog2 by it's process id. That is precisely what I'm hoping for. The problem I'm having is that the book I've learned VC++ from doesn't give much(if any) information regarding CreateProcess() or WaitForSingleObject(). By typing them into my project, I can see what arguments they require, but I don't fully understand them. Guess I need a more advanced book than "Sam's Teach Yourself Visual C++ 6 in 21 Days", eh? (whew, long title, don't hold your breath while saying it) --EDIT-- Actually, now that I look at it. I do understand how to use WaitForSingleObject(), but I'm still confused on CreateProcess(). Oh, well, give me a few days, and I might puzzle that one out too. --END EDIT-- "Don't tell me I'm smart, I might actually believe you!"

      T Offline
      T Offline
      Taka Muraoka
      wrote on last edited by
      #8

      Yep. You need "Teach Yourself VC in 90 days", at least! :laugh: Sounds like you don't have the MSDN - you can look things up online here: msdn.microsoft.co[^]m (use the search box in the top-left corner)


      he he he. I like it in the kitchen! - Marc Clifton (on taking the heat when being flamed) Awasu v0.4a[^]: A free RSS reader with support for Code Project.

      M 1 Reply Last reply
      0
      • T Taka Muraoka

        Yep. You need "Teach Yourself VC in 90 days", at least! :laugh: Sounds like you don't have the MSDN - you can look things up online here: msdn.microsoft.co[^]m (use the search box in the top-left corner)


        he he he. I like it in the kitchen! - Marc Clifton (on taking the heat when being flamed) Awasu v0.4a[^]: A free RSS reader with support for Code Project.

        M Offline
        M Offline
        Magius96
        wrote on last edited by
        #9

        Used to have the full MSDN, but lost the disks, and had to reinstall my system at some point in the past. Really do miss em though. "Don't tell me I'm smart, I might actually believe you!"

        1 Reply Last reply
        0
        • T Taka Muraoka

          There's any number of ways you can do this but yes, CreateProcess() is probably the best. It will give you a HANDLE that represents the process that you can use to test if it is still running. You won't even need a timer - call WaitForSingleObject() with an INFINITE timeout and it will block until the process finishes.


          he he he. I like it in the kitchen! - Marc Clifton (on taking the heat when being flamed) Awasu v0.4a[^]: A free RSS reader with support for Code Project.

          M Offline
          M Offline
          Magius96
          wrote on last edited by
          #10

          After doing some rather deep digging here in the forums, I've found the following snippet: STARTUPINFO siInfo; PROCESS_INFORMATION piInfo; siInfo.cb = sizeof(STARTUPINFO); siInfo.lpReserved = 0; siInfo.lpDesktop = NULL; siInfo.lpTitle = NULL; siInfo.dwFlags = STARTF_USESHOWWINDOW; siInfo.cbReserved2 = 0; siInfo.lpReserved2 = NULL; CreateProcess("F:\\path\\program.exe", "", NULL, NULL, false, NORMAL_PRIORITY_CLASS, NULL, "F:\\path\\", &siInfo, &piInfo); Now, you were saying to pass the handle to WaitForSingleObject(), but which of the above is the handle? (plus, i'm still slightly confused on what all that is, but if it works, great) "Don't tell me I'm smart, I might actually believe you!"

          T 1 Reply Last reply
          0
          • M Magius96

            After doing some rather deep digging here in the forums, I've found the following snippet: STARTUPINFO siInfo; PROCESS_INFORMATION piInfo; siInfo.cb = sizeof(STARTUPINFO); siInfo.lpReserved = 0; siInfo.lpDesktop = NULL; siInfo.lpTitle = NULL; siInfo.dwFlags = STARTF_USESHOWWINDOW; siInfo.cbReserved2 = 0; siInfo.lpReserved2 = NULL; CreateProcess("F:\\path\\program.exe", "", NULL, NULL, false, NORMAL_PRIORITY_CLASS, NULL, "F:\\path\\", &siInfo, &piInfo); Now, you were saying to pass the handle to WaitForSingleObject(), but which of the above is the handle? (plus, i'm still slightly confused on what all that is, but if it works, great) "Don't tell me I'm smart, I might actually believe you!"

            T Offline
            T Offline
            Taka Muraoka
            wrote on last edited by
            #11

            Consulting my MSDN (:-)), CreateProcess() returns the following in piInfo:

            HANDLE hProcess; 
            HANDLE hThread; 
            DWORD dwProcessId; 
            DWORD dwThreadId; 
            

            where hProcess is the handle (i.e. an internal system cookie, if you like) for the process and hThread, a handle for the process's primary thread. The ID's are unique numbers for the process and thread but are not terribly useful. So, you pass the hProcess in to WaitForSingleObject(). If you specify a timeout of INFINITE, this call will block until the process ends. Or if you use 0, it will do a quick check to see if the process is alive at that moment. Or you can pass in a number of milliseconds - the call will wait for that amount of time for the process to finish and return a value indicating whether it did or not. Don't forget to call CloseHandle() for both these handles when you're done otherwise you'll get a leak.


            he he he. I like it in the kitchen! - Marc Clifton (on taking the heat when being flamed) Awasu v0.4a[^]: A free RSS reader with support for Code Project.

            M 1 Reply Last reply
            0
            • T Taka Muraoka

              Consulting my MSDN (:-)), CreateProcess() returns the following in piInfo:

              HANDLE hProcess; 
              HANDLE hThread; 
              DWORD dwProcessId; 
              DWORD dwThreadId; 
              

              where hProcess is the handle (i.e. an internal system cookie, if you like) for the process and hThread, a handle for the process's primary thread. The ID's are unique numbers for the process and thread but are not terribly useful. So, you pass the hProcess in to WaitForSingleObject(). If you specify a timeout of INFINITE, this call will block until the process ends. Or if you use 0, it will do a quick check to see if the process is alive at that moment. Or you can pass in a number of milliseconds - the call will wait for that amount of time for the process to finish and return a value indicating whether it did or not. Don't forget to call CloseHandle() for both these handles when you're done otherwise you'll get a leak.


              he he he. I like it in the kitchen! - Marc Clifton (on taking the heat when being flamed) Awasu v0.4a[^]: A free RSS reader with support for Code Project.

              M Offline
              M Offline
              Magius96
              wrote on last edited by
              #12

              Your answer, although informational, was still a bit cryptic. However, you did give me enough information to work with, that I could use msdn.microsoft.com to find the rest. This little bit of sample code, along with your advice, lead me to just what I was looking for. STARTUPINFO si; PROCESS_INFORMATION pi; ZeroMemory( &si, sizeof(si) ); si.cb = sizeof(si); ZeroMemory( &pi, sizeof(pi) ); // Start the child process. if( !CreateProcess( NULL, // No module name (use command line). "MyChildProcess", // Command line. NULL, // Process handle not inheritable. NULL, // Thread handle not inheritable. FALSE, // Set handle inheritance to FALSE. 0, // No creation flags. NULL, // Use parent's environment block. NULL, // Use parent's starting directory. &si, // Pointer to STARTUPINFO structure. &pi ) // Pointer to PROCESS_INFORMATION structure. ) { ErrorExit( "CreateProcess failed." ); } // Wait until child process exits. WaitForSingleObject( pi.hProcess, INFINITE ); // Close process and thread handles. CloseHandle( pi.hProcess ); CloseHandle( pi.hThread ); "Don't tell me I'm smart, I might actually believe you!"

              T T 2 Replies Last reply
              0
              • M Magius96

                Your answer, although informational, was still a bit cryptic. However, you did give me enough information to work with, that I could use msdn.microsoft.com to find the rest. This little bit of sample code, along with your advice, lead me to just what I was looking for. STARTUPINFO si; PROCESS_INFORMATION pi; ZeroMemory( &si, sizeof(si) ); si.cb = sizeof(si); ZeroMemory( &pi, sizeof(pi) ); // Start the child process. if( !CreateProcess( NULL, // No module name (use command line). "MyChildProcess", // Command line. NULL, // Process handle not inheritable. NULL, // Thread handle not inheritable. FALSE, // Set handle inheritance to FALSE. 0, // No creation flags. NULL, // Use parent's environment block. NULL, // Use parent's starting directory. &si, // Pointer to STARTUPINFO structure. &pi ) // Pointer to PROCESS_INFORMATION structure. ) { ErrorExit( "CreateProcess failed." ); } // Wait until child process exits. WaitForSingleObject( pi.hProcess, INFINITE ); // Close process and thread handles. CloseHandle( pi.hProcess ); CloseHandle( pi.hThread ); "Don't tell me I'm smart, I might actually believe you!"

                T Offline
                T Offline
                Taka Muraoka
                wrote on last edited by
                #13

                Magius96 wrote: Your answer, although informational, was still a bit cryptic. Well, we don't want to make it *too* easy for ya! What you've got looks pretty spot on.


                he he he. I like it in the kitchen! - Marc Clifton (on taking the heat when being flamed) Awasu v0.4a[^]: A free RSS reader with support for Code Project.

                M 1 Reply Last reply
                0
                • T Taka Muraoka

                  Magius96 wrote: Your answer, although informational, was still a bit cryptic. Well, we don't want to make it *too* easy for ya! What you've got looks pretty spot on.


                  he he he. I like it in the kitchen! - Marc Clifton (on taking the heat when being flamed) Awasu v0.4a[^]: A free RSS reader with support for Code Project.

                  M Offline
                  M Offline
                  Magius96
                  wrote on last edited by
                  #14

                  Ok, now that I've got that much working, perhaps you'd entertain me to give me a suggestion(tease me more like it), on how to convert a CString to char. I'm passing command line arguments from a user interface, but the darn compiler is complaining that they must be char's and not CString class. My luck eh? Almost complete, and I throw a wrench into the darn thing. "Don't tell me I'm smart, I might actually believe you!"

                  T 1 Reply Last reply
                  0
                  • M Magius96

                    Ok, now that I've got that much working, perhaps you'd entertain me to give me a suggestion(tease me more like it), on how to convert a CString to char. I'm passing command line arguments from a user interface, but the darn compiler is complaining that they must be char's and not CString class. My luck eh? Almost complete, and I throw a wrench into the darn thing. "Don't tell me I'm smart, I might actually believe you!"

                    T Offline
                    T Offline
                    Taka Muraoka
                    wrote on last edited by
                    #15

                    Cast it it a (LPCTSTR). Or look at GetBuffer(). Hmmm... Not cryptic enough, by far.


                    he he he. I like it in the kitchen! - Marc Clifton (on taking the heat when being flamed) Awasu v0.4a[^]: A free RSS reader with support for Code Project.

                    1 Reply Last reply
                    0
                    • M Magius96

                      Your answer, although informational, was still a bit cryptic. However, you did give me enough information to work with, that I could use msdn.microsoft.com to find the rest. This little bit of sample code, along with your advice, lead me to just what I was looking for. STARTUPINFO si; PROCESS_INFORMATION pi; ZeroMemory( &si, sizeof(si) ); si.cb = sizeof(si); ZeroMemory( &pi, sizeof(pi) ); // Start the child process. if( !CreateProcess( NULL, // No module name (use command line). "MyChildProcess", // Command line. NULL, // Process handle not inheritable. NULL, // Thread handle not inheritable. FALSE, // Set handle inheritance to FALSE. 0, // No creation flags. NULL, // Use parent's environment block. NULL, // Use parent's starting directory. &si, // Pointer to STARTUPINFO structure. &pi ) // Pointer to PROCESS_INFORMATION structure. ) { ErrorExit( "CreateProcess failed." ); } // Wait until child process exits. WaitForSingleObject( pi.hProcess, INFINITE ); // Close process and thread handles. CloseHandle( pi.hProcess ); CloseHandle( pi.hThread ); "Don't tell me I'm smart, I might actually believe you!"

                      T Offline
                      T Offline
                      Todd Smith
                      wrote on last edited by
                      #16

                      STARTUPINFO si = {0};
                      PROCESS_INFORMATION pi = {0};
                      si.cb = sizeof(si);

                      int rv = CreateProcess(0, pCmdLine, 0, 0, 0, 0, 0, 0, &si, &pi);
                      if (rv)
                      {
                      WaitForSingleObject(pi.hProcess, INFINITE);
                      CloseHandle(pi.hProcess);
                      CloseHandle(pi.hThread);
                      }

                      You made it this far. Why not just search google.com, msdn.microsoft.com, or THIS site for CreateProcess? Todd Smith

                      M 1 Reply Last reply
                      0
                      • T Todd Smith

                        STARTUPINFO si = {0};
                        PROCESS_INFORMATION pi = {0};
                        si.cb = sizeof(si);

                        int rv = CreateProcess(0, pCmdLine, 0, 0, 0, 0, 0, 0, &si, &pi);
                        if (rv)
                        {
                        WaitForSingleObject(pi.hProcess, INFINITE);
                        CloseHandle(pi.hProcess);
                        CloseHandle(pi.hThread);
                        }

                        You made it this far. Why not just search google.com, msdn.microsoft.com, or THIS site for CreateProcess? Todd Smith

                        M Offline
                        M Offline
                        Magius96
                        wrote on last edited by
                        #17

                        I'm sorry, I had forgotten to post that I figured it out after the previously not so cryptic message. Thanks for your help anyways. "Don't tell me I'm smart, I might actually believe 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