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

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.
 
P

Peter Q

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
 
W

Willy Denoyette [MVP]

Should be:
MC++:

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

C# :
foo(ref i);

Willy.
 

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