|
JM
Intel's OCL (Open Class Library) made heavy use of the DllImportAttribute in order to reference key functionality in older C/C++ based DLLs. For example, I/O made heavy use of the standard C runtime library in msvcrt.dll and the Windows kernel in kernel32.dll. I would say that in an implementation of the CLI standard, DllImport is one of the most, if not the most, important attribute available.
AN
In this attribute, the CharSet value chosen not only affects how Strings, StringBuilders, and Chars are marshaled, but it controls what entry points the CLR will look for (e.g., if it looks for MessageBoxW or MessageBoxA in addition to MessageBox).
AN
It is kind of strange that DllImport has a Boolean PreserveSig field that is completely independent of PreserveSigAttribute. The reason for this is that we needed a mechanism to "turn off" signature-preserving (which is the default behavior for PInvoke), and PreserveSigAttribute can only turn it on! Had PreserveSigAttribute contained any instance data, such as a Boolean field, then this detail wouldn't have needed to bleed into DllImportAttribute.
AN
A little-known fact about DllImportAttribute's SetLastError is that the CLR pretends that it is always set to true for several Win32 DLLs (kernel32, gdi32, user32, etc.) regardless of its real setting. That is because for such DLLs, it is almost always the correct behavior and there is very little penalty for having it set on APIs that don't make use of the SetLastError mechanism. This is similar in spirit to the fact that VB.NET Declare statements always set SetLastError to true. |