[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:01:03 EDT 2011


HI Dan,

Many thanks for your prompt response! As a requirement for my project I need to use VS2010. Thanks for the tip on the strings, as I thought it was a problem marshalling data I tried all the possible combinations.

Susana

From: Dan Mueller [via ITK Insight Users] [mailto:ml-node+6382794-1040629777-317753 at n2.nabble.com]
Sent: 19 May 2011 17:40
To: Susana Avila Garcia
Subject: Re: PInvokeStackImbalance error when calling from VS2010 an ITK function defined in a dll.

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 <[hidden email]</user/SendEmail.jtp?type=node&node=6382794&i=0>> 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
_____________________________________
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-tp6382376p6382794.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-tp6382376p6386849.html
Sent from the ITK Insight Users mailing list archive at Nabble.com.


More information about the Insight-users mailing list