The code is building without error but when I run it, it gets stuck in the function that gets called from DllImport in csharp. I tracked this with Debug.WriteLine() to confirm where the code gets stuck.
The DllImport line in the csharp project:
[DllImport("GettingData.dll", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
static unsafe extern bool GenerateItems(out ItemsSafeHandle itemsHandle,
out double* items, out int itemCount);
[DllImport("GettingData.dll", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
static unsafe extern bool ReleaseItems(IntPtr itemsHandle);
The dllexport code from cpp project is as follow. Note that the codes use header file and cpp because of a build error, where the functions being defined in the cpp project were being "already" defined in App.xaml.h. So to fix this, the class is defined in the header file and the member function definitions are in the cpp file.
header:
#define EXPORT extern "C" __declspec(dllexport) typedef intptr_t ItemListHandle;
cpp:
EXPORT bool GenerateItems(ItemListHandle* hItems, double** itemsFound, int* itemCount) { auto items = new std::vector<double>(); for (int i = 0; i < 500; i++) { items->push_back((double)i); } *hItems = reinterpret_cast<ItemListHandle>(items); *itemsFound = items->data(); *itemCount = items->size(); return true; } EXPORT bool ReleaseItems(ItemListHandle hItems) { auto items = reinterpret_cast<std::vector<double>*>(hItems); delete items; return true; }
I am following the top-voted response to this post here to get the minimum required code working. As I am running this in a UWP solution and couldn't use C++/CLI wrapper as another project as part of this solution.