Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - Run C++ code in C#.NET

Reply
 
Old 10-28-2008   #1 (permalink)
Curious


 
 

Run C++ code in C#.NET

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[i] << 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 System SpecsSystem Spec
Old 10-28-2008   #2 (permalink)
Cowboy \(Gregory A. Beamer\)


 
 

Re: Run C++ code in C#.NET


"Curious" <fir5tsight@xxxxxx> wrote in message
news:1d769399-6bdd-483e-8fe7-9387f7acd4c6@xxxxxx
Quote:

>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! |
********************************************

My System SpecsSystem Spec
Old 10-28-2008   #3 (permalink)
David Anton


 
 

RE: Run C++ code in C#.NET

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" wrote:
Quote:

> 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[i] << 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 System SpecsSystem Spec
Old 10-29-2008   #4 (permalink)
Erik Funkenbusch


 
 

Re: Run C++ code in C#.NET

On Tue, 28 Oct 2008 07:07:37 -0700 (PDT), Curious wrote:
Quote:

> 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.
Your business associate was wrong.

Now, there is a version of C++ for the .NET framework, called C++/CLI, but
there are typical certain changes that need to be made to adapt a generic
C++ program to it.
My System SpecsSystem Spec
Old 10-29-2008   #5 (permalink)
Curious


 
 

Re: Run C++ code in C#.NET

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!
My System SpecsSystem Spec
Old 10-29-2008   #6 (permalink)
Curious


 
 

Re: Run 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?
My System SpecsSystem Spec
Old 10-30-2008   #7 (permalink)
David Anton


 
 

Re: Run C++ code in C#.NET

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


"Curious" wrote:
Quote:

> 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?
>
My System SpecsSystem Spec
Old 11-03-2008   #8 (permalink)
Curious


 
 

Re: Run C++ code in C#.NET

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?
My System SpecsSystem Spec
Old 11-03-2008   #9 (permalink)
Family Tree Mike


 
 

Re: Run C++ code in C#.NET

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 .

"Curious" <fir5tsight@xxxxxx> wrote in message
news:16b3dbf3-5047-46c3-8899-c83f1cf1587f@xxxxxx
Quote:

> 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?
My System SpecsSystem Spec
Old 11-04-2008   #10 (permalink)
Curious


 
 

Re: Run C++ code in C#.NET

On Nov 3, 10:23*am, "Family Tree Mike"
<FamilyTreeM...@xxxxxx> wrote:
Quote:

> 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 .
>
> "Curious" <fir5tsi...@xxxxxx> wrote in message
>
> news:16b3dbf3-5047-46c3-8899-c83f1cf1587f@xxxxxx
>
>
>
Quote:

> > My business associate insists that he can run C++ functions from C#
> > code.
>
Quote:

> > 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
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?
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Can I code HTML code straight to Webbrowser control? .NET General
Error Code 80048820; extended error code 80048439 Live Messenger
Code 8004888d can't sign in Msnger or install beta 8.5 code 0x8009 Vista security
ATI Radeon Drivers - Code 43, Code 37 & Code 10 Vista hardware & devices
ATI Display Drivers - Code 43, Code 37, Code 10 Vista hardware & devices


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46