[英]Returning Managed C# List to Unmanaged C++ code
I have a C# dll ( for which register for COM interop option is set). 我有一个C#dll(设置了COM interop选项的寄存器)。
This C# dll has the below interface and class 这个C#dll具有以下接口和类
interface IMyInterface
{
bool IsNameExists(string name);
List<string> GetNameList();
}
public class MyClass : IMyInterface
{
public bool IsNameExists(string name)
{
//DO Something
}
public List<string> GetNameList()
{
// DO something
}
}
I need to call the methods IsNameExists and GetNameList from unmanaged C++. 我需要从非托管C ++调用方法IsNameExists和GetNameList。
#import "..\..\ProdCon\bin\ProdCon.tlb" raw_interfaces_only
void main()
{
HRESULT hr =::CoInitialize(NULL);
IMyInterface pIMyInterface(__uuidof(MyClass));
VARIANT_BOOL bRet = FALSE;
BSTR bstrName = ::SysAllocString(_T("RAJESH"));
hr = pIMyInterface->IsNameExists(bstrName,&bRet);
}
So I created COM object as above and called the IsNameExists mehtod without any issue. 所以我创建了如上所述的COM对象并且没有任何问题地调用了IsNameExists mehtod。
Since GetNameList method returns list, I am getting the below warning 由于GetNameList方法返回列表,我收到以下警告
'MyDll.IMyInterface.GetNameList(#0), MyDll'.
'MyDll.IMyInterface.GetNameList(#0),MyDll'。 Warning: Type library exporter encountered a generic type instance in a signature.
警告:类型库导出器在签名中遇到泛型类型实例。 Generic code may not be exported to COM.
通用代码可能无法导出到COM。
Please help me how to return C# list to unmanaged C++ code. 请帮助我如何将C#列表返回到非托管C ++代码。 So that unmanaged C++ code can use this list.
因此,非托管C ++代码可以使用此列表。
'Generic code may not be exported to COM.' '通用代码可能无法导出到COM。' => hence its the type parameter string in public List GetNameList().
=>因此它是公共List GetNameList()中的类型参数字符串。 Thus essentially you need access to a non-generic c# method to get the data.
因此,基本上您需要访问非通用的c#方法来获取数据。
If you have control of the MyClass codebase you could (for example) add a: 如果您可以控制MyClass代码库,您可以(例如)添加:
public string[] GetNameArray()
{
return GetNameList.ToArray();
}
If not then you'll need to write a proxy/wrapper class to do something similar to the above and expose that via COM, either as a one off or a 'general' methodology using reflection say. 如果没有,那么你需要编写一个代理/包装类来做类似于上面的事情并通过COM公开,或者作为一个关闭或者使用反射的'一般'方法。
See for example http://weblog.west-wind.com/posts/2007/Jul/10/Generics-and-COM-Interop-dont-mix 参见例如http://weblog.west-wind.com/posts/2007/Jul/10/Generics-and-COM-Interop-dont-mix
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.