tags:

views:

64

answers:

2

I have a native C/C++ struct

typedef struct
{
...
} AStruct;

and in C++/CLI code i define one delegate and one cli array as following

public delegate void UpdateDataDelegate(AStruct% aSt,AStruct% bSt);

cli::Array<AStruct>^ args=gcnew cli::Array<AStruct>(2); // complile failed!!!!。

this->Invoke(updateData,args);

AStruct has many fields and was used by many modules so if i don't like to write a mananged wrap for AStruct, how to make above code works?

many thanks

A: 

you should try marshaling AStruct into C# array. here is an example: http://stackoverflow.com/questions/188299/marshal-c-struct-array-into-c

HPT
you should try marshaling AStruct into C# array. here is an example: i don't want to do marshaling due to the AStruct was defined by other team/company, and the AStruct will change occasionally ,so i don't want to write a wrap for it like the one showed in the your your link thank you!
Biwier
+2  A: 

The element type of a managed array must be a managed type. One workaround is store pointers:

array<AStruct*>^ args=gcnew array<AStruct*>(2);
args[0] = new AStruct;
// etc...

UpdateDataDelegate^ dlg = gcnew UpdateDataDelegate(Mumble);
dlg->Invoke(*args[0], *args[1]);
Hans Passant
thanks Hans Passanti'll try your workaround.
Biwier
Better: it must be a managed type *or a primitive type*. That's why pointers are permitted.
Ben Voigt
Pointers are managed types.
Hans Passant
By what definition of "managed type" is a pointer a managed type? It's not managed by the garbage collector, and it is usable in an unmanaged program.
Ben Voigt
Ecma-335. Other big hints are that you can use them in C#, a language that doesn't support generating unmanaged code, and being able to store them in an array.
Hans Passant
Conversely, that could simply mean that C# supports both managed types and primitive types (in fact it does).
Ben Voigt
The phrase "managed type" appears in ECMA-335 exactly twice, and both times the behavior mentioned does not apply to pointers (pointers have fixed layout, according to ECMA-335 managed types do not).
Ben Voigt