Using c++ library in c#

后端 未结 5 1056
生来不讨喜
生来不讨喜 2020-11-27 21:40

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\'

相关标签:
5条回答
  • 2020-11-27 21:57

    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

    0 讨论(0)
  • 2020-11-27 21:58

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

    0 讨论(0)
  • 2020-11-27 22:00

    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.

    0 讨论(0)
  • 2020-11-27 22:05

    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.

    0 讨论(0)
  • 2020-11-27 22:18

    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.

    0 讨论(0)
提交回复
热议问题