2

I am trying to develop a system that will allow me to compile a .exe that can run other programs I make. Right now, it can, but it can only run the external programs once. Everything runs in one cmd window. I type a command, it does the action(running a seperate .exe) then waits for a second action.

I'll try to simplify what I'm doing as much as possible.

The running .exe. Lets call it TheCauser.exe

int main()
{
.
.
.
if(stuff is met)
                {
                    .
                    .
                    .
                    system(foundtextchar);//Windows run program
                    cout << endl;
                }
}

The .exe to be run from the code above. Lets call it DoMe.exe

int main()
{
    //It just does whatever
    .
    .
    .
    return 0;
} 

Absolutely basic. Though things do run smoothly I can only run DoMe.exe and have the material appear on the cmd window once. Kind of. I have a small notification in TheCauser.exe that tells me when DoMe.exe is running. When I run DoMe.exe a second time that notification comes up but no material from DoMe.exe. My assumption is, that once DoMe.exe runs the first time, it never really closes but just continues to run.

I feel it's also important to mention, if I were to have a second program I wanted to run, lets call it, HeyListen.exe, if DoMe.exe was running earlier, HeyListen.exe would not show it's material but the notification would pop up saying it is running. HeyListen.exe would be built the same way as DoMe.exe.

I feel as if my problem is in DoMe.exe, in that, it doesn't end it's process the way I'm hoping it does. Is this correct? How could I get this to work?

I wanted to post a picture of the cmd window to help give a visual, but apparently I don't have enough reputation. Sorry.

6
  • Posting a picture of the command window wouldn't do anything other than get you downvoted into oblivion. If there is something on that command window you want us to see, cut and paste the text into your question. Use an image as a last resort. Other than that, what does the debugger show you when you run your code? Commented Sep 7, 2015 at 20:38
  • Thats good to know. I'll remember that for the future. Honestly, nothing. Everything works cleanly. No warnings or errors. Even when I run DoMe.exe a second time nothing comes up. Is it how DoMe.exe ends code wise, that it's not closing the way I think it is? I should have mentioned that I want the code to execute then end. Then the cmd waits for my next input. Commented Sep 7, 2015 at 20:45
  • You can test your conjecture that doMe is still running with the task manager. (CTRL+ALT+DELETE and click task manager) Commented Sep 7, 2015 at 20:50
  • Going to try that. give me a sec. Commented Sep 7, 2015 at 20:53
  • @user4581301 Everything runs on the same cmd window and when I run DoMe.exe, it's actions are so minute, they don't change the space it's taking up. Commented Sep 7, 2015 at 20:56

1 Answer 1

1

Spawn the programs using CreateProcess(). (https://msdn.microsoft.com/en-us/library/windows/desktop/ms682425%28v=vs.85%29.aspx)

Example (from MSDN)

#include <windows.h>
#include <stdio.h>
#include <tchar.h>

void _tmain( int argc, TCHAR *argv[] )
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

    if( argc != 2 )
    {
        printf("Usage: %s [cmdline]\n", argv[0]);
        return;
    }

    // Start the child process. 
    if( !CreateProcess( NULL,   // No module name (use command line)
        argv[1],        // 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
    ) 
    {
        printf( "CreateProcess failed (%d).\n", GetLastError() );
        return;
    }

    // Wait until child process exits.
    WaitForSingleObject( pi.hProcess, INFINITE );

    // Close process and thread handles. 
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
}
Sign up to request clarification or add additional context in comments.

4 Comments

I see. I'm trying to find some examples. From sources I've found, one from being on this site is this function, Opens an exe, Waits for exe to finish, then closes all handles. Correct?
That's a much better way of doing spawning windows processes than system. For one thing you get a handle back that you can use to check process state and wait on termination. But you should summarize the link so the answer isn't rendered useless when the MSDN page location gets juggled.
Oh, sorry. Good idea. Unfortunately, I don’t have the opportunity to test any code right now, but Google should be able to find some examples. I’ll edit.
Quoted the example from MSDN.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.