[英]error C2664 - Need help and clarification
我有COM组件(dll)ThirdParty定义了接口,并且有一些COM类ThirdPartyObjectClass实现了此接口。 我有相应的文件ThirdParty.h和ThirdParty_i.c允许在C ++中对其进行编译。
IThirdParty { HRESULT ComFoo(); }
我使用“ tlbimp / sysarray”构建了一个名为ThirdPartyInterop.dll的互操作dll,该DLL公开了.Net接口ThirdPartyObject
我编写了新的C#组件,该组件具有对ThirdPartyInterop.dll的引用
using ThirdPartyInterop; namespace CsComponent { public class CsClass { public void NetFoo( ThirdPartyObject thirdPrty ) { thirdPrty.ComFoo(); } } }
ThirdPartyClass的metadada为:
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?
由于在您的情况下,每个IThirdParty实际上都是ThirdPartyObject,因此您可以只使用强制类型转换。 但是,除了在新指令中,永远不要使用com对象的具体类型,而只能使用接口。 更改您的NetFoo()方法以将IThirdParty用作参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.