Use the Win32 API through dllimport In the. NET Framework Program

Source: Internet
Author: User

In the. NET Framework Program Using the Win32 API through dllimport

The. NET Framework Program can access the local machine through the static DLL entry point. Code Library. The dllimport attribute is used to specify the DLL location that contains the implementation of an external method.

The dllimport attribute is defined as follows:

Namespace system. runtime. interopservices
{
[Attributeusage (attributetargets. Method)]
Public class dllimportattribute: system. Attribute
{
Public dllimportattribute (string dllname ){...}
Public callingconvention;
Public charset;
Public String entrypoint;
Public bool exactspelling;
Public bool preservesig;
Public bool setlasterror;
Public String Value {get {...}}
}
}

Note:

1. dllimport can only be placed on method declaration.

2. dllimport has a single positioning parameter: Specify the dllname parameter that contains the DLL name of the imported method.

3. dllimport has five naming parameters:

A. The callingconvention parameter indicates the call convention of the entry point. If no callingconvention is specified, use the default value callingconvention. winapi.

B. The charset parameter indicates the character set used in the entry point. If charset is not specified, the default value charset. Auto is used.

C. The entrypoint parameter specifies the name of the DLL entry point. If entrypoint is not specified, the method name is used.

D. The exactspelling parameter indicates whether the entrypoint must exactly match the spelling of the indicated entry point. If exactspelling is not specified, use the default value false.

E. The preservesig parameter indicates whether the method signature should be retained or converted. When the signature is converted, it is converted to a signature with an additional output parameter named retval that has the hresult return value and the return value. If preservesig is not specified, the default value true is used.

The F and setlasterror parameters indicate whether the method retains Win32 "previous error ". If setlasterror is not specified, the default value false is used.

4. It is a one-time attribute class.

5. In addition, the method modified with the dllimport attribute must have an extern modifier.

The following is an example of C # calling the Win32 MessageBox function:

Using system;
Using system. runtime. interopservices;
Class mainapp
{// Reference the user32.dll class through dllimport. MessageBox comes from the user32.dll class
[Dllimport ("user32.dll", entrypoint = "MessageBox")]
Public static extern int MessageBox (INT hwnd, string strmessage, string strcaption, uint uitype );
Public static void main ()
{
MessageBox (0, "Hello, this is pinvoke! ",". Net ", 0 );
}
}

Object-oriented Programming Language Abstract classes provide more flexibility for abstract objects. C # is no exception. C # deepens the application of abstract classes by overwriting virtual interfaces. For more information, see the next section-overwrite virtual interfaces.

 

Address: http://blog.csdn.net/abaowu/archive/2004/11/24/193626.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.