Click here to Skip to main content
15,885,165 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Friends
I stuck in a problem. I have to use the functions of c# dll into VC++. Please somebody help me to resolve this issue. My code is as below.
In C# dll:
C#
namespace MathOperationsLib
{
    public class Math
    {
        public int Addition(int val1, int val2)
        {
            return (val1 + val2);
        } 
    }
}


In VC++ file:
#include "stdafx.h"

// Import the type library.
#import "C:\Vaibhav\C#Dll\MathOperationsLib\bin\Debug\MathOperationsLib.tlb" raw_interfaces_only

using namespace MathOperationsLib;
int _tmain(int argc, _TCHAR* argv[])
{
      HRESULT hr = CoInitialize(NULL);
      return 0;
}


I am confused that how I call the Addition function of C# dll in VC++.
I do one more thing that I add the following lines into AssemblyInfo.cs file of C#
[assembly: ComVisible(true)]
[assembly: AssemblyDelaySign(false)]
[assembly: AssemblyKeyFile("C:\\Vaibhav\\C#Dll\\MathOperationsLib\\MyKeyFile.SNK")]


Please explain me how to resolve this problem.
Posted

You could refer this

Calling Managed .NET C# COM Objects from Unmanaged C++ Code[^]

Also there is direct method to add managed code to VC++, only thing is u neeed to change a project property. I will find it out and share with you.

Pls mark it as answer, if it solves your problem.
 
Share this answer
 
Comments
Vaibhav_J_Jaiswal 16-Oct-12 7:20am    
I read this article but the problem is that in my code I am not able to create com_ptr pointer as created in the given article(MyInterop::IMyDotNetInterfacePtr pDotNetCOMPtr;). Is this problem is persist because of the interface? In my c# code I am not using any of interface object.
Vaibhav_J_Jaiswal 17-Oct-12 3:54am    
Is there any other way by which I access the C# dll functions in vc++ code without using Interface in C# Dll? If there then please share with me.
Funny how this is just a copy-cat of what I already answered yesterday: call c# dll or class within c++ project[^].
 
Share this answer
 
Comments
Vaibhav_J_Jaiswal 16-Oct-12 7:53am    
Here also same thing.I am not able to create the com pointer object as you create (ICalculatorPtr pICalc(__uuidof(ManagedClass))).
Maxim Kartavenkov 16-Oct-12 9:26am    
Solution described in there should works fine.
You need to create interface with your method to export so you can query it, otherwise you should look what is imported from type library. In there should be generated interface for your object and your coclass (You can also use OleView tool and open resulted tlb). If you have no tlb - you have to export typelibrary with regasm tool. Along with it auto generated interface is always inherited from IDispatch - and you can figure out it's methods in runtime. You may use CoCreateInstance function instead of smart pointers.
Vaibhav_J_Jaiswal 16-Oct-12 9:42am    
The C# dll is already create as per our functionality so it is not possible to change the code of the dll. Is there another way by which I expose the function of C# dll in VC++ by calling function of the dll? If it is possible plz tell me how to do this.
Marius Bancila 16-Oct-12 10:47am    
Yes, it is possible through a mixed-mode DLL in C++/CLI.
Vaibhav_J_Jaiswal 17-Oct-12 2:09am    
Can u explain how it is possible or give some link for reference?

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