Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Shutdown in Windows NT

Posted on 1999-01-05
9
Medium Priority
?
214 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

597 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