Jason Bower's WebLog

PInvoke Calls

I was just pointed to a great website to help you write PInvoke calls. Http://www.pinvoke.net. It's a Wiki that has a bunch of PInvoke calls for common Win32 API's. I was only pointed to this site after my developer and I spent a number of days trying to debug an application crash that was caused by us corrupting memory by improperly marshaling objects between .NET and Win32.

Question to the community:

How could the SDK make it easier for you to write PInvoke calls from .NET?

We have been bouncing around some ideas internally, but I'm curious how people figure out the proper way to write a PInvoke call and how Microsoft could make it easier for you?

 

Published Thursday, February 24, 2005 9:01 PM by jbower

Comments

 

Pavel Lebedinsky said:

Last time I looked at pinvoke.net I wasn't impressed. Lots of incomplete or just plain wrong code (pointer sized parameters marshaled as 32 bit ints etc). Just take a look at the CreateProcessWithLogonW page for example.

The best thing you could do is to provide most common signatures with the framework or the SDK.

Failing that, improve the documentation. Add a few more samples using real win32 APIs. Add some docs describing most common errors and how to debug them. For example, very few people know that pageheap (gflags -p) is a very effective tool for troubleshooting marshaling problems. I've seen people spend weeks chasing mysterious NullReferenceExceptions with an empty stack trace. Usually these symptoms are caused by heap corruption due to bad pinvokes, and pageheap catches such problems instantly.
February 24, 2005 11:34 PM
 

Dean Harding said:

The best thing, I think, is exactly what we got: pinvoke.net. I mean, it tells me exactly how to call a given API. The fact that I don't have to worry about debugging at all is brilliant.

Of course, the problem with pinvoke.net is that it doesn't have every single API I could ever want to call, so I guess at some point, *someone* has to do some debugging...

I've added a few methods to pinvoke.net in my time and I guess the main point is that as long as the information is available there (and accurate) then you don't really need any more!!
February 24, 2005 11:55 PM
 

Lars Wilhelmsen said:

It would have been nice with c/c++ parser (typically for header files) that could output suggestions for p/invoke signatures.
February 25, 2005 4:15 AM
 

Rodney Richardson said:

PInvoke.net is great, as is the VS.NET addin for it - but I don't necessarily trust what's on it. It's inconsistant, and some things are just plain wrong.

The most useful thing would be for Microsoft to add all API calls into the managed framework - but that's probably asking too much. It would be so nice if we didn't have to worry about calling into Win32 APIs.

If Microsoft published a full list of Win32 API signatures, including correct attributes and caveats in an easily accessible way. In fact, if Microsoft could spend some time verifying and updating PInvoke.net, that would improve the situation immensely.
February 25, 2005 6:21 AM
 

Issam Elbaytam said:

It would be great if MS could provide full PInvoke source (not PIA) for user32, kernel32, gdi32, ole those are the most common and could be embedded or used as a learning tool for other PInvoke endeveurs. The source should include XmlDoc from Windows SDK so we don't have to go hunting it down.
A good tool called COM2dotNet from aurigma.com was very useful but seems to be discontinued. MS Should create something similar. it produced clean code from type libraries, better than axImport tool.
February 27, 2005 11:44 PM
Anonymous comments are disabled

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker