我正在使用 COM 在 C++ 和 C# 之间进行通信。我的问题是我已经实现了包含参数作为接口(interface)对象的方法,并且该接口(interface)由 C++ 类实现,而 C++ 类又由另一个 C++ 类实现。 下面提到了我在 C# 中的代码场景:
namespace Example
{
public class First:ITest
{
void GetObjectOfC#Interface(ISample sample);
}
}
namespace Example
{
Interface ISample
{
void Test();
}
}
Class First 以及 ITest 接口(interface)都是 COmVisible ,通过 Com 暴露给 C++。 ISample 接口(interface)也在 COM 中公开,但未由 C# 类 First 实现。
C++ 中的代码场景:
Class SampleFirst: public Example::ISample
{
};
这个 SampleFirst 类再次被另一个 C++ 类头文件继承,如下所示:
Class SampleSecond:public SampleFirst
{
public:
void MakeAnObject();
private:
Example::ITestPtr _testPtr;
Example::ISamplePtr _samplePtr;
};
并且在 SampleSecond.Cpp 文件中,
void SampleSecond::MakeAnObject()
{
_testPtr.CreateInstance(__uuidof(Example::First));
samplePtr=this;
_testPtr->GetObjectOfC#Interface(samplePtr);
}
这意味着通过考虑继承功能将同一类对象分配给 samplePtr。 但在 .tli 文件中,HResult 失败。请让我知道是否有相同的解决方案。
最佳答案
不需要静态转换,只需通过简单的转换即 QueryInterface 中的 (ISample)this
就足够了
关于C++继承的C#接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20538410/