Problem wrapper c++ -> C#. Problem with call method in C#

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<div style="color:Black;background-color:White; <pre>
Hello, I<span style="color:#A31515; m beginner in programming . I have to do a wrapper c++ to c#. I have a problem with call a method(form1) in C#. When I m starting with build . It s ok - good.
[/code]
<div style="color:Black;background-color:White; <pre>
But When I <span style="color:#A31515; m going to start with debuging . It doesnt compile good. I reveived a message :An unhandled exception of type <span style="color:#A31515; System.Runtime.InteropServices.MarshalDirectiveException occurred <span style="color:Blue; in WrapperLandmark4.exe
[/code]
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; using System;
<span style="color:Blue; using System.Collections.Generic;
<span style="color:Blue; using System.Linq;
<span style="color:Blue; using System.Text;
<span style="color:Blue; using System.Runtime.InteropServices;

<span style="color:Blue; namespace WrapperLandmark4
{
<span style="color:Blue; internal <span style="color:Blue; static <span style="color:Blue; class UnsafeNativeMethods
{
<span style="color:Blue; const <span style="color:Blue; string dllLocation = <span style="color:#A31515; "D:\WrapperIGSTK4\WrapperLandmark4\WrapperLandmark4\LandmarkDLL.dll";


[DllImport(dllLocation)]
<span style="color:Blue; public <span style="color:Blue; static <span style="color:Blue; extern <span style="color:Blue; double[] Rejestracja(<span style="color:Blue; double[] dane, IntPtr pWynik);

}
}
[/code]
<br/>
Hello,

Im beginner in programming .
I have to do a wrapper c++ to c# .
I have a problem with call method in C#.
could someone help me please ?
I would appreciate for any help ;)
I attach my code :
Form1.cs

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; using System;
<span style="color:Blue; using System.Collections.Generic;
<span style="color:Blue; using System.ComponentModel;
<span style="color:Blue; using System.Data;
<span style="color:Blue; using System.Drawing;
<span style="color:Blue; using System.Linq;
<span style="color:Blue; using System.Text;
<span style="color:Blue; using System.Windows.Forms;
<span style="color:Blue; using System.Runtime.InteropServices;

<span style="color:Blue; namespace WrapperLandmark4
{
<span style="color:Blue; public <span style="color:Blue; partial <span style="color:Blue; class Form1 : Form
{
<span style="color:Blue; public Form1()
{
InitializeComponent();
Run();
}


<span style="color:Blue; public <span style="color:Blue; static <span style="color:Blue; void Run()
{
<span style="color:Blue; double[] dane = <span style="color:Blue; new <span style="color:Blue; double[30];

<span style="color:Blue; #region Dane
dane[0] = 2.3;
dane[1] = 2.4;
dane[2] = 4.5;
dane[3] = 5.5;
dane[4] = 5.5;
dane[5] = 6.7;
dane[6] = 5.6;
dane[7] = 4.5;
dane[8] = 5.5;
dane[9] = 4.4;
dane[10] = 2.3;
dane[11] = 2.3;
dane[12] = 4.5;
dane[13] = 5.5;
dane[14] = 5.5;
dane[15] = 6.7;
dane[16] = 2.4;
dane[17] = 4.5;
dane[18] = 5.5;
dane[19] = 5.5;
dane[20] = 6.7;
dane[21] = 5.6;
dane[22] = 4.5;
dane[23] = 5.5;
dane[24] = 4.4;
dane[25] = 2.3;
dane[26] = 2.3;
dane[27] = 4.5;
dane[28] = 5.5;
dane[29] = 5.5;
<span style="color:Blue; #endregion Dane

<span style="color:Blue; double[] wynik = <span style="color:Blue; new <span style="color:Blue; double[16];
IntPtr pWynik = Marshal.AllocHGlobal(16 * 8);
UnsafeNativeMethods.Rejestracja(dane, pWynik); <span style="color:Green; // Problem here ??
Marshal.Copy(pWynik, wynik, 0, 16);



<span style="color:Blue; for (<span style="color:Blue; int i = 0; i < 16; i++)
{
MessageBox.Show(wynik.ToString());
}

Marshal.FreeHGlobal(pWynik);
}
}
}

