Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / November 2004

Tip: Looking for answers? Try searching our database.

Unmarshalling LsaEnumerateAccountRights() list of privileges

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Seng - 22 Nov 2004 02:39 GMT
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?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.