我正在将最初用vb6编写的应用程序转换为vb.net.这个应用程序的一个功能是它将一个“Type”对象发送到一个dll.我尝试将类型转换为结构并调用dll但它似乎不起作用.我被困了一个星期,任
这是类型的vb6代码
'Define WICS Communications Control Block (CCB). Type WicsCCBType ' Create user-defined type. CCBNum As String * 1 CCBVer As String * 1 Resp1 As String * 4 Resp2 As String * 4 PLUA As String * 8 LLUA As String * 8 Mode As String * 8 ReqMax As String * 5 ResMax As String * 5 End Type
以下是dll的调用方式
Private Declare Sub WICSRASP Lib "wicsrasp.dll" (MyWicsCCB As WicsCCBType) WICSRASP MyWicsCCB
这是我尝试使用vb.net,但它无法正常工作
'Define WICS Communications Control Block (CCB). <System.Runtime.InteropServices.StructLayoutAttribute( _ System.Runtime.InteropServices.LayoutKind.Sequential, _ CharSet:=System.Runtime.InteropServices.CharSet.[Unicode])> _ Structure WicsCCBType ' Create user-defined type. <MarshalAs(UnmanagedType.ByValTStr, sizeconst:=1)> Dim CCBNum As String <MarshalAs(UnmanagedType.ByValTStr, sizeconst:=1)> Dim CCBVer As String <MarshalAs(UnmanagedType.ByValTStr, sizeconst:=4)> Dim Resp1 As String <MarshalAs(UnmanagedType.ByValTStr, sizeconst:=4)> Dim Resp2 As String <MarshalAs(UnmanagedType.ByValTStr, sizeconst:=8)> Dim PLUA As String <MarshalAs(UnmanagedType.ByValTStr, sizeconst:=8)> Dim LLUA As String <MarshalAs(UnmanagedType.ByValTStr, sizeconst:=8)> Dim Mode As String <MarshalAs(UnmanagedType.ByValTStr, sizeconst:=5)> Dim ReqMax As String <MarshalAs(UnmanagedType.ByValTStr, sizeconst:=5)> Dim ResMax As String End Structure
这是我试图称之为的地方
<System.Runtime.InteropServices.DllImportAttribute("C:\windows\system32\wicsrasp.dll")> _ Public Shared Sub WICSRASP( ByRef CCB As WicsCCBType, ByRef Request As DAWicsRequestType, ByRef Response As DAWicsResponseType) End Sub Dim CCB As New modWICSDiary.WicsCCBType() CCB.CCBNum = "B" CCB.CCBVer = "2" CCB.LLUA = " " CCB.Mode = "CICSMO2 " CCB.ReqMax = "2100 " CCB.ResMax = "2100 " CCB.Resp1 = "0 " CCB.Resp2 = "0 " CCB.PLUA = "WICSPLU " NativeMethods.WICSRASP(CCB)
对于值,相同的值适用于vb6类型
再次感谢你
我不知道OP是否已经解决了这个问题,但这是我的看法.VB6中UDT内的固定长度字符串不作为指针编组,而是内联到结构中.此外,VB6在编组之前将Unicode转换为Ansi. VB6使用4字节对齐.
该类型将使用填充在内存中查看此内容,每个连续字段在图中命名为A到I,并且_由于对齐而是填充字节.
CCBNum As String * 1 | |+-CCBVer As String * 1 || || Resp1 As String * 4 || | || | Resp2 As String * 4 || | | || | | PLUA As String * 8 || | | | || | | | LLUA As String * 8 || | | | | || | | | | Mode As String * 8 || | | | | | || | | | | | ReqMax As String * 5 || | | | | | | || | | | | | | ResMax As String * 5 || | | | | | | | || | | | | | | | VV V V V V V V V AB__CCCCDDDDEEEEEEEEFFFFFFFFGGGGGGGGHHHHH___IIIII___
因此CharSet应该是Ansi,StructAlignment应该是4.使用ByValTString很好,就像使用SizeConst一样.