试试看用Char* 定义API吧,没时间帮你写代码,给个例子给你吧,希望有帮助
private static extern unsafe bool LookupAccountSid(
string sSystemName,System.IntPtr lpSID,
Char* lpAccountName,
ref int iANameSize,
Char* lpDomainName,
ref int iDNameSize,
ref int lpAccountType
);

解决方案 »

  1.   

    调用方式如下:
    fixed(Char* lpUserBufferPointer = lpUserNameBuffer,lpDomainBufferPointer = lpDomainNameBuffer)
    {
    if( ConvertStringSidToSid(sSIDString,&pSID))
    {
    Trace.WriteLine("call ConvertStringSidToSid() to Get SID point successful!");
    try
    {
    fRtn = LookupAccountSid(null,pSID,
    lpUserBufferPointer,ref lpUserNameBufferSize,
    lpDomainBufferPointer ,ref lpDomainNameBufferSize,ref lpAccountType);
    if (fRtn)
    {
    Trace.WriteLine("call LookupAccountSid() to Get user account info successful!");
    try
    {
    sUserName = new String(lpUserNameBuffer,0,lpUserNameBufferSize);
    sDomainName = new String(lpDomainNameBuffer,0,lpDomainNameBufferSize);
    iReturnCode = CI_Error_NoError;
    }
    catch
    {
    iReturnCode = CI_Error_SystemError;
    }
    }
    else
    {
    iReturnCode = GetLastError();
    Trace.WriteLine("call LookupAccountSid() failed!Last Error code = " + iReturnCode.ToString());
    iReturnCode = CI_Error_APIError;
    }
    }
    catch(Exception e)
    {
    iReturnCode = CI_Error_APIError;
    Trace.WriteLine("call LookupAccountSid() Error!Error is " + e.Message);
    }
    //Free buffer memery
    //waiting code
    try
    {
    Trace.WriteLine("Start to free memory!");
    LocalFree(pSID);  
    }
    catch(Exception e)
    {
    Trace.WriteLine("Free memory Error!Error is " + e.Message);
    } }
    }
      

  2.   

    它提示说不安全代码只能在/unsafe条件下使用,我应该怎么办?是要unsafe编译环境吗?可是我这里没有该怎么办?多谢!!!
      

  3.   

    你有没有设置你的project属性,里面要设置为unsafe的。