Click here to Skip to main content
15,914,071 members
Home / Discussions / Visual Basic
   

Visual Basic

 
AnswerRe: DataControl ---> Data "object" HowTo??? Pin
chris foote17-Apr-02 7:00
chris foote17-Apr-02 7:00 
AnswerRe: DataControl ---> Data "object" HowTo??? Pin
bjwoodburn21-Apr-02 22:32
bjwoodburn21-Apr-02 22:32 
GeneralPrinting error in VB app Pin
MS le Roux11-Apr-02 23:28
MS le Roux11-Apr-02 23:28 
GeneralAdding Background to the whole Form Pin
Ahuva11-Apr-02 11:04
Ahuva11-Apr-02 11:04 
GeneralRe: Adding Background to the whole Form Pin
David Wengier11-Apr-02 14:57
David Wengier11-Apr-02 14:57 
GeneralRe: Adding Background to the whole Form Pin
Aleksey Suvorov28-Apr-02 6:16
Aleksey Suvorov28-Apr-02 6:16 
GeneralIntegrate mp3 file in VB Pin
Ahuva11-Apr-02 10:56
Ahuva11-Apr-02 10:56 
GeneralAPI functions Pin
bjwoodburn11-Apr-02 1:08
bjwoodburn11-Apr-02 1:08 
Help Please...!
I'm trying to add a shutdown routine to a program using the ExitWindowsEx API function. I'm running on Windows XP. The problem is that the LookUpPrivilegeValue function keeps crashing out saying : Instance of an object is not set. As far as I know I'm not referencing an object merely calling a function. Can anyone help?

Here is the code I am using:

Option Explicit On
Imports System
Imports System.Runtime.InteropServices

Public Class ShutDownAPI
Private Const EWX_LogOff As Long = 0
Private Const EWX_SHUTDOWN As Long = 1
Private Const EWX_REBOOT As Long = 2
Private Const EWX_FORCE As Long = 4
Private Const EWX_POWEROFF As Long = 8

<dllimport("user32.dll")> Public Shared Function ExitWindowsEx(ByVal dwOptions As Long, ByVal dwReserved As Long) As Long
End Function
<dllimport("kernel32.dll")> Public Shared Function GetLastError() As Long
End Function
<dllimport("kernel32.dll")> Public Shared Function GetVersion() As Long
End Function
<dllimport("kernel32.dll")> Public Shared Function GetCurrentProcess() As Long
End Function
<dllimport("advapi32.dll")> Public Shared Function OpenProcessToken(ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, ByVal TokenHandle As Long) As Long
End Function
Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, ByVal lpLuid As LUID) As Long

<dllimport("advapi32.dll")> Public Shared Function AdjustTokenPrivileges(ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, ByVal NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, ByVal PreviousState As TOKEN_PRIVILEGES, ByVal ReturnLength As Long) As Long
End Function
<dllimport("kernel32.dll")> Public Shared Sub SetLastError(ByVal dwErrCode As Long)
End Sub

Private Const mlngWindows95 = 0
Private Const mlngWindowsNT = 1

Private glngWhichWindows32 As Long

Public Structure LUID
Public UsedPart As Long
Public IgnoredForNowHigh32BitPart As Long
End Structure

Public Structure LUID_AND_ATTRIBUTES
Public TheLuid As LUID
Public Attributes As Long
End Structure

Public Structure TOKEN_PRIVILEGES
Public PrivilegeCount As Long
Public TheLuid As LUID
Public Attributes As Long
End Structure

