本文介绍了我所谓的成功ADVAPI32的LsaEnumerateAccountRights()从C#。现在,我怎么解组LSA​​_UNICODE_STRING它返回数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个指向结构。我发现一些代码的倒数,即从C#字符串创建一个 LSA_UNICODE_STRING 。你可以看到,在下面的助手代码部分。

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.

我有多达什么,并包括调用 LsaEnumerateAccountRights()似乎工作就好了。明智的值则返回数组指针和计数。

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?

更新:在下面他的答案nobugz的辅助函数几乎是正确的,你只需要通过<$ C $划分长度C> UnicodeEncoding.CharSize 。多亏了他,我现在可以看到数组中的第一个字符串。参见以下两个代码段的结尾更新。

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?

更新2.5:作为功能代码中看到答案。我失去了旧的,错误的代码。

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

推荐答案

找到了!在。现在,下面的代码修改工作充分。 !它甚至64位的安全

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

的主要代码:

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);

的辅助函数,进口等:

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);
}

这篇关于我所谓的成功ADVAPI32的LsaEnumerateAccountRights()从C#。现在,我怎么解组LSA​​_UNICODE_STRING它返回数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 22:16