Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C# DLL
I am developing c++ application. Now i want to call a class in c# within the c++ program. is there any way to this? Is it possible to make the c# program as web service and add service reference to the c++ project.
Posted 14-Oct-12 23:21pm
Edited 14-Oct-12 23:22pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

It is possible if you use COM. You should make your C# class exposed to COM and then you can use it in C++. Here is an example inspired by a question here:
 
namespace ManagedDLL
{
   // Interface declaration.
   [Guid("32529FAE-6137-4c62-9945-DE4198FA9D1B")]
   [InterfaceType(ComInterfaceType.InterfaceIsDual)]
   public interface ICalculator
   {
      [DispId(1)]
      int Add(int Number1, int Number2);
   };
}
 
namespace ManagedDLL
{
   // Interface implementation.
   [Guid("9F2F180D-94A9-47e6-91CC-6BCFABD1DDEB")]
   [ClassInterface(ClassInterfaceType.None)]
   [ProgId("ManagedDLL.ManagedClass")]
   public class ManagedClass : ICalculator
   {
      public int Add(int Number1, int Number2)
      {
         return Number1 + Number2;
      }
   }
}
 
Then you can use this in C++:
 
#import "..\bin\ManagedDLL.tlb" raw_interfaces_only
using namespace ManagedDLL;
 
int _tmain(int argc, _TCHAR* argv[])
{
   // Initialize COM.
   HRESULT hr = CoInitialize(NULL);
 
   // Create the interface pointer.
   ICalculatorPtr pICalc(__uuidof(ManagedClass));
 
   long lResult = 0;
 
   // Call the Add method.
   pICalc->Add(5, 10, &lResult);
 
   wprintf(L"The result is %d\n", lResult);
 

   // Uninitialize COM.
   CoUninitialize();
 
   return 0;
}
  Permalink  
Comments
doree007 at 18-Oct-12 3:48am
   
Thanks for your reply. these solutions are really helpful for me.
doree007 at 13-Nov-12 0:43am
   
how can i use COM. I am new to this area. Do i have to make class library or is there any way to use existing windows application.
can u help me with step by step instructions.. it is really helpful for me to understand what is happening when calling the function.
doree007 at 13-Nov-12 3:20am
   
i am getting an error saying 'IcalculatorPtr': undeclared identifier.. How can i fix this???
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

No need to make it a web service. Using C++/CLI you can instantiate your C#'s class in your C++ program almost like C++ classes. You need to use gcnew (instead of new) to instantiate .NET classes.
 
For details, see:
http://en.wikipedia.org/wiki/C%2B%2B/CLI[^]
  Permalink  
Comments
doree007 at 18-Oct-12 4:01am
   
using this methods is it possible to call a c# windows forms class.
yogeshacharya at 22-Aug-13 1:54am
   
I have a C# dll only and I want to make use the function in C# dll in my C++ application. How can I do this ?
Marius Bancila at 18-Oct-12 4:04am
   
Yes, of course it is.
manoranjan at 22-Aug-13 2:58am
   
Use gcnew to instantiate your C# class in your C++ app and then use it.
 
E.g.:
MyCsClass ^cso = gcnew MyCsClass;
cso->f1();
doree007 at 18-Oct-12 4:11am
   
how can i make the references for c# application within c++ application. because i am calling a c# windows forms class. I am new to c++ programming. so can u please help me with this.
again thanks for your time
yogeshacharya at 22-Aug-13 6:08am
   
Thanks for the solution
manoranjan at 18-Oct-12 5:05am
   
Create a C++ project that supports CLR (e.g. Windows Forms Application project). You will then be able to add references through the project's 'properties' dialog.
manoranjan at 22-Aug-13 9:35am
   
you are welcome!

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 575
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 369
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,466
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,897


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 13 Nov 2013
Copyright © CodeProject, 1999-2014
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