简体   繁体   English

从C#调用非托管.dll的性能

[英]Performance of Calling Unmanaged .dll from C#

How long is the typical overhead added by calling a .dll written in C++ from a C# application using the following syntax? 使用以下语法从C#应用程序调用C ++编写的.dll会增加多长时间?

[DllImport("abc.dll", EntryPoint = "xcFoo", CallingConvention = CallingConvention.Cdecl)]
public extern static Result Foo(out IntPtr session,
                [MarshalAs(UnmanagedType.FunctionPtr)]ObjectCallback callback, 
                UInt64 turnKey,
                string serverAddress, 
                string userId, 
                string password);

Is there a more efficient way to do it? 有更有效的方法吗?

Check out this article on how to improve interop performance. 查看这篇文章,了解如何提高互操作性能。 What to do and what best to avoid. 该做什么以及最好避免什么。

http://msdn.microsoft.com/en-us/library/ms998551.aspx http://msdn.microsoft.com/en-us/library/ms998551.aspx

Are you talking about the overhead of invoking the native method? 你在谈论调用本机方法的开销吗? If so, I dont think it is significant at all, as there are a lot of such calls in the .NET framework class libraries. 如果是这样,我认为它根本不重要,因为.NET框架类库中有很多这样的调用。

Now, whether the overhead is significant for your scenario can only be answered by doing performance measurements, and comparing them against what you expect. 现在,只有通过执行性能测量并将它们与您期望的进行比较,才能回答开销对于您的场景是否重要。

The marshalling into the native method will cost three memory allocations from the NT heap, which is not so bad. 编组到本机方法将花费三个来自NT堆的内存分配,这并不是那么糟糕。 It's the delegate back that gets worrysome. 这是代表回来的问题。

我知道这个问题已经过时但我已经设法快速调用本机函数,不仅使用calli CIL指令而且还使用特殊技巧,但是如果你处理包括字符串在内的复杂类型,你需要自己处理pinnig和/或编组参数。 。

A good method to check this sort of thing is throw in a break point where you make calls. 检查此类事情的一个好方法是在一个断点处进行调用。 Don't know when the library is loaded, so maybe only check the break point on the second call (unless loading cost is your main concern). 不知道何时加载库,因此可能只检查第二次调用的断点(除非加载成本是您主要考虑的因素)。 Then open up the disassembly window in visual studio and see how many lines there are before your dll function is being invoked. 然后在visual studio中打开反汇编窗口,看看在调用dll函数之前有多少行。

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

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