VB.NET和VB6互操作,字符串未正确终止

VB.NET和VB6互操作,字符串未正确终止,vb.net,vb6,pinvoke,Vb.net,Vb6,Pinvoke,我需要将数据从VB6应用程序传递到托管的VB.NET程序集DLL。为此,我成功地使用了RGiesecke中的DLLExport,并使用适当的封送,我可以传递我想要的任何类型的字符串、数字等 现在,为了方便起见,我想使用struct,而不是传递单个参数。这是我的代码: NET端 Imports RGiesecke.DllExport Imports System.Runtime.InteropServices Public Module myLib <System.Runtime.I

我需要将数据从VB6应用程序传递到托管的VB.NET程序集DLL。为此,我成功地使用了RGiesecke中的DLLExport,并使用适当的封送,我可以传递我想要的任何类型的字符串、数字等

现在,为了方便起见,我想使用struct,而不是传递单个参数。这是我的代码:

NET端

Imports RGiesecke.DllExport
Imports System.Runtime.InteropServices
Public Module myLib
    <System.Runtime.InteropServices.StructLayoutAttribute(
        System.Runtime.InteropServices.LayoutKind.Sequential,
        Pack:=4,
        CharSet:=System.Runtime.InteropServices.CharSet.Ansi)>
    Structure sStruct
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=128)> Dim nome As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=128)> Dim cognome As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=32)> Dim codfis As String
        <MarshalAs(UnmanagedType.I2)> Dim eta As Short
    End Structure

    <DllExport()>
    Public Sub getStruct(<MarshalAs(UnmanagedType.Struct)> ByRef p As sStruct)
        p.nome = "NOME"
        p.cognome = "COGNOME"
        p.codfis = "CODFIS"
        p.eta = 33
    End Sub
End Module
我正确地获取了值,但是字符串没有正确终止,因为我没有在msgbox中获取最终的引号。我认为字符串正在以null结尾,VB6不喜欢这样


感谢您的帮助

Gieseke的工具非常难以有效使用。微小的错误会变成很难调试的问题。比如把MarshalAs属性搞错了。如果有更好的选择,那就没有意义了。目前,VB.NET已经具备了与VB6进行互操作的能力。使用该属性。在VB6端,必须找到空值并调整Strings的大小,例如TrimToNull,从此处开始:相关:和。
Private Type sStruct
    nome As String * 128
    cognome As String * 128
    codfis As String * 32
    eta As Integer
End Type

Private Declare Sub getPatient Lib "myLib.dll" (ByRef p As sStruct)
Private Sub Command1_Click()
    Dim pp As sStruct
    getPatient pp
    MsgBox """" & pp.nome & """"
End Sub