如何将字符串从VB.NET传递给const char *到C ++编程的DLL [英] How to pass a String to const char * from VB.NET to a DLL programmed in C++

查看:279
本文介绍了如何将字符串从VB.NET传递给const char *到C ++编程的DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have created a C++ exported function this way:

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程序.我已经这样声明了:

On the other hand, I have a VB.NET program that uses that C++ function. 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

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

The calling works and I can debug both the VB.NET program and the C++ function.

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

When debugging, I realized that the strings are not correctly passed. They are actually passed as garbage. For example, the first parameter:

实际电话是:

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,它确实包含要传递的值.

As you see, first parameter is sTelefono and it does contain a value to be passed.

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

Any help to solve this problem will be appreciated

推荐答案

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

You must remove the ByRef declaration in your VB.NET code, and just pass string parameters like this:

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

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

Passing ByRef can be useful for inout-output parameters, i.e. the callee can modify the parameter and these modifications are reflected to the caller, which is not the case here. (Moreover, such parameters tend to require an additional level of pointer indirection in C/C++.)

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

Note also that the conversion from .NET Unicode strings to "ANSI" strings can be lossy; you may want to use const wchar_t* in C++ for Unicode UTF-16 strings, and <MarshalAs(UnmanagedType.LPWStr)> in VB.NET.

这篇关于如何将字符串从VB.NET传递给const char *到C ++编程的DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