C# – DllImport user32 vs user32.dll

cdllimportnetpinvokeuser32

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.

Best Answer

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.