在 C# 中使用 C++ 库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/772041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 00:31:34  来源:igfitidea点击:

Using c++ library in c#

c#visual-studiovisual-c++

提问by Prashant Cholachagudda

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.
---------------------------
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.

采纳答案by Lucero

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.

如果它是“普通”DLL(不是 COM,不是托管 C++),则不能添加这样的引用。您必须为要在 DLL 中调用的导出添加 p/invoke 签名(外部静态方法定义)。

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

Have a look at the DllImportattribute in the online help.

查看联机帮助中的DllImport属性。

回答by Jacob Stanley

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 的详细信息?

回答by James Curran

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. It is, however, apparently not a COM object.

但是,.NET 包含 COM 对象的桥接技术,因此如果您将 DLL 映像为 COM 对象,则它可以工作。但是,它显然不是 COM 对象。

回答by Dave Van den Eynde

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.

如果它是一个干净的 C++ DLL,您只导出与 C 兼容的函数,那么您可以使用 P/Invoke 来使用这些函数。如果您将该 C++ DLL 转换为带有类型库的 COM DLL,则使用它会更容易:您可以将类型库导入到 .NET 中,并且会为您创建 .NET 包装器(称为运行时可调用包装器)。

回答by The real napster

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:

如果它是非托管 dll,则无法添加对它的引用。您必须使用 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

干杯