简体   繁体   English

C#托管dll调用还是非托管dll调用?

[英]C# managed dll call or unmanaged dll call?

I was asking to do two dll calls from our application. 我要求从我们的应用程序执行两个dll调用。 These two dlls are from other group and other company. 这两个dlls来自其他组和其他公司。 Have read a little about managed and unmanaged. 已阅读有关托管和非托管的内容。 I would prefer to do managed call. 我希望进行托管通话。 But whether use managed or unmanaged is the decision of the caller only or it also depends on the callee? 但是使用托管还是非托管是仅由呼叫者决定还是取决于被呼叫者? All dlls can be called with managed code? 可以用托管代码调用所有dll吗? If callee is also a factor, how can I know this dll can be called with managed code? 如果被调用方也是一个因素,我怎么知道可以使用托管代码来调用此dll?

If the DLL is written in managed code it'll be a managed call. 如果DLL是用托管代码编写的,那么它将是托管调用。 Otherwise you'll have to do an unmanaged call. 否则,您将不得不进行非托管呼叫。 Both parts have to be managed for it to be a managed call. 这两个部分都必须进行管理才能成为托管呼叫。

I just means a .Net dll or a non .Net call. 我只是说一个.Net dll或一个非.Net调用。

Managed code will remove a level of risk when interoperating with an uncontrolled codebase as you have all the same .Net datatypes and call semantics are the same. 与所有不受控制的代码库进行互操作时,托管代码将消除一定程度的风险,因为您拥有所有相同的.Net数据类型和调用语义。

Depending what language your DLL was written in if it isn't .Net you may have issues with the order of arrays and how to push object references onto the stack. 如果不是.Net,则取决于您的DLL用什么语言编写,因此数组的顺序以及如何将对象引用推入堆栈可能会遇到问题。 Plus you may be forced to pin objects in memory to call a DLL safely if it performs mutations on a given object/primitive value directly. 另外,如果DLL直接在给定的对象/原始值上执行突变,则可能会强制将对象固定在内存中以安全地调用DLL。

You definately need more information about the DLLs you are trying to use. 您肯定需要有关您尝试使用的DLL的更多信息。

If the DLL is unmanaged, you are going to need additional information about the exported functions so that you can write the appropriate P/Invoke information to make the calls. 如果DLL是不受管的,则将需要有关导出函数的其他信息,以便可以编写适当的P / Invoke信息来进行调用。 See this MSDN Article or http://pinvoke.net for examples. 有关示例,请参见此MSDN文章http://pinvoke.net

If the DLLs are COM based or .NET, then you can add a reference in your project through Visual Studio and access them directly. 如果DLL是基于COM或.NET的,则可以通过Visual Studio在项目中添加引用,然后直接访问它们。

I would suggest writing your DLL in .NET to handle your managed code calls. 我建议在.NET中编写DLL来处理托管代码调用。 If you set your .net assembly as COM visible and use the regasm tool you can register and call the same object with unmanaged code. 如果将.net程序集设置为COM可见并使用regasm工具,则可以使用非托管代码注册并调用同一对象。

[ComVisible(true)]

http://edn.embarcadero.com/article/32754 http://edn.embarcadero.com/article/32754

http://msdn.microsoft.com/en-us/library/tzat5yw6(VS.71).aspx http://msdn.microsoft.com/en-us/library/tzat5yw6(VS.71).aspx

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

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