如何在Win32桌面应用程序中使用自定义的WinRT库? [英] How to use a custom WinRT library in a Win32 desktop app?

查看:239
本文介绍了如何在Win32桌面应用程序中使用自定义的WinRT库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的基于Win32的桌面应用程序中有一个WinRT类(C ++ / CX参考类)。它访问WinRT API并且工作正常。我使用本指南获取它加工。现在我试图将这个类放在桌面应用程序可以使用的库中。我有这么多的麻烦。这是我在Visual Studio 2013中所做的:


  1. 通过选择已安装>模板> Visual C ++> Store Apps创建一个新项目> Windows应用程序> DLL(Windows)

  2. 将这个新的DLL项目添加到包含我的桌面应用程序的解决方案中。

  3. 添加了一个引用到DLL项目

  4. 在配置属性> C / C ++>常规>其他#using目录我添加了.winmd文件由DLL构建的目录项目。

  5. 在桌面应用程序的.cpp文件中,我添加了:



  #using< MyLib.winmd> 
#using< Windows.winmd>
#using< Platform.winmd>

[MTAThread] //初始化WinRT运行时
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine,
_In_ int nCmdShow){
MyWinRTClass ^ myObject = ref new MyWinRTClass();
}

Intellisense工作,我写了代码从库中实例化类。桌面应用程序构建,但运行时我得到:


MyDesktopApp.exe中的0x76494598的第一次异常:Microsoft C ++异常:平台:: ClassNotRegisteredException ^在内存位置0x00A8F99C。


发生了什么事?这是正确的方法吗?

解决方案

这是不可能的。为了使应用程序使用定制的WinRT组件,组件需要首先注册。对于打包(AppX)应用程序,可以通过将一些信息添加到包的AppXManifest.xml中来完成。然而,对于常规的Win32 Desktop应用程序,现在有这样的注册方式。您可以直接从操作系统提供的WinRT API,但您无法写入自己的自定义WinRT API以供其他应用程序使用。


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?

解决方案

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.

这篇关于如何在Win32桌面应用程序中使用自定义的WinRT库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