Can You Use C++ Dlls in C# Code in a Uwp

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.

How do I put a C program in a C# UWP?

Please check the following steps:

  1. Create a C# UWP project in a new solution.
  2. Add a C++ DLL(Universal Windows) project(named MyDll1) in the same solution.
  3. Add your C code in the C++ DLL project. For example:
//MyDll1.h
#pragma once

extern "C" _declspec(dllexport) int Sum(int a, int b);

//MyDll1.cpp
#include "pch.h"
#include "MyDll1.h"
int Sum(int a, int b)
{
return a + b;
}

  1. Add a Windows Runtime Component(C++/WinRT) project in the same project.
  2. Right-click on the name of the Windows Runtime Component(C++/WinRT) project, and select option Add > Reference, check your DLL project in Projects tab. Click OK.
  3. There is an auto-generated class Class, you could use the class or add other class(Add > New Item > Code > Midl File(.idl)) as needed. The new class must be generated from a midl file. You could get more information about authoring api referring to the document.
  4. Take Class class as an example. Include the header file of dll in Windows Runtime Component(C++/WinRT) project.
//Class.h
#include "..\MyDll1\MyDll1.h"

  1. There is a sample method named MyProperty shown in Class class. The MyProperty method is added to the Class.idl file and the complier will generate the corresponding methods in Class.h and Class.cpp after you build the project. And you need to go to the locations \RuntimeComponent\ RuntimeComponent \Generated Files\sources\Class.h and Class.cpp in File Explorer and open the .h and .cpp file to copy the generated methods into your code in Visual Studio. You could use MyProperty method to pass values to C# project or add other methods in classes. Refer to the document for more information about how to add new method in idl file.
  2. You could call the Sum(int a, int b) of MyDll1 project in MyProperty method.
int32_t Class::MyProperty()
{
int t = Sum(1, 2);
return t;
}

  1. Right-click on the name of the C# UWP project, and select option Add > Reference, check your Windows Runtime Component(C++/WinRT) project in Projects. Click OK.
  2. Add include statement in C# UWP project.
using RuntimeComponent;   // RuntimeComponent is the name of Windows Runtime Component(C++/WinRT) project.

  1. You could call the MyProperty method in C# UWP project.
RuntimeComponent.Class myClass = new Class();
var value = myClass.MyProperty;

Distributing C# Class Library for UWP

Is there a way to distribute my dlls in all-in-one structure? (like aar)

You may refer .NetStandard library, and mix them in one .NET Standard library. And if you want this library could run in the multiple platform, you need to refer Xamarin Forms class library structure, for more detail please refer this blog.

Using a UWP C++ dll in Win32 C# WPF application

Although WPF applications can use system-provided WinRT types (those in the Windows namespace), they cannot use custom WinRT types because Windows doesn't know how to locate them. The solution is to use the Desktop Bridge to add your WPF application to an AppX package, at which point Windows will be able to locate and activate your types.

(Note: you do not have to submit your app to the Store to use the Desktop Bridge; it's just a mechanism to package your app).

Call GetDiskFreeSpaceExA from UWP (C#)

You could add the broadFileSystemAccess capability in Package.appxmanifest.
This is a restricted capability. Access is configurable in Settings > Privacy > File system.

Please refer to the following code.

Package.appxmanifest:

<?xml version="1.0" encoding="utf-8"?>
<Package
xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"

xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces="uap mp rescap">
……
<Capabilities>
<rescap:Capability Name="broadFileSystemAccess" />
<Capability Name="internetClient" />
</Capabilities>

Code behind:

public async void test()
{

const String k_freeSpace = "System.FreeSpace";
const String k_totalSpace = "System.Capacity";

try
{

StorageFolder folder = await StorageFolder.GetFolderFromPathAsync("C:\\");
var props = await folder.Properties.RetrievePropertiesAsync(new string[] { k_freeSpace, k_totalSpace });
Debug.WriteLine("FreeSpace: " + (UInt64)props[k_freeSpace]);
Debug.WriteLine("Capacity: " + (UInt64)props[k_totalSpace]);
}
catch (Exception ex)
{
Debug.WriteLine(String.Format("Couldn't get info for drive C."));
}

}

Difference Between: Native DLL, UWP DLLs and C++/CLI DLLs

Both of them are native code. C++/CX shares some syntax with C++/CLI, but it has no ties to .NET. A Windows Runtime Component is a re-usable component that can be called from other applications in any supported language (C++, C#, VB, JavaScript). The other project is just a stand-alone app.

Note that the CLR dependencies are only present in Debug builds (due to some XAML debug dependencies); in Release builds there are no CLR dependencies.



Related Topics



Leave a reply



Submit