无法将User32.dll导入Visual Studio

jov*_*kov 8 c# user32 visual-studio-2012

我试过了:

  • 要添加user32.dll中参考经理,并从进口它的Windows\SYSTEM32\user32.dll中,我得到了错误信息:

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

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

  • 以管理员身份启动Visual Studio

什么都行不通......它让我感到紧张我试着用2个小时来导入这个该死的.dll ......

jrb*_*rly 9

您不需要添加对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");
}
Run Code Online (Sandbox Code Playgroud)

也可以看看: