Can you use C++ DLLs in C# code in a UWP?

前端 未结 1 1200
天命终不由人
天命终不由人 2020-12-11 04:31

I wrote a C++ Class Library in Visual Studio that just defines a function that invokes some Python:

#pragma once

#include 

extern \"C\"
__d         


        
相关标签:
1条回答
  • 2020-12-11 04:46

    Firstly, UWP can't consume a legacy C++ dll just by DLLImport.

    If you want to expose legacy c++ functions to C#, the first suggestion is to wrap that C++ logic using a WinRT component. Then you can reference this component in UWP application by following steps: adding it to the project, open the files' properties in the Solution Explorer window, and mark them as content to be included in the app package. This post would be helpful. This one provides more detailed steps.

    If you want to PInvoke the dll, you can follow these steps (You can refer to this MSDN post):

    1. Add win32 dll into your UWP project making sure to set its type as 'content'

    2. Then in the proper cs file, using DllImport to PInvoke the dll.

    There is one more thing: You need to make sure your Python dll is not using prohibited APIs in WinRT. You can check this by using /ZW compile option for the dll.

    0 讨论(0)
提交回复
热议问题