Parameter Passing from C# to C++


R

Rich

I need to build a managed DLL in C++ and call it from C#. I need this
to work with remoting as well.

In C#, class objects are reference types, so if an object is passed
directly to a C++ method it is considered a reference to the object.


C# call:
i = some_method( some_object );


C++ method:
int some_method( some_class ^ some_object)
{
int result;


some_object->member1 = 123;
result = some_object->member2 * 2;


return result;
}


This is fine until we need to run with remoting. For C# to marshal the

class object in both directions (so the function can change members of
the object and pass the new value back), then in C# we must use a "ref"

call:
i = some_method( ref some_object );


How should the C++ method be written to accept this call?


Thanks - any suggestions are appreciated.
 
B

Ben Voigt

Rich said:
I need to build a managed DLL in C++ and call it from C#. I need this
to work with remoting as well.

In C#, class objects are reference types, so if an object is passed
directly to a C++ method it is considered a reference to the object.


C# call:
i = some_method( some_object );


C++ method:
int some_method( some_class ^ some_object)
{
int result;


some_object->member1 = 123;
result = some_object->member2 * 2;


return result;
}


This is fine until we need to run with remoting. For C# to marshal the

class object in both directions (so the function can change members of
the object and pass the new value back), then in C# we must use a "ref"

call:
i = some_method( ref some_object );


How should the C++ method be written to accept this call?
int some_method( some_class^% some_object);

But I think you should be using InAttribute and OutAttribute to tell
remoting whether to copy the object before and after. Haven't used remoting
before though, only interop, so this is just a wild guess.
 
Ad

Advertisements


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