Solved

Shutdown in Windows NT

Posted on 1999-01-05
9
201 Views
Last Modified: 2010-04-02
Windows NT 4.0
C++ 5.0

I'd like to restart the system from a C++ program.
I've found functions like

  InitiateSystemShutdown or ExitWindowsEx

Please give me an example-code with all inlcudeFiles I need.

Thanks
    Juerg
0
Comment
Question by:juerg
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1181524
Here are some basic examples
http://www.ppdonline.com/demos/restart.htm
0
 

Author Comment

by:juerg
ID: 1181525
Thanks to Mirkwood for this very fast Comment, but the example is Visual Basic and I need a C++ Example (I know nothing about Visual Basic).
Thanks anyway
Juerg
0
 
LVL 11

Expert Comment

by:alexo
ID: 1181526
Check the following URLs:
  http://premium.microsoft.com/msdn/library/sdkdoc/sysmgmt/shutdown_5g6q.htm
  http://premium.microsoft.com/msdn/library/sdkdoc/sysmgmt/shutdown_5q25.htm

[The first time you access MSDN-online you'll have to go through a one-time registeration process]

You don't need specific include files, just the regular:

    #define STRICT
    #include <windows.h>

And the actual call is something like:

    ExitWindowsEx(EWX_SHUTDOWN, 0);

Is that wnat you need?
0
 
LVL 32

Expert Comment

by:jhance
ID: 1181527


            // Reboot Windows NT
            HANDLE hToken;
            TOKEN_PRIVILEGES tkp;
            char szBuf[100];

        if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
            wsprintf(szBuf, "OpenProcessToken Error #%d", GetLastError ());
            AfxMessageBox(szBuf, MB_OK);
                  EndDialog(IDC_REBOOT);
        }

        // Get the LUID for shutdown privilege
        if(!LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid)){
            AfxMessageBox("LookupPrivilegeValue() FAILED", MB_OK);
                  EndDialog(IDC_REBOOT);
        }
        else{
            tkp.PrivilegeCount = 1;  // one privilege to set
            tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
        }

            // Get shutdown privilege for this process.
        if (!AdjustTokenPrivileges(hToken, FALSE, &tkp, NULL, NULL, NULL)){
            wsprintf(szBuf, "AdjustTokenPrivileges Error #%d", GetLastError ());
            AfxMessageBox(szBuf, MB_OK);
                  EndDialog(IDC_REBOOT);
        }

        if(GetLastError() != ERROR_SUCCESS){
            wsprintf(szBuf, "AdjustTokenPrivileges() failed for reason %d", GetLastError());
            AfxMessageBox(szBuf, MB_OK);
                  EndDialog(IDC_REBOOT);
        }
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 32

Expert Comment

by:jhance
ID: 1181528
Oops,

I left out the last part of this....



            if(!InitiateSystemShutdown(NULL, "Shutting Down...", 5, bForceReboot, FALSE)){
                  wsprintf(szBuf, "Error %d in InitiateSystemShutdown()", GetLastError());
              AfxMessageBox(szBuf, MB_OK);
                  EndDialog(IDC_REBOOT);
            }
0
 

Author Comment

by:juerg
ID: 1181529
Help me !

After the comment from alexo at 7:22, I tried again and it works.
Now (a few minutes later) there is an asnwer from another.

I would't like to give the points to alexo, because after his comment it was OK.

Please answer what to do

Juerg
0
 
LVL 32

Expert Comment

by:jhance
ID: 1181530
1) Reject the currently posted answer.
2) Ask alexo to post an answer to this question.
3) Accept his answer.
0
 

Author Comment

by:juerg
ID: 1181531
Thanks jhance
Excuse me, but alexo was faster

to alexo, please give an answer

Juerg
0
 
LVL 11

Accepted Solution

by:
alexo earned 50 total points
ID: 1181532
>> to alexo, please give an answer
Done!  I'm glad it works for you.

BTW, jhance's answer is more general, in that it can be used to shutdown a remote workstation with only minor modifications.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now