How do I figure out the correct argument structure for an unmanaged dll?

核能气质少年 提交于 2019-11-28 02:07:20

As already noted by Hans Passant you should use the managed alternatives instead.

However to answer your actual question: You basically just need to check the MSDN documentation article Windows Data Types, and based on a type's declaration determine the respective .NET type.

For instance, a DWORD:

DWORD
A 32-bit unsigned integer. The range is 0 through 4294967295 decimal.

This type is declared in IntSafe.h as follows:

typedef unsigned long DWORD;

In this case we can either go by the range (0 - 4294967295) or the definition (unsigned long) in order to determine that this should be an unsigned 32-bit integer (UInt32 or UInteger). In C/C++ a long is the same thing as an int, which is why it's mapped to an integer and not the Long/ULong.

Here's a summary of the most common types:

(Big thanks to David Heffernan for helping me correct some of the string declarations!)

+------------------+------------------------------------------------------+
|   Windows Type   |                   .NET equivalent                    |
+------------------+------------------------------------------------------+
| BOOL             | <MarshalAs(UnmanagedType.Bool)> Boolean  (vb.net)    |
| BOOL             | [MarshalAs(UnmanagedType.Bool)] bool     (c#)        |
| BYTE             | Byte   / Byte     (vb.net) / byte  (c#)              |
| CHAR             | Char   / Char     (vb.net) / char  (c#)              |
| DWORD            | UInt32 / UInteger (vb.net) / uint  (c#)              |
| DWORDLONG        | UInt64 / ULong    (vb.net) / ulong (c#)              |
| DWORD_PTR        | UIntPtr                                              |
| FLOAT            | Single / Single   (vb.net) / float (c#)              |
|                  |                                                      |
| HANDLE           | IntPtr                                               |
| HBITMAP          | IntPtr                                               |
| HCURSOR          | IntPtr                                               |
| HDESK            | IntPtr                                               |
| HDC              | IntPtr                                               |
| HICON            | IntPtr                                               |
| HINSTANCE        | IntPtr                                               |
| HRESULT          | Int32 / Integer (vb.net) / int (c#)                  |
| HWND             | IntPtr                                               |
|                  |                                                      |
| INT              | Int32 / Integer (vb.net) / int (c#)                  |
| INT_PTR          | IntPtr                                               |
| INT8             | SByte                                                |
| INT16            | Int16 / Short   (vb.net) / short (c#)                |
| INT32            | Int32 / Integer (vb.net) / int   (c#)                |
| INT64            | Int64 / Long    (vb.net) / long  (c#)                |
| LONG             | Int32 / Integer (vb.net) / int   (c#)                |
| LONGLONG         | Int64 / Long    (vb.net) / long  (c#)                |
| LONG_PTR         | IntPtr                                               |
| LPARAM           | IntPtr                                               |
|                  |                                                      |
| LPCSTR           | String (Specify CharSet.Ansi in DllImport)           |
| LPCTSTR          | String                                               |
| LPCWSTR          | String (Specify CharSet.Unicode in DllImport)        |
| LPDWORD          | (= DWORD,  declared as ByRef (vb.net) / ref (c#))    |
| LPHANDLE         | (= HANDLE, declared as ByRef (vb.net) / ref (c#))    |
| LPINT            | IntPtr                                               |
| LPLONG           | IntPtr                                               |
|                  |                                                      |
| LPSTR            | StringBuilder (Specify CharSet.Ansi in DllImport)    |
| LPTSTR           | StringBuilder                                        |
| LPVOID           | IntPtr                                               |
| LPWORD           | (= WORD, declared as ByRef (vb.net) / ref (c#))      |
| LPWSTR           | StringBuilder (Specify CharSet.Unicode in DllImport) |
|                  |                                                      |
| QWORD            | UInt64 / ULong (vb.net) / ulong (c#)                 |
| SHORT            | Int16  / Short (vb.net) / short (c#)                 |
| SIZE_T           | UIntPtr                                              |
| UCHAR            | Byte  / Byte    (vb.net) / byte (c#)                 |
| UINT             | Int32 / Integer (vb.net) / int  (c#)                 |
| UINT_PTR         | IntPtr                                               |
| UINT8            | Byte   / Byte     (vb.net) / byte   (c#)             |
| UINT16           | UInt16 / UShort   (vb.net) / ushort (c#)             |
| UINT32           | UInt32 / UInteger (vb.net) / int    (c#)             |
| UINT64           | UInt64 / ULong    (vb.net) / long   (c#)             |
| ULONG            | UInt32 / UInteger (vb.net) / int    (c#)             |
| ULONGLONG        | UInt64 / ULong    (vb.net) / long   (c#)             |
| ULONG_PTR        | UIntPtr                                              |
| USHORT           | UInt16 / UShort   (vb.net) / ushort (c#)             |
| WORD             | UInt16 / UShort   (vb.net) / ushort (c#)             |
+------------------+------------------------------------------------------+
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!