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 IsIniti