C# class libraries DLL to C++ application with UWP

RW 21 Reputation points
2022-10-04T18:54:00.64+00:00

Hi,

I'm currently searching for a solution that would allow me to use my C#-UWP-based DLL (inside containing two classes) and using the methods contained within in another UWP-based C++ application. After an exhaustive search, I'm seeing the only possible method is to expose the DLL as a COM-type object(?) and reference it in my C++ project, however, many of the examples are based on .NET approach.

I haven't been successful in finding any clear example on how to do this. And based on what I've read, it also appears this may not be possible because its UWP-based(?). I would appreciate if someone might be able to provide an explanation or direction to where or how I could tackle this problem? Thank you.

Universal Windows Platform (UWP)
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,527 questions
{count} votes

Accepted answer
  1. Roy Li - MSFT 31,766 Reputation points Microsoft Vendor
    2022-10-07T06:38:25.493+00:00

    Hello,

    Welcome to Microsoft Q&A!

    You couldn't directly use a C# class library in your C++ application. You need to use a C# Windows Runtime Component instead. And you will need to do some extra steps to make sure the C# component is compiled correctly with the C++ application.

    Here are the steps to create a simple test sample:

    1. Create a new "Blank App (Universal Windows - C++/CX)"
    2. Create a new "Windows Runtime Component (Universal Windows)", C#, Called TestWinRTLib in the solution.
    3. Add a simple test method in the Class1.cs:
       public static string GetName()  
              {  
                  return "David";  
              }  
      
    4. Add referece of the TestWinRTLib in the C++ project. And call the method like this:
      using namespace TestWinRTLib;  
      
      void TestCXProJECT::MainPage::Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)  
      {  
      	auto str = Class1::GetName();  
      }  
      
    5. Right click on the C++/CX project, choose Manage NuGet Packages.
    6. Search for Microsoft.Net.Native.Compiler, and install it
    7. Right-click the C++/CX project -> Unload Project -> Edit .vcxproj, add following properties to the PropertyGroup. Remember to replace the 2.2.3 version above with the version of Microsoft.Net.Native.Compiler nuget package you installed
      <PropertyGroup Label="Globals">  
           <-- other properties -->  
      
          <UseDotNetNativeToolchain>true</UseDotNetNativeToolchain>  
          <DotNetNativeVersion>2.2.3</DotNetNativeVersion>  
       </PropertyGroup>  
      
    8. Reload the C++/CX project first, then right click the Solution, choose Restore NuGet Packages
    9. Run the C++ app and click the button, you could get string from the C# Component.

    The result looks like this:

    248368-image.png

    You could refer these this as well: Could not load file or assembly.

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful