воскресенье, 7 марта 2010 г.

Calling Win32 DLLs in C# with P/Invoke






Enter P/Invoke
Style
The DLL Import Attribute
Optional DllImportAttribute Properties
Data Marshaling
Marshaling Numerical and Logical Scalars
Parameters that are Pointers
Marshaling Opaque Pointers: a Special Case
Marshaling Text
Summing it Up
I have noticed a trend in my programming of late, and that trend has inspired the topic of this month's column. Recently, I have done a fair amount of Win32® Interop in my Microsoft® .NET Framework-based apps. I am not saying that my apps are full of custom interop code, but from time to time I bump into a minor, but nagging, inadequacy in the .NET Framework Class Library that can quickly be alleviated by a call into the Windows® API.
As I think about it, any feature limitation in the .NET Framework version 1.0 or 1.1 class library that is not shared by Windows doesn't come as a huge surprise. After all, 32-bit Windows, in all of its incarnations, is a mature operating system that has served a wide breadth of customers for over a decade. The .NET Framework is, in comparison, a newcomer.
As an increasing developer base moves its production applications to managed code, it seems only natural that there will be even more occasions for developers to dip down into the underlying operating system for some critical tidbit of functionality—at least for the time being.
Thankfully, the interop features of the common language run-time (CLR), called Platform Invoke (P/Invoke), are very complete. In this column I am going to focus on the practical use of P/Invoke for calling Windows API functions. P/Invoke is used as a noun when referring to the COM Interop functionality of the CLR and is used as a verb when referring to the use of this feature. I am not going to address COM Interop directly because it is paradoxically both more accessible and more complex than P/Invoke, making it less straightforward as a column topic. 


http://msdn.microsoft.com/en-us/magazine/cc164123.aspx

Комментариев нет: