Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7778851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:30:07+00:00 2026-06-01T18:30:07+00:00

I have a library I am trying to use with a Dot Net 4

  • 0

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

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.

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.

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).

<configuration>
  <runtime>
    <NetFx40_PInvokeStackResilience enabled="1" />
  </runtime>
  .....
</configuration>

The problem is this doesn’t seem to do anything regarding the debugger, the debugger still invokes the error.

My questions are should I still have to disable the PInvokeStackImbalance MDA?

And how can I tell that NetFx40_PInvokeStackResilience has worked and the marshaling layer is doing its job?

PS I am using Visual Studio 2010 Express

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T18:30:08+00:00Added an answer on June 1, 2026 at 6:30 pm

    An imbalanced stack is a nasty kind of runtime problem. You tend to get away with it in the Debug build because the way a method sets up and tears down the stack frame can hide the problem with the incorrect stack pointer value. The stack pointer is restored at method exit. You’ll have fewer odds for that kind of luck in the Release build. Particularly due to method inlining, it removes the stack setup/teardown code.

    The NetFx40_PInvokeStackResilience element was needed in .NET 4 because they optimized the pinvoke marshaller. Which can break bad code that worked in earlier releases, adding the element disables that optimization. It has a global effect on the entire program. It was not meant to disable the MDA, that would be hiding a very real problem. It merely gives IT staff a way to work around a compatibility problem.

    You can lower the risk by putting the pinvoke call into a separate method that does nothing but make the call. You must decorate it with [MethodImpl(MethodImplOptions.NoInlining)] so the optimizer can’t inline it. That helper method cannot have any out or ref arguments and must not return a value of a struct type and should not have any additional code after the call.

    Clearly you’ll want to invest as much time as possible to really fix this problem. There’s somebody out there that can help you with the original C declaration, we can’t help you find him however. Not posting the pinvoke declaration also removes any odds of somebody having a guess at it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use the JSONKit library and have included the source files
I have a .NET library that I'm trying to use via COM (hMailServer's VBScript
I have a library I am trying to use. To install it I need
I have a problem. I'm trying to use urllib library in python. but, I
I have been trying to create an XML using the simplexml library (v2.6.2) http://simple.sourceforge.net/home.php
I am trying to access database from a Class Library Project. I have connection
I've spent two days on this and have gotten nowhere. I'm trying to use
I'm trying to use an external library (wingraph) in a simple program. I have
I'm trying to use this library : http://pastebin.com/xgPXpGtw (an example of use: http://pastebin.com/fNFAW3Fh )
I have a Class library application in which I'm trying to use SSL connection.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.