繁体   English   中英

如何将字符串从VB.NET传递给const char *到C ++编程的DLL

[英]How to pass a String to const char * from VB.NET to a DLL programmed in C++

我以这种方式创建了C ++导出函数:

extern "C" __declspec(dllexport) 
    int __stdcall AlmacenarPedidoLipigas(const char * pszTelefono, const char * pszFechaPedido, const char * pszHoraPedido, const char * pszCodigoInterno, const char * pszDescripcionProducto,
                                         const char * pszCantidadProducto, const char * pszValorUnitario, const char * pszFechaEntrega, const char * pszHoraEntrega, const char * pszKilosProducto, 
                                         const char * pszFechaDespacho, const char * pszHoraDespacho)

另一方面,我有一个使用该C ++函数的VB.NET程序。 我已经这样声明了:

<DllImport("ComTest.dll", CharSet:=CharSet.Ansi)>
Function AlmacenarPedidoLipigas(<MarshalAs(UnmanagedType.LPStr)> ByRef pszTelefono As String,
                                <MarshalAs(UnmanagedType.LPStr)> ByRef pszFechaPedido As String, <MarshalAs(UnmanagedType.LPStr)> ByRef pszHoraPedido As String,
                                <MarshalAs(UnmanagedType.LPStr)> ByRef pszCodigoInterno As String, <MarshalAs(UnmanagedType.LPStr)> ByRef pszDescripcionProducto As String,
                                <MarshalAs(UnmanagedType.LPStr)> ByRef pszCantidadProducto As String, <MarshalAs(UnmanagedType.LPStr)> ByRef pszValorUnitario As String,
                                <MarshalAs(UnmanagedType.LPStr)> ByRef pszFechaEntrega As String, <MarshalAs(UnmanagedType.LPStr)> ByRef pszHoraEntrega As String,
                                <MarshalAs(UnmanagedType.LPStr)> ByRef pszKilosProducto As String,
                                <MarshalAs(UnmanagedType.LPStr)> ByRef pszFechaDespacho As String, <MarshalAs(UnmanagedType.LPStr)> ByRef pszHoraDespacho As String) As UInt16

调用工作正常,我可以调试VB.NET程序和C ++函数。

调试时,我意识到未正确传递字符串。 它们实际上是作为垃圾传递的。 例如,第一个参数:

C ++导出功能

实际的调用是:

Sub Main()
    Dim sTelefono As String = "229188562"
    Dim sFechaPedido As String = "16/12/2016"
    Dim sHoraPedido As String = "20:30"
    Dim sCodigoInterno As String = "123456"
    Dim sDescripcionProducto As String = "CARGA CODIGAS CATALITICO 15 KILOS"
    Dim sCantidadProducto As String = "2"
    Dim sValorUnitario As String = "14000"
    Dim sFechaEntrega As String = "19/12/2016"
    Dim sHoraEntrega As String = "15:14"
    Dim sKilosProducto As String = "15"
    Dim sFechaDespacho As String = "19/12/2016"
    Dim sHoraDespacho As String = "10:00"
    Dim iPedido As Integer = AlmacenarPedidoLipigas(sTelefono, sFechaPedido, sHoraPedido, sCodigoInterno, sDescripcionProducto, sCantidadProducto, sValorUnitario, sFechaEntrega, sHoraEntrega, sKilosProducto, sFechaDespacho, sHoraDespacho)
    Console.WriteLine(iPedido)
End Sub

如您所见,第一个参数是sTelefono,它确实包含要传递的值。

解决此问题的任何帮助将不胜感激

您必须在VB.NET代码中删除ByRef声明,并仅传递如下字符串参数:

Function AlmacenarPedidoLipigas(<MarshalAs(UnmanagedType.LPStr)> telefono As String,
...

传递ByRef可能对inout输出参数有用,即,被调用方可以修改参数,并且这些修改将反映给调用方,在此情况并非如此。 (此外,在C / C ++中,此类参数往往需要更高级别的指针间接寻址。)

还请注意,从.NET Unicode字符串到“ ANSI”字符串的转换可能是有损的。 您可能要在C ++中将const wchar_t*用于Unicode UTF-16字符串,并在VB.NET中使用<MarshalAs(UnmanagedType.LPWStr)>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM