A
Adrian Wallis
Hello,
I'm trying to call LsaOpenPolicy, but get the error
'System.AccessViolationException'.
I originally tried a conversion from working VB6 code and I've compared
the code with working C# code and cannot see where the problem lies -
can anyone help to point out the problem please:
Structure LSA_UNICODE_STRING
Dim Length As Short
Dim MaximumLength As Short
Dim Buffer As String
End Structure
Structure LSA_OBJECT_ATTRIBUTES
Dim Length As Integer
Dim RootDirectory As IntPtr
Dim ObjectName As IntPtr
Dim Attributes As Integer
Dim SecurityDescriptor As IntPtr
Dim SecurityQualityOfService As IntPtr
End Structure
Declare Unicode Function LsaOpenPolicy Lib "advapi32.dll" (ByRef
SystemName() As LSA_UNICODE_STRING, ByRef ObjectAttributes As
LSA_OBJECT_ATTRIBUTES, ByVal DesiredAccess As Int32, ByRef PolicyHandle
As IntPtr) As Int32
Private POLICY_ALL_ACCESS As Integer = &HF0FFF
Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim SystemName(0) As LSA_UNICODE_STRING
SystemName(0) = New LSA_UNICODE_STRING
'initialize a pointer for the policy handle
Dim PolicyHandle As IntPtr = IntPtr.Zero
'these attributes are not used, but LsaOpenPolicy wants them to
exists
Dim ObjectAttributes As New LSA_OBJECT_ATTRIBUTES
ObjectAttributes.RootDirectory = IntPtr.Zero
ObjectAttributes.ObjectName = IntPtr.Zero
ObjectAttributes.Attributes = 0
ObjectAttributes.SecurityDescriptor = IntPtr.Zero
ObjectAttributes.SecurityQualityOfService = IntPtr.Zero
ObjectAttributes.Length = 24 ' IN C# :
Marshal.SizeOf(typeof(LSA_OBJECT_ATTRIBUTES));
'get a policy handle
SystemName(0).Length = "192.168.1.1".Length * 2 ' Can't get
UnicodeEncoding.CharSize to work? How to declare?
SystemName(0).MaximumLength = ("192.168.1.1".Length + 1) * 2 '
Can't get UnicodeEncoding.CharSize to work? How to declare?
SystemName(0).Buffer = "192.168.1.1"
Dim ResultPolicy As UInteger
Dim Access = POLICY_ALL_ACCESS
ResultPolicy = LsaOpenPolicy(SystemName, ObjectAttributes,
Access, PolicyHandle)
Many thanks
Adrian
I'm trying to call LsaOpenPolicy, but get the error
'System.AccessViolationException'.
I originally tried a conversion from working VB6 code and I've compared
the code with working C# code and cannot see where the problem lies -
can anyone help to point out the problem please:
Structure LSA_UNICODE_STRING
Dim Length As Short
Dim MaximumLength As Short
Dim Buffer As String
End Structure
Structure LSA_OBJECT_ATTRIBUTES
Dim Length As Integer
Dim RootDirectory As IntPtr
Dim ObjectName As IntPtr
Dim Attributes As Integer
Dim SecurityDescriptor As IntPtr
Dim SecurityQualityOfService As IntPtr
End Structure
Declare Unicode Function LsaOpenPolicy Lib "advapi32.dll" (ByRef
SystemName() As LSA_UNICODE_STRING, ByRef ObjectAttributes As
LSA_OBJECT_ATTRIBUTES, ByVal DesiredAccess As Int32, ByRef PolicyHandle
As IntPtr) As Int32
Private POLICY_ALL_ACCESS As Integer = &HF0FFF
Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim SystemName(0) As LSA_UNICODE_STRING
SystemName(0) = New LSA_UNICODE_STRING
'initialize a pointer for the policy handle
Dim PolicyHandle As IntPtr = IntPtr.Zero
'these attributes are not used, but LsaOpenPolicy wants them to
exists
Dim ObjectAttributes As New LSA_OBJECT_ATTRIBUTES
ObjectAttributes.RootDirectory = IntPtr.Zero
ObjectAttributes.ObjectName = IntPtr.Zero
ObjectAttributes.Attributes = 0
ObjectAttributes.SecurityDescriptor = IntPtr.Zero
ObjectAttributes.SecurityQualityOfService = IntPtr.Zero
ObjectAttributes.Length = 24 ' IN C# :
Marshal.SizeOf(typeof(LSA_OBJECT_ATTRIBUTES));
'get a policy handle
SystemName(0).Length = "192.168.1.1".Length * 2 ' Can't get
UnicodeEncoding.CharSize to work? How to declare?
SystemName(0).MaximumLength = ("192.168.1.1".Length + 1) * 2 '
Can't get UnicodeEncoding.CharSize to work? How to declare?
SystemName(0).Buffer = "192.168.1.1"
Dim ResultPolicy As UInteger
Dim Access = POLICY_ALL_ACCESS
ResultPolicy = LsaOpenPolicy(SystemName, ObjectAttributes,
Access, PolicyHandle)
Many thanks
Adrian