views:

212

answers:

3

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?

+1  A: 

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
A: 

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
A: 

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

Fadrian Sudaman
Thanks for the reference!
Michael Covelli