title: Stuck on calling convention calling Managed CLI method from unmanaged C++
body: <p>I am trying to call a managed method from unmanaged code. However, the managed code is requiring that I use the '(__clrcall) calling convention, and my unmanaged C++ code refuses to let me use the __clrcall calling convention without using the /clr option. I don't believe I want to do that as the unmanaged project is not mine to change to managed.</p>
  
  <p>I have gone through constructing all those delegates and function pointer marshaling in the managed side as I have seen on CodeGuru and MSDN but this error still pops up unless I call a static function using a __stdcall convention that cannot be a member of a ref class. The method needs to be an instance of a ref class.</p>
  
  <p>The only way I have been able to get around this is to do my method call from the unmanaged code in assembly skipping the popping of the ESP register (adding 4) after the call (the call has one parameter in the argument thus the '4'). That allows me to effectively do a __clrcall. That option stinks big time. </p>
  
  <p>Anybody have any ideas on how to work around this issue? It must surely be possible. I am missing some simple but vital piece of information.</p>
  
  <p>Here is my delegate definition (all of these belong to a single ref class ManagedSensor):</p>
  
  <pre><code>        delegate void OxpOnReceivedMeasurement(std::vector&lt;OBX&gt; *obxes);
  </code></pre>
  
  <p>Here is the instance method I want to call</p>
  
  <pre><code>        void LocalOxpMeasurementCallback(std::vector&lt;OBX&gt; *obxes);
  </code></pre>
  
  <p>And here is the marshaling of the ref class function to the unmanaged function done in the ref class constructor:</p>
  
  <pre><code>    // Now to make the 'delegate' unmanaged function pointer usable for unmanged code
      // The argument is the unmanaged callback.
      OxpOnReceivedMeasurement ^oxpMeasurementCallbackFP = gcnew OxpOnReceivedMeasurement(this, &amp;ManagedSensor::LocalOxpMeasurementCallback);
      // Lock it so the garbage collector doesnt get rid of it or move it
      gch = GCHandle::Alloc(oxpMeasurementCallbackFP);
      // Pass the marshaled function pointer to the unmanaged code
      IntPtr ip = Marshal::GetFunctionPointerForDelegate(oxpMeasurementCallbackFP);
      // fnOnReceiveMeasurement is defined in OxpLibTransports.h which matches the delegate
      // this passes the function pointer to the unmanaged Sensor class to be used when a 
      // measurement is received
      _sensor-&gt;RegisterForMeasurementCallback((fnOnReceiveMeasurement)(ip.ToPointer()));
  </code></pre>
  
  <p>Hope somebody has a nice answer to this issue. I am probably missing something stupid.</p>