473,811 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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\\Splu s61 Includes\\EasyE ngineConnect.h"
#include "c:\\data\\Splu s61 Includes\\EasyE ngineConnect.cp p"

#include "c:\\data\\Splu s61 Includes\\EasyC haracter.h"
//#include "c:\\data\\Splu s61 Includes\\EasyC haracter.cpp"

#include "c:\\data\\Splu s61 Includes\\EasyM atrix.h"
//#include "c:\\data\\Splu s61 Includes\\EasyM atrix.cpp"

#include "c:\\data\\Splu s61 Includes\\EasyN umeric.h"
//#include "c:\\data\\Splu s61 Includes\\EasyN umeric.cpp"

using namespace std;

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

// Connect to S+
EasyEngineConne ct easyconnect_(ar gc, argv);
/// Create matrix 9x9 elem.
//EasyMatrix x;
//x.Create("matri x(1:9, nrow=9, ncol=9)", "x");
// Create matrix 9x9 elem.
//EasyMatrix y;
//y.Create("matri x(1:9, nrow=9, ncol=9)", "y");

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

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

/// Connect to S+
// CSPengineConnec t engineConnect(a rgc, argv); //EasyEngineConne ct
easyconnect_(ar gc, argv);

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

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

/*
CSPengineConnec t engineConnect(a rgc, argv);

CSPobject x;
/// Create matrix 24x400 elem.
x.Create("matri x(1:223, nrow=24, ncol=400)");
//x.Create("matri x(1.2, nrow=, ncol=10)");
CSPobject y;
/// Create matrix 24x400 elem.
y.Create("matri x(231:1, nrow=24, ncol=400)");
/// Assign.
x.Assign("x");
y.Assign("y");
/// Call S+ regression "z<-x+y".
int iSuccess = engineConnect.S yncParseEval("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
CSPengineConnec t engineConnect(a rgc, argv);
/// Create vector 24.
CSPobject A_vector;
if (!A_vector.Crea te("vector(\"do uble\",10)"))
{
cout << "Can not create vector." << endl;
return 1;
}
CSPobject B_matrix;
if (!B_matrix.Crea te("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_matr ix + 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.S yncParseEval("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_matri x(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("o bjects", args);
/// Call.
returnObj = sCall.Eval(); // objects(1)
if (returnObj == NULL)
{
cout << "Function objects S+ failed" << endl;
return 1;
}

/// Get quantity.
int quantity = returnObj.lengt h();
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,r now=2)");
//M.Print();
*/

printf("Normal Termination\n") ;
return 0;
}

Oct 28 '08 #1
10 7932

"Curious" <fi********@yah oo.comwrote in message
news:1d******** *************** ***********@u57 g2000hsf.google groups.com...
>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! |
*************** *************** **************

Oct 28 '08 #2
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:
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\\Splu s61 Includes\\EasyE ngineConnect.h"
#include "c:\\data\\Splu s61 Includes\\EasyE ngineConnect.cp p"

#include "c:\\data\\Splu s61 Includes\\EasyC haracter.h"
//#include "c:\\data\\Splu s61 Includes\\EasyC haracter.cpp"

#include "c:\\data\\Splu s61 Includes\\EasyM atrix.h"
//#include "c:\\data\\Splu s61 Includes\\EasyM atrix.cpp"

#include "c:\\data\\Splu s61 Includes\\EasyN umeric.h"
//#include "c:\\data\\Splu s61 Includes\\EasyN umeric.cpp"

using namespace std;

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

// Connect to S+
EasyEngineConne ct easyconnect_(ar gc, argv);
/// Create matrix 9x9 elem.
//EasyMatrix x;
//x.Create("matri x(1:9, nrow=9, ncol=9)", "x");
// Create matrix 9x9 elem.
//EasyMatrix y;
//y.Create("matri x(1:9, nrow=9, ncol=9)", "y");

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

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

/// Connect to S+
// CSPengineConnec t engineConnect(a rgc, argv); //EasyEngineConne ct
easyconnect_(ar gc, argv);

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

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

