Can't import User32.dll into Visual Studio

夙愿已清 提交于 2020-01-02 02:19:06

问题


I tried:

  • To add user32.dll from Reference Manager, and imported it from Windows\System32\user32.dll, I got Error Message:

    A reference to 'C:\Windows\System32\user32.dll couldn't be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component.

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

  • To launch Visual Studio as Administrator

Nothing works... it goes on my nerves I am trying 2 hours to import this damn .dll...


回答1:


You do not need to add a reference to User32.dll. It is part of Windows and can be imported in your code without adding a reference. You do this using 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");
}

See Also:

  • Using P/Invoke from MSDN
  • Calling API Functions



回答2:


It's not a .NET dll. You don't "add reference" the same way you do with .NET dlls. Instead you have to add P/Invoke code to your app to invoke the functions you want. Here's a good resource for learning pinvoke: http://pinvoke.net/



来源:https://stackoverflow.com/questions/17912352/cant-import-user32-dll-into-visual-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!