Call exe from C ++ (windows)

I am using VS2010, and I would like to call the exe file that I created in another directory. I tried the following:

int main(){

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

but I get the message "Error in the system could not find the file."

I tried to run the exe file directly from the command line, and it only works when I get to its directory. Could you tell me how can I run it from another directory?

(I am using win7)

Thanks, Lee.

+3
source share
9 answers

Try opening the file for reading, just to make sure you have the correct path:

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's happening?

+3
source

ShellExecute : ( , ShellExecute ) 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

, , Win7, , ( ), , cmd admin exe

, , CreateProcess.

,

+2

System() cmd.exe, . cmd.exe /C.

System("C:\\WINDOWS\\system32\cmd.exe /C \"C:\\Users\\Li\\Desktop\\Debug\\modelExample_4pcs.exe\"");
+1
+1

, : C:\\Users\Li..

0

, modelExample_4pcs.exe? system() , .

Your main program is not on the way when you are outside its folder ...

0
source

Is modelExample_4pcs.exe an attempt to load another file from the current working folder, and SO, which generates an error?

Perhaps try chdir () before calling system ().

0
source

Just go to the directory first, as on the command line:

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

Source: https://habr.com/ru/post/1762319/


All Articles