title: How to pass a String to const char * from VB.NET to a DLL programmed in C++
body: <p>I have created a C++ exported function this way:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>On the other hand, I have a VB.NET program that uses that C++ function. I have declared it this way:</p>
  
  <pre><code>&lt;DllImport("ComTest.dll", CharSet:=CharSet.Ansi)&gt;
  Function AlmacenarPedidoLipigas(&lt;MarshalAs(UnmanagedType.LPStr)&gt; ByRef pszTelefono As String,
                                  &lt;MarshalAs(UnmanagedType.LPStr)&gt; ByRef pszFechaPedido As String, &lt;MarshalAs(UnmanagedType.LPStr)&gt; ByRef pszHoraPedido As String,
                                  &lt;MarshalAs(UnmanagedType.LPStr)&gt; ByRef pszCodigoInterno As String, &lt;MarshalAs(UnmanagedType.LPStr)&gt; ByRef pszDescripcionProducto As String,
                                  &lt;MarshalAs(UnmanagedType.LPStr)&gt; ByRef pszCantidadProducto As String, &lt;MarshalAs(UnmanagedType.LPStr)&gt; ByRef pszValorUnitario As String,
                                  &lt;MarshalAs(UnmanagedType.LPStr)&gt; ByRef pszFechaEntrega As String, &lt;MarshalAs(UnmanagedType.LPStr)&gt; ByRef pszHoraEntrega As String,
                                  &lt;MarshalAs(UnmanagedType.LPStr)&gt; ByRef pszKilosProducto As String,
                                  &lt;MarshalAs(UnmanagedType.LPStr)&gt; ByRef pszFechaDespacho As String, &lt;MarshalAs(UnmanagedType.LPStr)&gt; ByRef pszHoraDespacho As String) As UInt16
  </code></pre>
  
  <p>The calling works and I can debug both the VB.NET program and the C++ function.</p>
  
  <p>When debugging, I realized that the strings are not correctly passed. They are actually passed as garbage. For example, the first parameter:</p>
  
  <p><a href="https://i.stack.imgur.com/R6DVm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/R6DVm.png" alt="C++ exported function"></a></p>
  
  <p>The actual call is:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>As you see, first parameter is sTelefono and it does contain a value to be passed.</p>
  
  <p>Any help to solve this problem will be appreciated</p>