title: NetFx40_PInvokeStackResilience doesnt seem to work
body: <p>I have a library I am trying to use with a Dot Net 4 App in C#. This is a binaray runtime so I dont have access to the library source. I am getting the error </p>
  
  <blockquote>
    <p>A call to PInvoke function 'XXXXXXXXXXX.WinAPI::GetDCEx' has
    unbalanced the stack. This is likely because the managed PInvoke
    signature does not match the unmanaged target signature. Check that
    the calling convention and parameters of the PInvoke signature match
    the target unmanaged signature.</p>
  </blockquote>
  
  <p>If I disable the PInvokeStackImbalance MDA the application runs because it is not longer thrown by the debugger. Whilst this gets around the issue of the application not running there is obviously an issue under the hood.</p>
  
  <p>I recently read about NetFx40_PInvokeStackResilience an element you can place in the app.config which tells the runtime to use a marshaling layer to detect and fix incorrect platform invoke declarations (like in previous .Net versions).</p>
  
  <pre><code>&lt;configuration&gt;
    &lt;runtime&gt;
      &lt;NetFx40_PInvokeStackResilience enabled="1" /&gt;
    &lt;/runtime&gt;
    .....
  &lt;/configuration&gt;
  </code></pre>
  
  <p>The problem is this doesn't seem to do anything regarding the debugger, the debugger still invokes the error. </p>
  
  <p>My questions are should I still have to disable the PInvokeStackImbalance MDA?</p>
  
  <p>And how can I tell that NetFx40_PInvokeStackResilience has worked and the marshaling layer is doing its job?</p>
  
  <p>PS I am using Visual Studio 2010 Express</p>
  
  <p>PPS I have written to the owner of the library with a possible solutions amending his DllImports etc. Although the marshaling layer should get around the problem there may be a cost to performance. Plus its confusing and not ideal. However, in the meantime I still need a solution until the library gets updated.</p>