all groups > dotnet interop > december 2003 >
You're in the

dotnet interop

group:

calling LSALookupNames2 from C#: NullReferenceException


calling LSALookupNames2 from C#: NullReferenceException Lukas Alber
12/31/2003 9:28:53 AM
dotnet interop: Hi

I'm trying to call LSALookupNames2 from C# and get a NullReferenceException.
I assume that I made a mistake declaring LSALookupNames2 or
LSA_UNICODE_STRING

The LSAOpenPolicy and LSAFreeMemory calls work fine (I got most of the code
from another post to this newsgroup)

Can anyone tell me where I went wrong?

Thanks
Lukas

The code (CommandLine app):

using System;

using System.Runtime.InteropServices;

using System.Text;

internal sealed class LSA_AccessPolicy

{

public const int POLICY_VIEW_LOCAL_INFORMATION = 0x1;

public const int POLICY_VIEW_AUDIT_INFORMATION = 0x2;

public const int POLICY_GET_PRIVATE_INFORMATION = 0x4;

public const int POLICY_TRUST_ADMIN = 0x8;

public const int POLICY_CREATE_ACCOUNT = 0x10;

public const int POLICY_CREATE_SECRET = 0x20;

public const int POLICY_CREATE_PRIVILEGE = 0x40;

public const int POLICY_SET_DEFAULT_QUOTA_LIMITS = 0x80;

public const int POLICY_SET_AUDIT_REQUIREMENTS = 0x100;

public const int POLICY_AUDIT_LOG_ADMIN = 0x200;

public const int POLICY_SERVER_ADMIN = 0x400;

public const int POLICY_LOOKUP_NAMES = 0x800;

}

class testLSA

{

[StructLayout(LayoutKind.Sequential)]

internal struct LSA_UNICODE_STRING

{

public UInt16 Length;

public UInt16 MaximumLength;

public IntPtr Buffer;

}

//typedef struct LSA UNICODE STRING {

// USHORT Length;

// USHORT MaximumLength;

// PWSTR Buffer;

//} LSA UNICODE STRING, *PLSA UNICODE STRING;

//--------------------------------------------------------------------------
--//

[StructLayout(LayoutKind.Sequential)]

internal struct LSA_OBJECT_ATTRIBUTES

{

public UInt32 Length;

public IntPtr RootDirectory;

public LSA_UNICODE_STRING ObjectName;

public UInt32 Attributes;

public IntPtr SecurityDescriptor;

public IntPtr SecurityQualityOfService;

}

//typedef struct LSA OBJECT ATTRIBUTES {

// ULONG Length;

// HANDLE RootDirectory;

// PLSA UNICODE STRING ObjectName;

// ULONG Attributes;

// PVOID SecurityDescriptor;

// PVOID SecurityQualityOfService;

//} LSA OBJECT ATTRIBUTES, *PLSA OBJECT ATTRIBUTES;

//--------------------------------------------------------------------------
--//

[DllImport("advapi32.dll")]

internal static extern UInt32 LsaLookupNames2(

IntPtr PolicyHandle,

UInt32 Count,

ref LSA_UNICODE_STRING Names,

out IntPtr ReferencedDomains,

out IntPtr Sids

);

// NTSTATUS LsaLookupNames2(

// LSA_HANDLE PolicyHandle,

// ULONG Count,

// PLSA_UNICODE_STRING Names,

// PLSA_REFERENCED_DOMAIN_LIST* ReferencedDomains,

// PLSA_TRANSLATED_SID2* Sids

// );

//--------------------------------------------------------------------------
--//

[DllImport("advapi32.dll")]

public static extern UInt32 LsaNtStatusToWinError(

UInt32 Status);

//ULONG LsaNtStatusToWinError(

// NTSTATUS Status

//);

//--------------------------------------------------------------------------
--//

[DllImport("advapi32.dll", PreserveSig=false)]

public static extern UInt32 LsaOpenPolicy(

ref LSA_UNICODE_STRING SystemName,

ref LSA_OBJECT_ATTRIBUTES ObjectAttributes,

int DesiredAccess,

out IntPtr PolicyHandle);

//NTSTATUS LsaOpenPolicy(

// PLSA UNICODE STRING SystemName,

// PLSA OBJECT ATTRIBUTES ObjectAttributes,

// ACCESS MASK DesiredAccess,

// PLSA HANDLE PolicyHandle

//);

//--------------------------------------------------------------------------
--//

[DllImport("advapi32.dll")]

public static extern UInt32 LsaFreeMemory(

IntPtr Buffer);

//NTSTATUS LsaFreeMemory(

// PVOID Buffer

//);

//--------------------------------------------------------------------------
--//

static public void Main()

{

//Preprare Strucutres for Policy Open call

LSA_UNICODE_STRING SystemName = new LSA_UNICODE_STRING();

LSA_OBJECT_ATTRIBUTES ObjectAttributes = new LSA_OBJECT_ATTRIBUTES();

ObjectAttributes.Length = 0;

ObjectAttributes.RootDirectory = IntPtr.Zero;

//ObjectAttributes.ObjectName =; //Leaving Blank

ObjectAttributes.Attributes = 0;

ObjectAttributes.SecurityDescriptor = IntPtr.Zero;

ObjectAttributes.SecurityQualityOfService = IntPtr.Zero;

int DesiredAccess = LSA_AccessPolicy.POLICY_LOOKUP_NAMES;

IntPtr PolicyHandle = new IntPtr();

//Fetch a Policy Handle

UInt32 result = LsaOpenPolicy(ref SystemName, ref ObjectAttributes,
DesiredAccess, out PolicyHandle);

// Check for errors

if (result != 0)

Console.WriteLine("Openning Policy: " +
((int)LsaNtStatusToWinError(result)));

else

Console.WriteLine("Openning Policy: " + result);


//Prepare Structures to get names

LSA_UNICODE_STRING KeyName = new LSA_UNICODE_STRING();

string key = @"wotan\alber";

KeyName.Buffer = Marshal.StringToHGlobalUni(key);

KeyName.Length = (UInt16)(key.Length * UnicodeEncoding.CharSize);

KeyName.MaximumLength = (UInt16)((key.Length+ 1) *
UnicodeEncoding.CharSize);

IntPtr Domains;

IntPtr Sids;

UInt32 cnt = 1;

int win32result;

//Attempting to get names

result = LsaLookupNames2(PolicyHandle,cnt, ref KeyName, out Domains, out
Sids);

//Checking for errors

if (result != 0)

{

win32result = (int)LsaNtStatusToWinError(result);

Console.WriteLine("Fetching Key: " + win32result);

}

else

Console.WriteLine("Fetching Key: " + result);

//Freeing Memory

Marshal.FreeHGlobal(KeyName.Buffer);

result = LsaFreeMemory(PolicyHandle);

if (result != 0)

Console.WriteLine("Error Freeing Memory");

else

Console.WriteLine("Freeing Memory: " + result);

}

}

