Desktop Functions:

   Smart Device Functions:

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

Terms of Use
Privacy Policy
GetSecurityInfo (advapi32)
You can use the GetSecurityInfo function with the following types of objects

Local or remote files or directories on an NTFS file system
Named pipes
Local or remote printers
Local or remote Windows services
Network shares
Registry keys
Semaphores, events, mutexes, and waitable timers
Processes, threads, jobs, and file-mapping objects
Interactive service window stations and desktops
Directory service objects

C# Signature:

[DllImport("advapi32.dll", SetLastError=true)]
static extern uint GetSecurityInfo(
IntPtr handle,
out IntPtr pSidOwner,
out IntPtr  pSidGroup,
out IntPtr pDacl,
out IntPtr pSacl,
out IntPtr pSecurityDescriptor);

VB Signature:

      <DllImport("advapi32.dll", setlasterror:=True)> _
     Private Shared Function GetSecurityInfo( _
        ByRef hObject As IntPtr, _
        ByRef ObjectType As SE_OBJECT_TYPE, _
        ByRef SecurityInfo As SECURITY_INFORMATION, _
        ByRef pSidOwner As IntPtr, _
        ByRef pSidGroup As IntPtr, _
        ByRef pDacl As IntPtr, _
        ByRef pSacl As IntPtr, _
        ByRef pSD As IntPtr) _
     As Integer
      End Function

User-Defined Types:

Alternative Managed API:

Do you know one? Please contribute it!


I am currently trying to implement this function to get the security attributes for Network Shares.

Tips & Tricks:

Please add some!

Sample Code:


Compiled for 2.0 but I guess it should work for 1.1. (Of course 2.0 provides the AccessControl namespace which makes this sample only interesting for knowing the basics of how it is done)

This sample only retrieves the owner of the object (in this case a file but could be other object if you change the objectType)

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

using System.Runtime.InteropServices;

using System.Security.AccessControl;

namespace GetSecurityInfoTest


    class Program
    [DllImport("advapi32.dll", SetLastError = true)]
    static extern int GetSecurityInfo(
        IntPtr          handle,
        SE_OBJECT_TYPE         objectType,
        SECURITY_INFORMATION     securityInfo,
        out IntPtr          sidOwner,
        out IntPtr          sidGroup,
        out IntPtr          dacl,
        out IntPtr          sacl,
        out IntPtr          securityDescriptor);

    [DllImport("advapi32", CharSet = CharSet.Unicode, SetLastError = true)]
    static extern bool ConvertSidToStringSid(
        IntPtr              sid,
        out IntPtr          sidString);      

    [DllImport("kernel32.dll", SetLastError = true)]
    internal static extern IntPtr LocalFree(
        IntPtr         handle

        SACL_SECURITY_INFORMATION = 8,        

    static void Main(string[] args)
        FileStream fileStream = null;        
        IntPtr ownerSid;
        IntPtr groupSid;
        IntPtr dacl;
        IntPtr sacl;
        IntPtr securityDescriptor = IntPtr.Zero;

        int returnValue = 0;
        bool success = false;

        fileStream = File.Open(@"C:\Test\Test.txt", FileMode.Open);

        returnValue = GetSecurityInfo(fileStream.Handle, SE_OBJECT_TYPE.SE_FILE_OBJECT, SECURITY_INFORMATION.OWNER_SECURITY_INFORMATION | SECURITY_INFORMATION.DACL_SECURITY_INFORMATION, out ownerSid, out groupSid, out dacl, out sacl, out securityDescriptor);          

        IntPtr sidString = IntPtr.Zero;
        success = ConvertSidToStringSid(ownerSid, out sidString);



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 directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version