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 7047527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:46:56+00:00 2026-05-28T02:46:56+00:00

i’m trying to find a good way to detect if a feature exists before

  • 0

i’m trying to find a good way to detect if a feature exists before P/Invoking. For example calling the native StrCmpLogicalW function:

[SuppressUnmanagedCodeSecurity]
internal static class SafeNativeMethods
{
   [DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
   public static extern int StrCmpLogicalW(string psz1, string psz2);
}

will crash on some systems that do not have this feature.

i don’t want to perform version checking, as that is bad practice, and can sometimes just be wrong (for example when functionality is back-ported, or when the functionality can be uninstalled).

The correct way, is to check for the presence of the export from shlwapi.dll:

private static _StrCmpLogicalW: function(String psz1, String psz2): Integer;
private Boolean _StrCmpLogicalWInitialized;

public int StrCmpLogicalW(String psz1, psz2)
{
    if (!_StrCmpLogialInitialized)
    {
        _StrCmpLogicalW = GetProcedure("shlwapi.dll", "StrCmpLogicalW");
        _StrCmpLogicalWInitialized = true;
    }

    if (_StrCmpLogicalW)
       return _StrCmpLogicalW(psz1, psz2)
    else
       return String.Compare(psz1, psz2, StringComparison.CurrentCultureIgnoreCase);
}

The problem, of course, is that C# doesn’t support function pointers, i.e.:

_StrCmpLogicalW = GetProcedure("shlwapi.dll", "StrCmpLogicalW");

cannot be done.

So i’m trying to find alternative syntax to perform the same logic in .NET. i have the following pseudo-code so far, but i’m getting stymied:

[SuppressUnmanagedCodeSecurity]
internal static class SafeNativeMethods
{
   private Boolean IsSupported = false;
   private Boolean IsInitialized = false;

   [DllImport("shlwapi.dll", CharSet = CharSet.Unicode, Export="StrCmpLogicalW", CaseSensitivie=false, SetsLastError=true, IsNative=false, SupportsPeanutMandMs=true)]
   private static extern int UnsafeStrCmpLogicalW(string psz1, string psz2);

   public int StrCmpLogicalW(string s1, string s2)
   {
       if (!IsInitialized) 
       {
          //todo: figure out how to loadLibrary in .net
          //todo: figure out how to getProcedureAddress in .net
          IsSupported = (result from getProcedureAddress is not null);
          IsInitialized = true;
       }

       if (IsSupported) 
          return UnsafeStrCmpLogicalW(s1, s2);
       else
          return String.Compare(s1, s2, StringComparison.CurrentCultureIgnoreCase);
   }
}

and i need some help.


Another example of some exports that i want to detect presence of would be:

  • dwmapi.dll::DwmIsCompositionEnabled
  • dwmapi.dll::DwmExtendFrameIntoClientArea
  • dwmapi.dll::DwmGetColorizationColor
  • dwmapi.dll::DwmGetColorizationParameters (undocumented1, not yet exported by name, ordinal 127)
  • dwmapi.dll::127 (undocumented1, DwmGetColorizationParameters)

1 as of Windows 7 SP1

There must already be a design pattern in .NET for checking the presence of OS features. Can anyone point me to an example of the preferred way in .NET to perform feature detection?

  • 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-28T02:46:57+00:00Added an answer on May 28, 2026 at 2:46 am

    You could P/Invoke to LoadLibraryW to load shlwapi.dll and then P/Invoke to GetProcAddressW to find “StrCmpLogicalW”. If NULL is returned, then it’s not there.

    You don’t need the actual returned value from GetProcAddressW – as long as it’s not NULL, you know you can use the P/Invoke declaration of your choice.

    Note that GetProcAddressW also supports functions exported by ordinal value.

    EDIT: If you want to follow some kind of pattern, then this might work:

    First define a helper class NativeMethodResolver that tells you if a method exists in a library:

    public static class NativeMethodResolver
    {
        public static bool MethodExists(string libraryName, string methodName)
        {
            var libraryPtr = LoadLibrary(libraryName);
            var procPtr = GetProcAddress(libraryPtr, methodName);
    
            return libraryPtr != UIntPtr.Zero && procPtr != UIntPtr.Zero;
        }
    
        [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        private static extern UIntPtr LoadLibrary(string lpFileName);
    
        [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Ansi)]
        private static extern UIntPtr GetProcAddress(UIntPtr hModule, string lpProcName);
    }
    

    The above helper class can be consumed by derived classes of SafeNativeMethod that aids in boiler plating some common stuff:

    public abstract class SafeNativeMethod
    {
        private readonly string libraryName;
        private readonly string methodName;
        private bool resolved;
        private bool exists;
    
        protected SafeNativeMethod(string libraryName, string methodName)
        {
            this.libraryName = libraryName;
            this.methodName = methodName;
        }
    
        protected bool CanInvoke
        {
            get
            {
                if (!this.resolved)
                {
                    this.exists = Resolve();
                    this.resolved = true;
                }
    
                return this.exists; 
            }            
        }
    
        private bool Resolve()
        {
            return NativeMethodResolver.MethodExists(this.libraryName, this.methodName);
        }
    }
    

    A derived class that defines its own Invoke method can then call the base CanInvoke to see if a default value (or default implementation) should be returned in place of the return value of the sought native method. From your question, I’ll take shlwapi.dll/StrCmpLogicalW and dwmapi.dll/DwmIsCompositionEnabled as example implementations for SafeNativeMethod:

    public sealed class SafeStrCmpLogical : SafeNativeMethod
    {
        public SafeStrCmpLogical()
            : base("shlwapi.dll", "StrCmpLogicalW")
        {           
        }
    
        public int Invoke(string psz1, string psz2)
        {
            return CanInvoke ? StrCmpLogicalW(psz1, psz2) : 0;
        }
    
        [DllImport("shlwapi.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        private static extern int StrCmpLogicalW(string psz1, string psz2);
    }
    
    public sealed class SafeDwmIsCompositionEnabled : SafeNativeMethod
    {
        public SafeDwmIsCompositionEnabled()
            : base("dwmapi.dll", "DwmIsCompositionEnabled")
        {
        }
    
        public bool Invoke()
        {
            return CanInvoke ? DwmIsCompositionEnabled() : false;
        }
    
        [DllImport("dwmapi.dll", SetLastError = true, PreserveSig = false)]
        private static extern bool DwmIsCompositionEnabled();
    }
    

    Those two can then be used like this:

    static void Main()
    {
        var StrCmpLogical = new SafeStrCmpLogical();
        var relation = StrCmpLogical.Invoke("first", "second");
    
        var DwmIsCompositionEnabled = new SafeDwmIsCompositionEnabled();
        var enabled = DwmIsCompositionEnabled.Invoke();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Seemingly simple, but I cannot find anything relevant on the web. What is the
i got an object with contents of html markup in it, for example: string

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.