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)
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
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:
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.
Any help to solve this problem will be appreciated