in
The Win32 API is used in the .NET framework program through DLLIMPO
The .NET framework program can access the native code base by static DLL entry points. The DLLIMPORT property is used to specify a DLL location that contains an external method.
The DLLIMPORT attribute is defined as follows:
Namespace System.Runtime.InteropServices
{
[AttributeUSAG (AttributeTargets.method)]
Public Class DllimportAttribute: System.attribute
{
Public Dllimportattribute (String Dllname) {...}
Public CallingConvention CallingConvention;
Public Charset Charset;
Public String Entrypoint;
Public bool exactspell;
Public Bool PreservesIg;
Public bool setlasterror;
Public String Value {Get {...}}
}
}
Description:
1. DLLIMPORT can only be placed on the method statement.
2, DLLIMPORT has a single positioning parameter: specify a dllname parameter that contains the DLL name of the imported method.
3, DLLIMPORT has five named parameters:
A, the CallingConvention parameter indicates the invocation of the entry point. If you do not specify a callingconvention, use the default value CALLINGCONVENTION.WINAPI.
b, the Charset parameter indicates the character set in the entry point. If a charset is not specified, use the default value charset.Auto.
c, the entryPoint parameter gives the name of the DLL in the mouthpoint. If EntryPoint is not specified, the name of the method itself is used.
D, the exactspelling parameter indicates whether EntryPoint must match the spelling of the indicated entry point. If you do not specify EXACTSPELLING, use the default value false.
E, the signature of the preservesig parameter indication method should be retained or converted. When the signature is converted, it is converted into a signature of an additional output parameter named RETVAL with the HRESULT return value and the return value. If PRESERVESIG is not specified, use the default true true.
f, setLastError parameter indication method preserved Win32 "Previous Error". If setLastError is not specified, the default value FALSE is used.
4, it is a one-time property class.
5. In addition, the method of modifying the DLLIMPORT attribute must have an extern modifier.
Below is an example of C # calling the win32 messagebox function:
Using system;
Using system.Runtime.InteropServices;
Class Mainapp
{// Reference User32.dll class via DLLIMPORT. MessageBox comes from user32.dll class
[DLLIMPORT ("User32.dll", entrypoint = "mess")]]
Public Static Extern Int MessageBox (int hwnd, strmessage, string strcaption, uint uitype);
Public static void main ()
{
Messagebox (0, "Hello, this is pinvoke!", "
.NET ", 0);
}
}