Converting this small chunck of code to C++

Pages: 12
Hey guys just from some code in java, how would this look in c++
1
2
3
4
5
6
7
try{
    Runtime r = Runtime.getRuntime();
    r.exec("mspaint.exe");
}
catch(Exception ex){
                
} 


Just wondering if its possible to open other programs like that? And if so how would it look and what do i need to include?

Cheers
system("mspaint");

(Yes, system() is evil, but alterenatives are very hard to come by.)

No need for a variable, just this.
...I think.
Cheers for that man i actually just figured that out before haha, been seeing what else i can use like exec() or WinExec - just not sure yet what i need to include. Uhm one other question. Say for like i have a mysql server but i need to open a few .exe's. Say i wanted to do it all from one program etc
 
system("C:\Program Files\Folder\.exe File");

Any idea how i can do that? Thanks mate
You mean like this:

system("C:\Program Files\Folder\Exec.exe");?

Couldn't you just put a couple of them in a row?

Although, from my knowledge all system() does is open cmd.exe and run whatever you put in.
Yeah see i was trying that as i thought - yeah just using cmd. But for some reason it isn't working like that
What is it doing exactly? Running it until the first system then stopping?
It will build successfully etc im using
1
2
3
4
5
6
7
#include <iostream>

int main()
{
	system("c:\\server\\NewFolder\\logon.exe");
	return 0;
}


Reason for using the \\ is the character escape sequence - even if i take them out it wont run.

The output in the cmd is
"The system cannont find the path sepcified"
Weird i rebooted loaded the program again - ran it.. it worked this time?

Edit - Ok ran into another little bump - if the folder name has a space between it.. it wont load. I don't know what c++ uses for spaces - or system in that fact as %20 i don't think works in this does it?
Last edited on
Hmm...It should run fine, although it might be because it thinks those spaces are extra arguments that you can attach...if so, I don't really know how you can get around it...maybe pass an extra set of " " on the inside?
Damn, nope that doesn't work either :(
The system() command works by spawning the command shell. On Windows, this is cmd.exe (or whatever %COMSPEC% indicates, presumably. Read your compiler's documentation).

Hence, you must use the Windows methods of quoting stuff. Here's an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* a.c
 * Demonstrates using system() on Windows.
 */
#include <stdio.h>
#include <stdlib.h>

int main()
  {
  if (0 != system( "\"C:\\Program Files\\Windows NT\\Accessories\\"
                   "wordpad.exe\" a.c >nul 2>&1" ))
    {
    fprintf( stderr, "Failure to execute MS WordPad.\n" );
    return 1;
    }

  puts( "Success!" );
  return 0;
  }

A few notes:

1. Notice how the entire pathname was surrounded in double-quotes. Also notice how I had to escape them using the \" string-literal sequence.

2. As Mythios said for \\ .

3. Remember that in C you can break your string literals across multiple lines. The compiler will concatenate them for you. Hence, it is only one string being passed to the system() function there. (I suppose I didn't really need to do this here, but then I figured, why not? Might as well slip something interesting in there.)

4. Both standard output and standard error are redirected to the DOS nul device. (On *nix you would redirect to /dev/null .) This prevents the spawned cmd.exe shell from printing unwanted error messages on your terminal.

5. The system() command is a blocking command. It only returns when the spawned command shell terminates. The return code is zero if the shell was able to successfully execute the desired command, and non-zero on failure. This is only a very general guideline --it is entirely possible to get the command shell to think everything went fine even if something went wrong.

If you want just to start another process without waiting for it to terminate, use the start command:
1
2
/* Print a little wizard */
system( "start mspaint.exe /p C:\\WINDOWS\\system32\\oobe\\images\\merlin.gif" );
You cannot stop GUI's from popping-up messages (you can a few, if you know what you are doing, but in general it isn't worth the effort with system()), and that is the case with start. If it cannot find the program to execute it will display an error dialog, wait for the user to press OK, then return failure to your program.


Well, that should do it. If you wish to do anything more complex than above, then the system() function is not adequate; use a system-dependant function instead. On Windows, that is one of ShellExecute() or CreateProcess().

Hope this helps.
What you've said has helped - i tried your code as well with wordpad to see if it would load that - and well correct loaded up fine. I replaced your code with my path. But was unsuccessful.
Well it's working but in a weird way -
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int main()
{
	if(0 != system("\"C:\\server\\server files\\" "logon.exe\" a.c >nul 2>&1"))
	{
		fprintf( stderr, "Failure to execute Logon.\n" );
		return 1;
    }

	puts( "Success!" );
	return 0;
}


The problem is - this program comes up with a message for me to enter my username and password if i open it seperate. When i open it up through this. it just displays an empty console but i can type. If i type the username press enter then the password. It will come up with success? Howcome it wont display the previous data asking for the users input?

Thanks a heap though what you've said has helped :)
Last edited on
Change the >nul 2>&1 to just 2>nul. Since you are logging on, you want to let the server print stuff on the terminal.

See point 4 above. (Always read everything.)

:-)
Hehe, actually i did read that. I'm just confused how it works. I didn't relize that represented the >nul 2>&1. Thanks a heap - I'll try it now :)
Alright it works sweet. Thanks heaps heh! Only other problem is. I thought you could load anything - etc a .bat file as well for mySQL server loads up with a .bat file. But when i use this code with a bat file it tells me "The system cannot find the path specified"

EDIT - Also say one of my files runs in the cmd, is there a way to make it open in another window?
Last edited on
The error message can be generated from almost anything. Chances are that it is because the bat file is using a relative pathname somewhere, and it is choking when you use system() to start it. I don't know what directory it starts with by default, but you can embed a change in the input string:

hello.bat
@echo off
echo Hello world!
pause
exit


Using it
1
2
3
4
system(
  "\"cd C:\\Program Files\\quux\\bin\""
  "&& start hello.bat"
  );

The && is to separate argument commands to cmd.exe.

Heh... Perhaps you'd be better off just using CreateProcess() ?

:-)
Last edited on
Well the MySQL.bat file has
1
2
3
4
5
6
7
8
9
10
11
mysql\bin\mysqld --defaults-file=mysql\bin\my.cnf --standalone --console

if errorlevel 1 goto error
goto finish

:error
echo.
echo MySQL could not be started
pause

:finish


I used:
 
system("\"cd C:\\Server\\Server Files\\" "&& start MySQL.bat");


Which worked how i wanted it to - yay! Thanks heh. Just one question about that - why did you use "cd" just after system?
Last edited on
Well i just finished the program i needed to write and it starts my server in the perfect order and everything links. Thanks a heap for your help mate - saved me a hell of a job trying to figure out the spaces between the character. Cheers
The bat file expects to execute in a specific directory. So first you changed to that directory then executed the batch file there.

Glad to have helped. :-)

Especially with the character. XD
Pages: 12