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

Xiaofeng Z xf10036 at hotmail.com
Thu May 19 15:33:04 EDT 2011


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: susana.garcia at oerc.ox.ac.uk
> To: insight-users at itk.org
> 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
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20110519/5890f7c3/attachment.htm>


More information about the Insight-users mailing list