从 .Net 代码调用 RtlIsNameInExpression

Call RtlIsNameInExpression from .Net code

因为很多 intricacies in filename patternmatching in Windows (and former DOS) I'd like to call RtlIsNameInExpression from my .Net code to make sure my (console) application will behave identical to Windows applications. However, I don't seem to be able to find how to PInvoke this function; I have no idea what the DllImport should look like and I am unable to find any examples nor anything useful on pinvoke.net.

如有任何帮助,我们将不胜感激!

编辑: 我是盲人。我在谷歌搜索 FsRtlIsNameInExpression 应该 在谷歌搜索 RtlIsNameInExpression (解释 )。

无论如何;发现了一些东西 here 并且似乎有效。

找到下面的代码here. Posting / answering my own question to make sure it doesn't get lost. All credit to David Růžička

// UNICODE_STRING for Rtl... method
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct UNICODE_STRING
{
    public ushort Length;
    public ushort MaximumLength;
    [MarshalAs(UnmanagedType.LPWStr)]
    string Buffer;

    public UNICODE_STRING(string buffer)
    {
        if (buffer == null)
            Length = MaximumLength = 0;
        else
            Length = MaximumLength = unchecked((ushort)(buffer.Length * 2));
        Buffer = buffer;
    }
}

// RtlIsNameInExpression method from NtDll.dll system library
public static class NtDll
{
    [DllImport("NtDll.dll", CharSet=CharSet.Unicode, ExactSpelling=true)]
    [return: MarshalAs(UnmanagedType.U1)]
    public extern static bool RtlIsNameInExpression(
        ref UNICODE_STRING Expression,
        ref UNICODE_STRING Name,
        [MarshalAs(UnmanagedType.U1)]
        bool IgnoreCase,
        IntPtr Zero
        );
}

public bool MatchMask(string mask, string fileName)
{
    // Expression must be uppercase for IgnoreCase == true (see MSDN for RtlIsNameInExpression)
    UNICODE_STRING expr = new UNICODE_STRING(mask.ToUpper());
    UNICODE_STRING name = new UNICODE_STRING(fileName);

    if (NtDll.RtlIsNameInExpression(ref expr, ref name, true, IntPtr.Zero))
    {
        // MATCHES !!!
    }
}