[Insight-users] Problem Wrapper ITK to C#

Dan Mueller dan.muel at gmail.com
Wed Apr 11 20:50:34 EDT 2012


Hi,

Having a look at the code does not reveal any obvious issues.

I never wrap C++ code for C# by hand, but always use SWIG:
    http://www.swig.org/

The code generated by SWIG seems to be essentially identical to your
code (see below).

I suspect the issue is with the naming of the native dll. Are you sure
the name of the native dll is "ProgramITK.dll"? And this dll is in the
binary directory of the C# program you are executing? Try adding the
EntryPoint parameter to the DllImport attribute as demonstrated in the
SWIG generated code below.

Sorry I could not be of more help.

Regards, Dan

> swig -c++ -csharp -v NativeMethods.i

/* File : NativeMethods.i */
%include "std_string.i" // Required for std::string
%module "NativeMethods"
%{
#include "NativeMethods.h"
%}
%include "NativeMethods.h"

/* File : NativeMethods.h */
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
void KonwersjaPliku(std::string plikWE, std::string plikWY)
{
  typedef signed short PixelType;
  const unsigned int Dimension = 3;
  typedef itk::Image< PixelType, Dimension > ImageType;
  typedef itk::ImageFileReader< ImageType > ReaderType;
  typedef itk::ImageFileWriter<ImageType> WriterType;
  // Read input image
  ReaderType::Pointer reader = ReaderType::New();
  reader->SetFileName(plikWE);
  reader->Update();
  std::cout << reader << std::endl;
  // Write output image
  WriterType::Pointer writer = WriterType::New();
  writer->SetInput(reader->GetOutput());
  writer->SetFileName(plikWY);
  writer->Update();
}

/* File : NativeMethods_wrap.cxx (generated by SWIG) */
#define SWIGEXPORT __declspec(dllexport)
#define SWIGSTDCALL __stdcall
extern "C" {
SWIGEXPORT void SWIGSTDCALL CSharp_KonwersjaPliku(char * jarg1, char * jarg2) {
  std::string arg1 ;
  std::string arg2 ;
  if (!jarg1) {
    SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException,
"null string", 0);
    return ;
  }
  (&arg1)->assign(jarg1);
  if (!jarg2) {
    SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException,
"null string", 0);
    return ;
  }
  (&arg2)->assign(jarg2);
  KonwersjaPliku(arg1,arg2);
}
}

/* File : NativeMethodsPINVOKE.cs (generated by SWIG) */
[DllImport("NativeMethods", EntryPoint="CSharp_KonwersjaPliku")]
public static extern void KonwersjaPliku(string jarg1, string jarg2);

On 10 April 2012 19:08, agatte <wiatrak11 at poczta.onet.pl> wrote:
>
> Hi All Users ;)
>
>
> I have a problem with wrapper  to c# .
> I have already created method in ITK.
> It goes good. Program in C3 build good but I can't debug this project.
> Could anyone look at my code ?
> I would appreciate for any help.
>
> During debuging I received a message :
>
> An unhandled exception of type 'System.EntryPointNotFoundException' occurred
> in WrapperITK.exe
>
>
>
>
> Code in ITK (C++) :
>
>
> #include "itkOrientedImage.h"
> #include "itkGDCMImageIO.h"
> #include "itkGDCMSeriesFileNames.h"
> #include "itkImageSeriesReader.h"
> #include "itkImageFileWriter.h"
>
> #include "itkCommand.h"
> #include "itkImage.h"
> #include "itkImageFileReader.h"
> #include "itkImageFileWriter.h"
> using namespace std;
>
> extern "C"
> {
>        __declspec(dllexport) void __stdcall KonwersjaPliku(std::string plikWE,
> std::string plikWY)
> {
>
>
>
> typedef signed short PixelType;
> const unsigned int Dimension = 3;
> typedef itk::Image< PixelType, Dimension > ImageType;
> typedef itk::ImageFileReader< ImageType > ReaderType;
>
>        // read dcm file
>        ReaderType::Pointer reader = ReaderType::New();
>        reader->SetFileName(plikWE);
>        reader->Update();
>        std::cout << reader<< std::endl;
>
>       // Save  VTK image file
>       typedef itk::ImageFileWriter<ImageType> WriterType;
>       WriterType::Pointer writerVTK = WriterType::New();
>       writerVTK->SetInput(reader->GetOutput());
>       writerVTK->SetFileName(plikWY);
>       writerVTK->Update();
>
>
> }
> }
>
>
> Code in C# :
>
> using System;
> using System.Collections.Generic;
> using System.Linq;
> using System.Text;
> using System.Runtime.InteropServices;
>
> namespace WrapperITK
> {
>
>    internal static class UnsafeNativeMethods
>    {
>        const string dllLocation = "ProgramITK.dll";
>
>        [DllImport(dllLocation)]
>        public static extern void KonwersjaPliku(string plikWE, string
> plikWY);
>
>    }
> }
>
>
> Code in C# (Form1) :
>
> using System;
> using System.Collections.Generic;
> using System.ComponentModel;
> using System.Data;
> using System.Drawing;
> using System.Linq;
> using System.Text;
> using System.Windows.Forms;
> using System.Runtime.InteropServices;
>
> namespace WrapperITK
> {
>    public partial class Form1 : Form
>    {
>        public Form1()
>        {
>            InitializeComponent();
>            Run();
>        }
>
>        public static void Run()
>        {
>            string plikDCM = "plikDCM.dcm";
>            string plikVTK = "plikVTK.vtk";
>            UnsafeNativeMethods.KonwersjaPliku(plikDCM, plikVTK);
>        }
>
>        private void Form1_Load(object sender, EventArgs e)
>        {
>
>        }
>
>
>    }
> }
>
> I would appreciate for any help ;)


More information about the Insight-users mailing list