代码之家  ›  专栏  ›  技术社区  ›  RobIII Lukas

从调用RtlIsNameInExpression。网络代码

  •  1
  • RobIII Lukas  · 技术社区  · 6 年前

    因为很多 intricacies 在里面 filename patternmatching 在Windows(和以前的DOS)中,我想打电话 RtlIsNameInExpression 从我的。Net代码,以确保我的(控制台)应用程序的行为与Windows应用程序相同。然而,我似乎无法找到如何PInvoke这个函数;我不知道 DllImport 应该是这样的,我找不到任何示例,也找不到任何有用的 pinvoke.net

    任何帮助都将不胜感激!

    编辑 : 我是瞎子。我在谷歌上搜索 FsRtlIsNameInExpression 我在哪里 应该是的 一直在谷歌上搜索 RtlIsNameInExpression (说明 here )。

    无论如何发现了一些东西 here 这似乎奏效了。

    1 回复  |  直到 2 年前
        1
  •  2
  •   RobIII Lukas    6 年前

    找到下面的代码 here 。张贴/回答我自己的问题,以确保不会丢失。全部贷记至 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 !!!
        }
    }