在C ++中创建一个.dll以与C#一起使用 [英] Create a .dll in C++ to be used with C#

查看:125
本文介绍了在C ++中创建一个.dll以与C#一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有.lib文件和.header文件,用C ++编写。我想要一个相同的编译.dll。

如何使用它来构建.dll。创建.dll文件之后我需要在C#项目中使用它。我试过但是每当在我的C#项目中添加引用时它都显示错误。



任何人都可以帮助我



谢谢。

Hi,

I have .lib files and .header files with me written in C++. I want a compiled .dll of the same.
How can i use to build a .dll from them.After Creation of .dll file i need to use it in C# project. I tried but whenever add reference in my C# project it shows error.

Can any one help me

Thanks.

推荐答案

您可以使用常规C ++ DLL并通过P / Invoke通过.NET程序集(C#或非C)使用它,或者您可以使用C ++ / CLI并以常规方式生成常规.NET程序集和使用,通常由C#程序集引用它。



或者,您甚至可以创建混合模式(托管+非托管)C ++ / CLI DLL项目。从.NET程序集的角度来看,这样的项目将生成一个常规的.NET程序集,您也可以引用它。



请参阅:

http ://en.wikipedia.org/wiki/P/Invoke [ ^ ],

http:/ /msdn.microsoft.com/library/en-us/vcmxspec/html/vcmg_PlatformInvocationServices.asp [ ^ ],

http://www.codeproject.com/csharp/EssentialPInvoke.asp [ ^ ],

http:/ /en.wikipedia.org/wiki/C%2B%2B/CLI [ ^ ],

http:// www。 gotw.ca/publications/C++CLIRationale.pdf [ ^ ],

http:// www.ecma-international.org/publications/standards/Ecma-372.htm [ ^ ],

http://msdn.microsoft.com/en-us/library/xey702bw.aspx [ ^ ]。



-SA
You can either use regular C++ DLL and use it by a .NET assembly (C# or not) through P/Invoke, or you can use C++/CLI and produce regular .NET Assembly and use in a usual way, typically, reference it by your C# assembly.

Alternatively, you can even create mixed-mode (managed+unmanaged) C++/CLI DLL project. From the standpoint of a .NET assembly, such project will produce a regular .NET assembly which you can reference as well.

Please see:
http://en.wikipedia.org/wiki/P/Invoke[^],
http://msdn.microsoft.com/library/en-us/vcmxspec/html/vcmg_PlatformInvocationServices.asp[^],
http://www.codeproject.com/csharp/EssentialPInvoke.asp[^],
http://en.wikipedia.org/wiki/C%2B%2B/CLI[^],
http://www.gotw.ca/publications/C++CLIRationale.pdf[^],
http://www.ecma-international.org/publications/standards/Ecma-372.htm[^],
http://msdn.microsoft.com/en-us/library/xey702bw.aspx[^].

—SA


对于非托管c ++ dll,你必须使用dllimport:



http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute%28VS.71%29.aspx [<一个href =http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute%28VS.71%29.aspxtarget =_ blanktitle =New Window> ^ ]



参见此示例:



C#/C与DllImport互操作 [ ^ ]
For an unmanaged c++ dll you must use dllimport:

http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute%28VS.71%29.aspx[^]

See this example:

C#/C interop with DllImport[^]


这篇关于在C ++中创建一个.dll以与C#一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