Passing values by reference from C# to a C++ managed application 
Author Message
 Passing values by reference from C# to a C++ managed application

I apologize if this question has been asked, but I have only recently began
frequenting this newsgroup, and I can't find the answer in the messages I
have downloaded, nor on groups.google.com

I am dealing with some legacy C++ code that is in the form of a library.  To
get access to this, I am writing a C++ managed class wrapper.  This works
just fine, and calls the functions from the legacy library just fine, but
will not pass values back to my C# caller.. If I change the value in my C++
to "testing 123", I can follow it in the de{*filter*}, and see that it happens,
but when it steps out into the C# the value is gone and I am simply left
with a blank value.  I've even tried using the string's .Equals() function,
same exact thing happens.

Simple example below..

----------  some .cs file -------------
cplusplustest.mytestclass objTest = new cplusplustest.mytestclass();
string strTest = "";

objTest.SendByRef(strTest);   /* strTest should not be "" after this line,
but it stays constant, despite the de{*filter*} showing the c++ changing it */
/* I have tried doing objTest.SendByRef(ref strTest) but it complains that
there is no overloaded operator for it */
------------- end .cs file --------------

-----------Some C++ project ------------
using namespace System;

namespace cplusplustest
{
  public __gc class mytestclass
  {
    public:
      void SendByRef(String __gc *test)
      { test = new String("testing 123"); }
   }
  }
-------------- End C++ project -------------

This has me very stumped... I'm guessing that I need the 'ref' attribute up
in the C# code, but can't figure out how to tell my C++ class to accept
that.  Perhaps there is an attribute I can put on the C++ function that will
have it compile and show to the C# as (ref string) instead of (string).

If anybody has attempted this and made it work, I'd greatly appreciate it.
Unless it is completely impossible this is the only reasonable way for me to
do the function, as the actual library has about 8 arguments that get
returned values in this fashion.  I suppose I could add some properties in,
but that really is not desirable in my mind.

Thanks,

Mark E. Scott Jr.



Sat, 24 Apr 2004 08:42:19 GMT  
 Passing values by reference from C# to a C++ managed application
Hi Mike,

You're almost right, just need to change your C++ code a little:
--------------------------C++-------------------------------------
#using <mscorlib.dll>
using namespace System;

namespace cplusplustest {
  public __gc class mytestclass
  {
    public:
      void SendByRef(String __gc* __gc* test)   //MC++ an instance to a
class is always a pointer, in this case it's pointer to ref which is a
pointer.
      {
        (*test) = new String(S"testing 123");
       }
   };
  }

--------------------C#----------------------------
using System;
using cplusplustest;

public class A {
 public static void methA() {
 cplusplustest.mytestclass objTest = new cplusplustest.mytestclass();
 string strTest = "";

 objTest.SendByRef(ref strTest);
 Console.WriteLine(strTest);
  }
 public static void Main() {
 methA();
  }

Quote:
};

Ulzii-



Quote:
> I apologize if this question has been asked, but I have only recently
began
> frequenting this newsgroup, and I can't find the answer in the messages I
> have downloaded, nor on groups.google.com

> I am dealing with some legacy C++ code that is in the form of a library.
To
> get access to this, I am writing a C++ managed class wrapper.  This works
> just fine, and calls the functions from the legacy library just fine, but
> will not pass values back to my C# caller.. If I change the value in my
C++
> to "testing 123", I can follow it in the de{*filter*}, and see that it
happens,
> but when it steps out into the C# the value is gone and I am simply left
> with a blank value.  I've even tried using the string's .Equals()
function,
> same exact thing happens.

> Simple example below..

> ----------  some .cs file -------------
> cplusplustest.mytestclass objTest = new cplusplustest.mytestclass();
> string strTest = "";

> objTest.SendByRef(strTest);   /* strTest should not be "" after this line,
> but it stays constant, despite the de{*filter*} showing the c++ changing it */
> /* I have tried doing objTest.SendByRef(ref strTest) but it complains that
> there is no overloaded operator for it */
> ------------- end .cs file --------------

> -----------Some C++ project ------------
> using namespace System;

> namespace cplusplustest
> {
>   public __gc class mytestclass
>   {
>     public:
>       void SendByRef(String __gc *test)
>       { test = new String("testing 123"); }
>    }
>   }
> -------------- End C++ project -------------

> This has me very stumped... I'm guessing that I need the 'ref' attribute
up
> in the C# code, but can't figure out how to tell my C++ class to accept
> that.  Perhaps there is an attribute I can put on the C++ function that
will
> have it compile and show to the C# as (ref string) instead of (string).

> If anybody has attempted this and made it work, I'd greatly appreciate it.
> Unless it is completely impossible this is the only reasonable way for me
to
> do the function, as the actual library has about 8 arguments that get
> returned values in this fashion.  I suppose I could add some properties
in,
> but that really is not desirable in my mind.

> Thanks,

> Mark E. Scott Jr.




Sat, 24 Apr 2004 10:04:54 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Passing values by reference from C# to a C++ managed application

2. Passing values from C# to a C++ dll

3. passing a managed array by reference (Dotnet)

4. C++ References and C# references Question?

5. How to pass a function pointer from Managed C++ to unmanaged c++

6. C# client crashs when calling into Managed C++ which calls unmanaged c++ function

7. Interoperability UnManaged C++, Managed C++, C#

8. pointers to migration issues from existing C++ code to managed C++ or C#

9. How good is Managed C++ as a glue between C# and C++

10. pointers to migration issues from existing C++ code to managed C++ or C#

11. va_list passed by value/reference

12. passing values by reference ???

 

 
Powered by phpBB® Forum Software