[/code]

Class1.cs
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; using System;
<span style="color:Blue; using System.Collections.Generic;
<span style="color:Blue; using System.Linq;
<span style="color:Blue; using System.Text;
<span style="color:Blue; using System.Runtime.InteropServices;

<span style="color:Blue; namespace WrapperLandmark4
{
<span style="color:Blue; internal <span style="color:Blue; static <span style="color:Blue; class UnsafeNativeMethods
{
<span style="color:Blue; const <span style="color:Blue; string dllLocation = <span style="color:#A31515; "D:\WrapperIGSTK4\WrapperLandmark4\WrapperLandmark4\LandmarkDLL.dll";


[DllImport(dllLocation)]
<span style="color:Blue; public <span style="color:Blue; static <span style="color:Blue; extern <span style="color:Blue; double[] Rejestracja(<span style="color:Blue; double[] dane, IntPtr pWynik);

}
}
[/code]

In C++ :

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; extern <span style="color:#A31515; "C"
{


__declspec(<span style="color:Blue; dllexport) <span style="color:Blue; void __stdcall Rejestracja(<span style="color:Blue; double tab[30], <span style="color:Blue; double *pReturn)
{

igstk::RealTimeClock::Initialize();


<span style="color:Green; // BeginCodeSnippet
<span style="color:Blue; typedef igstk::Object::LoggerType LoggerType;
<span style="color:Blue; typedef itk::StdStreamLogOutput LogOutputType;

<span style="color:Blue; typedef igstk::Landmark3DRegistration
Landmark3DRegistrationType;
<span style="color:Blue; typedef igstk::Landmark3DRegistration::LandmarkPointContainerType
LandmarkPointContainerType;
<span style="color:Blue; typedef igstk::Landmark3DRegistration::LandmarkImagePointType
LandmarkImagePointType;
<span style="color:Blue; typedef igstk::Landmark3DRegistration::LandmarkTrackerPointType
LandmarkTrackerPointType;
<span style="color:Blue; typedef Landmark3DRegistrationType::TransformType::OutputVectorType
OutputVectorType;
<span style="color:Blue; typedef igstk::Transform TransformType;
<span style="color:Green; // EndCodeSnippet


<span style="color:Green; // BeginLatex
<span style="color:Green; //
<span style="color:Green; // Then, the registration component is instantiated as follows:
<span style="color:Green; //
<span style="color:Green; // EndLatex

<span style="color:Green; // BeginCodeSnippet
Landmark3DRegistrationType::Pointer landmarkRegister =
Landmark3DRegistrationType::New();
<span style="color:Green; // EndCodeSnippet


<span style="color:Green; // BeginLatex
<span style="color:Green; //
<span style="color:Green; // Next, the landmark containers that hold the landmark image and tracker
<span style="color:Green; // coordinates are instantiated:
<span style="color:Green; //
<span style="color:Green; // EndLatex

<span style="color:Green; // BeginCodeSnippet
LandmarkPointContainerType imagePointContainer;
LandmarkPointContainerType trackerPointContainer;
<span style="color:Green; // EndCodeSnippet


LandmarkImagePointType imagePoint;
LandmarkTrackerPointType trackerPoint;

<span style="color:Green; // BeginLatex
<span style="color:Green; //
<span style="color:Green; // Then, error event callback objects are instantiated and added to the observer
<span style="color:Green; // list of the registration component, as follows:
<span style="color:Green; //
<span style="color:Green; // EndLatex

<span style="color:Green; // BeginCodeSnippet
Landmark3DRegistrationInvalidRequestCallback::Pointer
lrcb = Landmark3DRegistrationInvalidRequestCallback::New();

<span style="color:Blue; typedef igstk::InvalidRequestErrorEvent InvalidRequestEvent;

landmarkRegister->AddObserver( InvalidRequestEvent(), lrcb );

Landmark3DRegistrationErrorCallback::Pointer ecb =
Landmark3DRegistrationErrorCallback::New();
<span style="color:Blue; typedef igstk::Landmark3DRegistration::TransformComputationFailureEvent
ComputationFailureEvent;
landmarkRegister->AddObserver( ComputationFailureEvent(), ecb );

<span style="color:Green; // EndCodeSnippet

<span style="color:Green; // BeginLatex
<span style="color:Green; //
<span style="color:Green; // A logger can then be connected to the registration component for
<span style="color:Green; // debugging purpose, as follows:
<span style="color:Green; //
<span style="color:Green; // EndLatex


<span style="color:Green; // BeginCodeSnippet
LoggerType::Pointer logger = LoggerType::New();
LogOutputType::Pointer logOutput = LogOutputType::New();
logOutput->SetStream( std::cout );
logger->AddLogOutput( logOutput );
logger->SetPriorityLevel( LoggerType::DEBUG );
landmarkRegister->SetLogger( logger );
<span style="color:Green; // EndCodeSnippet


<span style="color:Green; // BeginLatex
<span style="color:Green; //
<span style="color:Green; // Next, landmark points are added to the image and tracker containers. The
<span style="color:Green; // state machine of this registration component is designed so that the image
<span style="color:Green; // and tracker coordinates that correspond to each landmark are added
<span style="color:Green; // consecutively. This scheme prevents the mismatch in landmark correspondence
<span style="color:Green; // that could occur when all landmarks image coordinates are recorded first and
<span style="color:Green; // followed by the tracker coordinates. This design choice is consistent with
<span style="color:Green; // the ``safety by design philosophy of IGSTK. The commands are as follows:
<span style="color:Green; //
<span style="color:Green; //EndLatex

<span style="color:Green; // BeginCodeSnippet
<span style="color:Green; // Add entry
imagePoint[0] = tab[0];
imagePoint[1] = tab[1];
imagePoint[2] = tab[2];
imagePointContainer.push_back(imagePoint);
landmarkRegister->RequestAddImageLandmarkPoint(imagePoint);

trackerPoint[0] = tab[3];
trackerPoint[1] = tab[4];
trackerPoint[2] = tab[5];
trackerPointContainer.push_back(trackerPoint);
landmarkRegister->RequestAddTrackerLandmarkPoint(trackerPoint);

<span style="color:Green; // Add 1st landmark
imagePoint[0] = tab[6];
imagePoint[1] = tab[7];
imagePoint[2] = tab[8];
imagePointContainer.push_back(imagePoint);
landmarkRegister->RequestAddImageLandmarkPoint(imagePoint);

trackerPoint[0] = tab[9];
trackerPoint[1] = tab[10];
trackerPoint[2] = tab[11];
trackerPointContainer.push_back(trackerPoint);
landmarkRegister->RequestAddTrackerLandmarkPoint(trackerPoint);

<span style="color:Green; // Add 2nd landmark
imagePoint[0] = tab[12];
imagePoint[1] = tab[13];
imagePoint[2] = tab[14];
imagePointContainer.push_back(imagePoint);
landmarkRegister->RequestAddImageLandmarkPoint(imagePoint);

trackerPoint[0] = tab[15];
trackerPoint[1] = tab[16];
trackerPoint[2] = tab[17];
trackerPointContainer.push_back(trackerPoint);
landmarkRegister->RequestAddTrackerLandmarkPoint(trackerPoint);

<span style="color:Green; // EndCodeSnippet

<span style="color:Green; // BeginLatex
<span style="color:Green; //
<span style="color:Green; // More landmarks can be added for the transform computation.
<span style="color:Green; //
<span style="color:Green; // EndLatex

<span style="color:Green; // Add 3n landmark
imagePoint[0] = tab[18];
imagePoint[1] = tab[19];
imagePoint[2] = tab[20];
imagePointContainer.push_back(imagePoint);
landmarkRegister->RequestAddImageLandmarkPoint(imagePoint);

trackerPoint[0] = tab[21];
trackerPoint[1] = tab[22];
trackerPoint[2] = tab[23];
trackerPointContainer.push_back(trackerPoint);
landmarkRegister->RequestAddTrackerLandmarkPoint(trackerPoint);

<span style="color:Green; // Add target
imagePoint[0] = tab[24];
imagePoint[1] = tab[25];
imagePoint[2] = tab[26];
imagePointContainer.push_back(imagePoint);
landmarkRegister->RequestAddImageLandmarkPoint(imagePoint);

trackerPoint[0] = tab[27];
trackerPoint[1] = tab[28];
trackerPoint[2] = tab[29];
trackerPointContainer.push_back(trackerPoint);
landmarkRegister->RequestAddTrackerLandmarkPoint(trackerPoint);

<span style="color:Green; // BeginLatex
<span style="color:Green; //
<span style="color:Green; // After all landmark coordinates are added, the transform computation is
<span style="color:Green; // requested as follows:
<span style="color:Green; //
<span style="color:Green; // EndLatex

<span style="color:Green; // Calculate transform

<span style="color:Green; // BeginCodeSnippet
landmarkRegister->RequestComputeTransform();
<span style="color:Green; // EndCodeSnippet

<span style="color:Blue; typedef itk::VersorRigid3DTransform<<span style="color:Blue; double> VersorRigid3DTransformType;
<span style="color:Blue; typedef VersorRigid3DTransformType::ParametersType ParametersType;




TransformType transform;
ParametersType parameters(6);

<span style="color:Green; // BeginLatex
<span style="color:Green; //
<span style="color:Green; // To access the transform parameters, a GetTransform callback is instantiated
<span style="color:Green; // to observe the transform event, as follows:
<span style="color:Green; //
<span style="color:Green; // EndLatex

<span style="color:Green; // BeginCodeSnippet
Landmark3DRegistrationGetTransformCallback::Pointer lrtcb =
Landmark3DRegistrationGetTransformCallback::New();

landmarkRegister->AddObserver(
igstk::CoordinateSystemTransformToEvent(), lrtcb );
<span style="color:Green; //EndCodeSnippet


<span style="color:Green; // BeginLatex
<span style="color:Green; //
<span style="color:Green; // To request that the registration component throw an event loaded with
<span style="color:Green; // transform parameters, a code{RequestGetTransform} function is invoked as
<span style="color:Green; // follows:
<span style="color:Green; //
<span style="color:Green; // EndLatex

<span style="color:Green; // BeginCodeSnippet
landmarkRegister->RequestGetTransformFromTrackerToImage();
std::cout<<<span style="color:#A31515; " "<<std::endl;
std::cout << <span style="color:#A31515; "Transform " << transform << std::endl;


<span style="color:Green; //double pReturn[16];


<span style="color:Blue; if(lrtcb->GetEventReceived())
{
transform = lrtcb->GetTransform();
vtkMatrix4x4 * t = vtkMatrix4x4::New();;
transform.ExportTransform( * t );

<span style="color:Blue; for ( <span style="color:Blue; int i = 0; i < 3; i++ )
{
<span style="color:Blue; for ( <span style="color:Blue; int j = 0; j < 3; j++ )
{
pReturn[4*i+j] = t->GetElement(i,j);
}
}

t->Delete();

}

}




}

[/code]

<br/>
Could Someone help me please ?
I would appreciate for any help.


Best,
wiatrak111@

View the full article
 
Back
Top