Thread: Executing a program from within another program

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    27

    Executing a program from within another program

    I am currently working in a group on a school project to create a Real Time Strategy game. I am trying to execute a game server program when one of the client's decides to 'Host' a game.

    I am using the spawnl function. The problem is that the newly executed program runs in the same console program as the test client. The real client will be a Win32 app, so I don't know if it is truley an issue yet or not. But I would like to be able to separate my client and server into different console windows. Anyone have any ideas?

    Code:
     spawnl(P_NOWAIT, "ServerMain.exe", "ServerMain.exe", strMapFilePath, iMaxPlayers, NULL );

    iMaxPlayers is being used as a string, so no issues there.

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Here is a windows function to suit your needs. It is based on code from FAQ: How do I run a program from within a program.

    Code:
    #include <windows.h>
    #include <cstdio>
    using namespace std;
    
    BOOL SpawnInNewConsole(LPCTSTR szPath)
    {
      STARTUPINFO si = { 0 };     // Defines how to start the program
      PROCESS_INFORMATION pi;     // Gives info on the thread and..
                                  // ..process for the new process
    
      si.cb = sizeof(si);         // Must set size of structure
    
      BOOL bRet = CreateProcess(
            szPath, // Path to executable file
            NULL,   // Command string - not needed here
            NULL,   // Process handle not inherited
            NULL,   // Thread handle not inherited
            FALSE,  // No inheritance of handles
            CREATE_NEW_CONSOLE,   // Create a new console for program
            NULL,   // Same environment block as this prog
            NULL,   // Current directory - no separate path
            &si,    // Pointer to STARTUPINFO
            &pi);   // Pointer to PROCESS_INFORMATION
    
      if(bRet)
      {
        CloseHandle(pi.hThread);    // Close handle to thread
        CloseHandle(pi.hProcess);   // Close handle to process
      }
    
      return bRet;
    }
    
    int main(void)
    {
      if (!SpawnInNewConsole("ServerMain.exe"))
      {
        printf("Failed to launch program!");
      }
    
      return 0;
    }

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    27
    Okay, so for the command string, would I seperate the things I need to send into it with spaces, or how does it delimit multiple strings that need to be sent in? My executable program requires argv[] for how it is initialized. I need to send both strMapFilePath and iMaxPlayers into it upon initialization.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 40
    Last Post: 09-01-2006, 12:09 AM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. Executing a program
    By trancedeejay in forum C Programming
    Replies: 7
    Last Post: 03-06-2006, 08:55 AM
  4. Problem executing sample program:
    By mrabiu in forum C++ Programming
    Replies: 4
    Last Post: 03-13-2004, 06:44 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM