Menu

Shutting down my computer with C++

2007-10-30
2012-09-26
  • Bronxernijn

    Bronxernijn - 2007-10-30

    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;
    }

     
    • Anonymous

      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.

       
    • Osito

      Osito - 2007-10-30

      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?

       
      • Osito

        Osito - 2007-10-30

        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"

         
    • Bronxernijn

      Bronxernijn - 2007-10-30

      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 :(

       
    • Anonymous

      Anonymous - 2007-10-30

      Look for the Window's API ShellExecute() and ShellExecuteEx() functions

      Old newbie.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.