如何编组结构的数组 - (.NET / C#=> C ++)数组、结构、GT、NET

2023-09-04 00:48:50 作者:做不了你老婆就你后妈

免责声明:接近零与编组概念。

Disclaimer: Near zero with marshalling concepts..

我有一个包含一个字符串+的结构C.数组,我需要跨越巨大的互操作性鸿沟发送给COM结构体。B - C ++消费 什么是正确的属性集我需要装饰我的结构定义?

I have a struct B that contains a string + an array of structs C. I need to send this across the giant interop chasm to a COM - C++ consumer. What are the right set of attributes I need to decorate my struct definition ?

[ComVisible (true)]
[StructLayout(LayoutKind.Sequential)]
public struct A
{
    public string strA
    public B b;
}


[ComVisible (true)]
[StructLayout(LayoutKind.Sequential)]
public struct B
{
    public int Count;

    [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.Struct, SizeParamIndex=0)]
    public C [] c;
}

[ComVisible (true)]
[StructLayout(LayoutKind.Sequential)]
public struct C
{
    public string strVar;
}

编辑:@Andrew 基本上,这是我的朋友的问题。他有这个东西在.net中工作 - 他做了一些自动的有.TLB / .tlh创建,他就可以在C ++领域使用。 麻烦的是,他不能修订数组的大小。

edit: @Andrew Basically this is my friends' problem. He has this thing working in .Net - He does some automagic to have the .tlb/.tlh created that he can then use in the C++ realm. Trouble is he can't fix the array size.

推荐答案

的 C ++:最强大的语言用于.NET Framework编程

我正要接近所需元帅整个C ++ / C#边界结构化数据的一个项目,但我发现这可能是一个更好的方法(特别是如果你知道C ++和喜欢学习新的编程语言)。如果你有机会到Visual Studio 2005或以上,你可能会考虑使用C ++ / CLI而不是封送处理。基本上,它允许你创建这个神奇的混合.NET /本地类库是用C#100%兼容(就像你写的一切在C#中,对于消费在另一个C#项目的目的),这也是与C 100%兼容和/或C ++。你的情况,你可以写一个C ++ / CLI的包装,封从C ++的数据在内存中的CLI内存类型。

I was about to approach a project that needed to marshal structured data across the C++/C# boundary, but I found what could be a better way (especially if you know C++ and like learning new programming languages). If you have access to Visual Studio 2005 or above you might consider using C++/CLI rather than marshaling. It basically allows you to create this magic hybrid .NET/native class library that's 100% compatible with C# (as if you had written everything in C#, for the purposes of consuming it in another C# project) that is also 100% compatible with C and/or C++. In your case you could write a C++/CLI wrapper that marshaled the data from C++ in memory to CLI in memory types.

我有pretty的好运气与此,采用纯C ++ code读取和写入了数据文件(这可能是某种形式的第三方库,甚至),然后我的C ++ / CLI code转换(复印件)C ++的数据转换成.NET类型,在内存中,可以直接食用,就好像我已经写在C#中的读/写库。对我来说,唯一的障碍是语法,因为你必须学会​​的CLI扩展的C ++。我希望我有StackOverflow的问语法问题,回来时,我是学这个!

I've had pretty good luck with this, using pure C++ code to read and write out datafiles (this could be a third party library of some kind, even), and then my C++/CLI code converts (copies) the C++ data into .NET types, in memory, which can be consumed directly as if I had written the read/write library in C#. For me the only barrier was syntax, since you have to learn the CLI extensions to C++. I wish I'd had StackOverflow to ask syntax questions, back when I was learning this!

在换取通过语法跋涉,你学习可能是最强大的编程语言可想而知。想想吧:优雅和C#和.NET库的完整性,以及C ++水平低和机库的兼容性。你不会希望写在C ++ / CLI的所有项目,但它的伟大获得C ++数据到C#/。NET项目。它只是工作。

In return for trudging through the syntax, you learn probably the most powerful programming language imaginable. Think about it: the elegance and sanity of C# and the .NET libraries, and the low level and native library compatibility of C++. You wouldn't want to write all your projects in C++/CLI but it's great for getting C++ data into C#/.NET projects. It "just works."

教程:

HTTP://www.$c$cproject.com/KB/ MCPP / cppcliintro01.aspx
 
精彩推荐
图片推荐