9

How long is the typical overhead added by calling a .dll written in C++ from a C# application using the following syntax?

[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?

Michael Covelli
  • 2,113
  • 4
  • 27
  • 41

5 Answers5

7

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

Mark Simpson
  • 23,245
  • 2
  • 44
  • 44
Fadrian Sudaman
  • 6,405
  • 21
  • 29
1

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.

Now, whether the overhead is significant for your scenario can only be answered by doing performance measurements, and comparing them against what you expect.

feroze
  • 7,380
  • 7
  • 40
  • 57
0

The marshalling into the native method will cost three memory allocations from the NT heap, which is not so bad. It's the delegate back that gets worrysome.

Joshua
  • 40,822
  • 8
  • 72
  • 132
0

i know this question is old but ive managed to call native functions blazingly fast with not only using calli CIL instruction but also with special trick, but ofc you need to handle pinnig and/or marshalling arguments yourself if you deal with complex types including strings..

TakeMeAsAGuest
  • 957
  • 6
  • 11
0

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.