• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1741
  • Last Modified:

A call to PInvoke function has unbalanced the stack

Making a function call to .NET 4 to native code is resulting in the following exception:

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

Here is my definition in Native Code:
	ULONG,    // secs elapsed
	LPARAM,   // custom callback param
	LPBOOL    // abort?

	LPCWSTR         url,
	IStream**       retval,
	LPCWSTR         usrname  = NULL,
	LPCWSTR         pwd      = NULL,
	BOOL            unzip    = TRUE,
	LPARAM          cb_param = 0,
	LPWSTR          ctype    = NULL,
	ULONG           ctypelen = 0

Here is the .NET counterpart:

[DllImport(dllPath, CharSet = CharSet.Unicode, SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
        internal static extern FETCH HttpFetchW
            string url,
            out System.Runtime.InteropServices.ComTypes.IStream retval,
            string usrname,
            string pwd,
            bool unzip,
            dlgDownloadingCB cb,
            IntPtr cb_param,
            string ctype,
            ulong ctypelen

Where FETCH is an enum.

        internal delegate void dlgDownloadingCB(ulong elapsedSec, IntPtr lParam, bool abort);

internal static void DownloadingCB(ulong elapsedSec, IntPtr lParam, bool abort)
           // Console.WriteLine("elapsedSec = " + elapsedSec.ToString());

Can anyone suggest an alternative within the .NET 4?

Thank you very much for your help.
1 Solution
Bob LearnedCommented:
The first suspicious type is LPBOOL.  That is a long pointer to a bool, which cannot be represented by C# bool.  My best guess for that is "ref int".
BMaadaraniAuthor Commented:
Thank you. That suggestion fixed the issue.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now