GetTokenInformation返回无效的SID?

时间:2014-06-09 20:10:35

标签: vb.net winforms winapi

在关注this时,我遇到了问题。

   <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)

对GetTokenInformation的第一次调用失败,因为它应该猜测...返回&#34; sidlength&#34;价值36。 第二次调用成功,SID Byte()被填充,但调用&#34; IsValidSID&#34;返回false ...我无法弄清楚为什么,如果成功填充了SIDbytes,问题是什么?

0 个答案:

没有答案