Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This was a little big to add as a comment to tenfour's response...</p> <p>If you want to still maintain a C++ API when using the DLL wrapper, you can put C++ to C conversion functions in the header file. This ensures that only C compatible data types ever cross the DLL boundary.</p> <p>As an example</p> <pre><code>//MyDLL.h class MyDLL { public: ... int Add2ToValues(std::vector&lt;int&gt;&amp; someValues) { int* cValues = new int[someValues.size()]; memcpy(cValues, &amp;someValues[0], someValues.size() * sizeof(int)); int retVal = Add2ToValues_Internal(cValues, someValues.size()); someValues.assign(std::begin(cValues), std::end(cValues)); delete [] cValues; return retVal; } private: int Add2ToValues_Internal(int* valuesOut, const int numValues); }; //MyDLL.cpp int MyDLL::Add2ToValues_Internal(int* values, const int numValues) { for(int i = 0; i &lt; numValues; ++i) { values[i] += 2; } return 0; } </code></pre> <p>One catch I ran into when doing these wrappers is that you must allocate and deallocate any memory within the header file. Since the header file will be compiled by the application that is using your library, it will use the CRT for whatever compiler you are using to build your application. All interaction with the DLL uses C, so you won't run into any runtime mismatches and all memory is allocated and freed either entirely within the DLL or entirely within the application so you don't have any cross DLL memory management issues either. In the example, I both allocated and deallocated in the header. If you need to allocate data in the _Internal function, you'll need to also add a function that allows you to free that memory within the DLL. Once inside of the _Internal functions, you are free to use as much C++ as you want.</p>
 

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