Re: calling LSALookupNames2 from C#: NullReferenceException Mattias Sjögren
1/1/2004 5:40:20 PM
Lukas,

[quoted text, click to view]

The ObjectName member of the LSA_OBJECT_ATTRIBUTES struct should be a
_pointer to_ a LSA_UNICODE_STRING struct (i.e. an IntPtr), not an
inline LSA_UNICODE_STRING.

The policy handle you get from LsaOpenPolicy should be closed with
LsaClose, not freed with LsaFreeMemory.

Those were the only two errors I could find in your code. It doesn't
seem like they would cause a NullReferenceException in the
LSALookupNames2 call though (assuming LsaOpenPolicy returns a valid
handle), so I'm not sure if correcting them will help or not.



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Re: calling LSALookupNames2 from C#: NullReferenceException Lukas Alber
1/5/2004 9:04:43 AM
Mattias

Thanks for your help. Unfortunately you were right: Correcting the two
errors you detected did not avoid the NullReferenceException.

Lukas

[quoted text, click to view]

Re: calling LSALookupNames2 from C#: NullReferenceException Mattias Sjögren
1/10/2004 1:24:14 AM
Lukas,

[quoted text, click to view]

Right, I tried it here and got the NullReferenceException too. It had
me stumped for a while, until I tried to write the same code in C++
and the compiler complained about the number of parameters to
LsaLookupNames2. Looking it up in the header file (Ntsecapi.h), it
turns out that it has an extra parameter that's missing from the docs.
So change the declaration to

[DllImport("advapi32.dll")]
internal static extern UInt32 LsaLookupNames2(
IntPtr PolicyHandle,
UInt32 Flags,
UInt32 Count,
ref LSA_UNICODE_STRING Names,
out IntPtr ReferencedDomains,
out IntPtr Sids
);

and it will probably work a lot better.

You should also remove the PreserveSig=false from the LsaOpenPolicy
DllImport attribute.

And I noticed that you never fill the SystemName variable with
anything. If you want the local system, you should change the
SystemName parameter to an IntPtr and pass IntPtr.Zero rather than a
non-initialized LSA_UNICODE_STRING.

And finally, don't forget to call LsaFreeMemory for the returned
Domains and Sids.



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Re: calling LSALookupNames2 from C#: NullReferenceException Mattias Sjögren
1/10/2004 1:33:17 AM

[quoted text, click to view]

Looks like that has been corrected in the online version of the
Platform SDK docs already BTW:

http://msdn.microsoft.com/library/en-us/security/security/lsalookupnames2.asp



Mattias

--
AddThis Social Bookmark Button