从C转换到vb.net后出错
我正在尝试转换本文中给出的C代码。转换后我有一些错误,但我已经更正了一些。我仍然得到了cbSize=sizeofTOKEN\u ELEVATION\u类型行的错误 错误是: 未声明“sizeof”。由于其保护级别,可能无法访问。D:\Desktop\WindowsApplication3\WindowsApplication3\Form1.vb 103 26 WindowsApplication3 “TOKEN_ELEVATION_TYPE”是一种类型,不能用作表达式。D:\Desktop\WindowsApplication3\WindowsApplication3\Form1.vb 103 33 WindowsApplication3 我尝试使用Len而不是sizeof,但第二个错误仍然存在。所以谁能帮我解决这两个错误呢。下面是表示错误的VB.NET代码从C转换到vb.net后出错,vb.net,c#-to-vb.net,Vb.net,C# To Vb.net,我正在尝试转换本文中给出的C代码。转换后我有一些错误,但我已经更正了一些。我仍然得到了cbSize=sizeofTOKEN\u ELEVATION\u类型行的错误 错误是: 未声明“sizeof”。由于其保护级别,可能无法访问。D:\Desktop\WindowsApplication3\WindowsApplication3\Form1.vb 103 26 WindowsApplication3 “TOKEN_ELEVATION_TYPE”是一种类型,不能用作表达式。D:\Desktop\Wi
Imports System.Runtime.InteropServices
Imports System.Security.Principal
Imports System.ComponentModel
Public Class Form1
Public Const TOKEN_DUPLICATE As UInt32 = &H2
Public Const TOKEN_IMPERSONATE As UInt32 = &H4
Public Const TOKEN_QUERY As UInt32 = &H8
Public Declare Function GetTokenInformation Lib "advapi32.dll" ( _
ByVal TokenHandle As IntPtr, ByVal TokenInformationClass As TOKEN_INFORMATION_CLASS, _
ByVal TokenInformation As IntPtr, ByVal TokenInformationLength As System.UInt32, _
ByRef ReturnLength As System.UInt32) As Boolean
Declare Function DuplicateToken Lib "advapi32.dll" (ExistingTokenHandle As IntPtr, _
SECURITY_IMPERSONATION_LEVEL As Int16, ByRef DuplicateTokenHandle As IntPtr) _
As Boolean
Enum TOKEN_ELEVATION_TYPE
TokenElevationTypeDefault = 1
TokenElevationTypeFull
TokenElevationTypeLimited
End Enum
Public Enum TOKEN_INFORMATION_CLASS
TokenUser = 1
TokenGroups
TokenPrivileges
TokenOwner
TokenPrimaryGroup
TokenDefaultDacl
TokenSource
TokenType
TokenImpersonationLevel
TokenStatistics
TokenRestrictedSids
TokenSessionId
TokenGroupsAndPrivileges
TokenSessionReference
TokenSandBoxInert
TokenAuditPolicy
TokenOrigin
TokenElevationType
TokenLinkedToken
TokenElevation
TokenHasRestrictions
TokenAccessInformation
TokenVirtualizationAllowed
TokenVirtualizationEnabled
TokenIntegrityLevel
TokenUIAccess
TokenMandatoryPolicy
TokenLogonSid
MaxTokenInfoClass
' MaxTokenInfoClass should always be the last enum
End Enum
Public Enum SECURITY_IMPERSONATION_LEVEL
SecurityAnonymous
SecurityIdentification
SecurityImpersonation
SecurityDelegation
End Enum
Function IsAdmin() As Boolean
Dim identity = WindowsIdentity.GetCurrent()
Return (identity IsNot Nothing AndAlso New WindowsPrincipal(identity).IsInRole(WindowsBuiltInRole.Administrator))
End Function
''' <summary>
''' The function checks whether the primary access token of the process belongs
''' to user account that is a member of the local Administrators group, even if
''' it currently is not elevated.
''' </summary>
''' <returns>
''' Returns true if the primary access token of the process belongs to user
''' account that is a member of the local Administrators group. Returns false
''' if the token does not.
''' </returns>
Function CanBeAdmin() As Boolean
Dim fInAdminGroup As Boolean = False
Dim hToken As IntPtr = IntPtr.Zero
Dim hTokenToCheck As IntPtr = IntPtr.Zero
Dim pElevationType As IntPtr = IntPtr.Zero
Dim pLinkedToken As IntPtr = IntPtr.Zero
Dim cbSize As Integer = 0
If IsAdmin() Then
Return True
End If
Try
' Check the token for this user
hToken = WindowsIdentity.GetCurrent().Token
' Determine whether system is running Windows Vista or later operating
' systems (major version >= 6) because they support linked tokens, but
' previous versions (major version < 6) do not.
If Environment.OSVersion.Version.Major >= 6 Then
' Running Windows Vista or later (major version >= 6).
' Determine token type: limited, elevated, or default.
' Allocate a buffer for the elevation type information.
cbSize = sizeof(TOKEN_ELEVATION_TYPE)
Dim cbSizeuint As UInteger = Convert.ToUInt32(cbSize)
pElevationType = Marshal.AllocHGlobal(cbSize)
If pElevationType = IntPtr.Zero Then
Throw New Win32Exception(Marshal.GetLastWin32Error())
End If
' Retrieve token elevation type information.
If Not GetTokenInformation(hToken, TOKEN_INFORMATION_CLASS.TokenElevationType, pElevationType, cbSizeuint, cbSizeuint) Then
Throw New Win32Exception(Marshal.GetLastWin32Error())
End If
' Marshal the TOKEN_ELEVATION_TYPE enum from native to .NET.
Dim elevType As TOKEN_ELEVATION_TYPE = CType(Marshal.ReadInt32(pElevationType), TOKEN_ELEVATION_TYPE)
' If limited, get the linked elevated token for further check.
If elevType = TOKEN_ELEVATION_TYPE.TokenElevationTypeLimited Then
' Allocate a buffer for the linked token.
cbSize = IntPtr.Size
Dim cbSizeuint_ee As UInteger = Convert.ToUInt32(cbSize)
pLinkedToken = Marshal.AllocHGlobal(cbSize)
If pLinkedToken = IntPtr.Zero Then
Throw New Win32Exception(Marshal.GetLastWin32Error())
End If
' Get the linked token.
If Not GetTokenInformation(hToken, TOKEN_INFORMATION_CLASS.TokenLinkedToken, pLinkedToken, cbSizeuint_ee, cbSizeuint_ee) Then
Throw New Win32Exception(Marshal.GetLastWin32Error())
End If
' Marshal the linked token value from native to .NET.
hTokenToCheck = Marshal.ReadIntPtr(pLinkedToken)
End If
End If
' CheckTokenMembership requires an impersonation token. If we just got
' a linked token, it already is an impersonation token. If we did not
' get a linked token, duplicate the original into an impersonation
' token for CheckTokenMembership.
If hTokenToCheck = IntPtr.Zero Then
If Not DuplicateToken(hToken, CInt(SECURITY_IMPERSONATION_LEVEL.SecurityIdentification), hTokenToCheck) Then
Throw New Win32Exception(Marshal.GetLastWin32Error())
End If
End If
' Check if the token to be checked contains admin SID.
Dim id As New WindowsIdentity(hTokenToCheck)
Dim principal As New WindowsPrincipal(id)
fInAdminGroup = principal.IsInRole(WindowsBuiltInRole.Administrator)
Catch
Return False
Finally
' Centralized cleanup for all allocated resources.
If pElevationType <> IntPtr.Zero Then
Marshal.FreeHGlobal(pElevationType)
pElevationType = IntPtr.Zero
End If
If pLinkedToken <> IntPtr.Zero Then
Marshal.FreeHGlobal(pLinkedToken)
pLinkedToken = IntPtr.Zero
End If
End Try
Return fInAdminGroup
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs)
If CanBeAdmin() Then
MessageBox.Show("admin")
Else
MessageBox.Show("not admin")
End If
End Sub
End Class
您可以使用Marshal.SizeOf获取基础类型的大小,从而获得C cbSize=sizeofTOKEN\u ELEVATION\u类型的效果
Dim undertype As Type = [Enum].GetUnderlyingType(GetType(TOKEN_ELEVATION_TYPE))
cbSize = System.Runtime.InteropServices.Marshal.SizeOf(undertype)
当我运行它时,undertype是System.Int32,cbSize是4。您可以使用Marshal.SizeOf获得基础类型的大小,从而获得C cbSize=sizeofTOKEN\u ELEVATION\u TYPE的效果
Dim undertype As Type = [Enum].GetUnderlyingType(GetType(TOKEN_ELEVATION_TYPE))
cbSize = System.Runtime.InteropServices.Marshal.SizeOf(undertype)
当我运行它时,undertype是System.Int32,cbSize是4。Google作为一个关注点抛出。@克里斯,我还有另一个错误“TOKEN\u ELEVATION\u TYPE”是一个类型,不能用作我在问题中提到的表达式。所以我的问题将如何重复。请帮我解决这个问题。使用我在问题中已经提到过的len,所以我需要第二个错误的答案。这个公认的答案谈到了这一点。VB中的“Len”操作符将执行此操作,但它对实例有效,因此您需要进行相应的调整。您传递的不是实例而是类型,它不需要类型。在我看来,答案告诉了你你需要知道的一切。你现在只需要应用这些知识。谷歌把它作为一个关注点。@Chris我还有另一个错误“TOKEN\u ELEVATION\u TYPE”是一个类型,不能用作我在问题中提到的表达式。那么我的问题将如何重复呢。请帮我解决这个问题。使用我在问题中已经提到过的len,所以我需要第二个错误的答案。这个公认的答案谈到了这一点。VB中的“Len”操作符将执行此操作,但它对实例有效,因此您需要进行相应的调整。您传递的不是实例而是类型,它不需要类型。在我看来,答案告诉了你你需要知道的一切。你现在只需要运用这些知识。