struggling with __gc arrays as parameters

M

microsoft

Hi All,

I am fairly new to Managed C++ extensions. I started trying to implement a
interface defined in a C# project, in C++. The problem was with passing
value arrays from c# to c++. I found examples on the web which worked, but
found mine didn't. After narrowing down the interface I found what I believe
to be the problem. The order of the parameters! The code below shows the
function that does not compile named QueryPositionsForward. When reversing
the parameters illustrated by function QueryPositionsReverse, all appears to
work. ildasm reveals that the function, when reversed, compiles to the same
signature as the c# interface with function reversed (not included in the
code).

Any ideas as to what is causing this? Is it a compiler glitch, or a user
glitch :)

Thanks

Olly

#include "stdafx.h"
#using <mscorlib.dll>

using namespace System;

public __gc class DriveClass
{
public:

void QueryPositionsForward(Object* AxisList __gc[], double (__gc *
Positions) __gc[]){}
void QueryPositionsReverse(double (__gc * Positions) __gc[], Object*
AxisList __gc[]){}

};
 
B

Brandon Bray [MSFT]

microsoft said:
void QueryPositionsForward(Object* AxisList __gc[],
double (__gc * Positions) __gc[]){}
void QueryPositionsReverse(double (__gc * Positions) __gc[],
Object* AxisList __gc[]){}

If you're trying to get something similar to a C# API, you don't want to
have an array of pointers to value types. double is a value type, so you
just want a plain array of them. Here's the code I would suggest:

void QueryPositionsForward(Object* AxisList __gc[],
double Positions __gc[]);
void QueryPositionsReverse(double Positions __gc[],
Object* AxisList __gc[]);

Hope that helps!
 
O

ollyw

Thanks for replying Brandon, but the value array parameter is a ref
parameter, so does need the pointer. Sorry I didn't explain that.

I have found more information. The following example fails to compile with
the error : 'Positions' : missing storage-class or type specifiers. Now the
way to fix it? remove the explicit __gc from the * in the second parameter!
Note that the explicit __gc works for the first parameter, and also the
pointer is (and must be?) a gc pointer. That is definately a compiler error.

Olly

#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;

public __gc class DriveClass
{
public:

void QueryPositionsForward(Object __gc* AxisList __gc[], double (__gc *
Positions) __gc[]){}

};

Brandon Bray said:
microsoft said:
void QueryPositionsForward(Object* AxisList __gc[],
double (__gc * Positions) __gc[]){}
void QueryPositionsReverse(double (__gc * Positions) __gc[],
Object* AxisList __gc[]){}

If you're trying to get something similar to a C# API, you don't want to
have an array of pointers to value types. double is a value type, so you
just want a plain array of them. Here's the code I would suggest:

void QueryPositionsForward(Object* AxisList __gc[],
double Positions __gc[]);
void QueryPositionsReverse(double Positions __gc[],
Object* AxisList __gc[]);

Hope that helps!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top