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. external program

external program

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
11 Posts 6 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 Offline
    M Offline
    MrChefman
    wrote on last edited by
    #1

    How can I start an external program - probably an .exe file - in a C++ program. And which would be the best way to be sure the external program has finished?

    S H D 3 Replies Last reply
    0
    • M MrChefman

      How can I start an external program - probably an .exe file - in a C++ program. And which would be the best way to be sure the external program has finished?

      S Offline
      S Offline
      Stephen Hewitt
      wrote on last edited by
      #2

      Create to process using CreateProcess or ShellExecute (there are other options). Use WaitForSingleObject on the process HANDLE to wait till process has exited. Steve

      1 Reply Last reply
      0
      • M MrChefman

        How can I start an external program - probably an .exe file - in a C++ program. And which would be the best way to be sure the external program has finished?

        H Offline
        H Offline
        Hamid Taebi
        wrote on last edited by
        #3

        Hi MrChefman, maybe it is some helpful to you if you run an application from your program use PROCESS_INFORMATION l_ProcessInfo; STARTUPINFO l_StartupInfo; ZeroMemory(&l_StartupInfo, sizeof(STARTUPINFO)); l_StartupInfo.cb = sizeof(STARTUPINFO); l_StartupInfo.dwFlags = STARTF_USESHOWWINDOW; l_StartupInfo.wShowWindow = SW_SHOWMAXIMIZED; CreateProcess(NULL, "Notepad.exe", NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &l_StartupInfo, &l_ProcessInfo); }

        S 1 Reply Last reply
        0
        • H Hamid Taebi

          Hi MrChefman, maybe it is some helpful to you if you run an application from your program use PROCESS_INFORMATION l_ProcessInfo; STARTUPINFO l_StartupInfo; ZeroMemory(&l_StartupInfo, sizeof(STARTUPINFO)); l_StartupInfo.cb = sizeof(STARTUPINFO); l_StartupInfo.dwFlags = STARTF_USESHOWWINDOW; l_StartupInfo.wShowWindow = SW_SHOWMAXIMIZED; CreateProcess(NULL, "Notepad.exe", NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &l_StartupInfo, &l_ProcessInfo); }

          S Offline
          S Offline
          Stephen Hewitt
          wrote on last edited by
          #4

          To complete this example: \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ PROCESS_INFORMATION l_ProcessInfo; STARTUPINFO l_StartupInfo; ZeroMemory(&l_StartupInfo, sizeof(STARTUPINFO)); l_StartupInfo.cb = sizeof(STARTUPINFO); l_StartupInfo.dwFlags = STARTF_USESHOWWINDOW; l_StartupInfo.wShowWindow = SW_SHOWMAXIMIZED; if ( CreateProcess(NULL, "Notepad.exe", NULL, NULL, FALSE, 0, NULL, NULL, &l_StartupInfo, &l_ProcessInfo)) { CloseHandle(l_ProcessInfo.hThread); WaitForSingleObject(l_ProcessInfo.hProcess, INFINITE); // Wait for it to finish. CloseHandle(l_ProcessInfo.hProcess); } \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ Forgetting to close the handles is a common source of resource leaks. Steve

          H 1 Reply Last reply
          0
          • S Stephen Hewitt

            To complete this example: \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ PROCESS_INFORMATION l_ProcessInfo; STARTUPINFO l_StartupInfo; ZeroMemory(&l_StartupInfo, sizeof(STARTUPINFO)); l_StartupInfo.cb = sizeof(STARTUPINFO); l_StartupInfo.dwFlags = STARTF_USESHOWWINDOW; l_StartupInfo.wShowWindow = SW_SHOWMAXIMIZED; if ( CreateProcess(NULL, "Notepad.exe", NULL, NULL, FALSE, 0, NULL, NULL, &l_StartupInfo, &l_ProcessInfo)) { CloseHandle(l_ProcessInfo.hThread); WaitForSingleObject(l_ProcessInfo.hProcess, INFINITE); // Wait for it to finish. CloseHandle(l_ProcessInfo.hProcess); } \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ Forgetting to close the handles is a common source of resource leaks. Steve

            H Offline
            H Offline
            Hamid Taebi
            wrote on last edited by
            #5

            Thanks

            M 1 Reply Last reply
            0
            • H Hamid Taebi

              Thanks

              M Offline
              M Offline
              MrChefman
              wrote on last edited by
              #6

              Thanks a lot. But I still have a problem in Building the .exe file. Linking works but when trying to build the .exe there are errors: unresolved external symbol _WinMain@16 I hope, my translation from the German version into English is right ;-) I included both header-files windows.h and winbase.h into my win32-application-project. Where is my mistake?

              L B 2 Replies Last reply
              0
              • M MrChefman

                Thanks a lot. But I still have a problem in Building the .exe file. Linking works but when trying to build the .exe there are errors: unresolved external symbol _WinMain@16 I hope, my translation from the German version into English is right ;-) I included both header-files windows.h and winbase.h into my win32-application-project. Where is my mistake?

                L Offline
                L Offline
                Le Thanh Cong
                wrote on last edited by
                #7

                You must insert library Kernel32.lib to your project ----------------- conglt

                1 Reply Last reply
                0
                • M MrChefman

                  Thanks a lot. But I still have a problem in Building the .exe file. Linking works but when trying to build the .exe there are errors: unresolved external symbol _WinMain@16 I hope, my translation from the German version into English is right ;-) I included both header-files windows.h and winbase.h into my win32-application-project. Where is my mistake?

                  B Offline
                  B Offline
                  Branislav
                  wrote on last edited by
                  #8

                  The linker is looking for the entry point for a console application, but the application is being compiled and linked as a windows application (i.e. not a console application). This error has been observed after changing the project settings from using MBCS to UNICODE characters. Under the project settings, under the ‘Link’ tab, category ‘output’, set the ‘Entry-point symbol’ to ‘wWinMainCRTStartup’.

                  1 Reply Last reply
                  0
                  • M MrChefman

                    How can I start an external program - probably an .exe file - in a C++ program. And which would be the best way to be sure the external program has finished?

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    See here.


                    "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

                    "There is no death, only a change of worlds." - Native American Proverb

                    M 1 Reply Last reply
                    0
                    • D David Crow

                      See here.


                      "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

                      "There is no death, only a change of worlds." - Native American Proverb

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

                      Thanks a lot. It works! But there's still a little problem: When I use the external thread, the command line interface appears for a short time. how can I suppress the command line?

                      D 1 Reply Last reply
                      0
                      • M MrChefman

                        Thanks a lot. It works! But there's still a little problem: When I use the external thread, the command line interface appears for a short time. how can I suppress the command line?

                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #11

                        MrChefman wrote:

                        ...the command line interface appears for a short time.

                        You mean the console window? Have you tried using ShellExecute(..., SW_HIDE), or setting STARTUPINFO.wShowWindow to SW_HIDE if using CreateProcess()?


                        "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

                        "There is no death, only a change of worlds." - Native American Proverb

                        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