. NET Framework programs use the Win32 API

Source: Internet
Author: User

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 property 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 positional parameter: Specifies the DllName parameter that contains the DLL name of the imported method.

3. DllImport has five named parameters:

A, the CallingConvention parameter indicates the calling convention of the entry point. If CallingConvention is not specified, the default value of 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 of 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 of false is used.

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

The F, setlasterror parameter indicates whether the method retains Win32 "previous error". If SetLastError is not specified, the default value of false is used.

4, it is a one-time attribute class.

5. In addition, methods decorated 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
{//User32.dll class is referenced by 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);
}
}

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.