Use of the C # extern modifier

Source: Internet
Author: User

Extren is primarily used to declare methods that are implemented externally, and what is called an external implementation method, which is generally the introduction of an unmanaged code assembly using the DllImport method of the System.Runtime.InteropServices service. For example, the calling program Api,c the method of language writing, and so on. In this case, the declaration must be static

At the same time, the Extren keyword can also define an external assembly alias so that different versions of the same component can be referenced from a single assembly.

Here is a simple example of rewriting from MSDN, calling the system winmm. DLL Playback WAV file:

Declaration of the call to the system API

[System.Runtime.InteropServices.DllImport ("winmm.") DLL ", EntryPoint =" PlaySound ", SetLastError = True)]

public static extern void PlaySound (string path,system.intptr hmod,playsoundflags flags);

Call this method

string path = @ "C:22.wav";

Try

{

PlaySound (Path, new System.IntPtr (), playsoundflags.snd_sync);

}

catch (Exception ex)

{

Throw (ex);

}

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.