C # based Interface Basics Tutorial VI (5)

Source: Internet
Author: User
The basic tutorial uses the Win32 API in the. NET Framework program through DllImport

The. NET Framework program can access the native code base by means of a static DLL entry point. The DllImport property is used to specify the DLL location that contains the implementation of the 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 callingconvention;
Public CharSet CharSet;
public string entrypoint;
public bool ExactSpelling;
public bool PreserveSig;
public bool SetLastError;
public string Value {get {...}}}
}
}

Description

1, DllImport can only be placed on the method declaration.

2, DllImport has a single positioning parameter: Specifies the DllName parameter that contains the DLL name of the method being imported.

3, DllImport has five named parameters:

A, callingconvention parameters indicate the calling convention for the entry point. If CallingConvention is not specified, the default value Callingconvention.winapi is used.

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.

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

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

E, the PreserveSig parameter indicates whether the signature of the method should be retained or converted. When the signature is converted, it is converted to a signature with an HRESULT return value and a return value of an additional output parameter named RetVal. If PreserveSig is not specified, the default value of True is used.

The F, setlasterror parameter indicates whether the method retains the 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, a method decorated with the DllImport property 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
{//User32.dll class is referenced by DllImport. MessageBox 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);
}
}

The concept of abstract classes is almost always used in object-oriented programming languages, and abstract classes provide greater flexibility for the realization of abstract things. C # is no exception, and C # deepens the application of abstract classes through the technology of covering virtual interfaces. For more information on this, see the next section-Overwrite virtual interface


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.