Alternatives to system()

yeah yeah, system is bad. But the fact of the matter is that everyone that has spoken that statement has not suggested an alternative to it. So, I ask you now: Is there an alternative to useing System()? I will not download new libraries, etc...

System Specs:

-Win7
-MinGW + Codeblocks

I believe that's all you need to know to answer my question. If you need any more information, please ask.

This is not homework/schoolwork, I am doing this in my own time, and useing the internet to help. All of your advice is very much appreciated!!
:-)

system() IS intrisically evil.

But we need to know what you're using it for to be able to propose an alternative.

Andy
Last edited on
But the fact of the matter is that everyone that has spoken that statement has not suggested an alternative to it.


The fact of the matter is that you just didn't look. :)
Last edited on
Always use CreateProcess() when using windows.
I tried Create process and it either has trouble compileing, or just doesn't work (no effect). Don't ask me why...
Show how you are using it, CreateProcess does work.
CreateProcess("Minecraft.exe"); CreateProcess("taskkill /im wahoo.exe /f");

Assuming the .exe is in the same folder as my program.
Last edited on
Did you even attempt to look up the documentation?
This has an example of how it is used http://msdn.microsoft.com/en-us/library/ms682512(VS.85).aspx
Last edited on
Yes, except WinAPI is way over-complicated. The documentation is hardly understandable to me....

http://www.goffconcepts.com/techarticles/development/cpp/createprocess.html
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425%28v=vs.85%29.aspx

Mabey an explanation is in order?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
	STARTUPINFO si;
	PROCESS_INFORMATION pi;
	memset(&si, 0, sizeof(si)); 
	memset(&pi, 0, sizeof(pi)); 
	si.cb = sizeof(si);
	
	std::cout << CreateProcess(L"C:\\Windows\\system32\\notepad.exe", //full path to exe
		NULL, //cmd line options (optional)
		NULL, //process security attr
		NULL, //thread security attr
		false, //inherit handles flag
		CREATE_DEFAULT_ERROR_MODE,//process creation flags
		NULL,//environment
		NULL, //current directory
		&si,//startup info
		&pi//process info
		);	
}
thanks. I will study your example and ask any questions I may have.
Don't mean to be rude, but naraku example is not recommended to use. It will work without issues in this particular case, but it will have problems in others, especiaaly in paths with spaces. Read MSDN documentation for how exactly CreateProcess works.

The recommended way is to always use NULL as first argument and use full path and command line arguments enclosed in quotes as second parameter for CreateProcess. There are numerous examples on the web.

Also the second argument must be a writable string, not a constant string if you use Unicode version of CreateProcess.
@modoran
Looks like I missed some bits when I skimmed through the documentation
If the executable module is a 16-bit application, lpApplicationName should be NULL, and the string pointed to by lpCommandLine should specify the executable module as well as its arguments.
The first parameter, lpApplicationName, can be NULL, in which case the executable name must be in the white space–delimited string pointed to by lpCommandLine. If the executable or path name has a space in it, there is a risk that a different executable could be run because of the way the function parses spaces. The following example is dangerous because the function will attempt to run "Program.exe", if it exists, instead of "MyApp.exe".
from http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx

Pretty good example of why reading the documentation fully is so important.
Last edited on
The recommended way is to always use NULL as first argument and use full path and command line arguments enclosed in quotes as second parameter for CreateProcess

Actually, this statement is incorrect. Microsoft actually recommends the opposite, for security reasons (the problem they are referring to is paths with spaces). From the MSDN entry on CreateProcess():

To avoid this problem, do not pass NULL for lpApplicationName. If you do pass NULL for lpApplicationName, use quotation marks around the executable path in lpCommandLine, as shown in the example below.

You should only ever pass NULL as the first parameter by choice if you need to support 16-bit apps.

Andy
Last edited on
So..... System()?
What's System() ??
So..... System()?

should not be used. Yes, we've covered that.
Topic archived. No new replies allowed.