0

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.

  1. header:

    #define EXPORT extern "C" __declspec(dllexport)
    typedef intptr_t ItemListHandle;
    
  2. 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.

soojcho
  • 1
  • 1
  • 1
    "I tracked this with Debug.WriteLine()" - future hint; Just use a debugger. – Jesper Juhl Sep 10 '18 at 19:28
  • All variables are passed on the stack between c# and c++. When you return from the c++ code all items are lost except under following conditions 1) The memory for the object are allocated in the c# code before calling the c++ 2) In c++ the memory is allocated using windows allocation so the memory is accessible from c# 3) The value(s) are the return value from c++. The return value is palced in the microporcessor AX register so it it not on the stack. – jdweng Sep 10 '18 at 19:53
  • To debug these kind of problems the best option is not to forget to check "Enable native code debugging" in project properties under debug in your C# projet. Then enable breakpoints on all exceptions using the Debug Tab. – PilouPili Sep 10 '18 at 20:15
  • hmm i don't have that option that shows up in this instruction that i believe you're referring to @NPE https://learn.microsoft.com/en-us/visualstudio/debugger/how-to-debug-in-mixed-mode?view=vs-2017 – soojcho Sep 10 '18 at 20:30
  • The link is gives the right steps. These options are only available in the C# project not C++. If you don't have that option try to go to Debug - Exceptions (Ctrl+D,E) and choose to break on Thrown or User-unhandled exceptions. – PilouPili Sep 10 '18 at 20:39
  • right, i'm right clicking on the C# project to get to the debugger tab but the option is not available in UWP apps https://learn.microsoft.com/en-us/visualstudio/debugger/start-a-debugging-session-for-a-store-app-in-visual-studio-vb-csharp-cpp-and-xaml?view=vs-2017. Not sure if there are equivalent options for UWP. i did check off the options on Debugger - Exception Settings. – soojcho Sep 10 '18 at 20:54
  • We don't recommend to use P/Invoke in UWP. Your UWP app can not upload to Store if you do that. Do you have any special requirement that must use DllImport? In general, wrapping function in a native Windows Runtime Component is easier than p-invoke. – Breeze Liu - MSFT Sep 11 '18 at 08:58
  • great thank you @BreezeLiu-MSFT. can you provide a link that i can refer to as reference? – soojcho Sep 11 '18 at 19:14
  • great thank you @BreezeLiu-MSFT. can you provide a link/instruction that i can refer to as reference with example codes would be great. i'll be wrapping my c++ function with winrt component to accesss it in c# code all in a uwp solution. – soojcho Sep 11 '18 at 19:29
  • You can see the official document here:https://learn.microsoft.com/en-us/windows/uwp/winrt-components/walkthrough-creating-a-basic-windows-runtime-component-in-cpp-and-calling-it-from-javascript-or-csharp – Breeze Liu - MSFT Sep 12 '18 at 01:53
  • so would i be essentially adding another project (c++/winrt component template) into my solution to package the cpp method there and then call it from csharp project? – soojcho Sep 17 '18 at 18:18
  • Yes and that is what the document introduced. – Breeze Liu - MSFT Sep 18 '18 at 02:45

0 Answers0