Run C++ code in C#.NET

  • Thread starter Thread starter Curious
  • Start date Start date
C

Curious

A business associate told me that I should be able to run C++ code in
C#.NET environment because C++ and C# belong to the same family.

He sent me the code below and asked me to run in C#.NET environment. I
got numerous compiling errors because #include stuff don't exist in
C#! Could anyone tell me if the code below should run in C#.NET?

-------------------------------------------------------------------------------------------------------------------------------

//#define NO_NEWIO

#include <iostream>

/// Include for CONNECT/C++
#include "stdafx.h"
#include "stdafx.cpp"
//#include "sconnect.h"

#include "c:\\data\\Splus61 Includes\\EasyEngineConnect.h"
#include "c:\\data\\Splus61 Includes\\EasyEngineConnect.cpp"

#include "c:\\data\\Splus61 Includes\\EasyCharacter.h"
//#include "c:\\data\\Splus61 Includes\\EasyCharacter.cpp"

#include "c:\\data\\Splus61 Includes\\EasyMatrix.h"
//#include "c:\\data\\Splus61 Includes\\EasyMatrix.cpp"

#include "c:\\data\\Splus61 Includes\\EasyNumeric.h"
//#include "c:\\data\\Splus61 Includes\\EasyNumeric.cpp"



using namespace std;

int main(int argc, char* argv[])
{

// Connect to S+
EasyEngineConnect easyconnect_(argc, argv);
/// Create matrix 9x9 elem.
//EasyMatrix x;
//x.Create("matrix(1:9, nrow=9, ncol=9)", "x");
// Create matrix 9x9 elem.
//EasyMatrix y;
//y.Create("matrix(1:9, nrow=9, ncol=9)", "y");

CSPobject Y;
if (!Y.Create("vector(\"double\",10.6)"))
{
cout << "Can not create vector." << endl;
return 1;
}

CSPobject X1;
if (!X1.Create("vector(\"double\",8.6)"))
{
cout << "Can not create vector." << endl;
return 1;
}
CSPobject X2;
if (!X2.Create("vector(\"double\",3.4)"))
{
cout << "Can not create vector." << endl;
return 1;
}

/// Connect to S+
// CSPengineConnect engineConnect(argc, argv); //EasyEngineConnect
easyconnect_(argc, argv);

/// Create matrix 9x9 elem.
CSPmatrix x;
x.Create("matrix(1:9, nrow=9, ncol=9)", "x");

CSPnumeric sx;
sx.Create("1:10","x");
CSPnumeric sy = sx * sx;
sy.Assign("y");
// engineConnect.SyncParseEval("z<-lm(y~x)");





/*
CSPengineConnect engineConnect(argc, argv);

CSPobject x;
/// Create matrix 24x400 elem.
x.Create("matrix(1:223, nrow=24, ncol=400)");
//x.Create("matrix(1.2, nrow=, ncol=10)");
CSPobject y;
/// Create matrix 24x400 elem.
y.Create("matrix(231:1, nrow=24, ncol=400)");
/// Assign.
x.Assign("x");
y.Assign("y");
/// Call S+ regression "z<-x+y".
int iSuccess = engineConnect.SyncParseEval("z<-x+y");
/// Error.
if (iSuccess == 0)
{
cout << "Error, Can not call \"z<-(x+y)\"" << endl;
return 1;
}
CSPmatrix z(engineConnect.get("z"));
if (!z.IsValid())
{
cout << "Error, Can not search object z" << endl;
}
/// Print.
for (int i = 1; i <= 5;i++); //z.GetNRow(); ++i)
{
for (int j = 1; j <= z.GetNCol(); ++j)
{
cout << (double)z(i, j) << " ";
}
cout << endl;
}




/*
//Create the connection to S-PLUS
CSPengineConnect engineConnect(argc, argv);
/// Create vector 24.
CSPobject A_vector;
if (!A_vector.Create("vector(\"double\",10)"))
{
cout << "Can not create vector." << endl;
return 1;
}
CSPobject B_matrix;
if (!B_matrix.Create("matrix(1.2, nrow=10, ncol=10)"))
{
cout << "Can not create matrix." << endl;
return 1;
}
/// C_matrix = B_matrix + A_vector.
CSPmatrix C_matrix(B_matrix + A_vector) ;
if (C_matrix == NULL)
{
cout << "Can not create matrix." << endl;
}
*/

/*
CSPnumeric sx;
sx.Create("1:10","x");
CSPnumeric sy = sx * sx;
sy.Assign("y");
engineConnect.SyncParseEval("z<-lm(y~x)");

*/

/*
/// Print C_matrix.
cout << "Print C_matrix = B_matrix + A_vector." << endl << endl;
for (int i = 1; i <= C_matrix.nrow(); ++i)
{
for (int j = 1; j <= C_matrix.ncol(); ++j)
{
cout << (double)C_matrix(i,j) << " "; //< Out to console.
}
cout << endl;
}

cout << endl << endl;

getchar();

/// Call procedure S+.

CSPcharacter returnObj;
/// Create a integer vector with one element
/// representing one argument to pass to function 'objects'
CSPinteger args("1");
/// Create a CSPcall object
CSPcall sCall;
/// Call function S+ (objects()).
cout << "Call function S+ (objects(1))...." << endl << endl;
sCall.Create("objects", args);
/// Call.
returnObj = sCall.Eval(); // objects(1)
if (returnObj == NULL)
{
cout << "Function objects S+ failed" << endl;
return 1;
}

/// Get quantity.
int quantity = returnObj.length();
cout << "Quantity objects : " << quantity << endl;
/// Print objects.
for (i = 0; i < quantity; ++i)
{
cout << " [ " << i+1 << " ] " << (char *)returnObj << endl;
}
*/

/*
//Create the connection to S-PLUS
g_engineConnect.Create( argc, argv);

//Create S object with name "x" in the current database.
//Same as x<-1:10 at the command line.
CSPnumeric sx;
sx.Create("1:10","x");

//Squaring sx, which is the same as S expression sy <- x*x in a local
frame,
//but here we set it to local C++ variable sy.

CSPnumeric sy = sx * sx;

// Assign the result as S object with name "y" in the current
database.
sy.Assign("y");

//Evaluate z<-lm(y~x)
g_engineConnect.SyncParseEval("z<-lm(y~x)");

maybe ------------------- CSPobject Eval ("z<-lm(y~x)");


//second example
CSPevaluator s;

CSPcharacter message("'hello'");
message.Print();

//CSPmatrix M("matrix(1:4,rnow=2)");
//M.Print();
*/

printf("Normal Termination\n");
return 0;
}
 
