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

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.

Leave a Comment