title: Safety of passing HBITMAP handle from unmanaged to managed code for created a System.Drawing.Bitmap
body: <p>I'm pretty new to managed/unmanaged interop, so I'm looking to get some opinions on how safe the following procedure is for getting a bitmap from unmanaged C++ to managed C#. The basic idea is:</p>
  
  <ol>
  <li>C# calls an interop function, <code>FetchImage</code>, which is in the unmanaged C++. It passes an <code>out int</code> param. <code>FetchImage</code> has a corresponding <code>long *</code> param.</li>
  <li>In C++, <code>FetchImage</code> creates a <code>CBitmap</code> somewhere safe, ie not local, draws something on it, uses <code>HandleToLong()</code> to convert the bitmap's <code>HBITMAP</code> handle to a <code>long</code>, stores it in the param for the C#, and returns.</li>
  <li>Back in C#, the <code>out int</code> param is converted to an <code>IntPtr</code> and uses <code>System.Drawing.Image.FromHbitmap</code> to copy the data and produce a <code>System.Drawing.Bitmap</code> object.</li>
  <li>C# then calls another interop function, <code>ReleaseImage</code>.</li>
  <li>In C++, <code>ReleaseImage</code> frees the resources associated with the <code>CBitmap</code> it created earlier.</li>
  </ol>
  
  <p>That's the gist for the impatient. More specific code examples below.</p>
  
  <p>C++ interop definitions for the functions:</p>
  
  <pre><code>namespace {
      std::unique_ptr&lt; CBitmap &gt; bitty;
  }
  HRESULT __stdcall Helper::FetchImage( /*[out]*/ long * hBitmap )
  {
      bitty.reset( new CBitmap );
  
      // call CreateBitmap and then draw something,
      // ensure it's not selected into a DC when done
  
      *hBitmap = HandleToLong( bitty-&gt;GetSafeHandle() );
      return S_OK;
  }
  HRESULT __stdcall Helper::ReleaseImage()
  {
      bitty.reset();
      return S_OK;
  }
  </code></pre>
  
  <p>IDL prototypes for the interop functions, which are wrapped in a helper class in C#:</p>
  
  <pre><code>[id(1)] HRESULT FetchImage( long * hBitmap );
  [id(2)] HRESULT ReleaseImage();
  </code></pre>
  
  <p>Produces these C# prototypes in the helper class:</p>
  
  <pre><code>void FetchImage( out int hBitmap );
  void ReleaseImage();
  </code></pre>
  
  <p>And the C# that calls them looks kind of like this:</p>
  
  <pre><code>int ret;
  helper.FetchImage( out ret );
  Bitmap b = Image.FromHbitmap( (IntPtr)ret );
  helper.ReleaseImage();
  // do anything I want with b
  </code></pre>
  
  <p>The only issue I've come up with on my own is the case of a call to <code>FetchImage</code> or <code>ReleaseImage</code> from somewhere else getting things out of sync. So I'll probably have a list of <code>CBitmap</code>s instead of just one, then pass the handle back to <code>ReleaseImage</code> so it'll only destroy the one from the matching <code>FetchImage</code> call.</p>
  
  <p>Are there any gotchas I'm not aware of? I do have this working, I just wanted to make sure I'm not doing something dangerous because I don't know any better.</p>