Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
Hi friends,

I want to make a simple project in which VC++ exe calls C# dll's function without using COM functionality. I search it on net but nothing more useful get found. Please suggest me how to do this and also some links for reference.

Thanks in advance
Posted

This[^] could interest you.
 
Share this answer
 
Comments
[no name] 11-Jun-14 8:26am    
Page not found.
First create a static library project in the same solution and turn on Common Language Runtime support. Don't turn on CLR support for the EXE project.

Add a class to the library project(.h + .cpp). Then add your necessary functions to the class and implement them only in the .cpp.

Note: You shouldn't use any .NET framework classes in .h file. For example, take a function
C++
void ShowSomeMessageBox();

Let its implementation be,
C++
void YourClassName::ShowSomeMessageBox()
{
   System::Windows::Forms::MessageBox::Show("Hello World!");
}

Here the declaration doesn't of the method has no .NET objects as parameters. That is you can have data types like char, int or std::string as the parameters and not System::String for example.

After this, add reference to the library project in the EXE project properties. Then add the path where you've saved the header file. Then #include the header file. And you can use ShowSomeMessage() method without exposing your whole EXE project source to CLR.

Note: This trick will only work in Visual C++.
 
Share this answer
 
v2
I used Exporting functions in C#/VB.NET to native code[^] for a project that I needed to interface with a third party C# API. I created a C# wrapper DLL and inserted entry points. The DLL simply called the C# API functions.
 
Share this answer
 

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