473,396 Members | 1,836 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

VB.Net GetTokenInformation TokenGroups

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.Toke n
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

Nov 21 '05 #1
2 4235
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #2
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Vincent Nguyen | last post by:
Hi, Does anyone know how call Win32 native API GetTokenInformation() by using C#? Any sample code would be helpful. Thanks! Vincent
0
by: cameron | last post by:
I need to find a user's memberOf list, (and all nested groups), very quickly. My current method is to iterate through the the results of the the MemberOf property and then do the same to each of...
1
by: dhornyak | last post by:
I have been banging my head against the wall for a while now, and can't seem to id the problem. I've been through a ton of posts and the code doesn't seem any different. Can anybody see it? When...
5
by: Prisy | last post by:
In asp.net(C#) we can get the Process Name of a process using the following code: System.Diagnostics.Process item; item.ProcessName.ToString(); Is there a way to get the User Name of a...
0
by: cameron | last post by:
I need to find a user's memberOf list, (and all nested groups), very quickly. My current method is to iterate through the the results of the the MemberOf property and then do the same to each of...
5
by: Chad Beckner | last post by:
First, sorry for crossposting, not sure exactly where to place this question... I can not seem to find a way to get a users (or my) tokenGroups from ADS using VB.NET. I have seen several...
0
by: jlofgren111 | last post by:
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...
5
by: plmanikandan | last post by:
Hi, I need to check the SE_TCB_NAME previlige for the current user using GetTokenInformation api call.i used the api call and get the privilege,but i am unable to go thru each previlege to check...
2
by: eliang | last post by:
after calls to logonuser() impersonateloggedouuser() the call to openprocess() fails with Access is denied. what i need to do to get the access right back to call OpenProcess success.
3
by: =?Utf-8?B?Um9iS2lubmV5MQ==?= | last post by:
Hello, This could be REAL simple, but I cannot find any info on it anywhere after 2 hours of searching. How can I programmatically determine if the UAC is turned in Vista using C#? I need...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.