tags:

views:

145

answers:

9

Hi,

I'm using VS2010 and I would like to call an exe file which I've created in another directory. I've tried the following:

int main(){

 system("C:\\Users\\Li\\Desktop\\Debug\\modelExample_4pcs.exe");     
     return 0;
};

but I get "The system could not find the file specified" error.

I've tried to run the exe file directly from the command line, and it only works when I'm inside its directory. Could you please tell me how can I run it from a different directory?

(I'm using win7)

Thanks, Li.

A: 

Check your path, and make sure you escape all characters: C:\\Users\Li..

Leonid
my path is ok. I've copied it as is and cd-ed into it from the cmd prompt. I just can't run the exe
A: 

Is the error from running the main program, not from launching modelExample_4pcs.exe? Try commenting out the system() call and see if you get the same error.

Your main program is not on the path when you're outside its folder...

Graham Perks
The error is from my main program.I'm just trying to call a program from inside a program... Is there another way to do this?
+1  A: 

System() may not be able to find cmd.exe to open your environment. Try using cmd.exe to execute your app via the /C option.

System("C:\\WINDOWS\\system32\cmd.exe /C \"C:\\Users\\Li\\Desktop\\Debug\\modelExample_4pcs.exe\"");
jholl
-1, the failure mode for missing CMD.EXE is documented. Quoting MS, " If the command interpreter is not found, returns 0 and sets `errno` to `ENOENT`." The reported error, `"The system could not find the file specified"` is different.
MSalters
+1  A: 

Try this using CreateProcess. Less (or at least different) environmental dependencies than using system(). At least you will get a nice Win32 error code if this still fails.

http://msdn.microsoft.com/en-us/library/ms682425(VS.85).aspx

Steve Townsend
+2  A: 

You should try using CreateProcess Windows API funcion: http://msdn.microsoft.com/en-us/library/ms682425%28VS.85%29.aspx

lowliet
+2  A: 

Try opening the file for reading, just to check that you have the path right:

char* filename = "C:\\Users\\Li\\Desktop\\Debug\\modelExample_4pcs.exe" ;
FILE* fp = fopen (filename, "rb") ; // Open for reading, binayr mode
if (fp == 0) {
  printf ("Duh! File not found\n") ;
  exit (0) ;
  }
printf ("File found\n") ;
fclose (fp) ;

// Now try the system call, as before:
system(filename);

What happens?

TonyK
+2  A: 

You should be able to use ShellExecute like so: (adjusting the params sent to ShellExecute for your situation) http://msdn.microsoft.com/en-us/library/bb762153(VS.85).aspx?ppud=4

HINSTANCE hinst = ShellExecute( NULL, _T("open"), commandLine.c_str(), additionalParams.c_str(), NULL, SW_RESTORE );

if(hinst <= (HINSTANCE)SHELLEXERROR)// see: http://msdn2.microsoft.com/en-us/library/bb762153.aspx for further info on the return values

Now given that you are using Win7, you may be having a privilege issue and you need to run at an elevated level (i.e. administrator) you can test this by opening cmd as admin and running your exe from another directory

and as Steve mentioned above you can certainly use CreateProcess.

HTH,

EB

echobravo
A: 

Is modelExample_4pcs.exe trying to load another file from the current working folder, and THAT's what's generating the error?

Maybe try chdir() before the call to system().

Graham Perks
A: 

Just change to the directory first, like you would do from the command prompt:

system("C: && CD \\Users\\Li\\Desktop\\Debug\\ && modelExample_4pcs.exe"); 
MSalters