P/Invoke (Platform Invoke) links and resources
Let's admit it - P/Invoke is inevitable. Even with the extensive BCL (Base Class Library), there's more than once that we've had to resort to Win32 API calls. Then there are 3rd party dlls and legacy code. However, the .NET framework does make it quite a bit easier to interop with unmanaged code - P/Invoke being one of the techniques (apparently, interop seems to have been improved a lot more in .NET Framework v2.0 - so look out for that). P/Invoke is used to execute unmanaged DLL functions (exported functions). There are several good resources on P/Invoke but of course you wouldn't expect them to be all at one place, would you? To make it easy on my weak little brain and for others who might stumble across this post looking for P/Invoke stuff, I thought I'd consolidate whatever I've gathered into one blog post. So here you go...
First place to start would be MSDN:
MSDN Interop Link:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconInteroperatingWithUnmanagedCode.asp
This has extensive information on general interop techniques including samples, walkthroughs, etc
MSDN Consuming Unmanaged DLL Functions with P/Invoke:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconconsumingunmanageddllfunctions.asp
This one is specific to P/Invoke - has samples, etc. Gives you a fair idea of how to go about calling an exported function with all the semantics that are involved. Good place to start off..
One of the most important aspect of P/Invoke is Marshalling Data Types across the Managed-Unmanaged boundary. Its very important to match up the data types between the managed and unmanaged worlds or else you could end up in a lot of misery !
MSDN Marshalling Data with Platform Invoke (this link is on the page pointed to by the previous link):
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconmarshalingdatawithplatforminvoke.asp
Definitely read up on this one or else you're going to have a hard time figuring out what data type to use when trying to write your API prototype. There are lot of links from this page - the ones under 'In This Section' are particularly important. This reading will save you a lot of time especially if you're going to be using P/Invoke a whole lot.
Platform Invoke Cheat Sheet
This is an excellent resource for P/Invoke. It has a lot of articles, tutorials, other links, etc. It also has information on P/Invoke for the Compact Framework(which I'm totally oblivious to). Should be very helpful..
Here's a blog you would want to read up on - Adam Nathan's Interop-Centric CLR Blog:
http://blogs.msdn.com/adam_nathan
He's a test lead on the CLR team and knows almost anything you could think about interop between managed and unmanaged code. In fact, he even wrote the book:
.NET and COM: The Complete Interoperability Guide
If you're into a quite a bit of interop, then this book is for you..
Here's one of his articles on interop as well:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/manunmancode.asp
Here's another site I think is very interesting -
N/Direct - The .NET Interoperability Resource Center
It is what it says..resource for .net interop...excellent site with good information P/Invoke and Interop in general. It's got good samples and examples especially ones that are otherwise hard to find on other interop resources (eg. marshalling variable length structures, marshalling a structure containing an array of structures, etc etc) Pretty good stuff..maintained by a guy named Mattias Sjögren who's a real master on interop..definitely check it out..
Having a hard time figuring out how to declare prototypes for Win32 APIs? Need the signature in VB.NET and/or C#? Then this one's for you...www.pinvoke.net. Its got signatures for tons of Win32 APIs grouped by which library they belong to (kernel32, user32, etc) and since its a wiki site, people keep updating it as time goes by with VB.NET/C# signatures for more and more Win32 API functions. Some of them also have sample code which should give you a head start. Now, they also have a Visual Studio Add-In so you can directly get the signature for a particular API from within the VS.NET IDE - pretty cool, isn't it? You can submit signatures/samples either from the website or from within the IDE with the Add-In.
That's about all I've got right now on P/Invoke...I'm sure I've missed some..I'll keep adding more as and when I remember more resources on this. If you've got some that I've missed, feel free to add them to the comments and I can add it to this post - of course, including courtesy


2 Comments:
Imu, I've been meaning to ask you this for some time so here goes.
We have a C# module that is connected via a web service module in ASP.NET to clients. Now, there are these HUGE objects in C# that we need to save across sessions for a client. Two ways of doing this that we could identify are:
1. To store the state of the object and every time the client connects, start from the initial state and compute the object to the current state.
+ve of this technique: less information to be stored
-ve : too much computation time (if the state is too far away)
2. To store the object itself.
--> Dunno how you can store an object and then be able to retrieve it every session...any suggestions or pointers are appreciated.
Thanks,
Arn
thats tough
Bikramjit Singh
Post a Comment
<< Home