• Advertisement
Sign in to follow this  

[.net] PInvokeStackImbalance - How to solve this?

This topic is 4393 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm working on a card game in C#, and I figured, why not use the cards.dll for the drawing. So I wrote a small wrapper class around it, and it worked(Under .NET 1.1). So I proceeded to port most of the application to 2.0, and get it networked using Remoting. However, when I start drawing, I get a PInvokeStackImbalance exception: PInvokeStackImbalance was detected Message: A call to PInvoke function 'ZweedsPesten!ZweedsPesten.CardsDll::cdtDraw' 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. As the class worked under .NET 1.1, I took an example from Programmers Heaven using the DLL aswell, and it crashed aswell. I have no idea what the problem is. According to the MSDN, it's a stack corruption, which seems odd to me as it used to work. My code can be found here: http://upload.toolmaker.nl/CardsDll1.cs Toolmaker

Share this post


Link to post
Share on other sites
Advertisement
I fixed the problem myself. Shilbert explained to me nicely that .net long values are 64-bit, so I had to use int's.

Toolmaker

Share this post


Link to post
Share on other sites
Hi

I basically have the samme problem. I have some cards.dll code in VS.NET 2003 which works fine but when I run it in VS.NET 2005 it crashes with the same error. PInvoke has imbalanced the stack.

How did you fix it?

I figure its this line of code that I need to change but I dont know what to chance it to!

cdtDraw(hdc, 0, 0, CardValue(), 0, &HFFFFFF&)

Can you help me a little on this?

Using VB.NET

Share this post


Link to post
Share on other sites
Oh no problem. I frekin fixed it.

Just as Id hit post I thought of looking at my <DllImport("cards.dll") statement and changed Long to Integer and it works perfectly now.

But thank you very much for leading me on the right path

Share this post


Link to post
Share on other sites
Make sure to publish that cards game and the source (with CardsDll.cs) on your site as I'd definately love to have a look at it.

Share this post


Link to post
Share on other sites
http://upload.toolmaker.nl/CardsDll.cs

That contains a WORKING version of the CardsDll class. The problem was, that most of the parameters exported from the DLL contains long in the function header. However, .NET long's are 64-bit, and not compatible with the long exported from the DLL. Thus, you have to use int.

Toolmaker

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement