How to involve C++/WinRT lib in C# UWP App on Visual Studio 2022

Damon Pan 86 Reputation points
2023-03-27T11:25:33.5+00:00

I have a pre-built C++/WinRT lib (dll/lib/winmd...), I want to use it in my C# UWP App。

  • I want to query how to involve the ilb in my C# UWP project. (where to add reference of lib/winmd files in VS2022).
  • Since C++/WinRT is self language projection, so I can use it in C# code directly, am I right?

I try as "Generate a C# projection from a C++/WinRT component" mentioned, it works for me, but the C++/WinRT in this guide is "source code project" and the target app is ".NET 6 console application".

Please note: my requirement is involve a pre-built C++/WinRT lib in to C# UWP app. I find options are diff with the articles, and I don't know how to config correctly.

Thanks

Universal Windows Platform (UWP)
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,278 questions
0 comments No comments
{count} votes

Accepted answer
  1. Roy Li - MSFT 32,051 Reputation points Microsoft Vendor
    2023-03-28T03:16:40.55+00:00

    Hello,

    Welcome to Microsoft Q&A!

    Currently, it seems that it is not able to directly add a C++/WinRT component as a reference to a C# UWP app directly via Visual Studio. But you could do this manually by modifying the csproj file and the manifest file. You could go through this Github link: Unable to add C++/WinRT Runtime Component to UWP C# Application . In that link, Code-ScottLe tried to modify the csproj file and Scottj1s gives suggestions about how to make it work.

    In short, you need to modify the c# project and adding

         <ItemGroup>
        <ProjectReference Include="..\projectname\project.vcxproj" />
        </ItemGroup>
    

    And then change the c# manifest:

        <Extensions>
        <Extension Category="windows.activatableClass.inProcessServer">
          <InProcessServer>
            <Path>projectname.dll</Path>
            <ActivatableClass ActivatableClassId="projectname.classname" ThreadingModel="both"/>
          </InProcessServer>
        </Extension>
      </Extensions>  
    

    For more details, please go through the link: Unable to add C++/WinRT Runtime Component to UWP C# Application

    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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful