API in vb.net - OpenInputDesktop and GetUserObjectInformation


A

andrewbb

I'm trying to find the SID of the user who owns the desktop, but the
SID that's returned isn't recognizable. Is it a problem with the
declaration/marshaling?

The SID that's returned is: 130000055000000010620300 which isn't
like anything on this machine. The SecurityIdentifer class doesn't
recognize it as a SID either.


<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function OpenDesktop(ByVal lpszDesktop As String, ByVal
dwFlags As Integer, ByVal fInderit As Boolean, ByVal dwDesiredAccess
As Integer) As IntPtr
End Function

<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Private Shared Function GetUserObjectInformation(ByVal hObj As IntPtr,
ByVal nIndex As Integer, <MarshalAs(UnmanagedType.LPArray)> ByVal
pvInfo As Byte(), ByVal nLength As Integer, ByRef lpnLengthNeeded As
Integer) As Boolean
End Function


Dim length As Integer
Dim success As Boolean

Dim buf(99) As Byte

Dim desk As IntPtr = OpenInputDesktop(0, True, DESKTOP_READOBJECTS)

success = GetUserObjectInformation(desk, UOI_USER_SID, buf, 100,
length)

Dim ret As New StringBuilder(length)

For i As Integer = 0 To length - 1
ret.Append(CStr(buf(i)))
Next

return ret.ToString() 'returns 130000055000000010620300
 
Ad

Advertisements

M

Mattias Sjögren

The SID that's returned is: 130000055000000010620300 which isn't
like anything on this machine. The SecurityIdentifer class doesn't
recognize it as a SID either.

Shouldn't you be using the ConvertSidToStringSid function to get the
string representation of the SID instead of using your own routine?


Mattias
 
Ad

Advertisements

A

andrewbb

Shouldn't you be using the ConvertSidToStringSid function to get the
string representation of theSIDinstead of using your own routine?

Mattias

Thanks, made some progress and am getting a SID that appears to be
well formed, but what is it? S-1-5-5-0-52074

I thought I would be receiving the SID of the account that owns the
current desktop. What SID is this? Nothing in the registry matches
it.


<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Private Shared Function GetUserObjectInformation( _
ByVal hObj As IntPtr, _
ByVal nIndex As Integer, _
<MarshalAs(UnmanagedType.LPArray)> ByVal pvInfo As Byte(), _
ByVal nLength As Integer, _
ByRef lpnLengthNeeded As Integer) As Integer
End Function

<DllImport("advapi32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
_
Private Shared Function ConvertSidToStringSid( _
<MarshalAs(UnmanagedType.LPArray)> ByVal sid As Byte(), _
ByRef stringSID As IntPtr) As Integer
End Function

Dim desk As IntPtr = OpenInputDesktop(0, True, DESKTOP_READOBJECTS)
Dim station As IntPtr = OpenWindowStation("winsta0", True,
WINSTA_ENUMERATE)

GetUserSID(desk)
GetUserSID(station)

'both return S-1-5-5-0-52074


Private Function GetUserSID(ByVal handle As IntPtr) As String
Dim length As Integer
Dim result As Integer

Dim byteSID(255) As Byte
result = GetUserObjectInformation(handle, UOI_USER_SID,
byteSID, 256, length)
ReDim Preserve byteSID(length - 1)

Dim ptrSID As IntPtr = IntPtr.Zero
Dim stringSID As String = ""
Try
result = ConvertSidToStringSid(byteSID, ptrSID)
If result <> 1 Then
Throw New Exception("Failed converting SID. Last error
is: " + CStr(Marshal.GetLastWin32Error()))
End If

stringSID = Marshal.PtrToStringAuto(ptrSID)
Catch ex As Exception
Finally
Marshal.FreeHGlobal(ptrSID)
End Try
Return stringSID
End Function
 

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