简体   繁体   English

在 C# 中使用 C++ 库

[英]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?我正在尝试在我的 c# 项目中包含 c++ 库 (DLL),但是每次我这样做时,我都会在 VS2008 中收到以下错误消息,有什么建议吗?

EDIT: It's a C++ MFC DLL编辑:这是一个 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.如果它是“普通”DLL(不是 COM,不是托管 C++),则不能添加这样的引用。 You have to add p/invoke signatures (external static method definitions) for the exports you want to call in your DLL.您必须为要在 DLL 中调用的导出添加 p/invoke 签名(外部静态方法定义)。

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

Have a look at the DllImport attribute in the online help.查看联机帮助中的DllImport属性。

If it's a straight C++ library then it's not possible to reference it in this way.如果它是一个直接的 C++ 库,那么就不可能以这种方式引用它。

You have two options, you can compile the C++ library as an assembly an expose the unmanaged code with a C++/CLI wrapper.您有两个选择,您可以将 C++ 库编译为程序集,并使用 C++/CLI 包装器公开非托管代码。

-or- -或者-

You can use some p/invoke calls if the library exposes it's functionality via a C API.如果库通过 C API 公开其功能,您可以使用一些 p/invoke 调用。

Could you expand the question a bit to include some details about how you normally call imaging.dll from c++?您能否稍微扩展一下问题,以包括一些有关您通常如何从 C++ 调用 imageing.dll 的详细信息?

if it is a unmanaged dll you cannot add a reference to it.如果它是非托管 dll,则无法添加对它的引用。 You have to invoke it using pinvoke or the likes of it:您必须使用 pinvoke 或类似的方法调用它:

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如果您想将其转换为托管 dll,请查看此处: 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如果您想了解有关 pinvoke 和 dllimport 的更多信息,请查看此处: 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.托管代码 (C#) 和非托管代码 (WIn32 C++) 之间的调用约定和内存模型差异使它们不兼容。

However, .NET include a bridge technology for COM objects, so if you imaging DLL were a COM object, it could be made to work.但是,.NET 包含 COM 对象的桥接技术,因此如果您将 DLL 映像为 COM 对象,则它可以工作。 It is, however, apparently not a COM object.但是,它显然不是 COM 对象。

If it's a clean C++ DLL where you only export C compatible functions, then you can use P/Invoke to use those functions.如果它是一个干净的 C++ DLL,您只导出与 C 兼容的函数,那么您可以使用 P/Invoke 来使用这些函数。 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.如果您将该 C++ DLL 转换为带有类型库的 COM DLL,则使用它会更容易:您可以将类型库导入到 .NET 中,并且会为您创建 .NET 包装器(称为运行时可调用包装器)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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