7

I have created a dll using command line (csc). Suppose that dll contains Add(int,int) method. Now I want to use that add function in vc++??

How to do this?

Your Help will be Greatly Appreciated.

Here is what I am doing.

vcproj; for this project i have right click and linker section add additional dependencies.added the path where dll resides.

main.cpp

using namespace Test

void main()

{

  demo d;

  d.add(5,5);

}

error namespace Test Does not exist.

How to resolve this?

I need to use that dll as in unmanaged code

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Cute
  • 13,643
  • 36
  • 96
  • 112

3 Answers3

9

Your C# assembly needs the ComVisible attribute (among a few other things).

[assembly: ComVisible(true)] 

There's a guide to doing this here.

Matt Brindley
  • 9,739
  • 7
  • 47
  • 51
  • 2
    If the C++ application is managed, then it's even easier than this of course, and you can avoid COM altogether. – Noldorin Jun 11 '09 at 12:35
  • @Matthew Brindley i did what is given on http://support.microsoft.com/kb/828736 but i am getting an error of cannot convert from 'const _GUID' to 'const IISWebFrameworkDAAB::DataAccessInterface'" What should i do? – user1402643 Oct 08 '12 at 07:43
8

From MSDN forums Calling C# from unmanaged C++:

What you want to do would be to compile only the files where you want to load the C# code /clr and then call away using C++/CLI. The rest of your app will remain native while those cpp files you compile /clr will be mixed native and CLR. You should be able to just call into your C# library using C++/CLI syntax.

Once you throw /clr on the cl.exe command line for a file, within that file, you can call any managed code that you want, regardless of whether it was written in VB.net, C# or C++/CLI. This is by far the easiest way to do what you want to do (call C# code from your native C++ app), although it does have its caveat's and limitations. By and large though "It Just Works". Also, it's faster than p/invokes.

TWA
  • 12,756
  • 13
  • 56
  • 92
2

This highly depends on what type of C++ application you are building. If you are building a C++/CLI assembly then you need to make sure the project has a reference to the C# DLL. Once that is done you should be able to type the code as written assumping you have a definition of Demo like so.

namespace Test {
  public class demo {
    public void add(int left, int right) { 
      ...
    }
  }
}

If you are building a normal, non-managed, C++ project then you must use COM interop to access the assembly. This involves making the demo type in your C# project COMVisible and registering it for COM interop. After which you can access it like any other COM component.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • i followed this http://support.microsoft.com/kb/828736 all goes well before "Call the Managed DLL from Native C++ Code" but when i try'ed to create a pointer i get the following error "error C2259: 'IISWebFrameworkDAAB::DataAccessInterface' : cannot instantiate abstract class 1> due to following members: 1> 'HRESULT IUnknown::QueryInterface(const IID &,void **)' : is abstract" What should i do? – user1402643 Oct 08 '12 at 07:27
  • Actually the reason for error is "Reason: cannot convert from 'const _GUID' to 'const IISWebFrameworkDAAB::DataAccessInterface'" What should i do? – user1402643 Oct 08 '12 at 07:30