Like Tree1Likes

How to shutdown (or Restart) a pc with C++

This is a discussion on How to shutdown (or Restart) a pc with C++ within the C++ Programming forums, part of the General Programming Boards category; Yes, the function is defined in windows.h dwReserved, however, is not. dwReserved is, according to MSDN, held to remind you ...

  1. #16
    Registered User
    Join Date
    May 2002
    Posts
    50
    Yes, the function is defined in windows.h

    dwReserved, however, is not.

    dwReserved is, according to MSDN, held to remind you that you're supposed to put there a REASON CODE for shutting down the system, so it knows what to do (take a system snapshot, save the configuration, etc.).

    The reason codes are NOT defined in Windows.h.

    They are defined in Reason.h (according to MSDN), a file NOT included with MSVC 6.0 and apparently, not available on MSDN. So how the hell can we use such a useful function without the header file?!?!?!

    Shadow if you have the header, I'd love a copy. OKiesmokie, if you have the header, I'd love a copy. Either way... how the heck can I find the header?!

  2. #17
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Shadow if you have the header, I'd love a copy. OKiesmokie, if you have the header, I'd love a copy. Either way... how the heck can I find the header?!
    I don't have the header, and I know you probably weren't talking about me as another Shadow posted vital code to your question, but you could always try the following when looking for a pin point specific file ( whether or not it's shareware, or part of a commercial distrobution ) ....

    Use a file sharing program. I personally prefer KaZaA.
    Just search for reason.h.

    Simple way to find say ( uses simple example ) say ohhh, the old doom game full version, through a file sharing program:
    Search: Doom.zip click enter. In your case, Reason.h.

    With google, search for the following:
    FTP Reason.h
    The world is waiting. I must leave you now.

  3. #18
    Registered User
    Join Date
    May 2002
    Posts
    50
    Struck out on all counts..

    KaZaa only turns up 3 love songs containing the word "reason".

    Google seems to think that reason.h is two words, reason and h, and won't search for them together, no matter how hard I try...

    Oh well. I'll keep looking. Thanks anyway.

  4. #19
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Struck out on all counts..

    KaZaa only turns up 3 love songs containing the word "reason".

    Google seems to think that reason.h is two words, reason and h, and won't search for them together, no matter how hard I try...

    Oh well. I'll keep looking. Thanks anyway.
    I don't have the file myself, and I did the same things you did to check.

    I must say though, I repsect your effort.
    If we could get more posters like yourself...
    The world is waiting. I must leave you now.

  5. #20
    Registered User
    Join Date
    May 2002
    Posts
    50
    Well, ExitWindows() may be a strike out, but I have found a workaround...

    The following site on MSDN utilizes a function called InitiateSystemShutdown().

    http://msdn.microsoft.com/library/de...tdown_04ry.asp

    Here is some samplecode I ran that shut down my system quite nicely....

    Code:
    BOOL fResult;
    
    HANDLE hToken;              // handle to process token 
    TOKEN_PRIVILEGES tkp;       // pointer to token structure 
    
    
    int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR     lpCmdLine,
                         int       nCmdShow)
    {
    
    // Get the current process token handle so we can get shutdown 
    // privilege. 
     
    if (!OpenProcessToken(GetCurrentProcess(), 
            TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) 
        MessageBox(NULL,NULL,"OpenProcessToken failed.", MB_OK | MB_ICONINFORMATION); 
     
    // Get the LUID for shutdown privilege. 
     
    LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, 
            &tkp.Privileges[0].Luid); 
     
    tkp.PrivilegeCount = 1;  // one privilege to set    
    tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; 
     
    // Get shutdown privilege for this process. 
     
    AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, 
        (PTOKEN_PRIVILEGES) NULL, 0); 
     
    // Cannot test the return value of AdjustTokenPrivileges. 
     
    if (GetLastError() != ERROR_SUCCESS) 
        MessageBox(NULL,NULL,"AdjustTokenPrivileges enable failed.",MB_OK | MB_ICONINFORMATION); 
     
    // Display the shutdown dialog box and start the time-out countdown. 
     
    fResult = InitiateSystemShutdown( 
        NULL,                                  // shut down local computer 
        "Click on the main window and press the Escape key to cancel shutdown.",  // message to user 
        3,                                    // time-out period 
        TRUE,                                 // ask user to close apps 
        TRUE);                                 // reboot after shutdown 
     
    if (!fResult) 
    { 
        MessageBox(NULL,NULL,"InitiateSystemShutdown failed.",MB_OK | MB_ICONINFORMATION);
    } 
     
    // Disable shutdown privilege. 
     
    tkp.Privileges[0].Attributes = 0; 
    AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, 
            (PTOKEN_PRIVILEGES) NULL, 0); 
     
    if (GetLastError() != ERROR_SUCCESS) 
    {
        MessageBox(NULL,NULL,"AdjustTokenPrivileges disable failed.",MB_OK | MB_ICONINFORMATION);
    } 
    
    return 0;
    }
    You can set the timer, and the code can be altered to avert shutdown within the timer period, the code for that is included on the website...

    Maybe this will finally help the original poster

  6. #21
    Registered User
    Join Date
    Apr 2002
    Posts
    99

    ha!

    bunch of kidz...
    ROFLMAOPMP!!! You're calling us a bunch of 'kidz'?

  7. #22
    Unregistered
    Guest

    Shadow12345'S Quote

    In reference to Shadow's Quote, it is a "modification" of the chorus of a song.

    "You're perfect yes its true, but when you don't shower you smell like poo."

    The real song goes something like this:
    "You're perfect yes its true, but without me you're only you."

    The song is by Faith No More. I believe its called Midlife Crisis.

    - |2iiSc

  8. #23
    Registered User /\/\ E /\/ @'s Avatar
    Join Date
    Jul 2002
    Posts
    10
    concerning the initial question.....If it's possible to shutdown windows using C++....I have another question to add....is it possible to restart the computer in DOS MODE?
    Thanx in advance

  9. #24
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Wow, someone dusted off the archives.

    Anyhow.
    > is it possible to restart the computer in DOS MODE?
    Yes it is.
    The world is waiting. I must leave you now.

  10. #25
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    Wow, Someone quoted me :-D

    >>Ok I gotta ask..What is your quote taken from Shadow12345? And what is the story behind it?

    Its taken from my post (obviously) BUT, I took the information from my Win32 reference, and around the same stuff would probly be on MSDN, if you want an example program i made one awhile ago (around the same time i posted that)
    Attached Files Attached Files
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  11. #26
    Registered User /\/\ E /\/ @'s Avatar
    Join Date
    Jul 2002
    Posts
    10
    Originally posted by Shadow

    Anyhow.
    > is it possible to restart the computer in DOS MODE?
    Yes it is.
    I'm sorry...I shouldn't have worded it that way...could somebody please help me with restarting the computer in MS-DOS mode? I have tried for a long time and can't get it.
    Thanx

  12. #27
    Banned Yuri's Avatar
    Join Date
    Aug 2005
    Location
    Breukelen, The Netherlands
    Posts
    133
    Hi ( I am new here ).
    I am trying to shut down my comp. (or turn off power) with if but I fail. Log off does work, to bad Power off doesn't . So can anyone help me with this problem?
    Code:
    This DOES work:
    
        if ( Attempts <= 1 ){
        ExitWindowsEx(EWX_LOGOFF,0);
        } 
    
    This DOESN'T work:
    
        if ( Attempts <= 1 ){
        ExitWindowsEx(EWX_POWEROFF,0);
        }

  13. #28
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    > Hi ( I am new here ).
    Yes, and it's obvious that you didn't bother to read the rules.
    > 07-30-2002, 08:39 PM
    This thread is 3 years old!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to shutdown or restart a PC from a pthread-process
    By mynickmynick in forum C Programming
    Replies: 8
    Last Post: 08-07-2008, 10:11 AM
  2. Replies: 1
    Last Post: 10-27-2006, 02:21 PM
  3. Shutdown Pc Timer
    By miranda in forum Windows Programming
    Replies: 10
    Last Post: 12-31-2005, 01:36 AM
  4. Post you pc pics (mods)
    By biosninja in forum A Brief History of Cprogramming.com
    Replies: 33
    Last Post: 03-15-2004, 04:15 PM
  5. hOW TO MAKE A PROGRAM WHICH MAKES MY pC TO SHUTDOWN?
    By ALANAIR23 in forum Windows Programming
    Replies: 9
    Last Post: 09-01-2001, 02:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21