Pass parameters by ref from c# to managed c++

  • Thread starter Thread starter Pravin
  • Start date Start date
P

Pravin

I have a function as follows in managed c++.

public foo( object *objectValue)
{
// pseudo code below, as I don't have exact code at this very moment.
if( type of objectValue is int)
{
objectValue = objectValue * 2;
}
elseif(type of objectValue is short)
{
objectValue = objectValue * 3;
}
// This function does some more processing after this.
}

I wish to call above function from C#.

To say it briefly, from C# I wish to pass 'int' and 'short' type of objects
to a function in managed c++. Managed c++ code will alter the value. The
changed value should be available to me in C# code.

The problem: In C# if I call the above as:
int i = 10;
foo(ref i);
The above code gives me compilation error. If I do boxing, unboxing in c#
code to pass parameter, then the changed value is not reflected back in C#
function.

I request to please guide, how to achieve it? Thanks.
 
Your C++ code requires a pointer argument, 'ref' aren't exactly the same a
pointer.

You should try this:

unsafe public integerManipulation()
{
int i = 10;
foo(&i);
}

of course, turn on the unsafe option in ur compiler options
 
Should be:
MC++:

.....
public:
foo( Object **objectValue) {
*objectValue = ....
....
}

C# :
foo(ref i);

Willy.
 
Back
Top