How to use a custom WinRT library in a Win32 desktop app?

心不动则不痛 提交于 2019-12-01 06:33:50

问题


I have a WinRT class (C++/CX ref class) in my Win32-based desktop app. It accesses WinRT APIs and works just fine. I used this guide to get it working. Now I'm trying to put this class in a library that the desktop app can use. I'm having quite a bit of trouble with this. Here's what I did in Visual Studio 2013:

  1. Created a new project by selecting Installed > Templates> Visual C++ > Store Apps > Windows Apps > DLL (Windows).
  2. Added this new DLL project to the solution containing my desktop app.
  3. Added a reference to the DLL project
  4. In Configuration Properties > C/C++ > General > Additional #using Directories I added the directory where the .winmd file gets built by the DLL project.
  5. In a .cpp file in the desktop app, I added:
#using <MyLib.winmd>
#using <Windows.winmd>
#using <Platform.winmd>

[MTAThread] // initializes WinRT runtime
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPTSTR    lpCmdLine,
                     _In_ int       nCmdShow) {
    MyWinRTClass^ myObject = ref new MyWinRTClass();
}

Intellisense works and I wrote code to instantiate the class from the library. The desktop app builds, but when it runs I get:

First-chance exception at 0x76494598 in MyDesktopApp.exe: Microsoft C++ exception: Platform::ClassNotRegisteredException ^ at memory location 0x00A8F99C.

What's going on? Is this the right approach?


回答1:


Update as of 7/26/2019: Please see comments from "Adam Braden - MSFT" in the below thread.

This is not possible. In order for an app to use a custom WinRT component, the component needs to be "registered" first. For packaged (AppX) apps this is done by adding some information into the AppXManifest.xml of the package. However for regular Win32 Desktop apps, there is now way for such registration. You can call WinRT API directly provided by the OS but you cannot write to your own custom WinRT API to be consumed by other apps.



来源:https://stackoverflow.com/questions/29761911/how-to-use-a-custom-winrt-library-in-a-win32-desktop-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!