【问题标题】:what is proper syntax for using user32.dll in static method?在静态方法中使用 user32.dll 的正确语法是什么?
【发布时间】:2014-10-28 13:59:10
【问题描述】:

在下面的代码中,为什么user32会报错?

我认为通过在方法主体上方添加[DllImport("user32.dll", CharSet = CharSet.Unicode)],然后我可以做出类似user32.IsWindowVisible(hWnd) 这样的语句,但是该代码行的user32 部分会导致错误。

这是一个完整的例子。如果您将其复制粘贴到 Visual Studio 的类文件中,您将看到错误:

using System.Collections.Generic;
using System.Runtime.InteropServices;
using System;
using System.Text;

namespace Pinvoke.Automation.Debug.Examples
{

   internal static class ExampleEnumDesktopWindows
    {

        public delegate bool EnumDelegate(IntPtr hWnd, int lParam);


        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool IsWindowVisible(IntPtr hWnd);



        [DllImport("user32.dll", EntryPoint = "GetWindowText",
        ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
        public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpWindowText, int nMaxCount);


        [DllImport("user32.dll", EntryPoint = "EnumDesktopWindows",
        ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
        public static extern bool EnumDesktopWindows(IntPtr hDesktop, EnumDelegate lpEnumCallbackFunction, IntPtr lParam);

       [DllImport("user32.dll", CharSet = CharSet.Unicode)] 
        static void DoExample()
        {
            var collection = new List<string>();
            user32.EnumDelegate filter = delegate(IntPtr hWnd, int lParam)
            {
                StringBuilder strbTitle = new StringBuilder(255);
                int nLength = user32.GetWindowText(hWnd, strbTitle, strbTitle.Capacity + 1);
                string strTitle = strbTitle.ToString();

                if (user32.IsWindowVisible(hWnd) && string.IsNullOrEmpty(strTitle) == false)
                {
                    collection.Add(strTitle);
                }
                return true;
            };

            if (user32.EnumDesktopWindows(IntPtr.Zero, filter, IntPtr.Zero))
            {
                foreach (var item in collection)
                {
                    Console.WriteLine(item);
                }
            }
            Console.Read();
        }
    }
}

【问题讨论】:

  • “为什么 user32 会导致错误” ... 哪个错误?请明确点;我们看不到你的显示器。
  • “我认为通过在方法主体上方添加 [DllImport...],我可以做出类似 user32.IsWindowVisible(hWnd) 的语句” - 不,这根本不是它的工作原理。你已经声明了你的外部,直接引用它们。他们根本不需要user32.
  • 而且在非外部方法上添加[DllImport] 是没有意义的。

标签: c# pinvoke user32


【解决方案1】:

P/invoke 需要 DLL 名称和 EntryPoint 属性,两者都在 DllImport 属性中指定。

您的代码不关心这些。它只使用您在声明 DllImport 注释方法时使用的标识符。

在您的情况下,标识符是 IsWindowVisible,完全限定名称是 Pinvoke.Automation.Debug.Examples.ExampleEnumDesktopWindows.IsWindowVisible

【讨论】:

    【解决方案2】:

    必须在标记为“静态”和“外部”的方法上指定 DllImport 属性,因此您不能在 DoExample() 方法中使用它。

    尝试从该方法中删除它,然后删除 user32。来自 DoExample() 函数内部的方法调用。

    【讨论】:

      猜你喜欢
      • 2013-11-06
      • 2019-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多