2008-09-11 54 views
2

有人能給我一個從C#代碼調用AllocateAndInitializeSid函數的完整實例嗎?如何從C#調用AllocateAndInitializeSid函數?

我發現this

BOOL WINAPI AllocateAndInitializeSid(
    __in PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority, 
    __in BYTE nSubAuthorityCount, 
    __in DWORD dwSubAuthority0, 
    __in DWORD dwSubAuthority1, 
    __in DWORD dwSubAuthority2, 
    __in DWORD dwSubAuthority3, 
    __in DWORD dwSubAuthority4, 
    __in DWORD dwSubAuthority5, 
    __in DWORD dwSubAuthority6, 
    __in DWORD dwSubAuthority7, 
    __out PSID *pSid 
); 

,我不知道如何構建這個方法的簽名 - 我該怎麼做PSID_IDENTIFIER_AUTHORITYPSID類型?我應該如何通過它們 - 使用ref還是out

回答

1

使用P/Invoke Interop Assistant

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)] 
    public struct SidIdentifierAuthority { 

     /// BYTE[6] 
     [System.Runtime.InteropServices.MarshalAsAttribute(
      System.Runtime.InteropServices.UnmanagedType.ByValArray, 
      SizeConst = 6, 
      ArraySubType = 
      System.Runtime.InteropServices.UnmanagedType.I1)] 
     public byte[] Value; 
    } 

    public partial class NativeMethods { 

     /// Return Type: BOOL->int 
     ///pIdentifierAuthority: PSID_IDENTIFIER_AUTHORITY->_SID_IDENTIFIER_AUTHORITY* 
     ///nSubAuthorityCount: BYTE->unsigned char 
     ///nSubAuthority0: DWORD->unsigned int 
     ///nSubAuthority1: DWORD->unsigned int 
     ///nSubAuthority2: DWORD->unsigned int 
     ///nSubAuthority3: DWORD->unsigned int 
     ///nSubAuthority4: DWORD->unsigned int 
     ///nSubAuthority5: DWORD->unsigned int 
     ///nSubAuthority6: DWORD->unsigned int 
     ///nSubAuthority7: DWORD->unsigned int 
     ///pSid: PSID* 
     [System.Runtime.InteropServices.DllImportAttribute("advapi32.dll", EntryPoint = "AllocateAndInitializeSid")] 
     [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)] 
     public static extern bool AllocateAndInitializeSid(
      [System.Runtime.InteropServices.InAttribute()] 
      ref SidIdentifierAuthority pIdentifierAuthority, 
      byte nSubAuthorityCount, 
      uint nSubAuthority0, 
      uint nSubAuthority1, 
      uint nSubAuthority2, 
      uint nSubAuthority3, 
      uint nSubAuthority4, 
      uint nSubAuthority5, 
      uint nSubAuthority6, 
      uint nSubAuthority7, 
      out System.IntPtr pSid); 

    } 
0

對於平臺調用www.pinvoke.net是你最好的朋友!

http://www.pinvoke.net/default.aspx/advapi32/AllocateAndInitializeSid.html

+0

去過那裏。 根據MSDN,第一個參數是'指向SID_IDENTIFIER_AUTHORITY結構的指針',pinvoke.net說它是IntPtr。它是一樣的嗎?我應該如何創建這個指針? 請發佈_complete_示例。我嘗試了我在網上找到的所有內容,但無法完成工作。 – 2008-09-11 14:53:51

1

如果您的目標.NET 2.0或更高版本,System.Security.Principal.SecurityIdentifier包裝一個SID,並允許您避免容易出錯的Win32 API的類。

不完全是對你的問題的答案,但是誰知道它可能是有用的。