How do you make a C++ program open another file. Like if the program asked to type in a game and you typed in minesweeper it would open up minesweeper? Thanks in advance!
how to make a program open another programCan anyone tell me how to make a program open another program or file?
Page 1 of 1
8 Replies - 48689 Views - Last Post: 03 August 2008 - 04:13 AM
Replies To: how to make a program open another program
#2
Re: how to make a program open another program
Posted 02 August 2008 - 05:08 PM
There is the standard system() function which works on most platforms.
If you need more control and monitoring there are more sophisticated options. For unix systems there is the fork() function and the exec() functions. For Windows specifically there is the CreateProcess() function.
If you need more control and monitoring there are more sophisticated options. For unix systems there is the fork() function and the exec() functions. For Windows specifically there is the CreateProcess() function.
#3
Re: how to make a program open another program
Posted 02 August 2008 - 07:15 PM
Ok, here is my code but where it says "CreateProcess" that doesnt work. IT comes up with an error, can you make it so that it opens up that file?
Thank You in advance
#include<iostream>
#include<string>
using namespace std;
int main(void)
{
beginning:
system("CLS");
system("TITLE Enter Password");
string playgames;
string answer;
string password;
cout << "PASSWORD: ";
cin >> password;
if (password == "1990101172723630767"){
system("TITLE Welcome");
system("CLS");
cout << "Welcome, master HAXIFIX" << endl;
system("PAUSE");
goto playgames;
}else {
cout << "Incorrect Username" << endl;
cout << "-- CONNECTION TERMINATED --" << endl;
system("PAUSE");
goto exit;
}
exit:
return 0;
playgames:
system("CLS");
system("TITLE Hello, Mr. HAXIFIX");
cout << "Would you like to play a game?" << endl;
cout << "Answer: ";
cin >> playgames;
if (playgames == "Yes" || playgames == "yes"){
goto whatgame;
}else {
cout << "Ok, I will ask later." << endl;
system("PAUSE");
goto playgames;
}
whatgame:
system("CLS");
cout << "What game would you like to play?" << endl;
cout << "If you don't know a game please type 'list games'" << endl;
cout << "Answer: ";
getline (cin, answer);
if (answer == "List Games" || answer == "List games" || answer == "list games" || answer == "list Games"){
cout << "Minesweeper" << endl;
cout << "Solitaire" << endl;
cout << "Pinball" << endl;
system("PAUSE");
goto whatgame;
}else if (answer == "Minesweeper" || answer == "minesweeper"){
CreateProcess("%SystemRoot%\System32\winmine.exe");
system("PAUSE");
goto whatgame;
}else if (answer == "Solitaire" || answer == "solitaire"){
CreateProcess("%SystemRoot%\System32\sol.exe");
system("PAUSE");
goto whatgame;
}else if (answer == "Pinball" || answer == "pinball"){
CreateProcess("C:\Program Files\Windows NT\Pinball\PINBALL.EXE");
system("PAUSE");
goto whatgame;
}else {
goto whatgame;
}
return 0;
}
Thank You in advance
#4
Re: how to make a program open another program
Posted 02 August 2008 - 07:30 PM
I don't have a Windows system conveniently available for me to compile and test your code at the moment but CreateProcess() is a little more complicated then that. Take a look at the example on MSDN: http://msdn.microsof...512(VS.85).aspx
You should be able to just change those to system() calls though.
You should be able to just change those to system() calls though.
#5
Re: how to make a program open another program
Posted 02 August 2008 - 07:35 PM
Thanks, but I still don't understand how to make it open a file using that information. That peice of info didn't make sense to me. Sorry
#25
Re: how to make a program open another program
Posted 02 August 2008 - 09:48 PM
Instead of using CreateProcess() just use system() calls. The syntax fits with what you are trying to do. The only issue is that you are using Windows paths which seperate directories with '\'. That is the escape character so you have to escape it with another '\'. So it would look like this:
That will do what you are wanting. You should know you are really falling into some pit holes with some of what you are doing in this code. Such a reliance on goto can be considered potentially dangerous as well as messy. Instead you could use proper functions and loops to achieve what you are wanting to do. There is no situation where you will have no other option then goto and any other option is a better one.
system("%SystemRoot%\\System32\\winmine.exe");
That will do what you are wanting. You should know you are really falling into some pit holes with some of what you are doing in this code. Such a reliance on goto can be considered potentially dangerous as well as messy. Instead you could use proper functions and loops to achieve what you are wanting to do. There is no situation where you will have no other option then goto and any other option is a better one.
#250
Re: how to make a program open another program
Posted 02 August 2008 - 11:35 PM
The following code does the same thing you are trying to do but it makes use of functions, removes the unnecessary system() calls, and eliminates the goto() calls. It also demonstrates the use of CreateProcess() (which was taken directly from the example) and uses some other Windows specific code to do some of what you were doing with system() calls. I don't usually write Windows code (or even C++ for that matter as I am more a C guy) so if anyone has anything to add then feel free.
I didn't want to move to far away from what you were doing but this still isn't the exact way I would approach the problem. I would break even more code of from main() to start. If you have trouble understanding any of it feel free to ask questions but I used comments where functionality was used that wasn't found in your code. Good luck.
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
// This function will ask a user for a password.
int Authenticate()
{
string password;
cout << "Password: ";
cin >> password;
if(password == "12345")
return 1;
return 0;
}
// This function will list the available games.
void ListGames()
{
cout << "Games:" << endl;
cout << "Minesweeper" << endl;
cout << "Solitare" << endl;
cout << "Pinball" << endl;
// Sleep 500ms
_sleep(500);
}
// This function launches a game.
void LaunchGame(char *command)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( π, sizeof(pi) );
// Start the child process.
if( !CreateProcess( NULL, // No module name (use command line)
command, // 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
π ) // 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 );
}
int main(void)
{
string answer;
// Authenticate the user. If authentication fails quit.
if(!Authenticate())
{
cout << "Incorrect Username" << endl;
cout << "-- CONNECTION TERMINATED --" << endl;
_sleep(500);
exit(1);
}
// Set the console title.
SetConsoleTitle("Hello, Mr. HAXIFIX");
// Loop until the user quits or wants to play a game.
while((answer != "Yes") && (answer != "yes"))
{
// Empty the string so we can use it again.
answer.empty();
cout << "Would you like to play a game?" << endl;
cout << "Answer: ";
cin >> answer;
if((answer == "No") || (answer == "no"))
{
cout << "Ok, I will ask later." << endl;
// Sleep 1 second
_sleep(1000);
}
else if((answer == "quit") || (answer == "exit"))
{
cout << "Exiting now..." << endl;
exit(1);
}
}
// Clear the console.
system("CLS");
// Loop until the user is ready to quit.
while((answer != "quit") && (answer != "exit"))
{
// Empty the string so we can use it again.
answer.empty();
cout << "What game would you like to play?" << endl;
cout << "Type 'list games' for a list." << endl;
cout << "Answer: ";
// Flush stdin in case there are any stagnant carriage returns or line-feeds laying about.
fflush(stdin);
// We use getline() here because the answer might contain a space.
getline(cin, answer);
if((answer == "List Games") || (answer == "List games") || (answer == "list Games") || (answer == "list games"))
ListGames();
else if((answer == "Minesweeper") || (answer == "minesweeper"))
LaunchGame("C:\\Windows\\System32\\winmine.exe");
else if((answer == "Solitare") || (answer == "solitare"))
LaunchGame("C:\\Windows\\System32\\sol.exe");
else if((answer == "Pinball") || (answer == "pinball"))
LaunchGame("C:\\Program Files\\Windows NT\\Pinball\\PINBALL.EXE");
}
return 0;
}
I didn't want to move to far away from what you were doing but this still isn't the exact way I would approach the problem. I would break even more code of from main() to start. If you have trouble understanding any of it feel free to ask questions but I used comments where functionality was used that wasn't found in your code. Good luck.
This post has been edited by Hyper_Eye: 02 August 2008 - 11:43 PM
#251
Re: how to make a program open another program
Posted 03 August 2008 - 04:11 AM
thank you alot. this helped alot and now i get how to make code with no goto's. helped alot thank you
#263
Re: how to make a program open another program
Posted 03 August 2008 - 04:13 AM
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|