gpt4 book ai didi

c++ - 如何将 String 传递给 const char * 从 VB.NET 到用 C++ 编程的 DLL

转载 作者:太空宇宙 更新时间:2023-11-04 13:08:09 25 4
gpt4 key购买 nike

我用这种方式创建了一个 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++ exported function

实际调用是:

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

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

最佳答案

您必须删除 ByRef在 VB.NET 代码中声明,然后像这样传递字符串参数:

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

传递 ByRef对输入输出参数很有用,即被调用者可以修改参数并且这些修改会反射(reflect)给调用者,这里不是这种情况。 (此外,此类参数在 C/C++ 中往往需要额外的指针间接级别。)

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

关于c++ - 如何将 String 传递给 const char * 从 VB.NET 到用 C++ 编程的 DLL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41232992/

25 4 0
文章推荐: javascript - 如果

增长,则让

Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com