20

I am trying include c++ library (DLL) in my c# project but every time I do that I get following error message in VS2008, any suggestions?

EDIT: It's a C++ MFC DLL

---------------------------
Microsoft Visual Studio
---------------------------
A reference to 'C:\Users\cholachaguddapv\Desktop\imaging.dll' could not be added. Please make sure that the file is accessible, and
that it is a valid assembly or COM component.
peterh
  • 1
  • 15
  • 76
  • 99
Prashant Cholachagudda
  • 12,833
  • 21
  • 97
  • 161

5 Answers5

25

If it is a "normal" DLL (not COM, not managed C++), you cannot add a reference like this. You have to add p/invoke signatures (external static method definitions) for the exports you want to call in your DLL.

[DllImport("yourdll.dll")]
public static extern int ExportToCall(int argument);

Have a look at the DllImport attribute in the online help.

Lucero
  • 57,903
  • 8
  • 117
  • 151
10

If it's a straight C++ library then it's not possible to reference it in this way.

You have two options, you can compile the C++ library as an assembly an expose the unmanaged code with a C++/CLI wrapper.

-or-

You can use some p/invoke calls if the library exposes it's functionality via a C API.

Could you expand the question a bit to include some details about how you normally call imaging.dll from c++?

Jacob Stanley
  • 4,566
  • 3
  • 32
  • 35
6

if it is a unmanaged dll you cannot add a reference to it. You have to invoke it using pinvoke or the likes of it:

public classFoo

{

[DllImport("myunmanaged.dll", CharSet = CharSet.Ansi)]

private extern static int UnmanagedFunction(int type, int dest);

}

If you wanna convert it to a managed dll take a look here: http://msdn.microsoft.com/en-us/library/aa446538.aspx

If you wanna know some more about pinvoke and dllimport take a look here: http://msdn.microsoft.com/en-us/library/aa288468.aspx

Cheers

The real napster
  • 2,114
  • 7
  • 21
  • 33
3

Calling convention and memory model difference between managed code (C#) and non-Managed code (WIn32 C++) make them incompatible.

However, .NET include a bridge technology for COM objects, so if you imaging DLL were a COM object, it could be made to work. It is, however, apparently not a COM object.

James Curran
  • 98,636
  • 35
  • 176
  • 255
1

If it's a clean C++ DLL where you only export C compatible functions, then you can use P/Invoke to use those functions. If you turn that C++ DLL into a COM DLL with a Type Library, using it is even easier: you can import the type library into .NET and .NET wrappers (called Runtime Callable Wrappers) are created for you.

Dave Van den Eynde
  • 16,734
  • 7
  • 57
  • 88