[Insight-users] PInvokeStackImbalance error when calling from VS2010 an ITK function defined in a dll.

sus_avi susana.garcia at oerc.ox.ac.uk
Fri May 20 13:03:34 EDT 2011


Dear Xiaofeng,

Many thanks for your response and suggestion. I changed the calling convention and it is working now!

Cheers,

Susana

From: Xiaofeng Z [via ITK Insight Users] [mailto:ml-node+6383441-2031771742-317753 at n2.nabble.com]
Sent: 19 May 2011 20:38
To: Susana Avila Garcia
Subject: Re: PInvokeStackImbalance error when calling from VS2010 an ITK function defined in a dll.

I believe the default calling convension in x86 machine is __cdecl instead of __stdcall.  Try replace CallingConvention.StdCall with CallingConvention.Cdecl.

Xiaofeng



> Date: Thu, 19 May 2011 08:08:54 -0700

> From: [hidden email]</user/SendEmail.jtp?type=node&node=6383441&i=0>
> To: [hidden email]</user/SendEmail.jtp?type=node&node=6383441&i=1>
> Subject: [Insight-users] PInvokeStackImbalance error when calling from VS2010 an ITK function defined in a dll.
>
> Hi,
>
> I'm working with ITK 3.20 and VS2010 in x86 machine. I created a dll with
> some itk funtions as follows:
>
> ____________
>
> extern "C"
> {
> __declspec(dllexport) int BinaryThresholdImageFilter( char *
> InputImageFile, char * OutputImageFile, char * LowerThreshold, char *
> UpperThreshold, char * OutsideValue, char * InsideValue)
> }
>
> ______________
>
> The call from VS 2010 C# is defined as follows:
>
> ________________-
>
> [DllImport("itkFilters.dll", CallingConvention =
> CallingConvention.StdCall)]
> public static extern int BinaryThresholdImageFilter(Byte[]
> InputImageFile, Byte[] OutputImageFile, Byte[] LowerThreshold, Byte[]
> UpperThreshold, Byte[] OutsideValue, Byte[] InsideValue);
>
> resultbinaryfilter = BinaryThresholdImageFilter(stbyteinputfilename,
> stbyteoutputfilename, stbytelowerthreshold, stbyteupperthreshold,
> stbyteoutsidevalue, stbyteindsidevalue);
>
> ______________
>
> This works with no problem at all using VS2008. But when I run this on
> VS2010 I get the following error:
>
> ______________
>
> PInvokeStackImbalance was detected
> Message: A call to PInvoke function
> 'Test_ITKFilters!Test_ITKFilters.Program::BinaryThresholdImageFilter' 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.
> ________________
>
> I modified the code to see if the issue was about marshalling the data. I
> created a very simple function within the same file with no arguments and
> the code runs with no errors/warnings. But if I add a single parameter,
> let's say (int x) and print it, it does print the value but it fails and
> sends the same PInvoke error.
>
> Has anyone faced a similar issue? Suggestions are welcome and very much
> appreciated.
>
> Thanks,
>
> Susana
>
>
> --
> View this message in context: http://itk-insight-users.2283740.n2.nabble.com/PInvokeStackImbalance-error-when-calling-from-VS2010-an-ITK-function-defined-in-a-dll-tp6382376p6382376.html
> Sent from the ITK Insight Users mailing list archive at Nabble.com.
> _____________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Kitware offers ITK Training Courses, for more information visit:
> http://www.kitware.com/products/protraining.html
>
> Please keep messages on-topic and check the ITK FAQ at:
> http://www.itk.org/Wiki/ITK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.itk.org/mailman/listinfo/insight-users

_____________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html

Kitware offers ITK Training Courses, for more information visit:
http://www.kitware.com/products/protraining.html

Please keep messages on-topic and check the ITK FAQ at:
http://www.itk.org/Wiki/ITK_FAQ

Follow this link to subscribe/unsubscribe:
http://www.itk.org/mailman/listinfo/insight-users

________________________________
If you reply to this email, your message will be added to the discussion below:
http://itk-insight-users.2283740.n2.nabble.com/PInvokeStackImbalance-error-when-calling-from-VS2010-an-ITK-function-defined-in-a-dll-tp6382376p6383441.html
To unsubscribe from PInvokeStackImbalance error when calling from VS2010 an ITK function defined in a dll., click here<http://itk-insight-users.2283740.n2.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=6382376&code=c3VzYW5hLmdhcmNpYUBvZXJjLm94LmFjLnVrfDYzODIzNzZ8MTcxOTM3MzI1OA==>.


--
View this message in context: http://itk-insight-users.2283740.n2.nabble.com/PInvokeStackImbalance-error-when-calling-from-VS2010-an-ITK-function-defined-in-a-dll-tp6382376p6386859.html
Sent from the ITK Insight Users mailing list archive at Nabble.com.


More information about the Insight-users mailing list