title: Exporting a class which uses managed handles?
body: <p>I'm trying to create a native wrapper around a .Net library in C++/CLI, so that regular C++ code can consume it.  For this example, let's say this is the C# code I'm trying to wrap:</p>
  
  <pre><code>class Foo
  {
      public Bar GetBar() {...}
      public string SomeProperty { get; set; }
  }
  
  class Bar
  {
      public void Baz() {...}
  }
  </code></pre>
  
  <p>I'm basically trying to do roughly this in C++/CLI (dll project):</p>
  
  <pre><code>class __declspec(dllexport) NativeFoo
  {
  public:
      NativeBar GetBar();
      std::string GetName();
      void SetName(const std::string &amp;value);
  
  private:
      Foo ^m_foo;
  };
  
  class __declspec(dllexport) NativeBar
  {
      friend class NativeFoo;
  public:
      void Baz();
  
  private:
      Bar(Bar ^bar);
  
      Bar ^m_bar;
  };
  </code></pre>
  
  <p>That way, a C++ library could link to this, use NativeFoo as if it's a regular C++ class.  Internally, NativeFoo would convert parameters to pass the implementation over to m_foo, marshal anything managed back into a native representation and return that to its caller...</p>
  
  <p>However, the problem I'm running into is I can't have managed members of an unmanaged class:</p>
  
  <pre><code>error C3265: cannot declare a managed 'm_bar' in an unmanaged 'NativeFoo'
  </code></pre>
  
  <p>Similarly I can't mark NativeFoo as "ref" (being a managed class itself) because then I can't export it:</p>
  
  <pre><code>C3386: 'NativeFoo' : __declspec(dllexport)/__declspec(dllimport) cannot be applied to a managed type
  </code></pre>
  
  <p>What's the right way to use managed pointers in my C++ objects?</p>