如何找出非托管 dll 的正确参数结构?

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

我从 VBA 中加载了一些旧的 VB 函数,并且大部分都更新为 VB.NET,但是 "wininet.dll" 中的函数有函数声明这似乎没有正确匹配。

我得到的错误是:

Exception Thrown: Managed Debugging Assistant, 'PInvokeStackImbalance':...

总而言之,声明参数的长度需要明确,并且与 dll 中的实际函数不匹配。

我查了一下,我可以通过删除支票来获得,但它会有一个 "stack imbalance" 并最终在进行这些调用时耗尽所有堆栈。此外,这些被称为 "unmanaged" dll,一些声明已在 http://pinvoke.net 的 PInvoke 函数 wiki 上发布。这个 wiki 确实有一些我正在使用的函数调用,但不是全部。其中一些我不得不猜测一些事情,但没有成功。

我从旧代码复制的大部分更改是从 long 更改为 int32integer,还有一些更改为 IntPtr,其中从未在旧代码中使用过。我假设这使所有整数大小都正确(即 16/32/64 位),这可能是大多数问题所在。不过,有一个案例是从 longstring() 的更改,这看起来有点奇怪并且没有编译好。

那么,我如何实际查找 dll 中的函数长度并匹配 API?

我查了一些东西,但是当我尝试将对 c:\windows\system32\wininet.dll 的引用添加到我在 Visual Studio 中的 VB.NET 项目时,它说我可以'不要添加它。这似乎使我无法在 Visual Studio 中使用对象或程序集浏览器。它似乎不是 COM 对象。有什么帮助吗?

作为参考,这里是失败的旧代码:

Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" ( _
    ByVal Agent As String, ByVal AccessType As Long, ByVal ProxyName As String, _
    ByVal ProxyBypass As String, ByVal Flags As Long) As Long

Private Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" ( _
    ByVal hInternetSession As Long, ByVal ServerName As String, ByVal ServerPort As Integer, ByVal UserName As String, _
    ByVal Password As String, ByVal Service As Long, ByVal Flags As Long, ByVal Context As Long) As Long

Private Declare Function InternetCloseHandle Lib "wininet.dll" ( _
    ByVal hInet As Long) As Boolean

Private Declare Function InternetReadFile Lib "wininet.dll" ( _
    ByVal hConnect As Long, ByVal Buffer As String, ByVal NumberOfBytesToRead As Long, _
    NumberOfBytesRead As Long) As Boolean

Private Declare Function HttpOpenRequest Lib "wininet.dll" Alias "HttpOpenRequestA" ( _
    ByVal hHttpSession As Long, ByVal Verb As String, ByVal ObjectName As String, ByVal Version As String, _
    ByVal Referer As String, ByVal AcceptTypes As Long, ByVal Flags As Long, Context As Long) As Long

Private Declare Function HttpSendRequest Lib "wininet.dll" Alias "HttpSendRequestA" ( _
    ByVal hHttpRequest As Long, ByVal Headers As String, ByVal HeadersLength As Long, _
    ByVal sOptional As String, ByVal OptionalLength As Long) As Boolean

一样,您应该改用托管替代方案。

但是要回答您的实际问题:您基本上只需要查看 MSDN 文档文章 Windows Data Types,并根据类型的声明确定相应的 .NET 类型。

例如,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;

在这种情况下,我们可以通过范围 (0 - 4294967295) 或定义 (unsigned long) 来确定这应该是一个无符号的 32 位整数 (UInt32UInteger)。在 C/C++ 中,longint 相同,这就是为什么它映射到整数而不是 Long/ULong

以下是最常见类型的摘要:

(非常感谢 David Heffernan 帮助我更正了一些字符串声明!)

+------------------+------------------------------------------------------+
|   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#)             |
+------------------+------------------------------------------------------+