Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我成功地调用了advapi32';s LsaEnumerateAccountRights()来自C#。现在我如何解组它返回的LSA_UNICODE_字符串数组?_C#_Unicode_Marshalling_Advapi32 - Fatal编程技术网

我成功地调用了advapi32';s LsaEnumerateAccountRights()来自C#。现在我如何解组它返回的LSA_UNICODE_字符串数组?

我成功地调用了advapi32';s LsaEnumerateAccountRights()来自C#。现在我如何解组它返回的LSA_UNICODE_字符串数组?,c#,unicode,marshalling,advapi32,C#,Unicode,Marshalling,Advapi32,它是指向结构数组的指针。我发现了一些与此相反的代码,即从C字符串创建LSA\u UNICODE\u字符串。您可以在下面的帮助程序代码部分中看到这一点 我所做的,包括调用LsaEnumerateAccountRights()似乎很好。返回数组指针和计数的合理值 我不知道怎样才能抓住那些该死的弦。请帮忙?漂亮吗 更新:nobugz在下面的答案中的helper函数几乎正确,您只需将长度除以unicodeincode.CharSize。多亏了他,我现在可以看到数组中的第一个字符串。请参阅下面两个代码部分

它是指向结构数组的指针。我发现了一些与此相反的代码,即从C字符串创建
LSA\u UNICODE\u字符串。您可以在下面的帮助程序代码部分中看到这一点

我所做的,包括调用
LsaEnumerateAccountRights()
似乎很好。返回数组指针和计数的合理值

我不知道怎样才能抓住那些该死的弦。请帮忙?漂亮吗

更新:nobugz在下面的答案中的helper函数几乎正确,您只需将长度除以
unicodeincode.CharSize
。多亏了他,我现在可以看到数组中的第一个字符串。请参阅下面两个代码部分末尾的更新

现在,我该怎么做指针算术呢


更新2.5:有关功能代码,请参阅答案。我丢失了旧的“错误”代码。

这应该适用于您:

    private static string LSAUS2String(LSA_UNICODE_STRING lsa) {
        char[] cvt = new char[lsa.Length];
        Marshal.Copy(lsa.Buffer, cvt, 0, lsa.Length);
        return new string(cvt);
    }
找到了!在里面现在,下面修订的代码完全起作用。它甚至是64位安全的

主要代码:

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

似乎我必须对数组的每个成员使用Marshal.PtrToStructure(),但是如何遍历数组呢?指针算法???PTRTO结构?不,它是一个字符数组。Marshal.Copy将其从IntPtr复制到char[]。很简单,对不起,我刚才说的是更高级的数组,LSA_UNICODE_字符串的数组。我现在让它工作(见上图)。谢谢你的帮助!您应该从问题中提取答案并将其放在此处。此外,还需要确保您正在调用LsaClose()以关闭句柄。完成,完成。不幸的是,我丢失了原始的不起作用的代码,因此问题看起来有点不完整。
NTSTATUS
的长度是32位,而不是64位。请注意,此代码存在内存泄漏。Marshal.StringToHGlobalUni需要对Marshal.FreeHGlobal.Related的相应调用:和:
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);
}