Solved

Shutdown in Windows NT

Posted on 1999-01-05
9
205 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

Technology Partners: 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!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

733 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