Using C++/WinRT dll in WPF with elevated rights application

7

I have got WPF c# server application (WCF) and want to use c++/WinRT dynamic library inside it. I can simply do this by using UWP desktop bridge with WPF app and UWP empty app that includes my dll. This works fine for applications without elevated rights but WCF service require to run it as admin. I can achieve this by starting my application as admin or adding tags in projects manifest files (requireAdministrator for WPF and allowElevation for UWP).

My problem starts here as I get exception System.TypeLoadException with inner exception System.Runtime.InteropServices.COMException with code 0x80040154 (REGDB_E_CLASSNOTREG)) from System.StubHelpers.StubHelpers.GetWinRTFactoryObject(IntPtr pCPCMD) if I run app as admin.

This behavior is similar to what I get when I use c++/WinRT library without desktop bridge project. Is there any solution for such problem?

I compiled all projects against Windows 10 1809 (17763) build.

Edit:

I do not care about desktop bridge but it was only way that I could find to connect c# with c++/WinRT. What I want is to use classes exported through MSIDL to c# application.

Instruction to recreate this issue in VS 2017 (requirement is to have Windows 10 1809 or later):
1. To do so first install C++/WinRT projects templates from nuget.
2. Next create new project of Windows Runtime Component (C++/WinRT), chose target and minimum version of windows to 1809. You should get project with single class Class.
3. Now in same solution create C# WPF App project (Framework 4.7.2). In it add reference to runtime component project and set Copy Local to False for this reference. Use Class inside WPF project (ex. create field inside MainWindow class RuntimeComponent1.Class c = new RuntimeComponent1.Class();)
At this point both projects should be compilable but WPF project will throw an exception (TypeLoadException may be inner exception or root exception) at runtime. Also this step is what I want to achieved and all next steps are redundant for me but are necessary to create working application.
4. Next create Windows Universal Blank App (set platform versions to 1809) and as previous with WPF add reference to runtime component project and set Copy Local to False.
5. After that create Windows Application Packaging Project (target and minimum platform versions set to 1809), inside it to applications add both UWP and WPF Apps (it should be not possible to add library project). Set WPF app as Entry Point.
6. At this point Packaging project should be compilable and runnable without an error. Also it should be possible to run Packaging application as administrator from menu start but it will produce a runtime exception (TypeLoadException).
7. Finally to make application run by default with elevated rights add to WPF project Application Manifest File and change requestedExecutionLevel tag to <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />. Also inside Packaging project in Package.appxmanifest add <rescap:Capability Name="allowElevation" /> after <rescap:Capability Name="runFullTrust" />. This step require to build against 1809 as a minimum version.

After thous steps application will throw TypeLoadException (as root or inner exception depends how Class is used). To actually show it throwing something instead of just dying change WPF project type to console and then it will print exception for a brief moment before dying.

c#
c++
c++-cx
desktop-bridge
c++-winrt
asked on Stack Overflow Dec 17, 2018 by Logman • edited Jan 7, 2019 by Logman

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0