我正在将一些VB6代码迁移到VB.net, 代码包含一个包含1d数组,2d数组和其他少数变量的结构。
Vb.net结构的大纲如下
Public Structure Test
Dim a As Single
Dim b As Single
Dim c As Single
<VBFixedArray(2)> Dim arr1() As Single
<VBFixedArray(2, 2)> Dim mdarr1(,) As Single
<VBFixedArray(4)> Dim arr2() As Byte
<VBFixedArray(4)> Dim arr3() As Short
<VBFixedArray(3, 2)> Dim mdarr2(,) As Integer
Dim d As Integer
Dim e As Decimal
End Structure
对dll的调用声明为
Public Declare Sub getState Lib "val.dll" (ByRef state As Test)
Elsewhere在这个网站上我意识到我们必须“编组”结构,以使其与即将接受它的非托管代码兼容。
但是在运行代码时我仍然收到运行时错误,我对如何使用System.Runtime.InteropServices.Marshal
类没有任何线索。
将此结构传递给dll的正确方法是什么?
修改
原始VB6数据类型是
Public Type Test
a As Single
b As Single
c As Single
arr1(0 To 2) As Single
mdarr1(0 To 2, 0 To 2) As Single
arr2(0 To 4) As Byte
arr3(0 To 4) As Integer
mdarr2(0 To 3, 0 To 2) As Long
d As Long
e As Currency
End Type
答案 0 :(得分:1)
您是否在getState
中拥有val.dll
的源代码?如果它是用C或C ++编写的,并且您有源代码甚至只有标题,那么您可以使用P/Invoke Assistant自动生成VB.Net代码。
或者......(并且请发布原始的VB6结构!)
getState
之前分配数组,例如state.arr1 = {0.0, 0.0}
等。Decimal
变量e
可能会导致您遇到问题。在VB6中,这可能是Currency
变量,Decimal
并不是我记忆中的完全等价。有一种方法可以告诉VB.Net像货币那样编组它。也许像这样添加attribute ...... 示例代码:
Imports System.Runtime
Public Structure Test
''blah blah
<InteropServices.MarshalAs(InteropServices.UnmanagedType.Currency)> _
Dim e As Decimal
''blah blah