Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Can';t将User32.dll导入Visual Studio_C#_Visual Studio 2012_User32 - Fatal编程技术网

C# Can';t将User32.dll导入Visual Studio

C# Can';t将User32.dll导入Visual Studio,c#,visual-studio-2012,user32,C#,Visual Studio 2012,User32,我试过: 要从引用管理器添加user32.dll,并从Windows\System32\user32.dll导入它,我收到了错误消息: 无法添加对“C:\Windows\System32\user32.dll”的引用。 请确保该文件可访问,并且是有效的程序集或COM组件。 使用System.Runtime.InteropServices;[DllImport(“user32”)] 以管理员身份启动Visual Studio 什么都不管用。。。这让我很紧张,我正试图用两个小时导入这个该死的.d

我试过:

  • 要从引用管理器添加user32.dll,并从Windows\System32\user32.dll导入它,我收到了错误消息

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

  • 使用System.Runtime.InteropServices;[DllImport(“user32”)]

  • 以管理员身份启动Visual Studio


什么都不管用。。。这让我很紧张,我正试图用两个小时导入这个该死的.dll…

它不是.NET dll。您不会像使用.NET DLL那样“添加引用”。相反,您必须向应用程序中添加P/Invoke代码来调用所需的函数。这里有一个学习pinvoke的好资源:

您不需要添加对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");
}
另见:


添加该DLL的实际目标是什么?您提到了
DllImport
,它显然不需要添加本机DLL作为参考…@RohitVats:这个问题涉及.NET或COM DLL,而不是像
user32.DLL
那样的普通Win32 DLL。谢谢!我没有意识到/知道应该定义这个方法(publicstaticexternvoidsetwindowtext(inthwnd,stringtext);)好吧,需要定义的不仅仅是这个方法。从
user32.dll
引用的每个方法必须以以下格式出现:
[System.Runtime.InteropServices.DllImport(“user32.dll”)]
公共静态外部无效()