Public Enum sdAction
Reboot = 1
Shutdown = 2
Logoff = 3
End Enum
Private Sub AdjustToken()
Const TOKEN_ADJUST_PRIVILEGES = &H20
Const TOKEN_QUERY = &H8
Const SE_PRIVILEGE_ENABLED = &H2
Dim hdlProcessHandle As Long
Dim hdlTokenHandle As Long
Dim tmpLuid As New APITest2.TestAPI.LUID()
Dim tkp As New TOKEN_PRIVILEGES()
Dim tkpNewButIgnored As New TOKEN_PRIVILEGES()
Dim lBufferNeeded As Long
SetLastError(0)
hdlProcessHandle = GetCurrentProcess()
OpenProcessToken(hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), hdlTokenHandle)
LookupPrivilegeValue("", "SeShutdownPrivilege", tmpLuid)
tkp.PrivilegeCount = 1 ' One privilege to set
tkp.TheLuid = tmpLuid
tkp.Attributes = SE_PRIVILEGE_ENABLED
AdjustTokenPrivileges(hdlTokenHandle, False, tkp, Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded)
End Sub
Public Sub ExitWin(ByVal Act As sdAction, ByVal Force As Boolean)
Dim iAction As Integer
AdjustToken()
If Act = sdAction.Logoff Then iAction = EWX_LogOff
If Act = sdAction.Reboot Then iAction = EWX_REBOOT
If Act = sdAction.Shutdown Then iAction = EWX_SHUTDOWN
If Force = True Then
ExitWindowsEx((iAction Or EWX_FORCE), CLng(&HFFFF))
Else
ExitWindowsEx(iAction, CLng(&HFFFF))
End If
End Sub
Public Sub New()
Dim lngVersion As Long
lngVersion = GetVersion()
If ((lngVersion And &H80000000) <> 0) Then
glngWhichWindows32 = mlngWindowsNT
Else
glngWhichWindows32 = mlngWindows95
End If
End Sub
End Class

Using the ExitWin Sub to call the shutdown procedeure. Any Ideas
GeneralRe: API functions Pin
Paul M Watt21-Apr-02 10:38
mentorPaul M Watt21-Apr-02 10:38 
GeneralDHTML from VB Pin
Gerosa9-Apr-02 11:00
Gerosa9-Apr-02 11:00 
GeneralRe: DHTML from VB Pin
Jeremy Falcon17-Apr-02 7:15
professionalJeremy Falcon17-Apr-02 7:15 
GeneralVBA and templates... Pin
H.P.9-Apr-02 10:35
H.P.9-Apr-02 10:35 
GeneralWrite binary file . URGENT !!!!! Pin
mimi9-Apr-02 9:23
mimi9-Apr-02 9:23 
GeneralRe: Write binary file . URGENT !!!!! Pin
David Wengier11-Apr-02 15:06
David Wengier11-Apr-02 15:06 
GeneralRe: Write binary file . URGENT !!!!! Pin
Fernando Finelli26-Apr-02 7:36
Fernando Finelli26-Apr-02 7:36 
Generalconnect to encrypted Access file(*.mdb) Pin
9-Apr-02 6:48
suss9-Apr-02 6:48 
GeneralHelp me to display Unicode font on menu bar. Pin
Vu Truong9-Apr-02 2:10
Vu Truong9-Apr-02 2:10 
GeneralRe: Help me to display Unicode font on menu bar. Pin
Paul M Watt21-Apr-02 10:43
mentorPaul M Watt21-Apr-02 10:43 
Generallimit text lenght Pin
infinity_0068-Apr-02 10:19
infinity_0068-Apr-02 10:19 
GeneralRe: limit text lenght Pin
Henk Groenewoud18-Apr-02 2:53
Henk Groenewoud18-Apr-02 2:53 
GeneralCrystal User Defined Functions Pin
Gambit8-Apr-02 9:59
Gambit8-Apr-02 9:59 
GeneralRe: Crystal User Defined Functions Pin
Nick Parker10-Apr-02 6:27
protectorNick Parker10-Apr-02 6:27 
GeneralARRAY from VB into ACCESS TABLE . PLEASE HELP !!!!!! Pin
mimi8-Apr-02 9:20
mimi8-Apr-02 9:20 
GeneralTreeView Pin
7-Apr-02 5:02
suss7-Apr-02 5:02 
GeneralRe: TreeView Pin
David Wengier7-Apr-02 8:12
David Wengier7-Apr-02 8:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.