Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. How to expose an [out] parameter in a Managed C++ class library?

How to expose an [out] parameter in a Managed C++ class library?

Scheduled Pinned Locked Moved Managed C++/CLI
csharpc++visual-studiocomhelp
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    ralfoide
    wrote on last edited by
    #1

    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/

    N N 2 Replies Last reply
    0
    • R ralfoide

      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/

      N Offline
      N Offline
      Nick Parker
      wrote on last edited by
      #2

      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 to Param2. -Nick Parker

      R 1 Reply Last reply
      0
      • N 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 to Param2. -Nick Parker

        R Offline
        R Offline
        ralfoide
        wrote on last edited by
        #3

        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/

        N P 2 Replies Last reply
        0
        • R ralfoide

          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/

          N Offline
          N Offline
          Nick Parker
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • R ralfoide

            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/

            P Offline
            P Offline
            Paul Ingles
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • R ralfoide

              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/

              N Offline
              N Offline
              Nemanja Trifunovic
              wrote on last edited by
              #6

              Why don't you use something like void foo2( [out] short *value);

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups