简体   繁体   English

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

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

I have created a C++ exported function this way: 我以这种方式创建了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)

On the other hand, I have a VB.NET program that uses that C++ function. 另一方面,我有一个使用该C ++函数的VB.NET程序。 I have declared it this way: 我已经这样声明了:

<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

The calling works and I can debug both the VB.NET program and the C++ function. 调用工作正常,我可以调试VB.NET程序和C ++函数。

When debugging, I realized that the strings are not correctly passed. 调试时,我意识到未正确传递字符串。 They are actually passed as garbage. 它们实际上是作为垃圾传递的。 For example, the first parameter: 例如,第一个参数:

C ++导出功能

The actual call is: 实际的调用是:

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

As you see, first parameter is sTelefono and it does contain a value to be passed. 如您所见,第一个参数是sTelefono,它确实包含要传递的值。

Any help to solve this problem will be appreciated 解决此问题的任何帮助将不胜感激

You must remove the ByRef declaration in your VB.NET code, and just pass string parameters like this: 您必须在VB.NET代码中删除ByRef声明,并仅传递如下字符串参数:

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

Passing ByRef can be useful for inout-output parameters, ie the callee can modify the parameter and these modifications are reflected to the caller, which is not the case here. 传递ByRef可能对inout输出参数有用,即,被调用方可以修改参数,并且这些修改将反映给调用方,在此情况并非如此。 (Moreover, such parameters tend to require an additional level of pointer indirection in C/C++.) (此外,在C / C ++中,此类参数往往需要更高级别的指针间接寻址。)

Note also that the conversion from .NET Unicode strings to "ANSI" strings can be lossy; 还请注意,从.NET Unicode字符串到“ ANSI”字符串的转换可能是有损的。 you may want to use const wchar_t* in C++ for Unicode UTF-16 strings, and <MarshalAs(UnmanagedType.LPWStr)> in VB.NET. 您可能要在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