QUOTE(born2c0de @ 30 Jun, 2009 - 12:59 AM)

Long is the equivalent of IntPtr and not Integer.
CODE
Dim htok As IntPtr
htok = IntPtr.Zero
This would translate to:
CODE
Dim htok As Long
htok = 0L
Well I'm trying to convert an app in Vb.Net 2005 to VB6. The app has 3 buttons by which u can log off, restart and shutdown ur xp machine. In Vb.Net it's fine. But when I'm running it in Vb6 only log off is working and restart and shutdown isn't working. What am I missing? Here's my code both in VB.Net and VB 6.
VB.Net Code (Running fine):
--------------------------------
CODE
Imports System.Runtime.InteropServices
Public Class Form1
<StructLayout(LayoutKind.Sequential, Pack:=1)> _
Private Structure TokPrivLuid
Public intCount As Integer
Public lngLuid As Long
Public intAttr As Integer
End Structure
Private Declare Function GetCurrentProcess Lib "kernel32" () As IntPtr
Private Declare Function OpenProcessToken Lib "advapi32.dll" _
(ByVal h As IntPtr, ByVal acc As Integer, ByRef phtok As IntPtr) As Boolean
Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" _
(ByVal host As String, ByVal name As String, ByRef pluid As Long) As Boolean
Private Declare Function AdjustTokenPrivileges Lib "advapi32.dll" Alias "AdjustTokenPrivileges" _
(ByVal htok As IntPtr, ByVal disall As Boolean, ByRef newst As TokPrivLuid, ByVal len As Integer, _
ByVal prev As IntPtr, ByVal relen As IntPtr) As Boolean
Private Declare Function ExitWindowsEx Lib "user32" Alias "ExitWindowsEx" _
(ByVal flg As Integer, ByVal rea As Integer) As Boolean
Private Const SE_PRIVILEGE_ENABLED As Integer = &H2
Private Const TOKEN_QUERY As Integer = &H8
Private Const TOKEN_ADJUST_PRIVILEGES As Integer = &H20
Private Const SE_SHUTDOWN_NAME As String = "SeShutdownPrivilege"
Private Const EWX_LOGOFF As Integer = 0
Private Const EWX_SHUTDOWN As Integer = &H1
Private Const EWX_REBOOT As Integer = &H2
Private Const EWX_FORCE As Integer = &H4
Private Const EWX_POWEROFF As Integer = &H8
Private Const EWX_FORCEIFHUNG As Integer = &H10
Private Sub DoExitWin(ByVal flg As Integer)
Dim blnPInvoke As Boolean
Dim tp As TokPrivLuid
Dim hproc As IntPtr
Dim htok As IntPtr
Try
hproc = GetCurrentProcess()
htok = IntPtr.Zero
blnPInvoke = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, htok)
tp.intCount = 1
tp.lngLuid = 0
tp.intAttr = SE_PRIVILEGE_ENABLED
blnPInvoke = LookupPrivilegeValue(Nothing, SE_SHUTDOWN_NAME, tp.lngLuid)
blnPInvoke = AdjustTokenPrivileges(htok, False, tp, 0, IntPtr.Zero, IntPtr.Zero)
blnPInvoke = ExitWindowsEx(flg, 0)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
End Try
End Sub
Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
End
End Sub
Private Sub cmdLogoff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLogoff.Click
Call DoExitWin(EWX_LOGOFF)
End Sub
Private Sub cmdRestart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRestart.Click
Call DoExitWin(EWX_REBOOT)
End Sub
Private Sub cmdShutdown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdShutdown.Click
Call DoExitWin(EWX_POWEROFF)
End Sub
End Class
Now the Vb 6 code:
----------------------
CODE
Private Type TokPrivLuid
intCount As Long
lngLuid As Long
intAttr As Long
End Type
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function OpenProcessToken Lib "advapi32.dll" _
(ByVal h As Long, ByVal acc As Long, ByRef phtok As Long) As Long
Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" _
(ByVal host As String, ByVal name As String, ByRef pluid As Long) As Long
Private Declare Function AdjustTokenPrivileges Lib "advapi32.dll" _
(ByVal htok As Long, ByVal disall As Long, ByRef newst As TokPrivLuid, ByVal lnglen As Long, _
ByRef prev As TokPrivLuid, ByVal relen As Long) As Long
Private Declare Function ExitWindowsEx Lib "user32" _
(ByVal flg As Long, ByVal rea As Long) As Long
Private Const SE_PRIVILEGE_ENABLED As Long = &H2
Private Const TOKEN_QUERY As Long = &H8
Private Const TOKEN_ADJUST_PRIVILEGES As Long = &H20
Private Const SE_SHUTDOWN_NAME As String = "SeShutdownPrivilege"
Private Const EWX_LOGOFF As Long = 0
Private Const EWX_SHUTDOWN As Long = &H1
Private Const EWX_REBOOT As Long = &H2
Private Const EWX_FORCE As Long = &H4
Private Const EWX_POWEROFF As Long = &H8
Private Const EWX_FORCEIFHUNG As Long = &H10
Private Sub DoExitWin(ByVal flg As Long)
Dim blnPInvoke As Long
Dim tp As TokPrivLuid
Dim hproc As Long
Dim htok As Long
On Error GoTo errHndlr
hproc = GetCurrentProcess()
htok = 0
blnPInvoke = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, htok)
tp.intCount = 1
tp.lngLuid = 0
tp.intAttr = SE_PRIVILEGE_ENABLED
blnPInvoke = LookupPrivilegeValue(Empty, SE_SHUTDOWN_NAME, tp.lngLuid)
blnPInvoke = AdjustTokenPrivileges(htok, False, tp, 0, tp, 0)
blnPInvoke = ExitWindowsEx(flg, 0)
Exit Sub
errHndlr:
MsgBox Err.Description, vbCritical, "Error"
End Sub
Private Sub cmdCancel_Click()
End
End Sub
Private Sub cmdLogoff_Click()
Call DoExitWin(EWX_LOGOFF)
End Sub
Private Sub cmdRestart_Click()
Call DoExitWin(EWX_REBOOT)
End Sub
Private Sub cmdShutdown_Click()
Call DoExitWin(EWX_SHUTDOWN)
End Sub