Disclaimer: Near zero with marshalling concepts..
免责声明:使用编组概念接近于零
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 ?
我有一个结构B,它包含一个字符串+一个结构C数组。我需要将它跨越巨大的互操作差距发送给COM-C ++使用者。我需要装饰我的结构定义的正确属性集是什么?
[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;
}
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.
编辑:@Andrew基本上这是我朋友的问题。他有这个东西在.Net中运行 - 他创建了一些自动生成的.tlb / .tlh,然后可以在C ++领域中使用。麻烦的是他无法修复阵列大小。
2 个解决方案
#1
4
C++: The Most Powerful Language for .NET Framework Programming
C ++:.NET Framework编程最强大的语言
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.
我正要接近一个需要在C ++ / C#边界编组结构化数据的项目,但我发现了什么是更好的方法(特别是如果你了解C ++并且喜欢学习新的编程语言)。如果您可以访问Visual Studio 2005或更高版本,则可以考虑使用C ++ / CLI而不是编组。它基本上允许您创建这个与C#100%兼容的神奇混合.NET /本机类库(就好像您已经在C#中编写了所有内容,以便在另一个C#项目中使用它),它也与C 100%兼容和/或C ++。在您的情况下,您可以编写一个C ++ / CLI包装器,它将数据从内存中的C ++封装到内存类型的CLI中。
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 * to ask syntax questions, back when I was learning this!
我对此非常幸运,使用纯C ++代码读取和写出数据文件(这可能是某种类型的第三方库,甚至),然后我的C ++ / CLI代码将C ++数据转换(复制)到.NET类型,在内存中,可以直接使用,就像我在C#中编写了读/写库一样。对我来说唯一的障碍是语法,因为你必须学习C ++的CLI扩展。我希望我能让*提出语法问题,当我学习这个问题时!
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."
作为对语法跋涉的回报,您可能会学到可以想象的最强大的编程语言。想一想:C#和.NET库的优雅和健全,以及C ++的低级和本机库兼容性。您不希望在C ++ / CLI中编写所有项目,但它非常适合将C ++数据导入C#/ .NET项目。它“只是有效”。
Tutorial:
#2
1
The answer depends on what the native definitions are that you are trying to marshal too. You haven't provided enough information for anyone to be able to really help.
答案取决于你试图编组的原生定义是什么。您没有提供足够的信息供任何人真正提供帮助。
A common thing that trips people up when marshalling strings in native arrays is that native arrays often use a fixed-size buffer for the string that is allocated inline with the struct. Your definition is marshalling the strings as a pointer to another block of memory containing the string (which is the default).
在本机数组中编组字符串时,人们常常惹恼的是,本机数组通常使用固定大小的缓冲区作为与结构内联分配的字符串。您的定义是将字符串编组为指向另一个包含字符串的内存块的指针(这是默认值)。
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = ##)] might be what you are looking for...
[MarshalAs(UnmanagedType.ByValTStr,SizeConst = ##)]可能就是你要找的......
#1
4
C++: The Most Powerful Language for .NET Framework Programming
C ++:.NET Framework编程最强大的语言
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.
我正要接近一个需要在C ++ / C#边界编组结构化数据的项目,但我发现了什么是更好的方法(特别是如果你了解C ++并且喜欢学习新的编程语言)。如果您可以访问Visual Studio 2005或更高版本,则可以考虑使用C ++ / CLI而不是编组。它基本上允许您创建这个与C#100%兼容的神奇混合.NET /本机类库(就好像您已经在C#中编写了所有内容,以便在另一个C#项目中使用它),它也与C 100%兼容和/或C ++。在您的情况下,您可以编写一个C ++ / CLI包装器,它将数据从内存中的C ++封装到内存类型的CLI中。
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 * to ask syntax questions, back when I was learning this!
我对此非常幸运,使用纯C ++代码读取和写出数据文件(这可能是某种类型的第三方库,甚至),然后我的C ++ / CLI代码将C ++数据转换(复制)到.NET类型,在内存中,可以直接使用,就像我在C#中编写了读/写库一样。对我来说唯一的障碍是语法,因为你必须学习C ++的CLI扩展。我希望我能让*提出语法问题,当我学习这个问题时!
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."
作为对语法跋涉的回报,您可能会学到可以想象的最强大的编程语言。想一想:C#和.NET库的优雅和健全,以及C ++的低级和本机库兼容性。您不希望在C ++ / CLI中编写所有项目,但它非常适合将C ++数据导入C#/ .NET项目。它“只是有效”。
Tutorial:
#2
1
The answer depends on what the native definitions are that you are trying to marshal too. You haven't provided enough information for anyone to be able to really help.
答案取决于你试图编组的原生定义是什么。您没有提供足够的信息供任何人真正提供帮助。
A common thing that trips people up when marshalling strings in native arrays is that native arrays often use a fixed-size buffer for the string that is allocated inline with the struct. Your definition is marshalling the strings as a pointer to another block of memory containing the string (which is the default).
在本机数组中编组字符串时,人们常常惹恼的是,本机数组通常使用固定大小的缓冲区作为与结构内联分配的字符串。您的定义是将字符串编组为指向另一个包含字符串的内存块的指针(这是默认值)。
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = ##)] might be what you are looking for...
[MarshalAs(UnmanagedType.ByValTStr,SizeConst = ##)]可能就是你要找的......