Vb.net GetTokenInformation返回无效的SID?

Vb.net GetTokenInformation返回无效的SID?,vb.net,winforms,winapi,Vb.net,Winforms,Winapi,在跟踪过程中,我遇到了一个问题 <DllImport("advapi32.dll", SetLastError:=True)> _ Private Shared Function OpenProcessToken(ByVal ProcessHandle As IntPtr, ByVal DesiredAccess As Integer, ByRef TokenHandle As IntPtr) As Boolean End Function <DllImport("ad

在跟踪过程中,我遇到了一个问题

   <DllImport("advapi32.dll", SetLastError:=True)> _
Private Shared Function OpenProcessToken(ByVal ProcessHandle As IntPtr, ByVal DesiredAccess As Integer, ByRef TokenHandle As IntPtr) As Boolean
End Function

<DllImport("advapi32.dll", SetLastError:=True)> _
Private Shared Function GetTokenInformation(TokenHandle As IntPtr, TokenInformationClass As TOKEN_INFORMATION_CLASS, TokenInformation As IntPtr, TokenInformationLength As UInteger, ByRef ReturnLength As UInteger) As Boolean
End Function

 <DllImport("advapi32.dll", SetLastError:=True)> _
Private Shared Function IsValidSid(SID As Byte()) As Boolean
End Function


  For Each p As Process In Process.GetProcesses

        Dim processHandle As IntPtr = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, False, p.Id)
        If Not processHandle = Nothing Then
            Dim tokenhandle As IntPtr = Nothing
            Dim bool As Boolean = OpenProcessToken(processHandle, TOKEN_READ, tokenhandle)
            If bool = False Then
                Dim win32error As String = New Win32Exception(Marshal.GetLastWin32Error).Message

                MessageBox.Show(win32error)
            Else
                Dim sidlength As UInteger = Nothing
                Dim SIDbyte As Byte() = Nothing

                Dim somebool As Boolean = GetTokenInformation(tokenhandle, TOKEN_INFORMATION_CLASS.TokenUser, Nothing, 0, sidlength)

                If Not somebool Then
                    Dim win32error As String = New Win32Exception(Marshal.GetLastWin32Error).Message
                    MessageBox.Show(win32error)

                    ''RETURNS "The data area passed to a system call is too small" error.
                End If

                ReDim SIDbyte(35) '' I hardcoded '35' because it's what i'm getting with sidlength.

somebool = GetTokenInformation(tokenhandle, TOKEN_INFORMATION_CLASS.TokenUser, SIDbyte, SIDbyte.Length, sidlength)

''RETURNS TRUE THE SECOND TIME.

                If Not somebool Then
                    Dim win32error As String = New Win32Exception(Marshal.GetLastWin32Error).Message

                    MessageBox.Show(win32error)
                End If

                If IsValidSid(SIDbyte) Then
                    MessageBox.Show("Valid")
                Else
                    MessageBox.Show("Not Valid")
                End If

''RETURNS INVALID SID. (FAILS)
_
作为布尔值的私有共享函数OpenProcessToken(ByVal ProcessHandle作为IntPtr,ByVal DesiredAccess作为整数,ByRef TokenHandle作为IntPtr)
端函数
_
私有共享函数GetTokenInformation(TokenHandle作为IntPtr,TokenInformationClass作为TOKEN_INFORMATION_CLASS,TokenInformation作为IntPtr,TokenInformationLength作为UInteger,ByRef ReturnLength作为UInteger)作为布尔值
端函数
_
专用共享函数IsValidSid(SID为Byte())为布尔值
端函数
对于Process.GetProcesses中的每个p As进程
Dim processHandle As IntPtr=OpenProcess(进程查询有限信息,False,p.Id)
如果不是processHandle=Nothing,则
将令牌句柄设置为IntPtr=Nothing
Dim bool As Boolean=OpenProcessToken(processHandle,TOKEN\u READ,tokenhandle)
如果bool=False,则
Dim win32error As String=新的Win32Exception(Marshal.GetLastWin32Error)。消息
MessageBox.Show(win32error)
其他的
尺寸sidlength作为UInteger=无
Dim SIDbyte As Byte()=无
Dim somebool As Boolean=GetTokenInformation(令牌句柄,令牌信息,令牌类,令牌用户,Nothing,0,sidlength)
如果不是somebool那么
Dim win32error As String=新的Win32Exception(Marshal.GetLastWin32Error)。消息
MessageBox.Show(win32error)
“返回”传递给系统调用的数据区域太小“错误。
如果结束
ReDim SIDbyte(35)'我硬编码了'35',因为这是我用sidlength得到的。
somebool=GetTokenInformation(tokenhandle,TOKEN\u INFORMATION\u CLASS.TokenUser,SIDbyte,SIDbyte.Length,sidlength)
''第二次返回TRUE。
如果不是somebool那么
Dim win32error As String=新的Win32Exception(Marshal.GetLastWin32Error)。消息
MessageBox.Show(win32error)
如果结束
如果是IsValidSid(SIDbyte),则
MessageBox.Show(“有效”)
其他的
MessageBox.Show(“无效”)
如果结束
''返回无效的SID。(失败)
第一次调用GetTokenInformation失败了,我想应该是这样的。。。返回值为36的“sidlength”。
第二次调用成功并填充SID Byte(),但对“IsValidSID”的调用返回false。。。我不明白为什么,如果成功填充了SIDbytes,会出现什么问题?

您没有检查错误。第一步是添加错误检查。我编辑了我的代码,请验证,第一次调用GetInformationToken时返回false,返回“传递给系统调用的数据区域太小”,如上所述设置“sidlength”值,第二次调用成功,填充“SIDByte”,但对IsValidSID的调用始终返回false。。。有什么帮助吗?如果函数告诉您分配36个字节,为什么要分配35个字节?和硬编码?为什么?因为35索引是从0开始的第36项。虽然我删除了硬编码部分(我正在测试…),但函数成功了,但SID仍然无效,无法找出原因…GetTokenInformation和IsValidSid的声明不一致。尝试将IsValidSid的参数声明为IntPtr,看看是否有帮助。