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:
typedef void ( CALLBACK* CB_DOWNLOADING )
(
	ULONG,    // secs elapsed
	LPARAM,   // custom callback param
	LPBOOL    // abort?
);


FETCH_API HttpFetchW
(
	LPCWSTR         url,
	IStream**       retval,
	LPCWSTR         usrname  = NULL,
	LPCWSTR         pwd      = NULL,
	BOOL            unzip    = TRUE,
	CB_DOWNLOADING  cb       = NULL,
	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
        (
            [MarshalAs(UnmanagedType.LPWStr)]
            string url,
            out System.Runtime.InteropServices.ComTypes.IStream retval,
            [MarshalAs(UnmanagedType.LPWStr)]
            string usrname,
            [MarshalAs(UnmanagedType.LPWStr)]
            string pwd,
            bool unzip,
            dlgDownloadingCB cb,
            IntPtr cb_param,
            [MarshalAs(UnmanagedType.LPWStr)]
            string ctype,
            ulong ctypelen
        );

Where FETCH is an enum.

[UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)]
        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.
BMaadaraniAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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".
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BMaadaraniAuthor Commented:
Thank you. That suggestion fixed the issue.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Lotus IBM

From novice to tech pro — start learning today.