Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VC6 C# .NET DLL VC++
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:
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 16-Oct-12 0:57am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
vaibhavj_navdurga at 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.
vaibhavj_navdurga at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Funny how this is just a copy-cat of what I already answered yesterday: call c# dll or class within c++ project[^].
  Permalink  
Comments
vaibhavj_navdurga at 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 at 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.
vaibhavj_navdurga at 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 at 16-Oct-12 10:47am
   
Yes, it is possible through a mixed-mode DLL in C++/CLI.
vaibhavj_navdurga at 17-Oct-12 2:09am
   
Can u explain how it is possible or give some link for reference?
vaibhavj_navdurga at 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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 16 Oct 2012
Copyright © CodeProject, 1999-2014
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