Curious said:
A business associate told me that I should be able to run C++ code in
C#.NET environment because C++ and C# belong to the same family.

He sent me the code below and asked me to run in C#.NET environment. I
got numerous compiling errors because #include stuff don't exist in
C#! Could anyone tell me if the code below should run in C#.NET?

Just because C++ has curly braces does not mean the C# compiler understands
its constructs.

Try this to compile:
http://www.microsoft.com/express/vc/

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://feeds.feedburner.com/GregoryBeamer#

or just read it:
http://feeds.feedburner.com/GregoryBeamer

********************************************
| Think outside the box! |
********************************************
 
Only the most trivial example methods will compile in both C# and C++. And
this is looking only at individual methods. Entire applications will never
compile in both. In general, C# will not be able to compile C++ code and C++
will not be able to compile C# code. The same will be true for any pair of
languages you choose.
--
http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
VB & C# to Java
Java to VB & C#
Instant C#: VB to C#
Instant VB: C# to VB
Instant C++: VB, C#, or Java to C++/CLI


Curious said:
A business associate told me that I should be able to run C++ code in
C#.NET environment because C++ and C# belong to the same family.

He sent me the code below and asked me to run in C#.NET environment. I
got numerous compiling errors because #include stuff don't exist in
C#! Could anyone tell me if the code below should run in C#.NET?

-------------------------------------------------------------------------------------------------------------------------------

//#define NO_NEWIO

#include <iostream>

/// Include for CONNECT/C++
#include "stdafx.h"
#include "stdafx.cpp"
//#include "sconnect.h"

#include "c:\\data\\Splus61 Includes\\EasyEngineConnect.h"
#include "c:\\data\\Splus61 Includes\\EasyEngineConnect.cpp"

