hello,

how i can using a C + + class or library in a C # code

I don’t think there’s a way to do this directly in C#, as they are 2 different environments (unmanaged and managed) and compilers.

However GHI allows you to call “Runtime Lodable Procedures” or RLP from C#. They are C++ compiled code libraries that you can call from .NETMF:

https://www.ghielectronics.com/docs/search?q=rlp

There’s also a section on this forum that covers RLP:

https://www.ghielectronics.com/community/forum/board?id=17

Also, you can look at the Codeshare for examples:

https://www.ghielectronics.com/community/codeshare/search?q=rlp

You can also consider to port the C++ code to C#.

Depending on the kind of code/library you have in C++ every solution might be the right one.

Could you share more info about your C++ code/library you want to use.

I have a class matrice I want to add :

the header and source code

1 //-------------source code ----------matrice.cpp----------------------------------------------------

#include “stdafx.h”

//-------------------------------------------------------------

matrice::matrice(unsigned short n,unsigned short m)

{

this->n = n;

this->m = m;

T = new float*[n];

for(int i = 0;i<n;i++)

{

T[i] = new float[m];

for(int j = 0; j<m ; j++)

T[i][j] = 0;

}

}

matrice::~matrice()

{

for(int i = 0; i<n ; i++)

delete T[i];

delete T;

}

matrice::matrice()

{

n = 0; m = 0; T = NULL;

}

matrice::matrice(const matrice& R)

{

this->n = R.n;

this->m = R.m;

T = new float*[n];

for(int i = 0;i<n;i++)

{

T[i] = new float[m];

for(int j = 0; j<m ; j++)

T[i][j] = R.T[i][j];

} //*/
// printf(“constructeur de copier\n”);
}
matrice & matrice::operator=(const matrice& R)
{
if (T!=NULL)
{
for(int i = 0; i<n ; i++)
delete T[i];
delete T;
}
n = R.n;
m = R.m;
T = new float*[n];

for(int i = 0;i<n;i++)

{

T[i] = new float[m];

for(int j = 0; j<m ; j++)

T[i][j] = R.T[i][j];

} //*/

// printf(“operateur =\n”);

return (*this);

}

matrice & matrice::operator==(const matrice& R)

{

```
n = R.n;
m = R.m;
for(int i = 0;i<n;i++)
{
for(int j = 0; j<m ; j++)
T[i][j] = R.T[i][j];
} //*/
```

// printf(“operateur =\n”);

return (*this);

}

float &matrice::operator()(unsigned short int i,unsigned short int j)

{

```
if(i<n && j<m)
return T[i][j];
```

}

matrice matrice::transposee()

{

matrice resultat(m,n);

for (int i=0; i<m; i++)

for (int j=0; j<n; j++)

resultat(i,j)=T[j][i];

```
return resultat;
```

}

void matrice::affiche()

