Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 433
  • Last Modified:

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

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"
'http://msdn2.microsoft.com/en-us/library/aa376868.aspx
'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.
0
tkonan
Asked:
tkonan
2 Solutions
 
cookreCommented:
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
hProc=Process.GetCurrentProcess().Handle;

hToken=IntPtr.Zero;
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()+")");
       return;
       }
    }
catch (Exception e)
      {
      PutMsg("LPV excp <<<"+e.ToString()+">>>");
      }

TknPriv.PrivilegeCount=1; // Set just one
TknPriv.Privileges.Attributes=2; // SE_PRIVILEGE_ENABLED;
TknPriv.Privileges.pLuid=PrivLuid;
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()+")");
       return;
       }
    }
catch (Exception e)
      {
      PutMsg("ATP excp <<<"+e.ToString()+">>>");
      }

bool rc=ExitWindowsEx(18,2147745793); // REBOOT/FORCEIFHUNG , APP/MAINT/PLANNED
0
 
tkonanAuthor Commented:
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.
0
Independent Software Vendors: 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!

 
tkonanAuthor Commented:
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?
0
 
tkonanAuthor Commented:
fine. thanks. brilliant. good plan. maybe even 175 points. 100 fine too. cheers
0
 
modus_operandiCommented:
Forced accept.
modus_operandi
EE Moderator
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now