P/Invoke as in the following quote provides basically the way to calls into the unmanaged DLLs. the quote is as follow.
DllImport
DllImport has many a attribute, but fortunately you can visit all those attribute from the following page: Using P/Invoke to call Unmanaged APIs from your Managed Classes, attribute include
- Name
- EntryPoint
- SetLastError
- CharSet
- ExactSpelling
- BestFitMapping
- Calling Convention
- Perserve Signature
- ThrowOnUnmappedChar
Marshalling
As for marshalling,
as it is quoted:
As this is maybe very important for the common types to marshal across platform..
Windows Data Type | .NET Data Type |
---|---|
BOOL, BOOLEAN | Boolean or Int32 |
BSTR | String |
BYTE | Byte |
CHAR | Char |
DOUBLE | Double |
DWORD/LPDWORD | Int32 or UInt32 |
FLOAT | Single |
HANDLE (and all other handle types, such as HFONT and HMENU) | IntPtr, UintPtr, or HandleRef |
HRESULT | Int32 or UInt32 |
INT | Int32 |
LANGID | Int16 or UInt16 |
LCID | Int32 or UInt32 |
LONG | Int32 |
LPARAM | IntPtr, UintPtr, or Object |
LPCSTR | String |
LPCTSTR | String |
LPCWSTR | String |
LPSTR | String or StringBuilder* |
LPTSTR | String or StringBuilder |
LPWSTR | String or StringBuilder |
LPVOID | IntPtr, UintPtr, or Object |
LRESULT | IntPtr |
SAFEARRAY | .NET array type |
SHORT | Int16 |
TCHAR | Char |
UCHAR | SByte |
UINT | Int32 or UInt32 |
ULONG | Int32 or UInt32 |
VARIANT | Object |
VARIANT_BOOL | Boolean |
WCHAR | Char |
WORD | Int16 or UInt16 |
WPARAM | IntPtr, UintPtr, or Object |
StructLayout Attribute
Because we have to marshal types between native and managed code, we have to take care to handle the StructLayout which controls how they end up in the memory (imagine a native function which pass a struct out with special layout from what is expected from managed side or the reverse/inverse.)
So what is structLayout is about?
The following will control the how a StructLayout is
- Layout - one of the following kinds
- LayoutKind.Sequential
- LayoutKind.Union
- LayoutKind.Explicit
- Pack
- Charset
- Size
Besisde the explicit PInvoke, there is some implicit invoke - a more accurate way of saying is "C++ Interop (Implicit PInvoke) - whch is said to have better support in
managed and unmanaged code to exist in the same application/even same file - from unmanaged to managed ones
Better type safety, less tedious to implement, more forgiving if the unmanaged API is modified, makes performance enhancement possible which is not possible with explicit PInvoke.
References:
Using P/Invoke to call Unmanaged APIs from your Managed Classes