Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

FreeSid (advapi32)
 
.
Summary

C# Signature:

[DllImport("advapi32.dll")]
static extern IntPtr FreeSid(IntPtr pSid);
- or -
[DllImport("advapi32")]
public static extern void FreeSid(IntPtr pSid);
(since FreeSid returns a PVOID)

VB Signature:

Declare Function FreeSid Lib "advapi32.dll" (ByVal pSid As IntPtr) As IntPtr

User-Defined Types:

None.

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

using System;

using System.Runtime.InteropServices;

using System.Text;

class Test

{

  static void Main()  {
    string strSid = GetSIDForAccount("Administrator");
    Console.WriteLine( strSid  );
  }

  public static string GetSIDForAccount(string Account) {
    long winErrorCode = 0; //contains the last error

    //pointer and size for the SID
    IntPtr pSid = IntPtr.Zero;
    int iSidSize = 0;
    //StringBuilder for Domain
    StringBuilder sbDomain = new StringBuilder();
    int iDomainSize = 0;

    //account-type variable for lookup
    int iAccountType = 0;

    //get required buffer size  (iSidSize=0 causes this...)
    LookupAccountName(String.Empty, Account, pSid, ref iSidSize,
      sbDomain, ref iDomainSize, ref iAccountType);

    //allocate buffers
    sbDomain = new StringBuilder(iDomainSize);
    pSid = Marshal.AllocHGlobal(iSidSize);

    //lookup the SID for the account
    bool bResult = LookupAccountName(String.Empty,
                     Account,
                     pSid,
                     ref iSidSize,
                     sbDomain,
                     ref iDomainSize,
                     ref iAccountType);
    if (!bResult) {
      winErrorCode = GetLastError();
      Console.WriteLine("LookupAccountName failed: "+ winErrorCode);
      FreeSid(pSid);
      throw new ApplicationException("Could not read SID");
    }

    string strSID = "";
    if ( ConvertSidToStringSid( pSid, ref strSID)) {
      FreeSid(pSid);
      return strSID;
    } else {
      throw new ApplicationException("Could not convert SID");
    }
  }

  [DllImport("kernel32.dll")]
  private static extern int GetLastError();

  [DllImport( "advapi32.dll", CharSet=CharSet.Auto, SetLastError=true, PreserveSig=true)]
  private static extern bool LookupAccountName(
    string lpSystemName,
    string lpAccountName,
    IntPtr psid,
    ref int cbsid,
    StringBuilder domainName,    //StringBuilder or string?
    ref int cbdomainLength,
    ref int use );

  [DllImport("advapi32.dll", CharSet=CharSet.Auto)]
  private static extern bool ConvertSidToStringSid(
    IntPtr pSID,
    [In,Out,MarshalAs(UnmanagedType.LPTStr)] ref string strSID);

  [DllImport("advapi32")]
  private static extern void FreeSid(IntPtr pSid);  

}

Alternative Managed API:

Do you know one? Please contribute it!

Documentation
FreeSid on MSDN

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Find References
Show Printable Version
Revisions