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)?
.NET
Windows App SDK
C#
.NET Runtime
-
Castorix31 82,751 Reputation points
2021-10-05T08:33:41.693+00:00 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++ -
Kenny Brown 51 Reputation points
2021-10-05T08:34:36.183+00:00 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.
-
Kenny Brown 51 Reputation points
2021-10-05T08:36:49.34+00:00 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
What to TRY NEXT ???
-
Kenny Brown 51 Reputation points
2021-10-05T08:57:00.597+00:00 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.
-
Xiaopo Yang - MSFT 12,151 Reputation points • Microsoft Vendor
2021-10-05T09:04:34.707+00:00 I found the
DWriteCore
implementation inMicrosoft.ProjectReunion.DWrite
. -
Kenny Brown 51 Reputation points
2021-10-05T09:06:57.59+00:00 I know where the implementation is, I just need to know how to use it from Managed Code.
-
Castorix31 82,751 Reputation points
2021-10-05T11:36:58.323+00:00 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 : -
Kenny Brown 51 Reputation points
2021-10-05T11:55:55.69+00:00 Just this past hour I have started working on implementing. First problem, of course, is that the interface is defined in a C++ header.
I would love a sample, if you have the time. Would have expected 'collections' to be a thing, but appears not.
I prefer to go the DWriteCore route, for cross platform and such. I will set up my development environment to use the appropriate x32 - x64 version of the DLL from the Microsoft.WindowAppSDK.DWrite NuGet package.
-
Kenny Brown 51 Reputation points
2021-10-05T12:01:03.437+00:00 DWriteCore is supposed to be all about consolidating the factories (e.g. IDWriteFactory3 thru IDWriteFactory7).
-
Kenny Brown 51 Reputation points
2021-10-05T12:06:48.503+00:00 Also, we should probably be doing this in the other question-thread ( https://video2.skills-academy.com/en-us/answers/questions/574926/how-do-i-access-dwritecore-from-a-c-managed-applic.html ), as this wouldn't be the 'answer' to the current document's question. Let me know if I need to go delete an accepted answer or something to make it live again, I'm new to this site.
-
Castorix31 82,751 Reputation points
2021-10-05T19:40:36.33+00:00 OK, I will post my test in C# in the other thread then ...
(I just finished the declarations for IDWriteFactory7 and all other interfaces, it was longer than I thought...
-
Kenny Brown 51 Reputation points
2021-10-06T10:40:10.33+00:00 I have just used the Answer from https://video2.skills-academy.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.
Sign in to comment