VB School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a VB Expert!

Join 300,406 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,659 people online right now. Registration is fast and FREE... Join Now!




Equivalent of IntPtr.

 

Equivalent of IntPtr.

priyamtheone

28 Jun, 2009 - 06:25 AM
Post #1

New D.I.C Head
*

Joined: 2 Sep, 2008
Posts: 19

What is the equivalent of IntPtr of Vb.Net in Vb6? For example what'll be the equivalent of the following 2 lines in Vb6?
Dim htok As IntPtr
htok = IntPtr.Zero

Thanks.

User is offlineProfile CardPM
+Quote Post


Ghostwolf

RE: Equivalent Of IntPtr.

29 Jun, 2009 - 06:31 AM
Post #2

New D.I.C Head
*

Joined: 1 May, 2009
Posts: 20



Thanked: 2 times
My Contributions
If I'm understanding this
http://www.gnu.org/software/dotgnu/pnetlib...tr.Zero%20Field

it seems to me it'd be
CODE
Dim htok as Integer
htok = 0


But, if you're looking to hold a handle in that variable, I think you'll want to use Long instead of Integer in VB6.

This post has been edited by Ghostwolf: 29 Jun, 2009 - 06:44 AM
User is offlineProfile CardPM
+Quote Post

born2c0de

RE: Equivalent Of IntPtr.

30 Jun, 2009 - 12:59 AM
Post #3

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 4,542



Thanked: 98 times
Dream Kudos: 2825
Expert In: J2ME, 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
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

User is offlineProfile CardPM
+Quote Post

priyamtheone

RE: Equivalent Of IntPtr.

2 Jul, 2009 - 06:26 AM
Post #4

New D.I.C Head
*

Joined: 2 Sep, 2008
Posts: 19

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

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 10:55PM

Live VB Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month