我有 COM 组件 ( dll ) ThirdParty 定义了接口和一些 COM 类 ThirdPartyObjectClass 实现了这个接口。我有适当的文件 ThirdParty.h 和 ThirdParty_i.c 允许在 C++ 中编译它。
IThirdParty { HRESULT ComFoo(); }
我使用“tlbimp /sysarray”构建了名为 ThirdPartyInterop.dll 的互操作 dll,它公开了 .Net 接口 ThirdPartyObject
我编写了新的 C# 组件,它引用了 ThirdPartyInterop.dll
using ThirdPartyInterop; namespace CsComponent { public class CsClass { public void NetFoo( ThirdPartyObject thirdPrty ) { thirdPrty.ComFoo(); } } }
ThirdPartyClass 的元数据是:
using System.Runtime.InteropServices;
namespace ThirdPartyInterop
{
[CoClass(typeof(ThirdPartyObjectClass))]
[Guid("xxxx")]
public interface ThirdPartyObject : IThirdParty
{
}
}
和
using System;
using System.Runtime.InteropServices;
namespace ThirdPartyInterop
{
[TypeLibType(4160)]
[Guid("yyyy")]
public interface IThirdParty
{
[DispId(1)]
void ComFoo();
}
}
我有一个用托管 C++ 编写的旧代码。
with the following:
#include "stdafx.h"
#pragma managed(push, off)
#include "ThirdParty_i.c"
#include "ThirdParty.h"
#pragma managed(pop)
void CppFoo( IThirdParty* thirdParty )
{
...
thirdParty -> ComFoo();
...
}
我需要更改它以使用我的 CsClass:
#include "stdafx.h"
#pragma managed(push, off)
#include "ThirdParty_i.c"
#include "ThirdParty.h"
#pragma managed(pop)
void CppFoo( IThirdParty* thirdParty )
{
...
//thirdParty -> ComFoo();
CsComponent::CsClass^ csClass = gcnew CsComponent::CsClass();
csClass.NetFoo( thirdParty );
...
}
但这无法编译:错误 C2664: 'CsComponent::CsClass::NetFoo' : 无法将参数 1 从 'IThirdParty *' 转换为 'ThirdPartyInterop::ThirdPartyObject ^'
以下实现是可以的:
#include "stdafx.h"
#pragma managed(push, off)
#include "ThirdParty_i.c"
#include "ThirdParty.h"
#pragma managed(pop)
void CppFoo( IThirdParty* thirdParty )
{
...
//thirdParty -> ComFoo();
CsComponent::CsClass^ csClass = gcnew CsComponent::CsClass();
ThirdPartyInterop::ThirdPartyObject^ _thirdParty = gcnew ThirdPartyInterop::ThirdPartyObject();
//csClass.NetFoo( thirdParty );
csClass.NetFoo( _thirdParty );
...
}
但我需要使用 CppFoo 的参数 thirdParty。
我的问题是:
如何从给定的 IThirdParty* 创建 ThirdPartyInterop::ThirdPartyObject ?