<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Tahoma
}
--></style>
</head>
<body class='hmmessage'>
I believe the default calling convension in x86 machine is __cdecl instead of __stdcall. Try replace CallingConvention.StdCall with CallingConvention.Cdecl.<BR><BR>Xiaofeng<BR><BR><BR> <BR>
> Date: Thu, 19 May 2011 08:08:54 -0700<BR>> From: susana.garcia@oerc.ox.ac.uk<BR>> To: insight-users@itk.org<BR>> Subject: [Insight-users] PInvokeStackImbalance error when calling from VS2010 an ITK function defined in a dll.<BR>> <BR>> Hi,<BR>> <BR>> I'm working with ITK 3.20 and VS2010 in x86 machine. I created a dll with<BR>> some itk funtions as follows:<BR>> <BR>> ____________<BR>> <BR>> extern "C"<BR>> {<BR>> __declspec(dllexport) int BinaryThresholdImageFilter( char *<BR>> InputImageFile, char * OutputImageFile, char * LowerThreshold, char *<BR>> UpperThreshold, char * OutsideValue, char * InsideValue)<BR>> }<BR>> <BR>> ______________<BR>> <BR>> The call from VS 2010 C# is defined as follows: <BR>> <BR>> ________________-<BR>> <BR>> [DllImport("itkFilters.dll", CallingConvention =<BR>> CallingConvention.StdCall)]<BR>> public static extern int BinaryThresholdImageFilter(Byte[]<BR>> InputImageFile, Byte[] OutputImageFile, Byte[] LowerThreshold, Byte[]<BR>> UpperThreshold, Byte[] OutsideValue, Byte[] InsideValue);<BR>> <BR>> resultbinaryfilter = BinaryThresholdImageFilter(stbyteinputfilename,<BR>> stbyteoutputfilename, stbytelowerthreshold, stbyteupperthreshold,<BR>> stbyteoutsidevalue, stbyteindsidevalue);<BR>> <BR>> ______________<BR>> <BR>> This works with no problem at all using VS2008. But when I run this on<BR>> VS2010 I get the following error: <BR>> <BR>> ______________<BR>> <BR>> PInvokeStackImbalance was detected<BR>> Message: A call to PInvoke function<BR>> 'Test_ITKFilters!Test_ITKFilters.Program::BinaryThresholdImageFilter' has<BR>> unbalanced the stack. This is likely because the managed PInvoke signature<BR>> does not match the unmanaged target signature. Check that the calling<BR>> convention and parameters of the PInvoke signature match the target<BR>> unmanaged signature.<BR>> ________________<BR>> <BR>> I modified the code to see if the issue was about marshalling the data. I<BR>> created a very simple function within the same file with no arguments and<BR>> the code runs with no errors/warnings. But if I add a single parameter,<BR>> let's say (int x) and print it, it does print the value but it fails and<BR>> sends the same PInvoke error. <BR>> <BR>> Has anyone faced a similar issue? Suggestions are welcome and very much<BR>> appreciated. <BR>> <BR>> Thanks,<BR>> <BR>> Susana <BR>> <BR>> <BR>> --<BR>> 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<BR>> Sent from the ITK Insight Users mailing list archive at Nabble.com.<BR>> _____________________________________<BR>> Powered by www.kitware.com<BR>> <BR>> Visit other Kitware open-source projects at<BR>> http://www.kitware.com/opensource/opensource.html<BR>> <BR>> Kitware offers ITK Training Courses, for more information visit:<BR>> http://www.kitware.com/products/protraining.html<BR>> <BR>> Please keep messages on-topic and check the ITK FAQ at:<BR>> http://www.itk.org/Wiki/ITK_FAQ<BR>> <BR>> Follow this link to subscribe/unsubscribe:<BR>> http://www.itk.org/mailman/listinfo/insight-users<BR>                                            </body>
</html>