12,697,311 members (28,316 online)
Rate this:
See more:
Question 1:
I want to use the class and functions of c++ to C# code so i exported the class and created dll successfully using c++. How to import the class from dll to c#.
Here is my c++ code(Exporting the class):
```//MomentOfInertia.h
#ifdef COMPILE_MYLIBRARY
#define DllExport   __declspec( dllexport )
#else
#define DllExport   __declspec( dllimport )
#endif

class DllExport a
{
public:
double MOIRectangle(double b, double h);
double MOISemiCircle(double r);
double MOITriangle(double b, double h, int);
};
```

```// MomentOfInertia.cpp :
#include "stdafx.h"
// Calculation of the moment of inertia
#include "MomentOfInertia.h"
double a::MOIRectangle(double b, double h)
{
return b * h * h * h / 3;
}
double a::MOISemiCircle(double r)
{
const double PI = 3.14159;
return r * r * r * r * PI / 8;
}
double a::MOITriangle(double b, double h, int)
{
return b * h * h * h / 12;
}
```
Posted 18-Jan-13 1:07am
Updated 24-Jan-13 0:02am
v6
J.Surjith Kumar 21-Jan-13 7:10am

The link of the articles which is in the solution helped me to get more knowledge regarding my question and it helped me to got the result.

Rate this:

## Solution 3

v2
J.Surjith Kumar 21-Jan-13 7:07am

Rate this:

## Solution 2

How to Marshal a C++ Class[^]
J.Surjith Kumar 21-Jan-13 7:07am

Rate this:

## Solution 4

Create a mixed mode C++ DLL from pertinent source files. It might be preferable to create 2 DLLs.

Typically you would have a bunch of difference in source depending on which DLL you are building. For example, you might use CString in one case and System::String in the other. In one case, you want everything native and in the other case, you might prefer to have most if not all code managed (in that case, it might be easier to compile in mixed mode with /clr option alone).

Mixed mode (C++#CLI) is probably the easiest way to go if you have a lot of classes.

Otherwise P/Invoke or COM (Active/X) might be adequate.
Rate this:

## Solution 1

You don't import C++ classes into C# programs. You need to create a COM library or use the Platform Invoke mechanism[^]. However, given the simplicity of the code it would be much simpler just to incorporate the same logic into your C# classes.
J.Surjith Kumar 18-Jan-13 7:21am

By that code i created lib and dll files. I can access that class in mfc application.
Richard MacCutchan 18-Jan-13 7:23am

But C# is not MFC, it is managed code which runs within the .NET framework. Total different structure.
J.Surjith Kumar 18-Jan-13 7:28am

I have to create the dll using c++ which consists of many classes and their functions. How to use that classes and function in c#.
J.Surjith Kumar 21-Jan-13 7:07am

Got it.. Thankyou
J.Surjith Kumar 24-Jan-13 1:15am

Can you tell me reason for the 2nd question!!
Richard MacCutchan 24-Jan-13 4:52am

Sorry I don't know, and I'm not actually clear what you are doing there. Maybe you should open a new question and add some more detail about what is happening and what problems occur.
J.Surjith Kumar 24-Jan-13 5:18am

Ok i posted that in new question.

Top Experts
Last 24hrsThis month
 OriginalGriff 230 F-ES Sitecore 135 Asgard25 130 CPallini 105 Jochen Arndt 90
 OriginalGriff 3,721 Peter Leow 2,228 Mika Wendelius 1,753 ppolymorphe 1,647 Jochen Arndt 1,454