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

前端 未结 1 1554
天涯浪人
天涯浪人 2020-12-06 22:24

I was loading in some old VB functions from VBA, and mostly got everything updated to VB.NET, but there are function declarations for functions in \"wininet.dll\" which don\

相关标签:
1条回答
  • 2020-12-06 22:59

    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             | UInt32 / UInteger (vb.net) / uint  (c#)                 |
    | UINT_PTR         | IntPtr                                               |
    | UINT8            | Byte   / Byte     (vb.net) / byte   (c#)             |
    | UINT16           | UInt16 / UShort   (vb.net) / ushort (c#)             |
    | UINT32           | UInt32 / UInteger (vb.net) / uint    (c#)             |
    | UINT64           | UInt64 / ULong    (vb.net) / ulong   (c#)             |
    | ULONG            | UInt32 / UInteger (vb.net) / uint    (c#)             |
    | ULONGLONG        | UInt64 / ULong    (vb.net) / ulong   (c#)             |
    | ULONG_PTR        | UIntPtr                                              |
    | USHORT           | UInt16 / UShort   (vb.net) / ushort (c#)             |
    | WORD             | UInt16 / UShort   (vb.net) / ushort (c#)             |
    +------------------+------------------------------------------------------+
    
    0 讨论(0)
提交回复
热议问题