<p>Can somebody give me a complete and working example of calling the <code>AllocateAndInitializeSid</code> function from C# code?</p>

<p>I found <a href="http://msdn.microsoft.com/en-us/library/aa375213(VS.85).aspx" rel="nofollow noreferrer">this</a>:  </p>

<pre><code>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
);
</code></pre>

<p>and I don't know how to construct the signature of this method - what should I do with <code>PSID_IDENTIFIER_AUTHORITY</code> and <code>PSID</code> types? How should I pass them - using <code>ref</code> or <code>out</code>?</p>

[{'answer_id': 56745, 'author': 'Mendelt', 'author_id': 3320, 'author_profile': 'https://Stackoverflow.com/users/3320', 'pm_score': 1, 'selected': False, 'text': '<p>For Platform Invoke www.pinvoke.net is your new best friend!</p>\n\n<p><a href="http://www.pinvoke.net/default.aspx/advapi32/AllocateAndInitializeSid.html" rel="nofollow noreferrer">http://www.pinvoke.net/default.aspx/advapi32/AllocateAndInitializeSid.html</a></p>\n'}, {'answer_id': 58157, 'author': 'Paul Lalonde', 'author_id': 5782, 'author_profile': 'https://Stackoverflow.com/users/5782', 'pm_score': 2, 'selected': False, 'text': '<p>If you are targeting .NET 2.0 or later, the class System.Security.Principal.SecurityIdentifier wraps a SID and allows you to avoid the error-prone Win32 APIs.</p>\n\n<p>Not exactly an answer to your question, but who knows it may be useful.</p>\n'}, {'answer_id': 58180, 'author': 'jfs', 'author_id': 718, 'author_profile': 'https://Stackoverflow.com/users/718', 'pm_score': 3, 'selected': True, 'text': '<p>Using <a href="http://www.codeplex.com/clrinterop" rel="nofollow noreferrer">P/Invoke Interop Assistant</a>:</p>\n\n<pre><code>    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]\n    public struct SidIdentifierAuthority {\n\n        /// BYTE[6]\n        [System.Runtime.InteropServices.MarshalAsAttribute(\n            System.Runtime.InteropServices.UnmanagedType.ByValArray, \n            SizeConst = 6, \n            ArraySubType = \n            System.Runtime.InteropServices.UnmanagedType.I1)]\n        public byte[] Value;\n    }\n\n    public partial class NativeMethods {\n\n        /// Return Type: BOOL-&gt;int\n        ///pIdentifierAuthority: PSID_IDENTIFIER_AUTHORITY-&gt;_SID_IDENTIFIER_AUTHORITY*\n        ///nSubAuthorityCount: BYTE-&gt;unsigned char\n        ///nSubAuthority0: DWORD-&gt;unsigned int\n        ///nSubAuthority1: DWORD-&gt;unsigned int\n        ///nSubAuthority2: DWORD-&gt;unsigned int\n        ///nSubAuthority3: DWORD-&gt;unsigned int\n        ///nSubAuthority4: DWORD-&gt;unsigned int\n        ///nSubAuthority5: DWORD-&gt;unsigned int\n        ///nSubAuthority6: DWORD-&gt;unsigned int\n        ///nSubAuthority7: DWORD-&gt;unsigned int\n        ///pSid: PSID*\n        [System.Runtime.InteropServices.DllImportAttribute("advapi32.dll", EntryPoint = "AllocateAndInitializeSid")]\n        [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]\n        public static extern bool AllocateAndInitializeSid(\n            [System.Runtime.InteropServices.InAttribute()] \n            ref SidIdentifierAuthority pIdentifierAuthority, \n            byte nSubAuthorityCount, \n            uint nSubAuthority0, \n            uint nSubAuthority1, \n            uint nSubAuthority2, \n            uint nSubAuthority3, \n            uint nSubAuthority4, \n            uint nSubAuthority5, \n            uint nSubAuthority6, \n            uint nSubAuthority7, \n            out System.IntPtr pSid);\n\n    }\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/56729', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/95/']