Copying memory to struct pointers in C++ VS2005

To reuse some old C++ code, you can write managed wrappers for the old code.
You can then use the old C++ code from C# etc without having to use COM.

Using the new C++ extensions in VS2005, this is quite easy:


public ref class WrapperCar
{
public:
WrapperCar(
UnManagedCar* _pCar
)

{

pCar = _pCar;

}

More on the new managed extensions: (2 new operators for C++ too...)
http://msdn2.microsoft.com/en-us/library/xey702bw.aspx



Was using some old code, which copies memory directly to struct pointers. This is an efficient way to parse binary data, but is hard to maintain, hence reusing the old code.


However, had a problem where the sizes of structs were larger than expected. This caused the binary parsing to go a bit haywire ...


This turned out to be just a project setting, for packing of struct members:

"Configuration Properties C/C++ Code Generation ^ Struct Member Alignment"
























Article on parsing binary in .NET by Chris Taylor goes into more depth - might be for an older version of C++ managed extensions ...



http://www.dotnetjunkies.com/Article/DF6E5BE5-BCB6-43BB-BEE4-1013A2E3D085.dcik

Comments

  1. for the reverse case, if you are exposing a C# struct to C++:

    you can use this C# attribute:

    StructLayoutAttribute.Pack

    http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.structlayoutattribute.pack.aspx

    ReplyDelete

Post a Comment