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

Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
Biwier
  • 397
  • 1
  • 6
  • 17
  • What is updateData? Why UpdateDataDelegate accepts two AStruct parameters, and you try to create AStruct array? – Alex F Sep 12 '10 at 12:27
  • What's wrong with using a native container (e.g. `std::vector`) to store your native data? – Ben Voigt Sep 12 '10 at 17:28

1 Answers1

4

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
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Better: it must be a managed type *or a primitive type*. That's why pointers are permitted. – Ben Voigt Sep 12 '10 at 17:26
  • 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 Sep 13 '10 at 03:57
  • 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 Sep 13 '10 at 08:41
  • Conversely, that could simply mean that C# supports both managed types and primitive types (in fact it does). – Ben Voigt Sep 13 '10 at 16:20
  • 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 Sep 13 '10 at 16:26