Feature detection when P/Invoking in C# and .NET
Asked Answered
R

2

12

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?

Rodgers answered 9/1, 2012 at 20:47 Comment(4)
The design pattern in the .NET Framework source code is to check OS version numbers, but to do so intelligently as Larry Osterman ends up concluding in his blog post. I agree that Johann's solution is probably a better one, but I'm also a Win32 guy. LoadLibrary and GetProcAddress just make sense to me. I spend most of my time writing P/Invoke definitions when I write .NET code. I'm not sure if that's actually a good thing.Clime
@Cody: I'm not sure if that's actually a good thing - probably not, no. :-)Micheal
@CodeGray You cannot rely on version numbers. A feature might have been retroactively ported to an OS (making version numbers wrong). A feature could also be not installed by the user (making version numbers wrong).Rodgers
Yes, you can. It just requires that you do your research. You have to do a sufficient amount of research to figure out where the API you wish to call can be found (i.e., what DLL) even if you go with Johann's solution, which isn't that much different than figuring out if it can be optionally installed or if it has been retroactively ported to an old OS. The old OSes are known quantities since they've already been released at the time your code is being written, so you can check and verify all of that yourself. You specifically asked about a design pattern in .NET, and this is what it does.Clime
M
6

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();
}
Micheal answered 9/1, 2012 at 20:53 Comment(4)
You could also use Marshal.GetDelegateForFunctionPointer() to convert the returned address to a delegate.Peroration
@Hans: Yes, unless you're using the .NET Compact Framework. That method is unsupported in the Marshal class there.Micheal
See pinvoke.net for syntax for LoadLibrary, GetProcAddress, and FreeLibrary.Mendy
i'm trying to come up with the design pattern. How many flags do i use (available, checked), where do perform this test (during static initialization? on first use? use a class which performs the checks during construction and make the class a singleton? Have static methods that call into an internal singleton? Do i assign delegate methods and check for the method variable being null? Can a method delegate being null? etc, etc. i'm looking for the design pattern.Rodgers
C
1

Marshal.Prelink can be used to get the P/Invoke "not found" exception without actually calling the method. Here is how it could be used to complete your example:

[SuppressUnmanagedCodeSecurity]
internal static class SafeNativeMethods
{
    [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);

    static bool? _UnsafeStrCmpLogicalWIsSupported;
    static bool UnsafeStrCmpLogicalWIsSupported => _UnsafeStrCmpLogicalWIsSupported ?? IsSupported("UnsafeStrCmpLogicalW", out _UnsafeStrCmpLogicalWIsSupported);

    public static int StrCmpLogicalW(string s1, string s2)
    {
        if (UnsafeStrCmpLogicalWIsSupported)
            return UnsafeStrCmpLogicalW(s1, s2);
        else
            return String.Compare(s1, s2, StringComparison.CurrentCultureIgnoreCase);
    }

    static bool IsSupported(string method, out bool? field)
    {
        var result = IsSupported(method);
        field = result;
        return result;
    }
    static bool IsSupported(string method)
    {
        var methods = typeof(SafeNativeMethods).GetMethods().Where(m => m.Name == method).ToList();
        if (methods.Count == 0)
            throw new ArgumentException("Method not found: "+method, nameof(method));
        try
        {
            foreach (var m in methods)
                Marshal.Prelink(m);
            return true;
        }
        catch { return false; }
    }
}
Cromer answered 12/7, 2022 at 14:3 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.