简体   繁体   English

将托管C#列表返回到非托管C ++代码

[英]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.

相关问题 从托管 C# 回调到非托管 C++ 代码 - Callbacks from Managed C# to Unmanaged C++ Code 让非托管C ++代码调用托管C ++代码来调用C#代码 - Have Unmanaged c++ code call managed c++ code that calls c# code 在托管代码中使用非托管代码时处理错误(C++、C、C++/CLI、C#) - Handling errors while using unmanaged code in a managed one ( C++, C, C++/CLI, C#) 从托管C#回调非托管代码 - Callback unmanaged code from managed C# 将IWICStream / IStream从非托管C ++ DLL返回到托管C#并读取它 - Returning IWICStream / IStream from unmanaged C++ DLL to managed C# and reading it 跨C / C ++(非托管)和托管C#代码共享通用定义 - Shared common definitions across C/C++ (unmanaged) and managed C# code C#托管代码和C ++非托管代码之间的字符串混合编程 - mixed programming for a string between C# managed code and c++ unmanaged code 无法使用CLI层在非托管C ++代码中编译托管C#代码 - Unable to compile managed c# code within unmanaged c++ code using cli layer 从C#windows服务调用C ++ DLL(非托管代码)(用托管代码编写) - Calling a C++ dll (unmanaged code) from a C# windows service (written in managed code) 从非托管C ++代码调用托管C#组件,我该如何定义配置 - calling managed c# component from unmanaged c++ code, how do i define config
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM