Click here to Skip to main content
15,512,456 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
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):
 #define DllExport   __declspec( dllexport )
 #define DllExport   __declspec( dllimport )

class DllExport a
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;
Updated 24-Jan-13 0:02am
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.

Share this answer
J.Surjith Kumar 21-Jan-13 7:07am    
This article helps me a lot.. Thankyou
Try to check this article - it should help.
How to Marshal a C++ Class[^]
Share this answer
J.Surjith Kumar 21-Jan-13 7:07am    
This article helps me a lot.. Thankyou
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.
Share this answer
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.
Share this answer
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!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900