call an exe from within c++ (windows) - c++

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.

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

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?

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

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\"");

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

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

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...

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().

Just change to the directory first, like you would do from the command prompt:
system("C: && CD \\Users\\Li\\Desktop\\Debug\\ && modelExample_4pcs.exe");

Related

system API doesn't work in an exe when called from another exe or a batch file

I have an exe, let's name it ABC.exe. There is a system API to run a batch file inside ABC.exe.
....
int ret = system("C:\\Test.bat");
if(ret == -1)
printf("The system API failed");
printf("The ret value is = %d",ret);
....
When the ABC.exe is run standalone, the Test.bat file is executed correctly. But when ABC.exe is launched via another batchfile or another application, the Test.bat is not run and the system API returns 1. I am not sure what does the return value of 1 mean here.
Please note that, the above scenario works well in Win7 but not in Win-10. And everything has been opened with Admin privileges.
Please help me in resolving the above query.
Update:
The system call fails with "dir" command as well.
I tried with other commands like "ipconfig", "winver", these are working fine.
Thanks.

execute command and get working directory

I want to execute a shell command in C++ and at the end I would like to fetch the current working directory of the executed process.
e.g. I executing the command cd C:\
then at the end of the command I want to get the directory C:\
and store it in a variable.
What I tried was pipe = _popen(cmd, "r") to execute the command, but at the end of the command, even when _pclose(pipe) wasn't called yet, when I called _getcwd(NULL, 0), I got the cwd of the running C++ program and not the changed cwd from _popen.
Does anyone know, how I could achieve this?
I found a solution for this:
I am creating a new Process of "cmd.exe" using CreateProcess() and injecting a piece of assembly code in the created process.
http://forum.codecall.net/topic/61271-how-to-get-current-directory-of-another-process/
I combined this with the sample from msdn for redirecting the stdin and stdout of the child process.

How can I keep open the command prompt in my windows form application in c++?

I wrote a project in windows form application in C++ by Visual Studio 2010. I need to open the cmd and then type special command and run other program.
I use this function :
system("cmd.exe /c dir c:\\");
but by this function I just saw cmd for a second and then it was disappeared.
then I add this line :
cin.get();
but it did not work.
also I use this function :
char program[] = "C:\Windows\System32\cmd.exe";
WinExec((LPCSTR)program, SW_SHOWMINIMIZED);
but it did not work either! Can you help me please?
Have you tried the following?
system("cmd /k dir c:\\");
/k keeps the cmd prompt window open after the executing process has terminated.
But, to be honest, it may be better to use the Windows Terminal Services API for finer control, if you so desire. But, depending on what you want to do - that might be overkill.
And, regarding your second question: don't forget to escape your backslashes in:
const char program[] = "C:\\Windows\\System32\\cmd.exe";
WinExec((LPCSTR)program, SW_SHOWMINIMIZED);
References:
https://superuser.com/questions/306167/how-to-prevent-the-command-prompt-from-closing-after-execution
You could try
cin.ignore();
maybe also in combination with cin.get()
cin.get();
cin.ignore();
I think normally cin.get() worked fine in my programs.

system() c++ wont run in VirtualBox

I'm trying to compile and run the app, which was created 4 years ago. It was developed for Windows in Embarcadero RAD Studio C++ builder. Now I try to compile and run it in Windows in VirtualBox using the latest version of RAD Studio. I have a system call to another app in my app:
system("dot.exe -Tjpg -o nfa.jpg NFA_graph.txt");
It keeps returning 1 and the file is not created. I also tried
system("Echo %CD% >> z:\log.txt");
and the file is not created. I also tried like this:
FILE *fpipe;
char *command = "Echo %CD% >> z:\log.txt";
char line[256];
if (0 == (fpipe = (FILE*)_popen(command, "r")))
{
perror("popen() failed.");
exit(1);
}
while (fread(line, sizeof line, 1, fpipe))
{
ShowMessage(line);
}
_pclose(fpipe);
And nothing I get. I wonder if the reason of such strange behaviour is that I'm running this all in VirtualBox?
You're not escaping your \ characters. You should use / in file paths, or \\ if you must. In addition, Windows 7 won't let you write to the root directory of a hard drive w/o administrator access.
To determine if a command environment is available, first do this:
if (!system(NULL)) {
// Can't make any system() calls
}
If your command environment is available, then you need to fully specify the path, making sure to escape the \'s like I mentioned above, and don't write anything to a drive's root directory. Also make note that opening files does not default create directories.
No, it's very unlikely. I see few issues with your code: you did not check errno if system() returns 1. It can help you to spot a real problem. Also, all backslashes must be Esc'ed.
I'm suggesting that dot.exe is not in PATH environment variable, that's the reason of the system() failure.

What are the possible reasons the system() function can not find the executable?

if( system("tail -500 log.txt") == -1)
{
//Error calling tail.exe on log
//errno is a system macro that expands int returning
//the last error. strerror() converts the error to it's
//corresponding error message.
printf("Error calling tail.exe with system(): %s",strerror( errno ));
}
System() is calling Tail.exe with log.txt
All are in the same directory as the executable calling it.
Getting the error ENOENT- No such file or directory
Also, specified paths to everything, same error.
Any advice is appreciated, thank you.
From the docs on system() that you linked:
ENOENT
Command interpreter cannot be found.
So the problem isn't that it can't find tail.exe, the problem is that it can't find the command interpreter. This suggests that something larger is going wrong. We'll need more information to diagnose the real problem. Also from the same page:
The system function passes command to
the command interpreter, which
executes the string as an
operating-system command. system
refers to the COMSPEC and PATH
environment variables that locate the
command-interpreter file (the file
named CMD.EXE in Windows NT and
later). If command is NULL, the
function simply checks to see whether
the command interpreter exists.
This suggests a couple of avenues for investigation: What does system(NULL) return? And what are the values for the COMSPEC and PATH environment variables when your program runs?
You might try system("cmd tail -500 log.txt") - that's been necessary on some windows boxes.