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

Dan Mueller dan.muel at gmail.com
Thu May 19 12:39:50 EDT 2011


Hi Susana,

I have not encountered the issue you report because I let SWIG do all
the work for me: http://www.swig.org/

Here is your example done using SWIG:

== header.h ==
// I slightly changed your interface,
// because I don't like passing pixel values as strings...
int BinaryThresholdImageFilter(
char* InputImageFile,
char* OutputImageFile,
double LowerThreshold,
double UpperThreshold,
double OutsideValue,
double InsideValue);

== header.i ==
%module header
%{
#include "header.h"
%}
%include "header.h"

> swig -c++ -csharp header.i

== headerPINVOKE.cs ==
[DllImport("header", EntryPoint="CSharp_BinaryThresholdImageFilter")]
public static extern int BinaryThresholdImageFilter(string jarg1,
string jarg2, double jarg3, double jarg4, double jarg5, double jarg6);

== header.cs ==
public class header {
  public static int BinaryThresholdImageFilter(string InputImageFile,
string OutputImageFile, double LowerThreshold, double UpperThreshold,
double OutsideValue, double InsideValue) {
    int ret = headerPINVOKE.BinaryThresholdImageFilter(InputImageFile,
OutputImageFile, LowerThreshold, UpperThreshold, OutsideValue,
InsideValue);
    return ret;
  }
}

== header_wrap.cxx ==
SWIGEXPORT int SWIGSTDCALL CSharp_BinaryThresholdImageFilter(char *
jarg1, char * jarg2, double jarg3, double jarg4, double jarg5, double
jarg6) {
  int jresult ;
  char *arg1 = (char *) 0 ;
  char *arg2 = (char *) 0 ;
  double arg3 ;
  double arg4 ;
  double arg5 ;
  double arg6 ;
  int result;

  arg1 = (char *)jarg1;
  arg2 = (char *)jarg2;
  arg3 = (double)jarg3;
  arg4 = (double)jarg4;
  arg5 = (double)jarg5;
  arg6 = (double)jarg6;
  result = (int)BinaryThresholdImageFilter(arg1,arg2,arg3,arg4,arg5,arg6);
  jresult = result;
  return jresult;
}

In short: you should be able to marshal the char* directly as string.

HTH

Cheers, Dan

On 19 May 2011 17:08, sus_avi <susana.garcia at oerc.ox.ac.uk> wrote:
> 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


More information about the Insight-users mailing list