简体   繁体   中英

Can I write a DLL with C++/WinRT to be consumed by a windows desktop app(not UWP) or as a Unity plugin?

I've experimented writing a simple DLL with C++/WinRT. It can be imported and run without any problems by a Windows desktop app (I've tried writing two desktop apps, one in C# and another in C++/WinRT.)

When I import it as a plugin for Unity, it runs fine, but causes Unity to freeze on exit. I've then narrowed it down to some possible threading issue. Specifically, if the plugin calls back Unity on a thread the plugin spawns, the call goes through, and the code runs fine, but when I try to exit Unity editor, it freezes.

Here's a short sample code for the DLL. #include "stdafx.h"

#include <winrt\base.h>

extern "C"
{
    typedef void(*testCallback)(Info info);

    __declspec(dllexport) void TryAsyncWinRT(testCallback callback);
}

using namespace winrt;
using namespace Windows::Foundation;

static testCallback handler;

IAsyncAction JustDoWork()
{
    co_await winrt::resume_background();

    int result{};

    /*
    Some calculation...
    */

    if (handler) {
        handler(result);
    }

    co_return;
}

void TryAsyncWinRT(testCallback callback)
{
    handler = callback;
    JustDoWork();
}

The compiler I used is Visual Studio 2017, with C++/WinRT extension installed. I did not change anything in the "DllMain" supplied by Visual Studio. I did not call "init_apartment()" in the DLL code as I suppose that's done by the host app, as each app can have only one multi-threaded apartment.

If I don't callback into Unity, there's no problem. If I just use the standard library's thread utilities, Unity works fine too.

void JustDoWork()
{
    int result{};

    /*
    Some calculation...
    */

    if (handler) {
        handler(result);
    }
}

void TryAsync(testCallback callback)
{
    handler = callback;
    std::thread(JustDoWork).detach();
}

I feel that I must be missing something, but I couldn't find any documentation on how to write a regular DLL (not a Windows Runtime Component) using C++/WinRT.

Could any kind soul shed some light? Thanks a lot!

when trying to implement BLE I stumbled across the same problem I guess. Specifically my C# program crashed when I tried to call a C# callback inside of the C++ winrt dll, and often it crashed when I used C++ winrt allocated memory inside the C# program even though I tried to follow the memory allocation advices.

I ended up avoiding marshalled callbacks completely and implement the mechanisms via poll-functions. Also I allocated all memory needed for function calls on the C# side.

For me that feels like a workaround but it also feels pretty safe. You can see an example of how I converted a callback to polling here .

您必须在 exe 主线程(UI 线程)中释放 C++/WinRT DLL 的所有资源。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM