CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2012
    Posts
    1

    passing by reference from unmanaged c++ -> c++/CLI -> c#

    I have a third party unmanaged c++ library.
    I created a managed c++ wrapper for it since I need to call it from c# code. Problem is one of native c++ functions passes an int by reference. I do not know how to route this through the managed wrapper to the c# code. For example

    class A //unmnaged SDK class
    {
    public:
    void Foo(int& i);
    }

    //managed c++ wrapper
    public ref class A_Wrapper
    {
    public:
    A_Wrapper() {_unmanaged = new A();};
    ~A_Wrapper() {delete unmanaged; };
    void Foo(int& i) { _unmanaged->Foo(i)};
    private:
    A * _unmanaged;
    }

    In my c# (yes #) code I try to call A_Wrapper::Foo() but I get compile errors

    c# code:
    public static void Main()
    {
    A_Wrapper aw = new A_Wrapper();
    int id = 0;
    aw.Foo(&id);
    }
    error CS0214: Pointers and fixed size buffers may only be used in an unsafe context

    I don't really want to wrap it in an unsafe {} block. How do I route the pass by reference from c++ to c# properly?

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: passing by reference from unmanaged c++ -> c++/CLI -> c#

    I'd suggest you introduce a "shadow variable" for the parameter in your managed wrapper, pass in the variable as a tracking reference from your C# code (don't know the C# syntax for that, though), assign it to the shadow variable and then assign it back after calling the wrapped native function.

    Somewhat like this:

    Code:
    // Test25.cpp: Hauptprojektdatei.
    
    #include "stdafx.h"
    
    using namespace System;
    
    class A //unmnaged SDK class
    {
    public:
      void Foo(int& i) { ++i; }
    };
    
    //managed c++ wrapper
    
    public ref class A_Wrapper 
    {
    public:
      A_Wrapper() { _unmanaged = new A; }
      ~A_Wrapper() { delete _unmanaged; }
    
      void Foo(int% i)
      {
        int wrapper_i = i;
        _unmanaged->Foo(wrapper_i);
        i = wrapper_i;
      }
    
    private:
      A * _unmanaged;
    };
    
    int main(array<System::String ^> ^args)
    {
      A_Wrapper ^wrapper = gcnew A_Wrapper;
      int i = 1;
      wrapper->Foo(i);
      Console::WriteLine(i);  // Will print 2
      Console::ReadLine();
      return 0;
    }
    Please use code tags when posting code.

    Also, there's no need to post your question twice. You probably did so because you didn't see the new thread showing up after submitting your post. However, posts by new members may be automatically placed in the "moderation queue" when submitted, and only become visible once a moderator manually has approved them. This explains the delay before your post actualy appeared.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured