Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>extern "C" __declspec(dllexport) LPSTR Get_GetDescription(HANDLE) </code></pre> <p>Returning a pointer like this is rather dangerous, as it is not clear who owns the memory and thus who should take responsibility for freeing it.</p> <p>It would be safer to create the buffer in your VB code and pass it into the DLL where the value can be memcpy'ed in. So we could rewrite the C++ side like:</p> <pre><code>extern "C" __declspec(dllexport) void Get_GetDescription(HANDLE, LPSTR) void Get_GetDescription(HANDLE resultBreakDown, LPSTR buffer){ memcpy(buffer, ((CalcBreakDown*)resultBreakDown)-&gt;GetDescription().c_str(), ((CalcBreakDown*)resultBreakDown)-&gt;GetDescription().length()+1); } </code></pre> <p>And then redo the VB code as follows:</p> <pre><code>&lt;DllImport("FeeEngineDll.dll", CharSet:=CharSet.Ansi, CallingConvention:=CallingConvention.Cdecl)&gt; _ Public Shared Sub Get_GetDescription(ByVal resultBreakDown As IntPtr, &lt;MarshalAs(UnmanagedType.LPStr)&gt; ByVal szFilename As StringBuilder) End Sub </code></pre> <p>I have added <code>CharSet:=CharSet.Ansi</code> to the DllImport. Your C++ code is not using unicode characters, whereas VB probably will be, so best specify that, you probably don't need to put it in but I like to make these things explicit.</p> <p>Note the use of <code>StringBuilder</code> instead of <code>String</code> as strings are immutable in VB. Finally, you will need to be careful to allocate enough space in your string builder for the description:</p> <pre><code>Dim buffer As StringBuilder = New StringBuilder(512) </code></pre> <p>You can either do this by using a large number in your VB code, as I have just done. This will however, cause problems if your C++ code ever copies more characters than you allocated.</p> <p>Other better options would either be to pass in the buffer size to the C++ code so that it knows how much it is allowed to write, or to have a get size function in the C++ code that can be used to determine how much space should be allocated for the buffer.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload