Biwier
Biwier

Reputation: 417

cli/C++ how to define cli::array with unmanaged type element?

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

Upvotes: 2

Views: 3720

Answers (1)

Hans Passant
Hans Passant

Reputation: 941465

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]);

Upvotes: 4

Related Questions