Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

InitiateSystemShutdown Problem 1

Status
Not open for further replies.

benlinkknilneb

Programmer
May 16, 2002
590
US
Hi all, I'm using NT Workstation and I want to reboot. I found this code:
Code:
Private Declare Function InitiateSystemShutdown Lib "ADVAPI32.DLL" (ByVal strMachineName As String, ByVal strMessage As String, ByVal lngTimeout As Long, ByVal blnForceAppsClosed As Long, ByVal blnRebootAfterShutdown As Long, ByVal dwReason As Long) As Long

Private Sub Command1_Click()
    InitiateSystemShutdown "", "", 0, True, True, 0
End Sub
But when it runs, I get an error that says "can't find the entry point InitiateSystemShutdown in advapi.dll". What am I missing?

Ben
 
Hello

This is what I found in the api text viewer

Code:
Public Declare Function InitiateSystemShutdown Lib "advapi32.dll" Alias "InitiateSystemShutdownA" (ByVal lpMachineName As String, ByVal lpMessage As String, ByVal dwTimeout As Long, ByVal bForceAppsClosed As Long, ByVal bRebootAfterShutdown As Long) As Long

I note 2 differences:
* ... Alias "InitiateSystemShutdownA"... is present in the code I am giving. I don't think this difference is giving you the error
* there is an extra function-variable 'dwReason As Long' present in your code. This I think cause the error (although I am not certain at all)

With my example, your code should look like this

Code:
Private Sub Command1_Click()
InitiateSystemShutdown "", "", 0, True, True
end sub

Take a look at it and tell us if that solved the problem.

Greetz

Mim
 
Thanks Mim, that fixed the error... but it's still not rebooting. I checked and the return value of the function is 0, which means that the shutdown process is not initiated... any ideas?
 
Hmmm...

You could try to look at the value of

Code:
err.LastDllError

to have more details...

I haven't found anything else yet
 
What happens if you have
Code:
InitiateSystemShutdown vbnullstring, vbnullstring, 0, True, True
Instead of
Code:
InitiateSystemShutdown "", "", 0, True, True
?
 
Still nothing...

err.LastDllError returns "5", but whether I use vbNullString or "", the call fails.
 
I did a little search on the net and found a way to know the message that goes with the '5' error you are getting...

Code:
Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000 
Private Const FORMAT_MESSAGE_IGNORE_INSERTS = &H200 
Private Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" (ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As Long, ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long, Arguments As Long) As Long 


Public Function WinError(ByVal lLastDLLError As Long) As String 
Dim sBuff As String 
Dim lCount As Long 
    
    ' Return the error message associated with LastDLLError: 
    sBuff = String$(256, 0) 
    lCount = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM Or FORMAT_MESSAGE_IGNORE_INSERTS, 0, lLastDLLError, 0&, sBuff, Len(sBuff), ByVal 0) 
    If lCount Then 
    WinError = Left$(sBuff, lCount) 
    End If 
    
End Function
 
You 'da man! Error '5' is "Access Denied"... so I guess I have to take it up with the sysadmin... Thanks for your help!

Ben
 
No problem!

Glad I could help someone...

And ... by the way... it's woman... :) Yes yes there are woman programmers :)

Greetz

Mim
 
As far as I can remember, (but I can't check with MSDN right now) Your process needs a specific priveledge to run the InitiateSystemShutdown. You can grab this priveledge at runtime though (me thinks l,ong tinme since I did it) AFAIK the sysadmin can't help (much)!


You can look this up in MSDN if you want and see how you can set it, you can wait till I get sometime to look at it (later tonight) or you can wait fro a passing strongm who carries this information in his head!

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Matt, strongm:

I'm really new to API stuff... I found the stuff about privileges, but I don't know how to set up the call. If you could provide VB syntax, I'd really appreciate it.

Ben
 
In the following function:
Code:
Private Sub EnableShutDown()
    Dim hProc As Long, hToken As Long, mLUID As LUID, mPriv As TOKEN_PRIVILEGES, mNewPriv As TOKEN_PRIVILEGES
    hProc = GetCurrentProcess()
    OpenProcessToken hProc, TOKEN_ADJUST_PRIVILEGES + TOKEN_QUERY, hToken
    LookupPrivilegeValue "", "SeShutdownPrivilege", mLUID
    mPriv.PrivilegeCount = 1
    mPriv.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
    mPriv.Privileges(0).pLuid = mLUID
    ' enable shutdown privilege for the current application
    AdjustTokenPrivileges hToken, False, mPriv, 4 + (12 * mPriv.PrivilegeCount), mNewPriv, 4 + (12 * mNewPriv.PrivilegeCount)
End Sub

I got "undefined type" errors on these two types:

TOKEN_PRIVILEGES
LUID

How do I define them? Also, I don't have the API Declarations for AdjustTokenPrivileges, LookupPrivilegeValue, or OpenProcessToken... do you have those handy?
 
Hey all, I was searching the site and ran across this, posted by strongm last fall:

Code:
Private Sub cmdShutdown_Click()

Set colOperatingSystems = GetObject("winmgmts:{(Shutdown)}").ExecQuery("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
    ObjOperatingSystem.Win32Shutdown(1)
Next

End Sub

My question is this: can I modify this to reboot? I copied it into the program and it worked like a dream. If I can set this to automatically restart, it will solve my problem completely.
 
First, a word of warning (which I suspect may have been mentioned in the orifginal thread; it certainly ought ot have been): be careful with this. Using the WMI (Windows Management Interface) is not a generic solution; most NT4 installations do not have WMI installed.

That being said, if you do have WMI, then you can shutdown in a variety of ways. Note that the Win32Shutdown takes a parameter which, in the original example, is 1. There are however various values you can pass instead, summarised below:

0 Log Off
0 + 4 Forced Log Off
1 Shutdown
1 + 4 Forced Shutdown
2 Reboot
2 + 4 Forced Reboot
8 Power Off
8 + 4 Forced Power Off
 
Thanks, strongm...

it seems to be working fine. What's the story on WMI? Your original thread did mention it... will the interface work with 98? Did it come with a certain OS in particular? Does anything bad happen if the computer doesn't have it installed, or does it just crash my program?
 
Im a little late, but referring back to the top, instead of using a null string, use the "Nothing" keyword.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top