【发布时间】:2013-01-17 04:13:03
【问题描述】:
在this question 被告知我首选的解决方案是不可能的后,我现在正在尝试实施一种解决方法。我没有在 C++/CX 中声明从 IClosable 继承的接口,而是在原始 IDL 中声明它。但这似乎也不起作用。
我创建了一个 IDL 文件 FooSpace.idl,其中包含
import "inspectable.idl";
import "Windows.Foundation.idl";
namespace FooSpace
{
[uuid(01234567-89AB-CDEF-FEDC-BA9876543210)]
[version(42)]
interface Foo : IInspectable requires Windows.Foundation.IClosable
{
}
}
并从中生成 Windows 运行时元数据
midlrt /nomidl /metadata_dir "C:\Program Files (x86)\Windows Kits\8.0\References\CommonConfiguration\Neutral" FooSpace.idl
当我用ildasm 反汇编生成的FooSpace.winmd 时,它看起来还不错——尤其是Foo 似乎继承自IClosable,就像IInputStream 在系统提供的Windows.winmd 中所做的一样.
但是,当我尝试从 C++/CX 中使用它时——甚至没有实现它,只是暂时假装其他人已经用 WRL 或其他方式实现了它——它似乎不起作用。这是我的测试 C++/CX 源文件:
void works(Windows::Storage::Streams::IInputStream^ istream) {
Platform::IDisposable^ local = istream ;
}
void doesnt(FooSpace::Foo^ foo) {
Platform::IDisposable^ local = foo ;
}
这会为Foo 产生错误,但不会为IInputStream 产生错误:
C:\cygwin\tmp>cl /nologo /c /ZW /FU FooSpace.winmd testit.cpp
testit.cpp
testit.cpp(5) : error C2440: 'initializing' : cannot convert from 'FooSpace::Foo ^' to 'Platform::IDisposable ^'
No user-defined-conversion operator available, or
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
我在这里做错了什么?
另一方面,等效的 C# 代码似乎编译得很好:
public class Whatever {
public static void Works(Windows.Storage.Streams.IInputStream istream) {
System.IDisposable local = istream ;
}
public static void AlsoWorks(FooSpace.Foo foo) {
System.IDisposable local = foo ;
}
}
【问题讨论】: