Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using PInvoke vs .NET provided functions

Tags:

c#

pinvoke

From version to version of .NET the more function that's equal to P/Invoke is added to .NET

Now there are 2 questions in my mind.

1) Which one is prefer over the other in term of speed, normally I use .Net function but in tight loop I don't really know which one is going to be faster.

2) Is there any website that provide the list of counter-parts?

like image 472
Athiwat Chunlakhan Avatar asked Apr 02 '26 19:04

Athiwat Chunlakhan


1 Answers

1) If there is a .NET function you should, of course, use it - unless you have a really good reason not to.

2) Yes, there is a page that maps Win32 API functions to .NET methods, though it's quite old - it applies to .NET 1.1. See http://msdn.microsoft.com/en-us/library/aa302340.aspx

Edit: There is no newer list that I'm aware of, but one trick you could try is opening the .NET framework assemblies (like mscorlib, System, etc.) in Reflector and searching for the Win32 method name. If the assembly invokes it internally you should be able to find it this way and then find where it's referenced to trace it to a public method that you can call, if there is any. If there isn't, at least you can copy the P/Invoke signature without having to figure it out.

like image 122
EMP Avatar answered Apr 04 '26 07:04

EMP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!