Hello, I want to make a program that shuts my computer down using C++. I just encountered a problem: When executing the file, the system cant find the specified path. When I use the same command in cmd.exe, it works fine. What could be the problem?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2007-10-31
Each system call creates a new cmd.exe session. Changing the working directory in one session does not affect another, so a sequence of system calls is not the same as executing the same sequence in a batch file to on the command line. If you are shtting down, the pause is probably redundant.
Another way of doing this is to create a batch file that you test works and then invoke that from the system() function.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
When you do "cd" I'm not sure which directory you're actually changing, but putting the path in the executable statement should always work no matter where you are currently.
Also that will only work on computers that have windows installed in the windows directory, not winnt, win95, etc. If you want it to work on more than just your computer, you would need to ask the OS for the windows installation folder.
Lastly, are you actually compiling and running when you get the error, or is the compiler/linker the one that is unable to find the specified path?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello, I want to make a program that shuts my computer down using C++. I just encountered a problem: When executing the file, the system cant find the specified path. When I use the same command in cmd.exe, it works fine. What could be the problem?
Code:
include <cstdlib>
include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
system("cd C:\windows\system32");
system("shutdown -s -t 30");
system("PAUSE");
return EXIT_SUCCESS;
}
Each system call creates a new cmd.exe session. Changing the working directory in one session does not affect another, so a sequence of system calls is not the same as executing the same sequence in a batch file to on the command line. If you are shtting down, the pause is probably redundant.
Another way of doing this is to create a batch file that you test works and then invoke that from the system() function.
I'm not familiar with the system command, but maybe try this:
system("C:\windows\system32\shutdown.exe -s -t 30");
When you do "cd" I'm not sure which directory you're actually changing, but putting the path in the executable statement should always work no matter where you are currently.
Also that will only work on computers that have windows installed in the windows directory, not winnt, win95, etc. If you want it to work on more than just your computer, you would need to ask the OS for the windows installation folder.
Lastly, are you actually compiling and running when you get the error, or is the compiler/linker the one that is unable to find the specified path?
p.s. You might need \ instead of \ in your path. I've used ShellExecute which seems kinda similar and it needs "c:\blahblahblah\filename.exe"
Ok, thanks. I know that I have to change the path for another PC. The double '\'s did a great deal. Its just that my PC still doesnt shut down :(
Look for the Window's API ShellExecute() and ShellExecuteEx() functions
Old newbie.