Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't import User32.dll into Visual Studio

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...

like image 458
jovanMeshkov Avatar asked Jul 28 '13 20:07

jovanMeshkov


People also ask

How do I fix user32 DLL error?

If the User32. dll error message appeared during or after you installed a program, a hardware component, or a driver, uninstall the program, the hardware component, or the driver. Then restart Windows, and reinstall the program, the hardware component, or the driver.

Where is user32 DLL located?

In 64-bit versions of Windows, the 64-bit implementation of Windows USER is called user32. dll and is located in the System32 directory, while a modified 32-bit version (also called user32. dll) is present in the SysWOW64 directory.

How does DLL import work C#?

It uses two core winapi functions. First is LoadLibrary(), the winapi function that loads a DLL into a process. It uses the name you specified for the DLL. Second is GetProcAddress(), the winapi function that returns the address of a function in a DLL.


2 Answers

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
like image 72
jrbeverly Avatar answered Sep 30 '22 13:09

jrbeverly


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/

like image 26
Dax Fohl Avatar answered Sep 30 '22 13:09

Dax Fohl