Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 591089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:35:53+00:00 2026-05-13T15:35:53+00:00

It’s a pointer to an array of LSA_UNICODE_STRING structures. I found some code that

  • 0

It’s a pointer to an array of LSA_UNICODE_STRING structures. I found some code that does the inverse, i.e., create a LSA_UNICODE_STRING from a C# string. You can see that in the helper code section below.

What I have up to and including the call to LsaEnumerateAccountRights() seems to work just fine. Sensible values are returned for the array pointer and for the count.

I am at a loss as to how to get at those blasted strings. Help please? Pretty please?

UPDATE: nobugz’s helper function in his answer below is ALMOST right, you only have to divide the length by UnicodeEncoding.CharSize. Thanks to him, I can now see the FIRST string in the array. See the updates at the end of both code sections below.

Now, how the netherworld do I do pointer arithmetic?

UPDATE 2.5: See answer for the functioning code. I lost the old, “wrong” code.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:35:54+00:00Added an answer on May 13, 2026 at 3:35 pm

    Found it! In this blog post. Now the amended code below works fully. It’s even 64-bit safe!

    The main code:

    IntPtr sid = IntPtr.Zero;
    int sidSize = 0;
    StringBuilder domainName = new StringBuilder();
    int nameSize = 0;
    int accountType = 0;
    
    LookupAccountName("\\\\" + tbHost.Text, tbUsername.Text, sid, ref sidSize,
        domainName, ref nameSize, ref accountType);
    domainName = new StringBuilder(nameSize);
    sid = Marshal.AllocHGlobal(sidSize);
    
    bool result = LookupAccountName("\\\\" + tbHost.Text, tbUsername.Text, sid, ref sidSize,
        domainName, ref nameSize, ref accountType);
    
    myResults.Text += String.Format("LookupAccountName(): Result {0}, SID {1}\n", result, sid);
    
    LSA_UNICODE_STRING systemName = string2LSAUS("\\\\" + tbHost.Text);
    IntPtr policyHandle = IntPtr.Zero;
    LSA_OBJECT_ATTRIBUTES objAttrs = new LSA_OBJECT_ATTRIBUTES();
    uint retVal = LsaOpenPolicy(ref systemName, ref objAttrs,
                        POLICY_LOOKUP_NAMES | POLICY_VIEW_LOCAL_INFORMATION, out policyHandle);
    
    myResults.Text += String.Format("LsaOpenPolicy(): Result {0}, Policy Handle {1}\n", retVal, policyHandle);
    
    IntPtr rightsArray = IntPtr.Zero;
    ulong rightsCount = 0;
    long lretVal = LsaEnumerateAccountRights(policyHandle, sid, out rightsArray, out rightsCount);
    retVal = LsaNtStatusToWinError(lretVal);
    
    if (retVal != 0)
        throw new System.ComponentModel.Win32Exception((int)retVal);
    
    myResults.Text += String.Format("LsaEnumerateAccountRights(): Result {0}, RightsArray {1}, Count {2}\n",
        retVal, rightsArray, rightsCount);
    
    LSA_UNICODE_STRING myLsaus = new LSA_UNICODE_STRING();
    for (ulong i = 0; i < rightsCount; i++)
    {
        IntPtr itemAddr = new IntPtr(rightsArray.ToInt64() + (long)(i * (ulong) Marshal.SizeOf(myLsaus)));
        myLsaus = (WinNetUtils.LSA_UNICODE_STRING)Marshal.PtrToStructure(itemAddr, myLsaus.GetType());
        string thisRight = WinNetUtils.LSAUS2string(myLsaus);
        NonBlockingPrint(wmiResults, "Right #{0}: {1}\n", i+1, thisRight);
    }
    LsaClose(policyHandle);
    

    The helper functions, imports etc:

    public const int POLICY_VIEW_LOCAL_INFORMATION = 0x1;
    public const int POLICY_LOOKUP_NAMES = 0x00000800;
    
    [DllImport("advapi32.dll", CharSet = CharSet.Unicode, PreserveSig = true)]
    public static extern UInt32 LsaNtStatusToWinError(
        long Status);
    
    [DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true, PreserveSig = true)]
    public static extern bool ConvertStringSidToSid(
        string StringSid, out IntPtr pSid);
    
    [DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true, PreserveSig = true)]
    public static extern bool LookupAccountName( 
        string lpSystemName, string lpAccountName, 
        IntPtr psid, ref int cbsid, 
        StringBuilder domainName, ref int cbdomainLength, 
        ref int use );
    
    [DllImport("advapi32.dll", CharSet = CharSet.Unicode, PreserveSig = true)]
    public static extern UInt32 LsaOpenPolicy(
        ref LSA_UNICODE_STRING SystemName,
        ref LSA_OBJECT_ATTRIBUTES ObjectAttributes,
        Int32 DesiredAccess,
        out IntPtr PolicyHandle );
    
    [DllImport("advapi32.dll", SetLastError = true, PreserveSig = true)]
    public static extern long LsaEnumerateAccountRights(
        IntPtr PolicyHandle, IntPtr AccountSid,
        out /* LSA_UNICODE_STRING[] */ IntPtr UserRights,
        out ulong CountOfRights); 
    
    [DllImport("advapi32.dll", SetLastError = true, PreserveSig = true)]
    public static extern long LsaClose(
                IntPtr PolicyHandle);
    
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct LSA_UNICODE_STRING 
    { 
      public UInt16 Length; 
      public UInt16 MaximumLength; 
      public IntPtr Buffer; 
    }
    
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct LSA_OBJECT_ATTRIBUTES
    {
        public IntPtr RootDirectory;
        public IntPtr SecurityDescriptor;
        public IntPtr SecurityQualityOfService;
        public LSA_UNICODE_STRING ObjectName;
        public UInt32 Attributes;
        public UInt32 Length;
    }
    
    public static LSA_UNICODE_STRING string2LSAUS(string myString)
    {
        LSA_UNICODE_STRING retStr = new LSA_UNICODE_STRING();
        retStr.Buffer = Marshal.StringToHGlobalUni(myString);
        retStr.Length = (UInt16)(myString.Length * UnicodeEncoding.CharSize);
        retStr.MaximumLength = (UInt16)((myString.Length + 1) * UnicodeEncoding.CharSize);
        return retStr;
    }
    
    public static string LSAUS2string(LSA_UNICODE_STRING lsaus)
    {
        char[] cvt = new char[lsaus.Length / UnicodeEncoding.CharSize];
        Marshal.Copy(lsaus.Buffer, cvt, 0, lsaus.Length / UnicodeEncoding.CharSize);
        return new string(cvt);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 499k
  • Answers 500k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is not pretty but it works: rm -R $(ls… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer Yes. Override the base1 and base2 methods in Derived to… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer No, you can't. Unfortunately, UIEvent doesn't expose any public way… May 16, 2026 at 12:45 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
I've got a string that has curly quotes in it. I'd like to replace
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.