Shutting down using Microsoft SDK API doesn't go all the way

Posted on 2007-10-03
Last Modified: 2013-12-04
Trying to effect a shutdown by software. It has an effect on my 98 system (which otherwise shuts down ok). Have tried other flags but like this, system just goes as far as logging off. Though once (maybe with different flags), got as far as shutting down but then rebooted. Attempting to shutdown only. Here's the core code with flag constants. Last run I got r=0:

'Function and Flags from "user32.lib"
'flags as long!?
Private Declare Function ExitWindowsEx Lib "user32" (flags As Integer, reason As Long) As Boolean
Const EWX_LOGOFF As Integer = &H0&
Const EWX_POWEROFF As Integer = &H8&
Const EWX_REBOOT As Integer = &H2&
Const EWX_RESTARTAPPS As Integer = &H40&
Const EWX_SHUTDOWN As Integer = &H1&

Const EWX_FORCE As Integer = 4
Const EWX_FORCEIFHUNG As Integer = &H10&

'***** Effect the Shutdown *****
Private Sub cmdDoTheDeed_Click()
Dim r As Boolean
Dim exit_type As Integer
  exit_type = EWX_SHUTDOWN
  MsgBox ("bye..." + Str$(exit_type) + "")
  r = ExitWindowsEx(exit_type, 0)
  lblCountdown = Val(r)
  'End 'exit program
End Sub

exit_type displayed = 1

microsoft's page gives the main paramater of ExitWindowsEx as an integer but other code on the net shows it as a long. Its supposed to be an unsigned integer but they aren't available in VB6. I'm assuming integer is equal in this case. Interestingly, microsoft list their flags in msdn in 8-digit hex format, i.e. 32-bit, or long format. This has got to be ambiguous surely.
Question by:tkonan
    LVL 9

    Accepted Solution

    LVL 23

    Assisted Solution

    My guess is that you didn't grant yourself shutdown privilege.  Even a System/Local process must explicitly request this right - it's not automatic.

    Here's a snippet from something I did a few years ago.  I suspect you can com up with the VB equivalent.

    // Make sure we have rights to reboot

    OpenProcessToken(hProc,40,ref hToken); // 40=TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY
    PrivLuid=new LUID();
    try {
        if (0==LookupPrivilegeValue("","SeShutdownPrivilege",ref PrivLuid))
           PutMsg("Unable to look up privilege value ("+GetLastError().ToString()+")");
    catch (Exception e)
          PutMsg("LPV excp <<<"+e.ToString()+">>>");

    TknPriv.PrivilegeCount=1; // Set just one
    TknPriv.Privileges.Attributes=2; // SE_PRIVILEGE_ENABLED;
    NewPriv=new TOKEN_PRIVILEGES();
    int silly=4;
    try {
        if (0==AdjustTokenPrivileges(hToken,0,ref TknPriv,16,ref NewPriv,ref silly))
           PutMsg("Unable to adjust privilege value ("+GetLastError().ToString()+")");
    catch (Exception e)
          PutMsg("ATP excp <<<"+e.ToString()+">>>");

    bool rc=ExitWindowsEx(18,2147745793); // REBOOT/FORCEIFHUNG , APP/MAINT/PLANNED

    Author Comment

    Excellent idea JonMny. Still not found time to try Cookre's suggestion...vb6/winapi/conversion from c code is such a slog... also am a tad sceptical about the privileges thing being the issue in win98.

    Author Comment

    Can't I wait for someone to give me a modified answer, that I would be better able to comprehend/put to use? Not enough points for that maybe?

    Author Comment

    fine. thanks. brilliant. good plan. maybe even 175 points. 100 fine too. cheers
    LVL 1

    Expert Comment

    Forced accept.
    EE Moderator

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    strDist challenge 35 71
    Adding to a VBA? 6 35
    Copy a row 12 38
    Microsoft C++ code failing in executable that worked 9 22
    This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
    Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    746 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