473,387 Members | 1,464 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,387 software developers and data experts.

API in vb.net - OpenInputDesktop and GetUserObjectInformation

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

Mar 3 '07 #1
2 4630
>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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Mar 3 '07 #2
On Mar 2, 6:31 pm, Mattias Sjögren <mattias.dont.want.s...@mvps.org>
wrote:
TheSIDthat's returned is: 130000055000000010620300 which isn't
like anything on this machine. The SecurityIdentifer class doesn't
recognize it as aSIDeither.

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

Mattias

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

Mar 3 '07 #3

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

Similar topics

0
by: Romeo | last post by:
Hello, I would like to know how i can detect if a user is online, in idle time or lock session. I know how to do this in C++ but i don't know in C#. In C++, for idle you can use...
15
by: Mephisto187 | last post by:
How can I find the currently visible desktop? By calling EnumWindowStations and EnumDesktops I get a list of all available desktops. But is there a way to figure out which one of them is currently...
3
by: Jay | last post by:
Hey There, Is it possible to get an unique identifier for a desktop that a particular app is running from, from a service? If there are two instances of the app running on different desktops (one...
0
by: yxq | last post by:
Hello, I found the code to lock and brush Windows desktop, and i have modified some code for disabling brush the Main Form(Me), but i can not see the changes of the controls on main form(Me) real...
1
by: Damjan Malis | last post by:
Hi guys... could anyone please help me with my problem getting mouse cursor position while desktop being locked (WIN + L). MSDN says: "The input desktop must be the current desktop when you...
2
jamesd0142
by: jamesd0142 | last post by:
Hi im just starting to dabble with windows API and i getting a headache!. Is there any way to find a list of functions available in user32 DLL file? any other info is welcome here... James
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.