Solved

Shutdown in Windows NT

Posted on 1999-01-05
9
206 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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…
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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

691 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