#include "c:\\data\\Splus61 Includes\\EasyCharacter.h"
//#include "c:\\data\\Splus61 Includes\\EasyCharacter.cpp"

#include "c:\\data\\Splus61 Includes\\EasyMatrix.h"
//#include "c:\\data\\Splus61 Includes\\EasyMatrix.cpp"

#include "c:\\data\\Splus61 Includes\\EasyNumeric.h"
//#include "c:\\data\\Splus61 Includes\\EasyNumeric.cpp"



using namespace std;

int main(int argc, char* argv[])
{

// Connect to S+
EasyEngineConnect easyconnect_(argc, argv);
/// Create matrix 9x9 elem.
//EasyMatrix x;
//x.Create("matrix(1:9, nrow=9, ncol=9)", "x");
// Create matrix 9x9 elem.
//EasyMatrix y;
//y.Create("matrix(1:9, nrow=9, ncol=9)", "y");

CSPobject Y;
if (!Y.Create("vector(\"double\",10.6)"))
{
cout << "Can not create vector." << endl;
return 1;
}

CSPobject X1;
if (!X1.Create("vector(\"double\",8.6)"))
{
cout << "Can not create vector." << endl;
return 1;
}
CSPobject X2;
if (!X2.Create("vector(\"double\",3.4)"))
{
cout << "Can not create vector." << endl;
return 1;
}

/// Connect to S+
// CSPengineConnect engineConnect(argc, argv); //EasyEngineConnect
easyconnect_(argc, argv);

/// Create matrix 9x9 elem.
CSPmatrix x;
x.Create("matrix(1:9, nrow=9, ncol=9)", "x");

CSPnumeric sx;
sx.Create("1:10","x");
CSPnumeric sy = sx * sx;
sy.Assign("y");
// engineConnect.SyncParseEval("z<-lm(y~x)");





/*
CSPengineConnect engineConnect(argc, argv);

CSPobject x;
/// Create matrix 24x400 elem.
x.Create("matrix(1:223, nrow=24, ncol=400)");
//x.Create("matrix(1.2, nrow=, ncol=10)");
CSPobject y;
/// Create matrix 24x400 elem.
y.Create("matrix(231:1, nrow=24, ncol=400)");
/// Assign.
x.Assign("x");
y.Assign("y");
/// Call S+ regression "z<-x+y".
int iSuccess = engineConnect.SyncParseEval("z<-x+y");
/// Error.
if (iSuccess == 0)
{
cout << "Error, Can not call \"z<-(x+y)\"" << endl;
return 1;
}
CSPmatrix z(engineConnect.get("z"));
if (!z.IsValid())
{
cout << "Error, Can not search object z" << endl;
}
/// Print.
for (int i = 1; i <= 5;i++); //z.GetNRow(); ++i)
{
for (int j = 1; j <= z.GetNCol(); ++j)
{
cout << (double)z(i, j) << " ";
}
cout << endl;
}




/*
//Create the connection to S-PLUS
CSPengineConnect engineConnect(argc, argv);
/// Create vector 24.
CSPobject A_vector;
if (!A_vector.Create("vector(\"double\",10)"))
{
cout << "Can not create vector." << endl;
return 1;
}
CSPobject B_matrix;
if (!B_matrix.Create("matrix(1.2, nrow=10, ncol=10)"))
{
cout << "Can not create matrix." << endl;
return 1;
}
/// C_matrix = B_matrix + A_vector.
CSPmatrix C_matrix(B_matrix + A_vector) ;
if (C_matrix == NULL)
{
cout << "Can not create matrix." << endl;
}
*/

/*
CSPnumeric sx;
sx.Create("1:10","x");
CSPnumeric sy = sx * sx;
sy.Assign("y");
engineConnect.SyncParseEval("z<-lm(y~x)");

*/

/*
/// Print C_matrix.
cout << "Print C_matrix = B_matrix + A_vector." << endl << endl;
for (int i = 1; i <= C_matrix.nrow(); ++i)
{
for (int j = 1; j <= C_matrix.ncol(); ++j)
{
cout << (double)C_matrix(i,j) << " "; //< Out to console.
}
cout << endl;
}

cout << endl << endl;

getchar();

/// Call procedure S+.

CSPcharacter returnObj;
/// Create a integer vector with one element
/// representing one argument to pass to function 'objects'
CSPinteger args("1");
/// Create a CSPcall object
CSPcall sCall;
/// Call function S+ (objects()).
cout << "Call function S+ (objects(1))...." << endl << endl;
sCall.Create("objects", args);
/// Call.
returnObj = sCall.Eval(); // objects(1)
if (returnObj == NULL)
{
cout << "Function objects S+ failed" << endl;
return 1;
}

/// Get quantity.
int quantity = returnObj.length();
cout << "Quantity objects : " << quantity << endl;
/// Print objects.
for (i = 0; i < quantity; ++i)
{
cout << " [ " << i+1 << " ] " << (char *)returnObj << endl;
}
*/

/*
//Create the connection to S-PLUS
g_engineConnect.Create( argc, argv);

//Create S object with name "x" in the current database.
//Same as x<-1:10 at the command line.
CSPnumeric sx;
sx.Create("1:10","x");

//Squaring sx, which is the same as S expression sy <- x*x in a local
frame,
//but here we set it to local C++ variable sy.

CSPnumeric sy = sx * sx;

// Assign the result as S object with name "y" in the current
database.
sy.Assign("y");

//Evaluate z<-lm(y~x)
g_engineConnect.SyncParseEval("z<-lm(y~x)");

maybe ------------------- CSPobject Eval ("z<-lm(y~x)");


//second example
CSPevaluator s;

CSPcharacter message("'hello'");
message.Print();

//CSPmatrix M("matrix(1:4,rnow=2)");
//M.Print();
*/

printf("Normal Termination\n");
return 0;
}
 
