Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8503133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:40:33+00:00 2026-06-11T01:40:33+00:00

There is object of type SECURITY_IDENTIFIER structure in the file. I need to get

  • 0

There is object of type SECURITY_IDENTIFIER structure in the file. I need to get owner SID from this structure. In order to do this I call GetSecurityDescriptorOwner WinAPI function and create System.Security.Principal.SecurityIdentifier (it has overload taking IntPtr as an argument)

The problem is this structure in file is broken sometimes, so the pointer I get from GetSecurityDescriptorOwner is invalid. It’s not IntPtr.Zero, it’s invalid, so when I create object of type SecurityIdentifier I get AccessViolationException, which is not possible to catch with .NET 4 with a simple try-catch.

I am aware of attribute which allows to catch such exceptions, so I used it for the time being, but I don’t like this solution. It’s not recommended to catch Corrupted State Exceptions (CSE), but I don’t see any other solutions. This WinAPI function returns me invalid pointer and I see no way to check it for validness. Any ideas?

update

WinAPI

BOOL WINAPI GetSecurityDescriptorOwner(
  _In_   PSECURITY_DESCRIPTOR pSecurityDescriptor,
  _Out_  PSID *pOwner,
  _Out_  LPBOOL lpbOwnerDefaulted
);

Extern definition

[DllImport("Advapi32.dll")]
static extern bool GetSecurityDescriptorOwner(
   IntPtr pSecurityDescriptor,
   out IntPtr owner,
   out bool defaulted);

update

private static SecurityIdentifier GetSecurityIdentifier()
{
    // Allocate managed buffer for invalid security descriptor structure (20 bytes)
    int[] b = new int[5] {1, 1, 1, 1, 1};

    // Allocate unmanaged memory for security descriptor 
    IntPtr descriptorPtr = Marshal.AllocHGlobal(b.Length);

    // Copy invalid security descriptor structure to the unmanaged buffer
    Marshal.Copy(b, 0, descriptorPtr, b.Length);

    IntPtr ownerSid;
    bool defaulted;

    if (GetSecurityDescriptorGroup(descriptorPtr, out ownerSid, out defaulted))
    {
        // GetSecurityDescriptorGroup returns true, but `ownerSid` is `1`
        // Marshal.GetLastWin32Error returns 0 here
        return new SecurityIdentifier(ownerSid);
    }

    return null;
}

This code throws sometimes throws Corrupted State Exceptions from SecurityIdentifier constructor. Any solutions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T01:40:35+00:00Added an answer on June 11, 2026 at 1:40 am

    Have you tried calling IsValidSecurityDescriptor?

    [DllImport("Advapi32.dll")]
    static extern bool IsValidSecurityDescriptor(IntPtr pSecurityDescriptor);
    
    
    if (IsValidSecurityDescriptor(descriptorPtr) && 
        GetSecurityDescriptorOwner(descriptorPtr, out ownerSid, out defaulted))
    {
         return new SecurityIdentifier(ownerSid);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to get the type of object in the arraylist? I
Is there a simple way to check the type of an object? I need
Is there a way to do a comparison on object type for a trigger?
Am I missing something or there really is no support for generic object type
In the Code Below there are Two Classes. One Object of type two is
In UML, if there's a class A having an object of type B as
is there anyway to unbox an object to its real type? Basically I am
I have a Type, a String and an Object. Is there some way I
Is there a way to determine the Object type, when passing a reference to
Is there a simple way in NumPy to flatten type object array? I know

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.