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

  • Home
  • SEARCH
  • 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 1036631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:44:10+00:00 2026-05-16T14:44:10+00:00

The following code exists in LogEntry.cs in the Enterprise Library’s Logging Application Block: private

  • 0

The following code exists in LogEntry.cs in the Enterprise Library’s Logging Application Block:

private bool UnmanagedCodePermissionAvailable
{
  get
  {
    if (!unmanagedCodePermissionAvailableInitialized)
    {
      // check whether the unmanaged code permission is available to avoid three potential stack walks
      bool internalUnmanagedCodePermissionAvailable = false;
      SecurityPermission unmanagedCodePermission = 
                  new SecurityPermission(SecurityPermissionFlag.UnmanagedCode);
      // avoid a stack walk by checking for the permission on the current assembly. this is safe because there are no
      // stack walk modifiers before the call.
      if (SecurityManager.IsGranted(unmanagedCodePermission))
      {
        try
        {
          unmanagedCodePermission.Demand();
          internalUnmanagedCodePermissionAvailable = true;
        }
        catch (SecurityException)
        { }
      }

      this.UnmanagedCodePermissionAvailable = 
          internalUnmanagedCodePermissionAvailable;
    }

    return this.unmanagedCodePermissionAvailable;
  }
  set
  {
    this.unmanagedCodePermissionAvailable = value;
    unmanagedCodePermissionAvailableInitialized = true;
  }
}

The function is called before any of several P/Invoke calls are made to retrieve various information to help fill in the LogEntry structure. If “UnmanagedCodePermission” is not available, then the corresponding LogEntry property is set to a string indicating such (“XXX is not available”).

For example, LogEntry wants to get the Win32 thread id and it uses the Win32 function, GetCurrentThreadId, called by P/Invoke to get it. Before calling GetCurrentThreadId, it checks to see if “unamanged code permission” is available. If so, it makes the call, if not, it does not. Something like this:

private void InitializeWin32ThreadId()
{
  if (this.UnmanagedCodePermissionAvailable)
  {
    try
    {
      this.Win32ThreadId = LogEntryContext.GetCurrentThreadId();
    }
    catch (Exception e)
    {
      this.Win32ThreadId = string.Format(
                                  CultureInfo.CurrentCulture,
                                  Properties.Resources.IntrinsicPropertyError,
                                  e.Message);
    }
  }
  else
  {
    this.Win32ThreadId = string.Format(CultureInfo.CurrentCulture,
                Properties.Resources.IntrinsicPropertyError,
                Properties.Resources.
                LogEntryIntrinsicPropertyNoUnmanagedCodePermissionError);
  }
}

From what I understand, which, admittedly, is not much, making calls to unmanaged code (e.g. P/Invoke) is not always possible due to security/permissions/trust. Having this check to see if unmanaged code calls are possible, makes it possible to protect all unmanaged calls in a uniform manner.

When I compile this code, I get a warning on this line:

          if (SecurityManager.IsGranted(unmanagedCodePermission))

Here is the warning:

System.Security.SecurityManager.IsGranted(System.Security.IPermission)’ is obsolete: ‘IsGranted is obsolete and will be removed in a future release of the .NET Framework. Please use the PermissionSet property of either AppDomain or Assembly instead.

(Note that I am building this on .Net 4.0 using VS2010).

So, it looks IsGranted is obsolete. I looked at the PermissionSet property for AppDomain and Assembly, and it wasn’t obvious exactly how to make the same check.

In the case of LogEntry, it looks like this information is not critical, so it is not considered a critical failure if unmanaged permission is not available. Consider the following questions from the same point of view. That is, if unmanaged code permission is not available, it is not a huge deal, I can live without the information.

Finally, a couple of questions:

  1. Is it a good idea to try to protect calls to unmanaged code (e.g. P/Invoke)? Sometimes, always, never?

  2. If it is a good idea to protect these calls, is this a reasonable pattern for doing so? Is there a better way?

  3. What would be the correct (i.e. not obsolete) way to make the equivalent check in .Net 4.0?

  • 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-05-16T14:44:11+00:00Added an answer on May 16, 2026 at 2:44 pm

    Before .NET 4, Code Access Security (CAS) was the security model used by the .NET Fx. Idea was to identify what code can do based on evidence and don’t allow to do it any other things (SandBoxing). For example, code present on your local computer by default gets full trust (essentially it can do anything) while code coming from internet will have restricted permissions (partial trust) scenario.

    IMO, if you are writing code that unlikely to run in partially trusted environment then you may not concern much about it. However for partially trusted assemblies,

    1. If they can tell their host (for example IE), what permissions it needs then host can decides whether to grant those based on security policy etc. Or host may prompt user and allow him to override. Or admin would know the permission set by inspecting assembly and he may decide to update policy to allow it.
    2. In case, host does not grant needed permission to the code then code can check it and handle it gracefully instead of generating into security exception.

    So above code illustrates ways to achieve this prior to .NET 4. In .NET 4, there is new security model that is more simpler to use. See this & this article for more information.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.