gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:45:22 25 4
gpt4 key购买 nike

我从 VBA 加载了一些旧的 VB 函数,并且大部分内容都更新到了 VB.NET,但是“wininet.dll”中的函数声明似乎不正确匹配。

我遇到的错误是:

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

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

我查了一下,我可以通过删除检查来解决,但它会出现“堆栈不平衡”,并最终随着这些调用的进行而随着时间的推移耗尽所有堆栈。此外,这些被称为“非托管”dll,并且一些声明已放在 PInvoke 函数 wiki 上 http://pinvoke.net 。这个 wiki 确实有一些我正在使用的函数调用,但不是全部。其中一些我不得不猜测一些事情,但没有成功。

我从旧代码复制的大部分更改都是从 long 更改为 int32integer,还有一些更改IntPtr,旧代码中从未使用过。我认为这会使所有整数大小正确(即 16/32/64 位),这可能是大多数问题所在。不过,有一种情况是从 long 更改为 string(),这看起来有点奇怪,并且无法正常编译。

那么,我如何实际查找 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

最佳答案

正如已经noted by Hans Passant您应该使用托管替代方案。

但是要回答您的实际问题:您基本上只需要查看 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#) |
+------------------+------------------------------------------------------+

关于vb.net - 如何找出非托管 dll 的正确参数结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46539039/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com