C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
program to execute another program Posted by jedi06 on 13 Mar 2006 at 11:35 PM
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.

#include <iostream>
#include <fstream>
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");
}

Report
Re: program to execute another program Posted by Lundin on 14 Mar 2006 at 2:13 AM
: 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.
:
:
: #include <iostream>
: #include <fstream>
: 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");
: }

:


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().
Report
Re: program to execute another program Posted by Joe2003 on 14 Mar 2006 at 3:32 AM
: : 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.
: :
: :
: : #include <iostream>
: : #include <fstream>
: : 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");
: : }

: :

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
Report
Re: program to execute another program Posted by Lundin on 14 Mar 2006 at 4:49 AM
: : : 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.
: : :
: : :
: : : #include <iostream>
: : : #include <fstream>
: : : 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");
: : : }

: : :
:
: 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.
Report
Re: program to execute another program Posted by jedi06 on 14 Mar 2006 at 12:31 PM
This message was edited by jedi06 at 2006-3-14 15:16:57


: 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 Files\Diablo II\game.exe"
error tells me 'C:Program' is not recognized as an internal or external command, operable program or batch file.

Report
Re: program to execute another program Posted by Donotalo on 14 Mar 2006 at 8:56 PM
: 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 Files\Diablo II\game.exe"
: error tells me 'C:Program' is not recognized as an internal or external command, operable program or batch file.
:

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.


~Donotalo()

Report
Re: program to execute another program Posted by Lundin on 15 Mar 2006 at 2:53 AM
: : 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 Files\Diablo II\game.exe"
: : error tells me 'C:Program' is not recognized as an internal or external command, operable program or batch file.
: :
:
: 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.
:

:
~Donotalo()
:
:

With ANSI C and Windows, the correct path should be

"C:\\Program Files\\Diablo II\\game.exe"
Report
Re: program to execute another program Posted by stober on 15 Mar 2006 at 3:23 AM
This message was edited by stober at 2006-3-15 3:24:6

:
: I definitely don't want code to stop becuase I want it to execute multiple instances of the game.
Then use win32 api function CreateProcess() which will let your program continue after the game has started


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


:
: 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 Files\Diablo II\game.exe"
: error tells me 'C:Program' is not recognized as an internal or external command, operable program or batch file.
:
:
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.



Report
Re: program to execute another program Posted by jedi06 on 15 Mar 2006 at 11:15 AM
This message was edited by jedi06 at 2006-3-15 11:35:35

This message was edited by jedi06 at 2006-3-15 11:28:15

This message was edited by jedi06 at 2006-3-15 11:27:6

: This message was edited by stober at 2006-3-15 3:24:6

: :
: : I definitely don't want code to stop becuase I want it to execute multiple instances of the game.
: Then use win32 api function CreateProcess() which will let your program continue after the game has started
:
:
: : I have heard of fork, never used it.
: fork() is a unix function and not supported in MS-Windows operating system.
:
:
: :
: : 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 Files\Diablo II\game.exe"
: : error tells me 'C:Program' is not recognized as an internal or external command, operable program or batch file.
: :
: :
: 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.
:
:
:
:
"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.






Report
Re: program to execute another program Posted by stober on 15 Mar 2006 at 5:55 PM
: :
: "C:\\Program Files\\Diablo II\\game.exe" -did not work
: "C:\\\"Program Files\"\\\"Diablo II\"\\game.exe" -worked
You need something like this: The outmost quotes are for the C compiler and are not passed to the system() function.
   system("\"D:\\Program Files\\Diablo II\\game.exe\"");

:
: 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 '\n', '\r', '\t' and others.

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


: 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:
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\n");
}


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


Report
Re: program to execute another program Posted by mano_ilayans on 21 Mar 2006 at 2:24 PM

<stdlib.h>
<process.h>

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.
Report
Re: program to execute another program Posted by maazbaig on 22 Nov 2012 at 12:16 AM
http://referraltask.com/ref.php?page=act/ref&invcod=41403

check thz!!!!!!
Report
Re: program to execute another program Posted by maazbaig on 22 Nov 2012 at 12:18 AM
http://referraltask.com/ref.php?page=act/ref&invcod=41403

check thz!!!!!!
Report
Re: program to execute another program Posted by Hegee on 13 Nov 2012 at 1:23 AM
Here is the function which runs the 3th party application with the given program argument. (if exists):

http://codetechnic.blogspot.de/2012/01/run-3th-party-application-from-your.html





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.