VB.Net GetTokenInformation TokenGroups

J

jlofgren111

I have been trying to get the list of user tokens using VB.Net. I have
been searching through the news groups and found some information, but
I am having issues getting that working. If I try the same method
within C# the process works correct.

Imports System.Runtime.InteropServices
Imports System.Runtime.InteropServices.Marshal

Public Class Class2
Public Const ANYSIZE_ARRAY As Integer = 100

Private Enum TOKEN_INFORMATION_CLASS
TokenUser = 1
TokenGroups
TokenPrivileges
TokenOwner
TokenPrimaryGroup
TokenDefaultDacl
TokenSource
TokenType
TokenImpersonationLevel
TokenStatistics
TokenRestrictedSids
TokenSessionId
TokenGroupsAndPrivileges
TokenSessionReference
TokenSandBoxInert
End Enum

Private Declare Auto Function GetTokenInfo Lib "Advapi32.dll" Alias
"GetTokenInformation" ( _
ByVal TokenHandle As IntPtr, _
ByVal eTokenInformationClass As TOKEN_INFORMATION_CLASS, _
ByRef TokenInformation As IntPtr, _
ByVal TokenInformationLength As Integer, _
ByRef iReturnLength As Integer) As Boolean

' Friend Structure TOKEN_GROUPS
' Public GroupCount As Integer
' Friend Groups As SID_AND_ATTRIBUTES()
' End Structure

Private Structure SID_AND_ATTRIBUTES
Dim Sid As Integer
Dim Attributes As Integer
End Structure

Private Structure TOKEN_GROUPS
Dim GroupCount As Integer
Dim Groups() As SID_AND_ATTRIBUTES
End Structure



Public Sub GetTokenGroups()
Dim hToken As IntPtr =
Security.Principal.WindowsIdentity.GetCurrent.Token
Dim tg As IntPtr
Dim cb As Integer
Dim cb2 As Integer
Dim oTokenInfo As TOKEN_GROUPS
Dim iSizeOfTokenInfo As Integer
Dim iSizeOfGroup As Integer
Dim iSizeOfReturnedTokenInfo As Integer
Dim groupCount As Integer

Const cSizeOfGroupCountField As Integer = 4
cb = 0
oTokenInfo = CreateTokenGroups(ANYSIZE_ARRAY)
iSizeOfGroup = SizeOf(New SID_AND_ATTRIBUTES)
iSizeOfTokenInfo = cSizeOfGroupCountField + (ANYSIZE_ARRAY *
iSizeOfGroup)
GetTokenInfo(hToken, TOKEN_INFORMATION_CLASS.TokenGroups,
IntPtr.Zero, 0, cb)
tg = Marshal.AllocHGlobal(cb)
GetTokenInfo(hToken, TOKEN_INFORMATION_CLASS.TokenGroups, tg,
cb, cb2)

groupCount = Marshal.ReadInt32(tg)
....
....
....
End Sub
End Class


It appears I have various issues with the unmanaged memory locations.
Both hToken and cb are having the values modified unexpectedly.

Thanks
 
M

Mattias Sjögren

Private Declare Auto Function GetTokenInfo Lib "Advapi32.dll" Alias
"GetTokenInformation" ( _
ByVal TokenHandle As IntPtr, _
ByVal eTokenInformationClass As TOKEN_INFORMATION_CLASS, _
ByRef TokenInformation As IntPtr, _
ByVal TokenInformationLength As Integer, _
ByRef iReturnLength As Integer) As Boolean


TokenInformation should be a ByVal parameter. You have to allocate the
native buffer that the function should fill.



Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top