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):
Add win32 dll into your UWP project making sure to set its type as 'content'
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:
- Create a C# UWP project in a new solution.
- Add a C++ DLL(Universal Windows) project(named
MyDll1
) in the same solution. - 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;
}
- Add a Windows Runtime Component(C++/WinRT) project in the same project.
- 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.
- 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. - Take
Class
class as an example. Include the header file of dll inWindows Runtime Component(C++/WinRT)
project.
//Class.h
#include "..\MyDll1\MyDll1.h"
- There is a sample method named
MyProperty
shown inClass
class. TheMyProperty
method is added to theClass.idl
file and the complier will generate the corresponding methods inClass.h
andClass.cpp
after you build the project. And you need to go to the locations\RuntimeComponent\ RuntimeComponent \Generated Files\sources\Class.h
andClass.cpp
in File Explorer and open the .h and .cpp file to copy the generated methods into your code in Visual Studio. You could useMyProperty
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. - You could call the
Sum(int a, int b)
ofMyDll1
project inMyProperty
method.
int32_t Class::MyProperty()
{
int t = Sum(1, 2);
return t;
}
- 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.
- Add include statement in C# UWP project.
using RuntimeComponent; // RuntimeComponent is the name of Windows Runtime Component(C++/WinRT) project.
- 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
How to Render Encoded Tags as Proper HTML, Rather Than Text
Check If a String Is a Palindrome
How to Dllexport a C++ Class for Use in a C# Application
Auto Create Database Tables from Objects, Entity Framework
Embedding Flash Player in a C++ or Java Application
How to Programmatically Apply a CSS Class to an ASP.NET Control
How to Configure ASP.NET Kestrel for Low Latency
Parsing HTML to Get Script Variable Value
Linq: How to Exclude Condition If Parameter Is Null
Monodevelop Failure "Unknown Msbuild Failure" on Linux
JSON Serialize List<Keyvaluepair<String, Object>>
Use or Clause in Queryover in Nhibernate
Should I Use Return/Continue Statement Instead of If-Else
Onchange Event for HTML.Dropdownlist