How to marshal an array of structs - (.Net/C# => C++)

前端 未结 2 1638
甜味超标
甜味超标 2021-01-07 02:17

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 chas

2条回答
  •  死守一世寂寞
    2021-01-07 03:00

    C++: The Most Powerful Language for .NET Framework Programming

    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.

    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!

    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."

    Tutorial:

    • http://www.codeproject.com/KB/mcpp/cppcliintro01.aspx

提交回复
热议问题