using LsaOpenPolicy in VB.NET

G

Guest

Hi all,

in VB.NET i want to enumerate the list of users with a special userright. So
i want try to use the LsaEnumerateAccountWithUserRight.

When i try to open a policy handle i return a nullreference error and i
don't know why?

here is my code:
Private Declare Function LsaOpenPolicy Lib "ADVAPI32.dll" ( _
ByVal SystemName As String, _
ByVal ObjectAttributes As
LSA_Object_Attributes, _
ByVal AccessMask As Integer, _
ByRef Handle As IntPtr _
) As Long
Public Function OpenHandle() As IntPtr
Dim Handle As IntPtr
Dim Ret As Long
Ret = LsaOpenPolicy("", Nothing, &HFFFF, Handle)
Return Handle
End Function
 
H

Herfried K. Wagner [MVP]

AndyL said:
in VB.NET i want to enumerate the list of users with a special userright.
So
i want try to use the LsaEnumerateAccountWithUserRight.

When i try to open a policy handle i return a nullreference error and i
don't know why?

here is my code:
Private Declare Function LsaOpenPolicy Lib "ADVAPI32.dll" ( _
ByVal SystemName As String, _

=> 'ByRef SystemName As LSA_UNICODE_STRING'.
ByVal ObjectAttributes As
LSA_Object_Attributes, _

=> 'ByRef ObjectAttributes As LSA_OBJECT_ATTRIBUTES'.
ByVal AccessMask As Integer, _
ByRef Handle As IntPtr _
) As Long

=> 'As Int32'.
 
G

Guest

Hi Herfried,

it doesn't work :-(

I receive always a NullReferenceError WHY?

CU Andy

Dim LsaObjectAttribs As New LSA_Object_Attributes
Dim LsaHandle As IntPtr
Dim computer As New LSA_UNICODE_STRING
Dim Access As Long = &HFFFF
computer.Length = 0
LsaObjectAttribs.Length = 0
If Not LsaOpenPolicy(computer, LsaObjectAttribs, Access, LsaHandle) = 0 Then
Exit Function
 
H

Herfried K. Wagner [MVP]

AndyL said:
I receive always a NullReferenceError WHY?

CU Andy

Dim LsaObjectAttribs As New LSA_Object_Attributes
Dim LsaHandle As IntPtr
Dim computer As New LSA_UNICODE_STRING
Dim Access As Long = &HFFFF

=> 'As Int32'.
computer.Length = 0
LsaObjectAttribs.Length = 0
If Not LsaOpenPolicy(computer, LsaObjectAttribs, Access, LsaHandle) = 0
Then

Post your declarations of 'LSA_UNICODE_STRING' and 'LSA_OBJECT_ATTRIBUTES'.
 
G

Guest

Hi,

here it is

<StructLayout(LayoutKind.Sequential, Pack:=1)> _
Structure LSA_UNICODE_STRING
Dim Length As Integer
Dim MaximumLength As Integer
Dim Buffer As Long
End Structure
<StructLayout(LayoutKind.Sequential, Pack:=1)> _
Structure LSA_Object_Attributes
Dim Length As Long
Dim RootDirectory As Long
Dim ObjectName As Long
Dim Attributes As Long
Dim SecurityDescriptor As Long
Dim SecurityQualityOfService As Long
End Structure

Private Declare Function LsaOpenPolicy Lib "ADVAPI32.dll" ( _
ByRef SystemName As LSA_UNICODE_STRING, _
ByRef ObjectAttributes As
LSA_Object_Attributes, _
ByVal DesiredAccess As Long, _
ByRef PolicyHandle As IntPtr) As Long

Public Function OpenHandle() As String
Dim LsaObjectAttribs As New LSA_Object_Attributes
Dim LsaHandle As IntPtr
Dim computer As New LSA_UNICODE_STRING
Dim Access As Long = &HFFFF
computer.Length = 0
LsaObjectAttribs.Length = 0
If Not LsaOpenPolicy(computer, LsaObjectAttribs, Access, LsaHandle)
= 0 Then Exit Function

End Function
 
H

Herfried K. Wagner [MVP]

AndyL said:
<StructLayout(LayoutKind.Sequential, Pack:=1)> _

I am curious why you set 'Pack' to 1 for this structure.
Structure LSA_UNICODE_STRING
Dim Length As Integer

=> 'As Short'.
Dim MaximumLength As Integer

=> 'As Short'.
Dim Buffer As Long

=> 'As String'.
<StructLayout(LayoutKind.Sequential, Pack:=1)> _
Dito.

Structure LSA_Object_Attributes
Dim Length As Long

=> 'As Int32'.
Dim RootDirectory As Long

=> 'As IntPtr'.
Dim ObjectName As Long

=> 'As PLSA_UNICODE_STRING'.
Dim Attributes As Long

=> 'As Int32'.
Dim SecurityDescriptor As Long

=> 'As IntPtr'.
Dim SecurityQualityOfService As Long

=> 'As IntPtr'.
ByVal DesiredAccess As Long, _

=> 'As Int32'.
ByRef PolicyHandle As IntPtr) As Long
Dim Access As Long = &HFFFF

=> 'As Int32'.
 

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