Click here to Skip to main content
13,145,068 members (54,424 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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
Updated 14-Oct-12 22:22pm
Member 12139035 15-Nov-15 11:56am
getting error for "ICalculatorPtr" to undeclared udentifier, what is the problem?
runfastman 5-Apr-16 12:30pm
I have the same problem, did you find a solution?
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 18-Oct-12 3:48am
Thanks for your reply. these solutions are really helpful for me.
doree007 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 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 18-Oct-12 4:01am
using this methods is it possible to call a c# windows forms class.
yogeshacharya 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 18-Oct-12 4:04am
Yes, of course it is.
manoranjan 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 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 22-Aug-13 6:08am
Thanks for the solution
manoranjan 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 22-Aug-13 9:35am
you are welcome!
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Member 9982800 9-Jul-15 0:49am
not able to access Method from COM library
Wht is the problem

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 |
Web04 | 2.8.170915.1 | Last Updated 13 Nov 2013
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