Click here to Skip to main content
11,580,000 members (72,138 online)
Rate this: bad
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 22:21pm
Edited 14-Oct-12 22:22pm
Rate this: bad
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.
   public interface ICalculator
      int Add(int Number1, int Number2);

namespace ManagedDLL
   // Interface implementation.
   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.
   return 0;
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
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:[^]
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.

MyCsClass ^cso = gcnew MyCsClass;
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 OriginalGriff 218
1 Frankie-C 170
2 Abhinav S 65
3 Sergey Alexandrovich Kryukov 49
4 Dave Kreskowiak 45
0 OriginalGriff 1,003
1 Sergey Alexandrovich Kryukov 750
2 Abhinav S 583
3 F-ES Sitecore 420
4 Dave Kreskowiak 419

Advertise | Privacy | Mobile
Web03 | 2.8.150603.1 | Last Updated 13 Nov 2013
Copyright © CodeProject, 1999-2015
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