Click here to Skip to main content
12,697,311 members (28,316 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C++ C C# VisualC++
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
Comments
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: bad
 
good
Please Sign up or sign in to vote.

Solution 3

  Permalink  
v2
Comments
J.Surjith Kumar 21-Jan-13 7:07am
   
This article helps me a lot.. Thankyou
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Try to check this article - it should help.
How to Marshal a C++ Class[^]
  Permalink  
Comments
J.Surjith Kumar 21-Jan-13 7:07am
   
This article helps me a lot.. Thankyou
Rate this: bad
 
good
Please Sign up or sign in to vote.

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.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

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.
  Permalink  
Comments
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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170118.1 | Last Updated 18 Dec 2015
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100