相关疑难解决方法(0)

C#:在shlwapi.dll中实现或替代StrCmpLogicalW

对于我的应用程序中的自然排序,我目前在shlwapi.dll中调用一个名为StrCmpLogicalW的函数.我正在考虑尝试在Mono下运行我的应用程序,但当然我不能拥有这个P/Invoke的东西(据我所知).

是否有可能在某处看到该方法的实现,或者是否有一个好的,干净且高效的C#片段可以做同样的事情?

我的代码目前看起来像这样:

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

public class NaturalStringComparer : IComparer<string>
{
    private readonly int modifier = 1;

    public NaturalStringComparer() : this(false) {}
    public NaturalStringComparer(bool descending)
    {
        if (descending) modifier = -1;
    }

    public int Compare(string a, string b)
    {
        return SafeNativeMethods.StrCmpLogicalW(a ?? "", b ?? "") * modifier;
    }
}
Run Code Online (Sandbox Code Playgroud)

所以,我正在寻找的是上述类的替代品,它不使用extern函数.

c# natural-sort extern

9
推荐指数
2
解决办法
3990
查看次数

标签 统计

c# ×1

extern ×1

natural-sort ×1