|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer|
Consuming Unmanaged DLL Functions
Platform invoke is a service that enables managed code to call unmanaged functions implemented in dynamic link libraries (DLLs), such as those in the Win32 API. It locates and invokes an exported function and marshals its arguments (integers, strings, arrays, structures, and so on) across the interoperation boundary as needed. For more information about this service, see A Closer Look at Platform Invoke.
This section introduces several tasks associated with consuming unmanaged DLL functions. In addition to the following tasks, there are general considerations and a link providing additional information and examples.
To consume exported DLL functions
Minimally, you must specify the name of the function and name of the DLL that contains it.
You can use an existing class, create an individual class for each unmanaged function, or create one class that contains a set of related unmanaged functions.
[Visual Basic] Use the Declare statement with the Function and Lib keywords. In some rare cases, you can use the DllImportAttribute with the Shared Function keywords. These cases are explained later in this section.
[C#] Use the DllImportAttribute to identify the DLL and function. Mark the method with the static and extern modifiers.
[C++] Use the DllImportAttribute to identify the DLL and function. Mark the wrapper method or function with extern "C".
For examples that demonstrate how to construct .NET-based declarations to be used with platform invoke, see Marshaling Data with Platform Invoke.