Examples of external system functions that are frequently called by. net

Source: Internet
Author: User
Tags mscorlib
Common usage of the dllimport attribute. Section 1 describes the advantages of using dllimport to call local code from a hosted application. The second section focuses on each aspect of the mail handling and dllimport attributes.

Call unmanaged code from a hosted application
The dllimport attribute is useful when you reuse existing unmanaged code in a hosted application. For example, a hosted application may need to call an unmanaged Win32 API.

The following code example describes the general scheme. MessageBox (in user32.lib) is called ):

# Using <mscorlib. dll>
Using namespace system: runtime: interopservices;
// For dllimportattribute

Namespace syswin32
{
[Dllimport ("user32.dll", entrypoint = "MessageBox", charset = Unicode)]
Int MessageBox (void * hwnd, wchar_t * lptext, wchar_t * lpcaption,
Unsigned int utype );
}

Int main ()
{
Syswin32: MessageBox (0, l "Hello world! ", L" greetings ", 0 );
}
The main note is the line of code that contains dllimport. This code line notifies the compiler based on the parameter value to declare the function in user32.dll and treats all strings (such as parameters or return values) in the signature as Unicode strings. If the entrypoint parameter is missing, the default value is the function name. In addition, because the charset parameter specifies Unicode, The Runtime Library of the common language will first look for functions called messageboxw (W is due to the Unicode Specification. If the Runtime Library does not find this function, it searches for MessageBox and the corresponding modifier according to the call conventions. Supported call conventions include _ cdecl and _ stdcall.

When calling a function contained in a User-Defined DLL, it is necessary to add extern "C" before the DLL function declaration, as shown below:

// The function declaration in sampledll. h file
Extern "C" sampledll_api int fnsampledll (void );
For more information about supported other parameter values, see dllimport.

Hosting and sending unstructured parameters is not managed
In addition to the preceding method, you can use another method to encapsulate managed parameters (from managed applications) into unmanaged parameters (In unmanaged DLL ).

The following code example describes the sending and receiving processing technology:

# Using <mscorlib. dll>
Using namespace system; // to bring system: String in
Using namespace system: runtime: interopservices;
// For dllimportattribute
Namespace syswin32
{
[Dllimport ("user32.dll", entrypoint = "MessageBox", charset = Unicode)]
Int32 MessageBox int32 (hwnd, string * lptext, string * lpcaption,
Uint32 utype );
}

Int main ()
{
Syswin32: MessageBox (0, s "Hello world! ", S" greetings ", 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.