Convert vb6 type to VB.net or C # Struct

I am converting an application written in vb6 to vb.net. One of the things this application does is that it sends a Type object to the DLL. I tried converting the type to a structure and p / call the dll, but it does not seem to work. Ive stuck for a week any help would be greatly appreciated

here is vb6 code for type

'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 

and this is what the dll is called

 Private Declare Sub WICSRASP Lib "wicsrasp.dll" (MyWicsCCB As WicsCCBType) WICSRASP MyWicsCCB 

this is what i tried with vb.net but it doesn't work

 '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 

and this is where I tried to call him

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

As for the values, the same values ​​work for the vb6 type

thanks again in advance

+4
source share
3 answers

I don't know if this was an OP solution yet, but here I take it on myself.

Fixed-length strings inside UDTs in VB6 are not sorted as pointers, but embedded in the structure. In addition, VB6 converts Unicode to Ansi before sorting. And VB6 uses 4-byte alignment.

The type will look like this in memory with filling, and each subsequent field will be indicated from A to Z in the illustration, and _ is the byte of the element due to alignment.

 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 VVVVVVV AB__CCCCDDDDEEEEEEEEFFFFFFFFGGGGGGGGHHHHH___IIIII___ 

Therefore, CharSet must be Ansi, StructAlignment must be 4. Using ByValTString is fine, as is using SizeConst.

+1
source

VB6 will marshal all of these string elements as ANSI strings. Change the Vb.Net marshalling code accordingly.

  • Try UnmanagedType.LPStr in these MarshalAs attributes?
  • Try changing CharSet from Unicode to Ansi?
  • Try Pack=4 in StructLayoutAttribute ?

Useful link explaining assumptions made by VB6 Declare

+1
source

VB6 used BSTR, right? Don't you marshal the BSTR?

0
source

Source: https://habr.com/ru/post/1415406/


All Articles