/*
CSPengineConnec t engineConnect(a rgc, argv);

CSPobject x;
/// Create matrix 24x400 elem.
x.Create("matri x(1:223, nrow=24, ncol=400)");
//x.Create("matri x(1.2, nrow=, ncol=10)");
CSPobject y;
/// Create matrix 24x400 elem.
y.Create("matri x(231:1, nrow=24, ncol=400)");
/// Assign.
x.Assign("x");
y.Assign("y");
/// Call S+ regression "z<-x+y".
int iSuccess = engineConnect.S yncParseEval("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
CSPengineConnec t engineConnect(a rgc, argv);
/// Create vector 24.
CSPobject A_vector;
if (!A_vector.Crea te("vector(\"do uble\",10)"))
{
cout << "Can not create vector." << endl;
return 1;
}
CSPobject B_matrix;
if (!B_matrix.Crea te("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_matr ix + 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.S yncParseEval("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_matri x(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("o bjects", args);
/// Call.
returnObj = sCall.Eval(); // objects(1)
if (returnObj == NULL)
{
cout << "Function objects S+ failed" << endl;
return 1;
}

/// Get quantity.
int quantity = returnObj.lengt h();
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,r now=2)");
//M.Print();
*/

printf("Normal Termination\n") ;
return 0;
}

Oct 28 '08 #3
On Tue, 28 Oct 2008 07:07:37 -0700 (PDT), Curious wrote:
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.
Oct 29 '08 #4
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!
Oct 29 '08 #5
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?
Oct 29 '08 #6
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:
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?
Oct 30 '08 #7
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?
Nov 3 '08 #8
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" <fi********@yah oo.comwrote in message
news:16******** *************** ***********@g17 g2000prg.google groups.com...
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?
Nov 3 '08 #9
On Nov 3, 10:23*am, "Family Tree Mike"
<FamilyTreeM... @ThisOldHouse.c omwrote:
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...@yah oo.comwrote in message

news:16******** *************** ***********@g17 g2000prg.google groups.com...
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
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?
Nov 4 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

51
5299
by: Mudge | last post by:
Please, someone, tell me why OO in PHP is better than procedural.
109
5942
by: Andrew Thompson | last post by:
It seems most people get there JS off web sites, which is entirely logical. But it is also a great pity since most of that code is of such poor quality. I was looking through the JS FAQ for any question that identifies the warning signs to look out for, the things that most easily and clearly identify the author of code as something less than a master of the art. I did not find an FAQ that answered it, but I think the FAQ
0
2099
by: Namratha Shah \(Nasha\) | last post by:
Hey Guys, Today we are going to look at Code Access Security. Code access security is a feature of .NET that manages code depending on its trust level. If the CLS trusts the code enough to allow it ro run then it will execute, the code execution depends on the permission provided to the assembly. If the code is not trusted wnough to run or it attempts to perform an action which doe not have the required permissions then its execution...
171
7827
by: tshad | last post by:
I am just trying to decide whether to split my code and uses code behind. I did it with one of my pages and found it was quite a bit of trouble. I know that most people (and books and articles) like it because you can split the code from the design. That is logical. But if you are the only one working on the code, it seem a little overkill. I use Dreamweaver to do my design and find it a bit of a hassle to have multiple files open...
4
2121
by: KenFehling | last post by:
Hello. I am wondering if there exists a piece of software that takes multiple .js files that are nicely indented and commented and create one big tightly packed .js file. I'm hoping the one file would be less of a burden for the user's browser to download. I guess the final code output by this hypothetical program could maybe even just be on one long line unless that would create problems. Maybe there is some kind of optimal line...
88
8106
by: Peter Olcott | last post by:
Cab you write code directly in the Common Intermediate language? I need to optimize a critical real-time function.
7
3432
by: blackrosezy | last post by:
#include char *code; void main() { char buf = "book";
2
4831
by: rn5a | last post by:
Assume that a user control (MyUC.ascx) encapsulates 2 TextBoxes with the IDs 'txt1' & 'txt2' respectively. To use this user control in an ASPX page, the following Register directive will be required: <%@ Register TagPrefix="UC" TagName="MyUserCtrl" Src="MyUC.ascx" %> Assuming that the ASPX page doesn't use a code-behind, I can access the properties, events etc. of the user control in the ASPX page in this way (assume that the ASPX page...
1
1541
by: ahammad | last post by:
Hello, I have written a fairly complex parsing tool that is used to parse information from company documents. The program works very well, but in order to insure that all the data is copied properly, I had to remove some of the things that are not part of the data, but are simply headings. Here is what I mean: *Heading <CODE1> = <COMMAND1> <CODE2> = <COMMAND2> The program reads in each line, and separates <CODE1> from <COMMAND1> at...
66
7480
by: Jon Skeet [C# MVP] | last post by:
I'm sure the net will be buzzing with this news fairly soon, but just in case anyone hasn't seen it yet: Microsoft are going to make the source code for the .NET framework (parts of it, including the BCL, ASP.NET and LINQ) available both for viewing and debugging into. I won't go into all the details here, as they're covered on Scott Guthrie's blog:
0
9734
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10397
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9214
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6897
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5564
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5700
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4353
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3878
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3027
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.