Lazy PInvoke Tips
So you need to write code that calls into a native API. You could spend time writing/testing the code needed to do this but odds are that if you are working with a common API then somebody else has already done this and posted it on the web.
First, there are two resources you should check with:
- The PInvoke.NET wiki
- The .NET CF PInvoke Library sample code
If those don't work, Google is your friend. Unfortunately, “PInvoke” isn't a very good search term since people will often not use the term at all and when they do, different people write it in different ways (p-invoke, p/invoke, and platform invoke are all common alternatives). So instead of using PInvoke in your search, you should use “DllImport”. People rarely ask “How to I DllImport XYZ?” but anyone giving an accurate answer is guaranteed use this term in their response since DllImport is the attribute used to call a native function from C# or VB.NET.
For example, here's a simple one... suppose you want to call RegCreateKeyEx. You could search for “pinvoke regcreatekeyex” and get back a small set of pretty useless results. Alternatively, you could search for “dllimport regcreatekeyex” which brings back a buch of results that are all right on the money.
[Author: Robert Levy]
Comments
- Anonymous
April 22, 2004
FYI, DllImport is optional in VB.NET (ref: http://www.webtropy.com/articles/art9-1.asp?f=RegCreateKeyEx) - Anonymous
April 22, 2004
Oh cool - thanks! So now you know which language I don't use :) - Anonymous
April 22, 2004
In this particular case I'd say use OpenNETCF's OpenNETCF.Win32.Registry family of classes, modelled very closely after Microsoft.Win32.Registry from the desktop Framework.
How much of this stuff has made it into Compact Framework 2.0? - Anonymous
April 22, 2004
ed.rocinantesoftware.com » Lazy PInvoke Tips - Anonymous
April 22, 2004
I have to echo Mike's remarks in that there's a little outfit I know that does a pretty mean PInvoke library. The name escapes me... I think it's OpenNETCF.org or something like that :) - Anonymous
April 23, 2004
OpenNETCF.org does have some awesome reusable code for common scenarios but often people simply want to know how to wrap a specific function. My suggestion for OpenNETCF.org would be to get the source code listings (http://www.opennetcf.org/SourceBrowse/browse.aspx) indexed by Google so they show up when people search using the DllImport keyword. - Anonymous
April 23, 2004
Good idea. I'll get our right on to it.