This article has been excerpted
from book "Graphics
Programming with GDI+".
One important feature of the .NET runtimes is COM and Win32 interoperability.
With runtime interoperability services, developers can use both COM and Win32
libraries in managed applications. The classes related to these services are
defined in the System.Runtime.InteropServices namespace.
We can use COM libraries in managed application by simply adding a reference to
the COM library using the Add Reference option of VS.NET or the Type Library
Importer (Tlbimp.exe) .NET tool. Both of these options allow developers to
convert a COM library of a .NET assembly, which can then be treated as other
.NET assemblies. The graphical user interface (GUI) functionality of Windows is
defined in a Win32 library called Gdi32.dll. Using Win32 libraries in managed
code is a little more difficult than using COM libraries. However, there is
nothing to worry about because the
System.Runtime.InteropServices.DllImportAttribute class allows developers to use
functionality defined in unmanaged libraries such as Gdi32.dll.
The DllImportAttribute Class
The DllImportAttribute class allows developers to import Win32 SDK functionality
into managed applications. The DllImportAttribute constructor is used to create
a new instance of the DllImportAttribute class with the name of the DLL
containing the method to import. For example, the GDI functionality is defined
in Gdi32.dll. So if we want to use GDI functions in our application, we need to
import them using DllImportAttribute. The following code imports the Gdi32.dll
library:
[System.Runtime.InteropServices.DllImportAttribute ("gdi32.dll"]
After adding this code, we're ready to use the functions defined in the
Gdi32.dll library in our .NET application.
Now let's take a look at simple program that uses the MoveFile function of Win32
defined in the KERNAL32.dll library. The code in Listing 14.1 first imports the
library and then calls the MoveFile function to move a file from one location to
another.
LISTING 14.1: Using the Win32 MoveFile function defined in KERNEL32.dll
[System.Runtime.InteropServices.DllImportAttribute
("KERNEL32.dll"]
public
static extern
bool MoveFile
(String src,
String dst);
private
void Move_Click (object
sender, System.EventArgs e)
{
MoveFile ("C:\\outpit.jpeg",
"f:\\NewOutput.jpeg");
}
As with KERNEL32.dll, we can import other Win32 libraries to use them in .NET
applications. The DllImportAttribute class provides six field members, which are
described in Table 14.1.
The CallingConvention enumeration specifies the calling convention required to
call methods implemented in unmanaged code. Its members are defined in Table
14.2.
The DllImportAttribute class has two properties: TypeId and Value. TypeId gets a
unique identifier for an attribute when the attribute is implemented in the
derived class, and Value returns the name of the DLL with the entry point.
TABLE 14.1: DllImportAttribute field members
Method | Description |
CallingConvention | Required to call methods implemented in unmanaged code; represented by the CallingConvention enumeration. |
CharSet | Controls name mangling and indicate how to marshal String arguments to the method. |
EntryPoint | Identifies the name or ordinal of the DLL entry point to be called. |
ExactSpelling | Indicates whether the name of the entry point in the unmanaged DLL should be modified to correspond to the CharSet value specified in the CharSet field. |
PreserveSig | Specifies that the managed method signature should not be transformed into an unmanaged signature that returns an HRESULT structure, and may have an additional argument (out or retval) for the return value. |
SetLastError | Specifies that the callee will call the Win32 API SetLastError method before returning from the named method. |
TABLE 14.2: CallingConvention members
Method | Description |
Cdec1 | The caller cleans the stack. The property enables calling functions withvarargs. |
FastCall | For future use. |
StdCall | The called cleans the stack. This is the default convention for calling unmanaged functions from managed code. |
ThisCall | The first parameter is the this pointer and is stored in the ECX register. Other parameters are pushed onto the stack. This calling convention is used to call methods in classes exported from an unmanaged DLL. |
Winapi | Uses the default platform-calling convention. For example, on Windows it's StdCall, and on Windows CE it's Cdecl. |