Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DllImport user32 vs user32.dll

What is the difference between the usages of DllImport here? Specifically, does "user32" just mean "user32.dll", or does it mean "user32.lib" or something else?

[DllImport("user32")]
protected static extern int GetKeyboardState(byte[] pbKeyState);

[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
protected static extern short GetKeyState(int vKey);

You can probably ignore the CharSet and CallingConvention.

If they are the same, I can rewrite this to be more consistent, but if not, I don't want to have a bunch of problems with it.

like image 759
Kendall Frey Avatar asked Mar 05 '12 22:03

Kendall Frey


People also ask

What is USER32 DLL used for?

USER32. DLL implements the Windows USER component that creates and manipulates the standard elements of the Windows user interface, such as the desktop, windows, and menus. It thus enables programs to implement a graphical user interface (GUI) that matches the Windows look and feel.

What is DllImport USER32 DLL in C#?

It means that the method declared below it is not in . NET - it is in a external (native) DLL file. In this case, it is in the User32. dll file, which is a standard Windows component.

What is DllImport?

The dllexport and dllimport storage-class attributes are Microsoft-specific extensions to the C and C++ languages. You can use them to export and import functions, data, and objects to or from a DLL.

How does DllImport work?

DllImport attribute uses the InteropServices of the CLR, which executes the call from managed code to unmanaged code. It also informs the compiler about the location of the implementation of the function used.


2 Answers

In this example, there is no difference. The .dll extension will automatically be appended to "user32" to create "user32.dll". However, this is not always the case. If the library file name contains a period, the .dll extension will not be automatically appended.

Some examples:

[DllImport("user32")] --> Resolves "User32.dll". Correct.

[DllImport("user32.dll")] --> Resolves "User32.dll". Correct.

[DllImport("mylib.version5")] --> Resolves "mylib.version5". Incorrect

[DllImport("mylib.version5.dll")] --> Resolves "mylib.version5.dll". Correct.

like image 95
ahawker Avatar answered Sep 24 '22 00:09

ahawker


On Windows there is no difference, the import will be performed successfully if you omit the extension. Normally omitting the extension is desired when running Mono with the <dllmap> configuration section, where the P/Invoke runtime will look for aliases.

like image 45
kprobst Avatar answered Sep 21 '22 00:09

kprobst