4

I'm looking to use P/Invoke to allow my C# assembly to interop with a native C library; this needs to be cross-platform (i.e. Mono), so a mixed-mode assembly cannot be used. I'm wondering if there is any performance advantage to using unsafe P/invoke calls, and handling all the conversions to pointers in an unsafe method, versus making the typical "safe" P/Invoke call. Thanks in advance!

CLARIFICATION: I am not looking to use a managed C++ wrapper, as is discussed here. I just want to know if there are performance differences between:

extern static void native_call_here(IntPtr parameter1, String parameter2)

and

extern static void native_call_here(int* parameter1, char* parameter2)

3
  • Possible duplicate: stackoverflow.com/questions/1433334/… Commented Apr 25, 2011 at 20:50
  • Thanks for the link - I actually investigated that on my way to posting. That user is asking for the performance differences between P/Invoke calls and a managed C++ wrapper. I'm looking for the performance differences between two different styles of P/Invoke. Commented Apr 25, 2011 at 20:52
  • Ah, didn't catch that, sorry. Glad it was useful though. Commented Apr 25, 2011 at 20:54

1 Answer 1

4

There is going to be Marshaling at some point. In the former case, the Marshaling occurs as soon as you make the PInvoke method call. In the latter case, since you're calling from unsafe code, you have control over when/how the Marshaling occurs. This could offer a benefit if you're doing multiple operations with unmanaged memory in your unsafe code, but if it's just passing the data through, all you've done is shifted where the Marshaling occurs. If anything, I would expect the PInvoke to be faster in this case, but, as always, profile it if it matters.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.