Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,187 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,083 people online right now. Registration is fast and FREE... Join Now!




Shutting down a pc using C++

3 Pages V  1 2 3 >  
Reply to this topicStart new topic

Shutting down a pc using C++, I want to create a program that shuts down windows when executed

Milan
29 Aug, 2007 - 11:23 PM
Post #1

New D.I.C Head
*

Joined: 29 Aug, 2007
Posts: 9


My Contributions
Out of interest, I would like to find out how to shut down a windows based PC using a program made in C++. I've tried different websites, but couldn't find anything useful.

Can you give me a solution? biggrin.gif

Best regards, Milan.
User is offlineProfile CardPM
+Quote Post

Xing
RE: Shutting Down A Pc Using C++
29 Aug, 2007 - 11:30 PM
Post #2

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
check out system() command.
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Shutting Down A Pc Using C++
30 Aug, 2007 - 03:10 AM
Post #3

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,451



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
I'm assuming you want Windows XP?

CODE

C:\Documents and Settings\Owner>shutdown /?
Usage: shutdown [-i | -l | -s | -r | -a] [-f] [-m \\computername] [-t xx] [-c "comment"] [-d up:xx:yy]

        No args                 Display this message (same as -?)
        -i                      Display GUI interface, must be the first option
        -l                      Log off (cannot be used with -m option)
        -s                      Shutdown the computer
        -r                      Shutdown and restart the computer
        -a                      Abort a system shutdown
        -m \\computername       Remote computer to shutdown/restart/abort
        -t xx                   Set timeout for shutdown to xx seconds
        -c "comment"            Shutdown comment (maximum of 127 characters)
        -f                      Forces running applications to close without warning
        -d [u][p]:xx:yy         The reason code for the shutdown
                                u is the user code
                                p is a planned shutdown code
                                xx is the major reason code (positive integer less than 256)
                                yy is the minor reason code (positive integer less than 65536)

User is online!Profile CardPM
+Quote Post

born2c0de
RE: Shutting Down A Pc Using C++
30 Aug, 2007 - 03:35 AM
Post #4

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,906



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
Alternatively, you can use the Windows API to shutdown your system.
First you will need to call AdjustTokenPrivileges() to obtain a SHUTDOWN privilege. Once you have Shutdown privileges, you can call the ExitWindowsEx() Function.
User is offlineProfile CardPM
+Quote Post

Tomas
RE: Shutting Down A Pc Using C++
30 Aug, 2007 - 08:16 AM
Post #5

New D.I.C Head
Group Icon

Joined: 12 Jun, 2007
Posts: 34


Dream Kudos: 100
My Contributions
Why dont yo try using this code:

CODE
system("SHUTDOWN");


I think this is valid, however i have not used it myself. ph34r.gif
User is offlineProfile CardPM
+Quote Post

Tomas
RE: Shutting Down A Pc Using C++
30 Aug, 2007 - 10:28 AM
Post #6

New D.I.C Head
Group Icon

Joined: 12 Jun, 2007
Posts: 34


Dream Kudos: 100
My Contributions
Just and update from my previous post
Try using:

CODE
system("SHUTDOWN/s");


This should work biggrin.gif
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Shutting Down A Pc Using C++
30 Aug, 2007 - 11:09 AM
Post #7

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,451



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
QUOTE(Tomas @ 30 Aug, 2007 - 11:28 AM) *

Just and update from my previous post
Try using:

CODE
system("SHUTDOWN/s");


This should work biggrin.gif

I would put a space after the N, before the switch s.
User is online!Profile CardPM
+Quote Post

Milan
RE: Shutting Down A Pc Using C++
30 Aug, 2007 - 11:48 PM
Post #8

New D.I.C Head
*

Joined: 29 Aug, 2007
Posts: 9


My Contributions
I'm looking for something which I can make into an exe file, using basic C++ skills
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Shutting Down A Pc Using C++
31 Aug, 2007 - 04:40 AM
Post #9

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
You can put the system command specified above into a C++ program, and compile it into an executable.
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Shutting Down A Pc Using C++
31 Aug, 2007 - 06:42 AM
Post #10

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,451



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
CODE

#include <stdio.h>

void main(void) {
  cout << "Now shutting down..."
  system("shutdown /s");
}

User is online!Profile CardPM
+Quote Post

Milan
RE: Shutting Down A Pc Using C++
31 Aug, 2007 - 06:46 AM
Post #11

New D.I.C Head
*

Joined: 29 Aug, 2007
Posts: 9


My Contributions
thanks alot!!!
User is offlineProfile CardPM
+Quote Post

Bench
RE: Shutting Down A Pc Using C++
31 Aug, 2007 - 07:37 AM
Post #12

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 617



Thanked: 14 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
QUOTE(no2pencil @ 31 Aug, 2007 - 03:42 PM) *

CODE

#include <stdio.h>

void main(void) {
  cout << "Now shutting down..."
  system("shutdown /s");
}

Careful not to mix languages - 'cout' is a C++ name from <iostream>
perhaps you meant printf("Now shutting down...");

Also, you need to #include <stdlib.h> for the system function

- and be careful to put int main(void)

This post has been edited by Bench: 31 Aug, 2007 - 07:39 AM
User is offlineProfile CardPM
+Quote Post

3 Pages V  1 2 3 >
Reply to this topicStart new topic
Time is now: 12/2/08 01:15AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month