Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A call to PInvoke function has unbalanced the stack. This is likely because the managed PInvoke .. (.NET 4)

Tags:

My project run successful without errors in .NET Frame work 3.5. But, When I target it to .NET Frame work 4. I got the error:

"A call to PInvoke function has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature."

I used unmanaged library as below:

[StructLayout(LayoutKind.Sequential )] public class DGNElemCore {     public int offset;     public int size;     public int element_id;     public int stype;               public int level;     public int type;     public int complex;     public int deleted;     public int graphic_group;     public int properties;     public int color;     public int weight;     public int style;     public int attr_bytes;            public IntPtr attr_data;       public int raw_bytes;     public IntPtr raw_data;                   }  [DllImport("DgnLib.dll", EntryPoint = "DGNOpen")]            public static extern IntPtr  DGNOpen(string fileName, int bUpdate) 

Do you know how to fix this error ?

like image 997
taibc Avatar asked Dec 04 '13 10:12

taibc


People also ask

Why does a call to PInvoke function'samplemethod'have unbalanced stack?

A call to PInvoke function 'SampleMethod' 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.

Why can't I call PInvoke from the target?

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 unmanged signature. Seems like they're trying to tell me what the problem is but I'm not a knowledgable enough programmer to understand the message.

Why is PInvoke not working with unmanaged signatures?

Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

Why do unmanaged functions get unbalanced on the stack?

Try explicitly specifying the calling convention on both the managed and unmanaged sides. It is also possible, although not as likely, that the unmanaged function unbalanced the stack for some other reason, such as a bug in the unmanaged compiler. Effect on the Runtime


1 Answers

Add this along with dll import.

, CallingConvention = CallingConvention.Cdecl)] 

As taken from here.

Platform invoke

To improve performance in interoperability with unmanaged code, incorrect calling conventions in a platform invoke now cause the application to fail. In previous versions, the marshaling layer resolved these errors up the stack.

Debugging your applications in Microsoft Visual Studio 2010 will alert you to these errors so you can correct them. If you have binaries that cannot be updated, you can include the element in your application's configuration file to enable calling errors to be resolved up the stack as in earlier versions. However, this may affect the performance of your application.

like image 117
Ehsan Avatar answered Oct 03 '22 10:10

Ehsan