简体   繁体   中英

Using c++ library in c#

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.

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.

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++?

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM