0

I'm trying to embed a WinUI control in a legacy C++ MFC application using Xaml Islands. I have followed the Host a custom WinRT XAML control in a C++ desktop (Win32) app as a guide. My MFC application compiles up to the moment I follow the "Host the custom WinRT XAML control in the desktop project" section of the tutorial. I get the following error when I reference IDesktopWindowXamlSourceNative and compile my application.

// Line with C2139 error
auto interop = _desktopWindowXamlSource.as<IDesktopWindowXamlSourceNative>();

Build Error:

**\Generated Files\winrt\base.h(1981,53):error C2139: IDesktopWindowXamlSourceNative: an undefined class is not allowed as an argument to compiler intrinsic type trait __is_base_of

I've successfully completed the tutorial example, but when I applied the steps to our legacy MFC application it is not compiling. I'm not sure what could be the problem.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • 2
    Did you forget to `#include `? – IInspectable Nov 10 '21 at 07:28
  • @IInspectable Thanks for the rely. I have the `#include `in my pre-compiled header file of the MFC application. Also I've tried adding the #include to my view.h header file and still get the same error. My visual C++ is rusty, but I suspect that `IDesktopWindowXamlSourceNative` is not defined by the time the base.h is being evaluated. – Hasan Williams Nov 10 '21 at 20:25
  • Try the [`/showIncludes`](https://learn.microsoft.com/en-us/cpp/build/reference/showincludes-list-include-files) compiler option to find out what the compiler actually sees. – IInspectable Nov 10 '21 at 21:11
  • I've added the `/showIncludes` compiler option and the header file for `IDesktopWindowXamlSourceNative` was in the list. As I worked through the tree of includes the error kept moving up the `/showIncludes` output tree, as I tried to isolate the header causing the issue. I decided to take another approach, I recreated the legacy MFC project from scratch, first by bringing in the xaml island and then our application code. I was able to get it working. So now I plan analyze the code diff for clues of the cause of this problem. I bet it is something simple. Thanks again. – Hasan Williams Nov 19 '21 at 20:24

2 Answers2

0

It happened for me when I tried to use XAML control in existing MFC DLL in my project.

And that existing MFC DLL is Extension DLL and it is dependent on bunch of other MFC DLL's and few are just "Regular" dll with no call AfxInitExtensionModule or "new CDynLinkLibrary(dll_name)".

So to resolve the problem I created new "MFC extension DLL" in my project. and then added reference to WinUI MyApp Project with below setting to reolve a link error

..\bin\Debug\$(AppProjectName)\$(AppProjectName).winmd true
0

I encountered the same error. In my case the cause was the value of NTDDI_VERSION macro which was NTDDI_VISTA.

Solution I found was to create a header with only WinRT includes and at the start of this header I do:

#define NTDDI_VERSION NTDDI_WIN10_19H1

and at the end of file I restore NTDDI_VERSION to the initial value.

Dorel Pîslan
  • 106
  • 1
  • 1
  • 4