且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

可以在同一项目中使用C ++ / CX和C ++ / WinRT吗?

更新时间:2022-12-07 19:20:05

关于 C ++ / WinRT是否可以使用在同一项目中使用C ++ / CX实现的类型?的问题。

Regarding the question "Can C++/WinRT consume types implemented with C++/CX in the same project?"

答案是是和否。 ref类在同一项目中定义,因为这样的项目必须在启用C ++ / CX的情况下进行编译,因此您的代码可以像使用任何ref类一样简单地使用该类。

The answer is Yes and No. With a 'ref class' defined in the same project, as such a project must be compiled with C++/CX enabled, your code can simply use the class as it can any ref class.

但是,如果您想将'ref class'用作C ++ / Win

However, if you want to consume the 'ref class' as a C++/WinRT projection, the answer is effectively no.

为了获得C ++ / WinRT投影类定义,您需要在元数据上运行cppwinrt.exe编译器以获取RT投影。 'ref class'。那将需要以某种方式获取元数据。您可以建立某种机制来编译 ref class一次,获取winmd,通过mdmerge对其进行处理以将其放入规范形式,在元数据上运行cppwinrt.exe以获得预计的类定义,然后包括生成的标头。

In order to get the C++/WinRT projected class definition, you need to run the cppwinrt.exe compiler over the metadata for the 'ref class'. That would require somehow getting the metadata. You could rig up some mechanism to compile the 'ref class' once, get the winmd, process it through mdmerge to put it in canonical form, run cppwinrt.exe on the metadata to get the projected class definition, then include the generated headers.

或者,您可以编写IDL来描述 ref类,使用MIDLRT将其编译为元数据,然后运行cppwinrt.exe。也不是实际的IMO。

Alternatively, you can write the IDL to describe the 'ref class', compile it to metadata using MIDLRT, then run cppwinrt.exe. Neither is practical IMO.

最合理的选择是像使用C ++ / CX类型一样直接使用ref类,因为定义在同一解决方案中。下一个最实用的解决方案是将类放在一个单独的项目中,对其进行编译以获取winmd,然后从winmd创建标头。这种方法还允许使用 ref类(通过投影)的单独项目无需使用C ++ / CX代码即可构建。

The most reasonable alternative is simply use the ref class as is as a C++/CX type, as the definition is in the same solution. Next most practical solution is put the class in a separate project, compile it getting the winmd, then creating headers from the winmd. This approach also allow the separate project that consumes the 'ref class' (via the projection) to build without use of C++/CX code.

要完全透明,请注意我们的初始版本(现已在 https://github.com/Microsoft/cppwinrt 上提供)不包括cppwinrt.exe编译器本身。相反,它包含C ++ / WinRT头文件,其中包含Windows 10周年更新SDK中定义的所有Windows运行时类型/ API的投影-其中包括通用平台API和所有扩展SDK API。

To be completely transparent, note that our initial release (now available at https://github.com/Microsoft/cppwinrt) does not include the cppwinrt.exe compiler itself. Rather, it contains the C++/WinRT header files containing projections for all Windows Runtime types/APIs defined in the Windows 10 Anniversary Update SDK -- this includes the Universal Platform APIs and all Extension SDK APIs.