{

for (unsigned short i=0;i<n;i++){

for (unsigned short j=0;j<m;j++){

cout<<T[i][j]<<’ ';}

cout<<endl;}

}

matrice matrice::operator+(matrice const& B)

{

if((n == B.n)||(m == B.m))

{

matrice S(n,m);

for(int i = 0;i<n;i++)

for(int j = 0;j<m;j++)

S.T[i][j] = T[i][j] + B.T[i][j];

return S;

}

}

matrice& matrice::operator+=(matrice const& B)

{

if((n == B.n)||(m == B.m))

{

//matrice C(n,m);

for(int i = 0;i<n;i++)

for(int j = 0;j<m;j++)

T[i][j] = T[i][j] + B.T[i][j];

return (*this);

}

};

matrice matrice::operator-(matrice const& B)

{

if((n == B.n)||(m == B.m))

{

matrice D(n,m);

for(int i = 0;i<n;i++)

for(int j = 0;j<m;j++)

D.T[i][j] = T[i][j] - B.T[i][j];

return D;

}

}

//

//matrice& matrice::operator *(float a)

//{

// //matrice reslt(n,m);

// unsigned short i,j;

//

// for(i=0;i<n;i++)

// {

// for(int j=0;j<n;j++)

// {

// //reslt(i,j)=T[i][j]*a;

// T[i][j]=T[i][j]*a;

// }

// }

// return (*this);

//}

//

//matrice& matrice::operator -(float a)

//{

// //matrice reslt(n,m);

// unsigned short i,j;

//

// for(i=0;i<n;i++)

// {

// for( j=0;j<n;j++)

// {

// //reslt(i,j)=T[i][j]-a;

// T[i][j]=T[i][j]-a;

// }

// }

// return (*this);

//}

//

//matrice& matrice::operator +(float a)

//{

// //matrice reslt(n,m);

// unsigned short i,j;

//

// for(i=0;i<n;i++)

// {

// for(j=0;j<n;j++)

// {

// //reslt(i,j)=T[i][j]+a;

// T[i][j]=T[i][j]+a;

// }

// }

// return (*this);

//}

matrice& matrice::operator-=(matrice const& B)

{

if((n == B.n)||(m == B.m)){

// matrice C(n,m);

for(int i = 0;i<n;i++)

for(int j = 0;j<m;j++)

T[i][j] = T[i][j] - B.T[i][j];

return (*this);

}

}

matrice matrice::operator*(matrice const& B)

{

if(m == B.n){

matrice C(n,B.m);// = new matrice(n,B.m);

for(int i = 0; i<n;i++){

for(int j = 0;j<B.m;j++){

float s = 0;

for(int k = 0; k<m;k++){

s = s + T[i][k] * B.T[k][j];

}

C.T[i][j] = s;

}

}

return ©;

}

}

matrice matrice::invQR()

{

if( n == m ){

int k,i,j;

matrice I(n,n);

float **Q, **R , **M, **IR;

float s,p,a;

```
// copier la matrice dans M
Q = new float*[n];
R = new float*[n];
IR = new float*[n];
M = new float*[n];
for(i = 0; i<n ;i++ ){
Q[i] = new float[n];
R[i] = new float[n];
M[i] = new float[n];
IR[i] = new float[n];
for(j = 0; j<n ;j++ ){
Q[i][j] = 0;
R[i][j] = 0;
IR[i][j] = 0;
M[i][j] = T[i][j];
}
IR[i][i] = 1;
}
/////////////////QR/////////////////////////7
for(k = 0; k < n;k++){
//R[k][k] = norm(M(:,k))
s = 0;
for(i = 0; i<n ;i++ )
s = s + (M[i][k] * M[i][k]);
R[k][k] = sqrt(s);
// Q(:,k) = M(:,k) / R(k,k);
for(i = 0; i<n ;i++ )
Q[i][k] = M[i][k] / R[k][k];
//for i = k+1:n
for(i = k+1; i<n ;i++ ){
//R(k,i) = (M(:,i)') * Q(:,k);
s = 0;
for(j = 0; j<n ;j++)
s = s + M[j][i] * Q[j][k];
R[k][i] = s;
for(j = 0; j<n ;j++)
M[j][i] = M[j][i] - R[k][i] * Q[j][k];
}
}
//////////////////////////IR = inverse de R
for(k = n-1; k>=0 ; k--){
p = 1/R[k][k];
for( j=k;j<n;j++ )
IR[k][j] = p * IR[k][j];
for(i = 0; i<k ;i++){
a = R[i][k];
for( j=k;j<n;j++ )
IR[i][j] = IR[i][j] - a * IR[k][j];
}
}
for(i = 0; i<n ; i++)
for(j = 0; j<n ; j++) // I.T[i][j] = IR[i][j] ;
for(k = 0; k<n ; k++)
I.T[i][j] += IR[i][k] * Q[j][k]; //
for(i = 0; i<n ;i++ ){
delete Q[i];
delete R[i];
delete M[i];
delete IR[i];
//**Q, **R , **M, **IR;
}
delete Q;
delete R;
delete M;
delete IR;
return I;
}
```

}

unsigned short int &matrice::GetN(){

return n;}

unsigned short int &matrice::GetM(){

return m;}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////// INVERSE LU /////////////////////////////////////////

matrice matrice::invLU()

{

if(n == m)

{

matrice I(n,n);

int k,i,j;

float **M,p,a;

```
M = new float*[n ];
for(k = 0 ; k<n ; k++)
{
M[k] = new float[n];
I.T[k][k] = 1;
for( j=0 ; j<n ; j++ )
M[k][j] = T[k][j];
}
for( k=0 ; k<n-1 ; k++)
{
p = M[k][k];
for( i=k+1 ; i<n ;i++ )
{
a = M[i][k]/p;
for( j=0 ; j<=k ; j++ )
I.T[i][j] = I.T[i][j] - a * I.T[k][j];
for( j=k ; j<n ; j++ )
M[i][j] = M[i][j] - a * M[k][j];
}
}
for( k=n-1 ; k>=0 ;k-- )
{
p = M[k][k];
for( j=0 ; j<n ; j++ )
I.T[k][j] = I.T[k][j]/p;
for( i=0 ; i<k ;i++ )
{
a = M[i][k];
for( j=0 ; j<n ; j++)
I.T[i][j] = I.T[i][j] - a * I.T[k][j];
}
}
////// delete M;
for(i = 0; i<n ;i++ )
{
delete M[i];
//**Q, **R , **M, **IR;
}
delete M;
return I;
}
```

}

```
matrice mult(matrice A,matrice B,unsigned short n,unsigned short m)
{
matrice Result (n,m);
float s;
unsigned short i,j,k;
for(i = 0; i<n;i++)
{
for(j = 0;j<m;j++)
{
s = 0;
for(k = 0;k<m;k++)
{
s = s + A(i,k)*B(k,j);
}
Result(i,j)= s;
}
}
return (Result);
}
```

2///// the header

// matrice.h

ifndef matriceH

#define matriceH

```
class matrice
{
public:
unsigned short n,m;
float **T;
matrice(unsigned short int n,unsigned short int m );
matrice(const matrice& R);
~matrice();
matrice();
float &operator()(unsigned short int i,unsigned short int j);
void affiche();
matrice transposee();
unsigned short int &GetN();
unsigned short int &GetM();
matrice operator+(matrice const& B);
matrice& operator+=(matrice const& B);
matrice operator-(matrice const& B);
//matrice& operator *(float a);
//matrice& operator -(float a);
//matrice& operator +(float a);
matrice& operator-=(matrice const& B);
matrice operator*(matrice const& B);
matrice& operator=(const matrice& R);
matrice& operator==(const matrice& R);
matrice invQR();
matrice invLU();
};
matrice mult(matrice A,matrice B,unsigned short n,unsigned short m);
```

#endif

sorry the code is big and I do not know how to paste the code in the forum

sorry

I would do one of the following two:

- Port this code to C#
- Look on codeplex.com and/or codeproject.com for a similar class that is already in C# (shouldn’t be so hard to find).

@ riad1986 - The code is portable to C# mostly, however you will not have access to pointers unless the code is marked unsafe as well as you will need to rework logic revolving around the multidimensional arrays as they are not supported.

I don’t believe MF officially supports unsafe code? ???

@ Mike - Actually you are correct. I have never run into a situation where I’ve needed to attempt to use unsafe outside of the full .NET Framework. My apologies.

Excerpt: “The platform does not support symmetric multiprocessing, multi-dimensional arrays, machine-dependent types, or unsafe instructions.”

Officially not, but I successfully used it to convert double to byte array on EMX and G120.

But in this case I would use some not unsafe code (or safe code, so to say).

In fact I would look for an matrix implementation that is already C#

Mike: James:@ riad1986 - The code is portable to C# mostly, however you will not have access to pointers unless the code is marked unsafe as well as you will need to rework logic revolving around the multidimensional arrays as they are not supported.

I don’t believe MF officially supports unsafe code? ???

Officially not, but I successfully used it to convert double to byte array on EMX and G120.

But in this case I would use some not unsafe code (or safe code, so to say).

In fact I would look for an matrix implementation that is already C#

I would be concerned using unsafe code. To use a pointer, the fixed statement also needs to be implemented. This is required to “pin” a managed object. Without fixed, GC could cause a pointer to go bad.

hllow

is that if I converted the class in c + +. DDL I can add it as an assembly and I use there after for C # project NET.MF

hllow

is that if I converted the class c++ at ( c + +. DDL).

can I add it as an assembly and I use there after for C # project NET.MF ?

You can not build an c++ .net assembly for netmf.

Only C# and VB.net are supported.

You need to rewrite the code in C#

thank you

I have an algorithm in C # without use the pointer but I fall into an other problem is that the processing time is slow.

is that there is a way reduce the time of calcule

You can implement the complex stuff using RLP(lite).

This is a possibility by GHI devices to execute native code, written in C.

You can find several sample projects in Codeshare.

This one:

https://www.ghielectronics.com/community/codeshare/entry/843

was made by me, runs on G120 and uses digital input and interrupts.

But for your sample you just need to pass parameters and return values.

@ Reinhard Ostermeier -

hello

I have no after including this article, is that using C + +. DDL as an assembly?

thank you

@ Reinhard Ostermeier -

hello

I have no after including this article:

http://sdt.bz/content/article.aspx?ArticleID=32341&page=1

is that using C + +. DDL as an assembly?

thank you

hello

I not understand after this article.

it is canusing C + +. DDL as an assembly?

thank you

Fore some boards you can build your own NETMF firmware.

There you can add what you want.

Never done this, have no idea how.

But many others on this forum have. (at least for some SoM’s)