?
Solved

Shutdown in Windows NT

Posted on 1999-01-05
9
Medium Priority
?
207 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

770 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