Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Storing and Executing paths

Status
Not open for further replies.

d0s

Programmer
Apr 15, 2004
48
US
Im making a C++ console application(will probably later convert it to a WinAPI program)that will ask which game they want to play. Well, when trying to store the path of the game "C:\wutever\somegame.exe" i don't know which way to store it into a variable. Then my second problem is probably going to be how to execute it. i can't really test this part yet till i figure out how to store the path. could i use return variable; ?? I did a search on executing a Windows program and found using WinExec() or CreateProcess(). can these functions be used in a console application or can they only be used on an WinAPI application? if thats the case, whats the best way to execute a program? Thanks
 
Store it in a STL string or character buffer - you could get the full string from the console with cin.getline(). Once you have that, it should not be a problem to run it with WinExec as you said. As long as you #include the necessary windows files, it will work in a console app, it just won't be cross platform. If that doesn't matter then go ahead and use it.
 
ok i used string to store the path and used WinExec and compiles with no errors. But after i type in the input and return it, it just closes out without executing the program. Any ideas? thanks

Code:
#include <iostream>
#include <windows.h>
using namespace std;

int main()
{
int moo;
string t2 = "C:\\Dynamix\\Tribes2\\GameData\\SierraUp.exe";

cout<<"1.Tribes\n";
cout<<"2.Planetside\n";
cout<<"3.Call of Duty\n";
cout<<"4.Unreal Tournament\n";
cout<<"Which game do you want to play: ";
cin>>moo;
switch(moo) 
{
	case 1:
	WinExec("t2", SW_SHOW);
    
}
	return 0;
}
 
Try
WinExec(t2, SW_SHOW);

without the quotes.


/Per

&quot;It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure.&quot;
 
i have to use quotes otherwise the compiler gives error:
'WinExec' : cannot convert parameter 1 from 'std::string' to 'LPCSTR'
 
It's
Code:
WinExec(t2.c_str(), SW_SHOW);
if you want to get a 'C' style string out of a C++ string object.

--
 
Sweet it works, well the programs don't open with some windows error but the WinExec works now...sweet thanks....btw if i was to use ShellExecute would i use the t2.c_str() also? Thanks again
 
Ok i figured out why the errors are coming up. Its because its looking for the game within the directory of the console program. do you think SetCurrentDirectory would work for that kind of problem? thanks
 
If you know where you're looking, do something like this after they input the file name.

t2 = "C:\\Program Files\\[the rest of the path]" + t2
 
Use CreateProcess instead of old (now deprecated) WinExec() 16-bit compatibility function (see MSDN or compiler help). CreateProcess() has a special parameter for child process working directory setting.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top