Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

program to execute another program

jedi06jedi06 Posts: 38Member
Is there a way to write a program to execute another program. I'm trying to start a game without me actually having to double click the icon. I tried writing a program below to just open the file game.exe but that doesn't work.

[code]
#include
#include
using namespace std;
int main(){
ifstream inFile;
inFile.open("game.exe");
if(!inFile){

cout<<"Cannot open file bish."<<endl;
system("pause");
return 1;
}

system("pause");
}[/code]
«1

Comments

  • LundinLundin Posts: 3,711Member
    : Is there a way to write a program to execute another program. I'm trying to start a game without me actually having to double click the icon. I tried writing a program below to just open the file game.exe but that doesn't work.
    :
    : [code]
    : #include
    : #include
    : using namespace std;
    : int main(){
    : ifstream inFile;
    : inFile.open("game.exe");
    : if(!inFile){
    :
    : cout<<"Cannot open file bish."<<endl;
    : system("pause");
    : return 1;
    : }
    :
    : system("pause");
    : }[/code]
    :


    Nothing wrong with .bat files... but if you want to do this from a program you could either use system("game.exe") or a Win API function like ShellExecute().
  • Joe2003Joe2003 Posts: 92Member
    : : Is there a way to write a program to execute another program. I'm trying to start a game without me actually having to double click the icon. I tried writing a program below to just open the file game.exe but that doesn't work.
    : :
    : : [code]
    : : #include
    : : #include
    : : using namespace std;
    : : int main(){
    : : ifstream inFile;
    : : inFile.open("game.exe");
    : : if(!inFile){
    : :
    : : cout<<"Cannot open file bish."<<endl;
    : : system("pause");
    : : return 1;
    : : }
    : :
    : : system("pause");
    : : }[/code]
    : :

    You could use :-

    system("./game");

    or whatever the file is called, but system will not return until the game has finished running (basically the C code will pause here untill the game has finished). If you still want control of the C code then I would do this:-

    int i;

    i = fork();

    if (i == 0){
    system("whatever command you use to run the game..");
    }
    else{
    rest of your program here
    }




    ..........I think :-s
  • LundinLundin Posts: 3,711Member
    : : : Is there a way to write a program to execute another program. I'm trying to start a game without me actually having to double click the icon. I tried writing a program below to just open the file game.exe but that doesn't work.
    : : :
    : : : [code]
    : : : #include
    : : : #include
    : : : using namespace std;
    : : : int main(){
    : : : ifstream inFile;
    : : : inFile.open("game.exe");
    : : : if(!inFile){
    : : :
    : : : cout<<"Cannot open file bish."<<endl;
    : : : system("pause");
    : : : return 1;
    : : : }
    : : :
    : : : system("pause");
    : : : }[/code]
    : : :
    :
    : You could use :-
    :
    : system("./game");
    :
    : or whatever the file is called, but system will not return until the game has finished running (basically the C code will pause here untill the game has finished). If you still want control of the C code then I would do this:-
    :
    : int i;
    :
    : i = fork();
    :
    : if (i == 0){
    : system("whatever command you use to run the game..");
    : }
    : else{
    : rest of your program here
    : }
    :
    :
    :
    :
    : ..........I think :-s
    :


    Don't use fork() in Windows... get some handle to the game (with EnumWindows, FindWindow etc) and poll that handle now and then from your program. Or make a thread executing the system() / ShellExecute() call.
  • jedi06jedi06 Posts: 38Member
    [b][red]This message was edited by jedi06 at 2006-3-14 15:16:57[/red][/b][hr]

    : Don't use fork() in Windows... get some handle to the game (with EnumWindows, FindWindow etc) and poll that handle now and then from your program. Or make a thread executing the system() / ShellExecute() call.
    :

    I definitely don't want code to stop becuase I want it to execute multiple instances of the game. I have heard of fork, never used it.

    When I try to execute the game with system("game.exe") it works within the game folder. But if i try to put the complete path of game.exe it does not work I think becuase windows has spaces in all its folders.
    so the pathname is "C:Program FilesDiablo IIgame.exe"
    error tells me 'C:Program' is not recognized as an internal or external command, operable program or batch file.

  • DonotaloDonotalo Posts: 715Member
    : I definitely don't want code to stop becuase I want it to execute multiple instances of the game. I have heard of fork, never used it.
    :
    : When I try to execute the game with system("game.exe") it works within the game folder. But if i try to put the complete path of game.exe it does not work I think becuase windows has spaces in all its folders.
    : so the pathname is "C:Program FilesDiablo IIgame.exe"
    : error tells me 'C:Program' is not recognized as an internal or external command, operable program or batch file.
    :
    [purple]
    u can try this pathname: "C:\"Program Files"\"Diablo II"\game.exe"
    or: "C:\Progra~1\Diablo~1\game.exe" if Diablo II is the only folder that starts with Diablo. otherwise, i think u need to use Diablo~2 or something like this.
    [/purple]
    [hr][purple]~Donotalo()[/purple]

  • LundinLundin Posts: 3,711Member
    : : I definitely don't want code to stop becuase I want it to execute multiple instances of the game. I have heard of fork, never used it.
    : :
    : : When I try to execute the game with system("game.exe") it works within the game folder. But if i try to put the complete path of game.exe it does not work I think becuase windows has spaces in all its folders.
    : : so the pathname is "C:Program FilesDiablo IIgame.exe"
    : : error tells me 'C:Program' is not recognized as an internal or external command, operable program or batch file.
    : :
    : [purple]
    : u can try this pathname: "C:\"Program Files"\"Diablo II"\game.exe"
    : or: "C:\Progra~1\Diablo~1\game.exe" if Diablo II is the only folder that starts with Diablo. otherwise, i think u need to use Diablo~2 or something like this.
    : [/purple]
    : [hr][purple]~Donotalo()[/purple]
    :
    :

    With ANSI C and Windows, the correct path should be

    "C:\Program Files\Diablo II\game.exe"
  • stoberstober Posts: 9,765Member ✭✭✭
    [b][red]This message was edited by stober at 2006-3-15 3:24:6[/red][/b][hr]
    :
    : I definitely don't want code to stop becuase I want it to execute multiple instances of the game.
    [blue]Then use win32 api function CreateProcess() which will let your program continue after the game has started[/blue]


    : I have heard of fork, never used it.
    [blue]fork() is a unix function and not supported in MS-Windows operating system.[/blue]


    :
    : When I try to execute the game with system("game.exe") it works within the game folder. But if i try to put the complete path of game.exe it does not work I think becuase windows has spaces in all its folders.
    : so the pathname is "C:Program FilesDiablo IIgame.exe"
    : error tells me 'C:Program' is not recognized as an internal or external command, operable program or batch file.
    :
    :
    [blue]I play Diablo II a lot too. The game will not allow multiple instances of the game on the same computer. There is really no point in doing that because, like most games, it takes over the entire monitor for game play.[/blue]



  • jedi06jedi06 Posts: 38Member
    [b][red]This message was edited by jedi06 at 2006-3-15 11:35:35[/red][/b][hr]
    [b][red]This message was edited by jedi06 at 2006-3-15 11:28:15[/red][/b][hr]
    [b][red]This message was edited by jedi06 at 2006-3-15 11:27:6[/red][/b][hr]
    : [b][red]This message was edited by stober at 2006-3-15 3:24:6[/red][/b][hr]
    : :
    : : I definitely don't want code to stop becuase I want it to execute multiple instances of the game.
    : [blue]Then use win32 api function CreateProcess() which will let your program continue after the game has started[/blue]
    :
    :
    : : I have heard of fork, never used it.
    : [blue]fork() is a unix function and not supported in MS-Windows operating system.[/blue]
    :
    :
    : :
    : : When I try to execute the game with system("game.exe") it works within the game folder. But if i try to put the complete path of game.exe it does not work I think becuase windows has spaces in all its folders.
    : : so the pathname is "C:Program FilesDiablo IIgame.exe"
    : : error tells me 'C:Program' is not recognized as an internal or external command, operable program or batch file.
    : :
    : :
    : [blue]I play Diablo II a lot too. The game will not allow multiple instances of the game on the same computer. There is really no point in doing that because, like most games, it takes over the entire monitor for game play.[/blue]
    :
    :
    :
    :
    "C:\Program Files\Diablo II\game.exe" -did not work
    "C:\"Program Files"\"Diablo II"\game.exe" -worked

    What is up with all the slashes and quotes. What are they doing?


    stober, do I need a certain header for win32 api function CreateProcess()?

    stober, I load up 40 instances of d2 using d2loader on 3 comps and play them in window mode with the -w command after the target in the shortcut properties.

    I'm just tired loading them all up all the time. I thought this might be an easy program to make. I just need them to load up just like i was double clicking the shortcuts, its a little more complicated than that but that would be a great start.






  • stoberstober Posts: 9,765Member ✭✭✭
    : :
    : "C:\Program Files\Diablo II\game.exe" -did not work
    : "C:\"Program Files"\"Diablo II"\game.exe" -worked
    [blue]You need something like this: The outmost quotes are for the C compiler and are not passed to the system() function. [/blue]
    [code]
    system(""D:\Program Files\Diablo II\game.exe"");
    [/code]
    :
    : What is up with all the slashes and quotes. What are they doing?
    :

    you need double quote when you wnat a literal slash in the string. Otherwise the C compiler will attempt to interpret the next character as an escape character, such as '
    ', '
    ', ' ' and others.

    :
    : stober, do I need a certain header for win32 api function CreateProcess()?
    :
    [blue] include windows.h[/blue]


    : stober, I load up 40 instances of d2 using d2loader on 3 comps and play them in window mode with the -w command after the target in the shortcut properties.
    :

    The below worked on my computer, where I installed Diablo on drive D:
    [code]
    STARTUPINFO sinfo;
    PROCESS_INFORMATION pinfo;
    memset(&sinfo,0,sizeof(STARTUPINFO));
    memset(&pinfo,0,sizeof(PROCESS_INFORMATION));
    sinfo.cb = sizeof(STARTUPINFO);

    if( CreateProcess(0,""D:\Program Files\Diablo II\game.exe"", 0,0,0,0,0,0,&sinfo,&pinfo)== 0)
    {
    printf("Create Process failed
    ");
    }
    [/code]

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/createprocess.asp


  • mano_ilayansmano_ilayans Posts: 6Member




    int i;
    ......
    ......
    i=spawnl(P_WAIT,"mano.exe",NULL);

    if(i=-1)
    {
    printf("Invalid File calling operation...");
    exit(1);
    }
    .....
    .....

    Here mano.exe should be present in the current working directory.
«1
Sign In or Register to comment.