Having trouble unmarshalling the list of rights returned by
LsaEnumerateAccountRights()
Declared it as...
[DllImport("advapi32.dll", SetLastError=true, PreserveSig=true)]
private static extern long LsaEnumerateAccountRights(
IntPtr PolicyHandle,
IntPtr AccountSid,
out LSA_UNICODE_STRING[] UserRights,
out long CountOfRights);
Called it...(after opening policy and obtaining SID)...
LSA_UNICODE_STRING[] userRights = null
long countOfRights = 0;
long res = LsaEnumerateAccountRights(policyHandle,sid,
out userRights,
out countOfRights);
string heldPrivName = Marshal.PtrToStringUni(userRights[0].Buffer);
string heldPrivName1 = Marshal.PtrToStringUni(userRights[1].Buffer);
It seems to work (in as much as 'CountOfRights' contains the correct count).
However, the userRights array that is returned only has 1 item in it -- not
2?
Can you please explain why there is a mismatch between the returned count
and the number of elements in the userRights array?
Seng - 23 Nov 2004 18:31 GMT
Turns out I was ascribing more intelligence to the mapping of types by
Interop than I should have. Needed to think more like a "C" programmer.
First my declaration was wrong, here it is corrected...
[DllImport("advapi32.dll", SetLastError=true, PreserveSig=true)]
private static extern long LsaEnumerateAccountRights(
IntPtr PolicyHandle,
IntPtr AccountSid,
out IntPtr UserRightsPtr,
out long CountOfRights);
Then, need to map the struct onto the elements of the UserRights array one
at a time...
// UserRightsPtr was returned by LsaEnumerateAccountRights
Int32 ptr = userRightsPtr.ToInt32();
LSA_UNICODE_STRING userRight;
bool hasPriv=false;
for(int i=0; i<countOfRights; i++)
{
// Here, 'ptr' is the pointer to the buffer returned by LSA
userRight = (LSA_UNICODE_STRING)Marshal.PtrToStructure(new
IntPtr(ptr),typeof(LSA_UNICODE_STRING));
String userRightStr = Marshal.PtrToStringAuto(userRight.Buffer);
if( userRightStr == privilegeName )
{
hasPriv = true;
break;
}
ptr += Marshal.SizeOf(userRight);
}
> Having trouble unmarshalling the list of rights returned by
> LsaEnumerateAccountRights()
[quoted text clipped - 26 lines]
> Can you please explain why there is a mismatch between the returned count
> and the number of elements in the userRights array?