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.
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With