Click here to Skip to main content
15,887,414 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Updated 6-Feb-20 9:43am
v2
Comments
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?

I created a C# ClassLibrary (DLL) that makes SOAP webservice calls. When creating it, make sure you go to your project properties > Build > Output > then make sure to check "Register for COM interop" checkbox.

If you intend to call this C# DLL that makes SOAP calls from a C++ app, you also need to make sure that SecurityProtocol is set properly. For more details, check this post on SO where I explain what problem I had and how I resolved it

C# DLL Calling SOAP Web Service From C++ App - Stack Overflow[^]
 
Share this answer
 
v2
 
Share this answer
 
Comments
Member 9982800 9-Jul-15 0:49am    
not able to access Method from COM library
Wht is the problem
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[^]
 
Share this answer
 
Comments
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.

E.g.:
MyCsClass ^cso = gcnew MyCsClass;
cso->f1();
Member 15074053 16-Feb-21 13:49pm    
in which file we need to add this
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:

C#
namespace ManagedDLL
{
   // Interface declaration.
   [Guid("32529FAE-6137-4c62-9945-DE4198FA9D1B")]
   [InterfaceType(ComInterfaceType.InterfaceIsDual)]
   public interface ICalculator
   {
      [DispId(1)]
      int Add(int Number1, int Number2);
   };
}


C#
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++:

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;
}
 
Share this answer
 
Comments
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???
Member 14919374 16-Apr-21 10:29am    
same here
malijooni 11-May-18 11:54am    
HiI applyed your above instructions and I used them in c++/cli interface after creating exe file it does not run on other system... what should I do?

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