question

GUI-Kenny avatar image
0 Votes"
GUI-Kenny asked GUI-Kenny commented

WHAT IS THE PROPER WAY TO CODE A CLASS LIBRARY, IN VISUAL STUDIO 2022 PREVIEW (or 2019), THAT NEEDS TO USE C++ (to access DWriteCore) FROM MANAGED CODE (C#) ?

WHAT IS THE PROPER WAY TO CODE A CLASS LIBRARY, IN VISUAL STUDIO 2022 PREVIEW (or 2019), THAT NEEDS TO USE C++ (to access DWriteCore) FROM MANAGED CODE (C#) ?

Is DWriteCore just for CPP apps?

Will a Managed Code implementation of the interface be coming in the future?

I need to get font information, and this interface has the functions and controls, and I'm using WinUI 3 Desktop, Windows App SDK (WindowsAppSDK - Experimental) , Visual Studio 2022 Preview.

I just need to know, since my app needs access to system font information (collections) - Am I - forced to go entirely CPP (looks to be), partial CPP - partial C#, or can I go entirely C# (preferred)?

dotnet-csharpdotnet-runtimedotnet-standardwindows-app-sdk
· 12
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

As I wrote in another thread, "entirely C#" is done actually with P/Invoke
P/Invoke is the usual way to declare C++ interfaces when they are not defined in Assemblies, which is the case for DWriteCore atm
I tested in C# with IDWriteFactory.GetSystemFontCollection and I get the same result as in C++

0 Votes 0 ·

This question is different, as it would be nice to know how to write C++ functions that can be consumed by C#, other than DWriteCore access. Windows 11 will show us a lot about where Microsoft is headed, so we can code accordingly.

I am going to try and implement the P/Invoke strategy in the next few days. I will comment on how it turns out and possibly add it as an answer to the other question.

I do want to thank you for your assistance.

0 Votes 0 ·

I can post a sample with P/Invoke, but it depends on which interfaces you want to use
I declared most of them from C++ headers, but not yet the last ones like IDWriteFactory7
I simply added the fonts from IDWriteFontCollection in a Listbox for the test :

137696-windowsappsdk-fonts.jpg


1 Vote 1 ·
Show more comments

I generated a C# projection from a C++/WinRT component to distribute as a NuGet package for .NET 5+ applications and need to use it in a WinUI 3 Desktop App.

PROJECT_A is a C++ Windows Component (WinUI 3)
PROJECT_B is a C# Class Library (WinUI 3 in Desktop)
PROJECT_C is the APP (C# WinUI 3 in Desktop) that needs access to PROJECT_A or PROJECT_B (to access DWriteCore in WindowsAppSDK) ???????

I have added, as projects, PROJECT_A and PROJECT_B (as an existing item, referenced them by drilling back-then-down to their respective project file). I then added:
to PROJECT_C.csproj file.

I had to add the following to the .csproj so Visual Studio 2022 Preview could locate the "PROJECT_A.0.1.0-prerelease.nupkg" file:

"
<PropertyGroup>
<RestoreSources>
https://api.nuget.org/v3/index.json;
../../../PROJECT_B/nuget
</RestoreSources>
</PropertyGroup>
"

I can see PROJECT_A in NuGet Package Manager and is installed for the project in question. I can see it as one of the Packages in Dependencies. But, neither #using PROJECT_A or #using PROJECT_B work, I get the following:

Error CS0246 The type or namespace name 'PROJECT_A' could not be found (are you missing a using directive or an assembly reference?) Character Map C:\Source\Code\Windows\10\WinUI 3\Fonts\PROJECT_C\PROJECT_C\MainWindow.xaml.cs

Also added PROJECT_B's project as a reference, no luck - #using did not work.

0 Votes 0 ·

PreviewSource
So, I referenced PROJECT_A's project (#using worked) but get the following :

Error NETSDK1130 PROJECT_A.winmd cannot be referenced. Referencing a Windows Metadata component directly when targeting .NET 5 or higher is not supported. For more information, see https://aka.ms/netsdk1130 Character Map C:\Program Files\dotnet\sdk\6.0.100-rc.1.21463.6\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.targets

Reference: https://docs.microsoft.com/en-us/windows/uwp/csharp-winrt/net-projection-from-cppwinrt-component

What to TRY NEXT ???

0 Votes 0 ·

I found the DWriteCore implementation in Microsoft.ProjectReunion.DWrite.

0 Votes 0 ·
GUI-Kenny avatar image GUI-Kenny XiaopoYang-MSFT ·

I know where the implementation is, I just need to know how to use it from Managed Code.

0 Votes 0 ·

I have just used the Answer from https://docs.microsoft.com/en-us/answers/questions/574926/how-do-i-access-dwritecore-from-a-c-managed-applic.html?childToView=579442#comment-579442 to implement a P/Invoke strategy to access DWriteCore directly from managed code.

But, I still need to know how to accomplish what this question is asking - How to have a class library written in C++ and have a C# managed application consume it.

0 Votes 0 ·

0 Answers