title: What is the C# equivalent of this C++ code (with InPtr)
body: <p>I'm implementing a <strong>Custom Credential Provider</strong> in <strong>C#</strong>. I'm using a <strong>C++</strong> project as example. This piece of C++ code provides an image to Windows. The way I see it <strong>phbmp</strong> is a pointer to the image-bitmap. The code either updates the pointer so it points to a new bitmap (read from Resource) or it loads the bitmap to the address pointed by <strong>phbmp</strong>. <em>I'm not sure if the pointer itself is changed or not</em>. </p>
  
  <pre><code>// Get the image to show in the user tile
  HRESULT CSampleCredential::GetBitmapValue(DWORD dwFieldID, _Outptr_result_nullonfailure_ HBITMAP *phbmp)
  {
      HRESULT hr;
      *phbmp = nullptr;
  
      if ((SFI_TILEIMAGE == dwFieldID))
      {
          HBITMAP hbmp = LoadBitmap(HINST_THISDLL, MAKEINTRESOURCE(IDB_TILE_IMAGE));
          if (hbmp != nullptr)
          {
              hr = S_OK;
              *phbmp = hbmp;
          }
          else
          {
              hr = HRESULT_FROM_WIN32(GetLastError());
          }
      }
      else
      {
          hr = E_INVALIDARG;
      }
  
      return hr;
  }
  </code></pre>
  
  <p>Below is the C# equivalent I'm implementing:</p>
  
  <pre><code>public int GetBitmapValue(uint dwFieldID, IntPtr phbmp)
  {
      if (dwFieldID == 2)
      {
          Bitmap image = Resource1.TileImage;
  
          ImageConverter imageConverter = new ImageConverter();
          byte[] bytes = (byte[])imageConverter.ConvertTo(image, typeof(byte[]));
  
          Marshal.Copy(bytes, 0, phbmp, bytes.Length);
  
          return HResultValues.S_OK;
      }
  
      return HResultValues.E_INVALIDARG;
  }
  </code></pre>
  
  <p>What I'm trying to do:</p>
  
  <ol>
  <li>Load the image from resource (this works, it has the correct length)</li>
  <li>Convert the Bitmap to an array of bytes</li>
  <li>Copy these bytes to the address pointed by <strong>phbmp</strong></li>
  </ol>
  
  <p>This crashes, I assume because of memory-allocation.</p>
  
  <p>The parameters in this method are defined by an interface (in <strong>CredentialProvider.Interop.dll</strong>, which is provided by Microsoft - I think). So I'm pretty sure it's correct and <strong>phbmp</strong> is not an <strong>out</strong>-parameter.</p>
  
  <p>Because it is not an out-parameter I can not change <strong>phbmp</strong> to let it point to my bitmap, right? I have assigned <strong>phbmp</strong> to <strong>Bitmap.GetHbitmap()</strong> and that doesn't crash but it isn't working either. I assume that the change to <strong>phbmp</strong> is only local in this method.</p>
  
  <p>I can understand that it is not possible to alloc memory to a predefined address. It's the other way around: you alloc memory and get an pointer to it. But then this change is local again. How does this work?</p>