【问题标题】:Can't import User32.dll into Visual Studio无法将 User32.dll 导入 Visual Studio
【发布时间】:2013-07-28 13:16:43
【问题描述】:

我试过了:

  • 要从 Reference Manager 添加 user32.dll,并从 Windows\System32\user32.dll 导入它,我得到了 错误信息

    无法添加对“C:\Windows\System32\user32.dll”的引用。 请确保该文件是可访问的,并且它是一个有效的程序集或 COM 组件。

  • using System.Runtime.InteropServices; [DllImport("user32")]

  • 以管理员身份启动 Visual Studio

没有任何效果...这让我很紧张我正在尝试 2 个小时来导入这个该死的 .dll...

【问题讨论】:

  • 您添加该 DLL 的实际目标是什么?您提到了DllImport,它显然不需要添加本机 DLL 作为参考...
  • @RohitVats:这个问题涉及 .NET 或 COM DLL,而不是像 user32.dll 这样的普通 Win32 DLL。

标签: c# visual-studio-2012 user32


【解决方案1】:

您不需要添加对 User32.dll 的引用。它是 Windows 的一部分,可以在您的代码中导入而无需添加引用。您可以使用 P/Invoke 执行此操作。

[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void SetWindowText(int hWnd, String text);

private void button3_Click(object sender, EventArgs e)
{
    IntPtr wHnd = this.Handle;//assuming you are in a C# form application
    SetWindowText(wHnd.ToInt32(), "New Window Title");
}

另见:

【讨论】:

  • 谢谢!我没有意识到/不知道应该定义该方法( public static extern void SetWindowText(int hWnd, String text); )
  • 嗯,需要定义的不是那种纯粹的方法。您从 user32.dll 引用的每个方法都必须以该格式存在:[System.Runtime.InteropServices.DllImport("user32.dll")] public static extern void <METHODNAME>(<PARAMS>);
【解决方案2】:

它不是 .NET dll。您不会像使用 .NET dll 那样“添加引用”。相反,您必须将 P/Invoke 代码添加到您的应用程序以调用您想要的功能。这是学习 pinvoke 的好资源:http://pinvoke.net/

【讨论】:

    猜你喜欢
    • 2011-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-09
    • 1970-01-01
    • 2012-03-23
    • 2017-09-20
    • 1970-01-01
    相关资源
    最近更新 更多