My business associate later admitted that he used C++ compiler
in .NET.

I guess I don't have to compile the C++ code in C#.NET!
 
C++ and C# are completely different:

1) To reference to a libray, you need to include the path in C++; but
need to add the .dll as a reference in C# and include that namespace;
2) Syntax of main(), input/output, ... are entirely different.

But I remember when I started to learn C#.NET, someone told me that C#
had close relations to C and C++. Why did they tell me that?
 
C/C++/C#/Java have the same 'style' of syntax - i.e., curly braces,
semi-colons, same syntax for many constructs, but there are enough
differences that code written in one will not compile with the compiler for
another language.
--
http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
VB & C# to Java
Java to VB & C#
Instant C#: VB to C#
Instant VB: C# to VB
Instant C++: VB, C#, or Java to C++/CLI
 
My business associate insists that he can run C++ functions from C#
code.

Will it be possible to compile C++ code (managed code) into a .dll,
and reference the .dll from C#.NET code, and call C++ code in the
library?
 
That is a very different question than you started. Yes, you can compile a
dll and call it from c#. http://www.pinvoke.net/ has examples on how to
call windows api dlls. You will do something similar with your associates
dll.

Your associate must be "fun" to work with :(.
 
That is a very different question than you started.  Yes, you can compile a
dll and call it from c#.  http://www.pinvoke.net/has examples on how to
call windows api dlls.  You will do something similar with your associates
dll.

Your associate must be "fun" to work with :(.

Family Tree Mike,

How would this work? The parameter types in C++ methods are different
from the data types in C#. For instance, I have the following
parameter types in C++:

char *
s_object *
long

What types shall I define for the parameters in C# to be passed to the
C++ so that they'll be recognized as types char *, s_object *, and
long?
 
If your function in C++ is (char * a, s_object * b, long c), then your
declare statement should be (string a, IntPtr b, int c).


That is a very different question than you started. Yes, you can compile a
dll and call it from c#. http://www.pinvoke.net/has examples on how to
call windows api dlls. You will do something similar with your associates
dll.

Your associate must be "fun" to work with :(.

Family Tree Mike,

How would this work? The parameter types in C++ methods are different
from the data types in C#. For instance, I have the following
parameter types in C++:

char *
s_object *
long

What types shall I define for the parameters in C# to be passed to the
C++ so that they'll be recognized as types char *, s_object *, and
long?
 
Back
Top