How to expose an [out] parameter in a Managed C++ class library?
-
Hi! I'm writing a Managed C++ class library to be used by a C# client, and I have a method which prototype should look like that from C#: MyClass.Method(string Param1, out int Param2); Problem is, I do not clearly understand how to declare an "out" parameter in Managed C++. I tried a pointer-on-int, as I used to do in COM with the [out] attribute, but it doesn't work (IntelliSense on the C# side wants an int* too!) I could use a reference of course, like that: public __gc class MyClass { public: void Method(System::String __gc *Param1, int &Param2) {... } } Not sure it works though and then the C# prototype would probably become "ref int Param2". Is there any clever solution to this? R/
-
Hi! I'm writing a Managed C++ class library to be used by a C# client, and I have a method which prototype should look like that from C#: MyClass.Method(string Param1, out int Param2); Problem is, I do not clearly understand how to declare an "out" parameter in Managed C++. I tried a pointer-on-int, as I used to do in COM with the [out] attribute, but it doesn't work (IntelliSense on the C# side wants an int* too!) I could use a reference of course, like that: public __gc class MyClass { public: void Method(System::String __gc *Param1, int &Param2) {... } } Not sure it works though and then the C# prototype would probably become "ref int Param2". Is there any clever solution to this? R/
ralfoide wrote: public __gc class MyClass { public: void Method(System::String __gc *Param1, int &Param2) {... } } The purpose of a ref parameter in C# is where the callee function assigns the value, which is exactly what your
void Method(System::String __gc *Param1, **int &Param2**)
function allows to happen toParam2
. -Nick Parker -
ralfoide wrote: public __gc class MyClass { public: void Method(System::String __gc *Param1, int &Param2) {... } } The purpose of a ref parameter in C# is where the callee function assigns the value, which is exactly what your
void Method(System::String __gc *Param1, **int &Param2**)
function allows to happen toParam2
. -Nick ParkerI beg to differ, but my understanding is that a "ref" is used when the called function will modify the value. An "out" seems preferable if the called function is just returning parameters. My sample prototype was of course simplified. My function returns several parameters, and rather than returning a struct I prefer to use out parameters. Example of C# prototype I want: MyClass.MyMethod(string inValue, out int Param1, out int Param2, out int Param3); So far I got it to work using "ref" if I declare the Param1..3 above using int __gc * in MC++: This MC++ real prototype in the class library: MyClass::MyMethod(System::String __gc* inValue, int __gc* Param1, int __gc* Param2, int __gc* Param3); is seen like this from the C# client: MyClass.MyMethod(string inValue, ref int Param1, ref int Param2, ref int Param3); Ideally I'd like out parameters. If not possible, well too bad :-D R/
-
I beg to differ, but my understanding is that a "ref" is used when the called function will modify the value. An "out" seems preferable if the called function is just returning parameters. My sample prototype was of course simplified. My function returns several parameters, and rather than returning a struct I prefer to use out parameters. Example of C# prototype I want: MyClass.MyMethod(string inValue, out int Param1, out int Param2, out int Param3); So far I got it to work using "ref" if I declare the Param1..3 above using int __gc * in MC++: This MC++ real prototype in the class library: MyClass::MyMethod(System::String __gc* inValue, int __gc* Param1, int __gc* Param2, int __gc* Param3); is seen like this from the C# client: MyClass.MyMethod(string inValue, ref int Param1, ref int Param2, ref int Param3); Ideally I'd like out parameters. If not possible, well too bad :-D R/
ralfoide wrote: Ideally I'd like out parameters. If not possible, well too bad I still think you are on the right track with the
ref
parameters. Is there an equivalent to C++'s reference parameters in C#? (i.e. void foo(int &i)) [^] shows what I was trying to explain (however I may have fouled it up :eek: ). -Nick Parker -
I beg to differ, but my understanding is that a "ref" is used when the called function will modify the value. An "out" seems preferable if the called function is just returning parameters. My sample prototype was of course simplified. My function returns several parameters, and rather than returning a struct I prefer to use out parameters. Example of C# prototype I want: MyClass.MyMethod(string inValue, out int Param1, out int Param2, out int Param3); So far I got it to work using "ref" if I declare the Param1..3 above using int __gc * in MC++: This MC++ real prototype in the class library: MyClass::MyMethod(System::String __gc* inValue, int __gc* Param1, int __gc* Param2, int __gc* Param3); is seen like this from the C# client: MyClass.MyMethod(string inValue, ref int Param1, ref int Param2, ref int Param3); Ideally I'd like out parameters. If not possible, well too bad :-D R/
Not sure if this will help much, but I think that ref and out perform pretty much the same job, they just set a different kind of contract. When using out you are effectively saying that the method is responsible for initialising and setting the contents, whereas with ref the calling code is responsible. What happens if you try and use the out keyword from the C# code? Does the compiler throw up an error? -- Paul "Put the key of despair into the lock of apathy. Turn the knob of mediocrity slowly and open the gates of despondency - welcome to a day in the average office." - David Brent, from "The Office" MS Messenger: paul@oobaloo.co.uk
-
Hi! I'm writing a Managed C++ class library to be used by a C# client, and I have a method which prototype should look like that from C#: MyClass.Method(string Param1, out int Param2); Problem is, I do not clearly understand how to declare an "out" parameter in Managed C++. I tried a pointer-on-int, as I used to do in COM with the [out] attribute, but it doesn't work (IntelliSense on the C# side wants an int* too!) I could use a reference of course, like that: public __gc class MyClass { public: void Method(System::String __gc *Param1, int &Param2) {... } } Not sure it works though and then the C# prototype would probably become "ref int Param2". Is there any clever solution to this? R/
Why don't you use something like void foo2( [out] short *value);