C # (8)--p/invoke

Source: Internet
Author: User

First, Intro try

The API in C # is WIN32API, platform invoke (P/invoke), a. NET attribute that calls a function in an unmanaged DLL. The Windows API is a dynamic-link library (DLL) that is part of the Windows operating system. You can use them to perform tasks when it is difficult to write an equivalent procedure yourself.
The advantage of using Windows APIs in code is that they save development time because they contain many useful functions that have been written and are waiting to be used. The disadvantage is that in the event of a failure, the Windows API may be difficult to process and irreversible.

Try the program:


The Windows API represents a special category of interoperability. Windows API does not use managed code and does not have a built-in type library, and it uses a different data type than the data type used in Visual Studio. Because of these differences, and the Windows API is not a COM object, interoperation with the Windows API and the. NET Framework is done by using platform invoke (PInvoke). Platform invoke is a service that enables managed code to invoke unmanaged functions implemented in a DLL ...
Platform invoke is a service that enables managed code to invoke an unmanaged function implemented in a DLL, such as invoking a system's API or dealing with a COM object, by System.Runtime.InteropServices the namespace in order to invoke a function from managed code in an unmanaged DLL. We're going to create a p/invoke wrapper.
In fact, the syntax used to create the P/invoke wrapper is essentially the same as the declarative syntax for creating a managed method. The only difference is that the P/invoke wrapper does not contain the function body, but only the method name, return value type, and parameter information.


Second, the Beep procedure

The Win32 API set is very large. They do not have enough resources to write managed interfaces for all Win32 APIs, test them, and write documents, so they can only prioritize the most important parts.

Many common operations have managed interfaces, but there are many complete Win32 parts that do not have managed interfaces.

C # Calling P/invoke is the most common way to accomplish this task. To call P/invoke in C #, write a prototype that describes how to invoke the function, and the runtime uses this information to make the call.

Another approach is to use Managed Extensions to C + + to wrap functions:

In Beep's attempt:

Can be found:

The definition in MSDN, I found that it has the following prototypes:

    1. BOOL beep{
    2. DWORD dwfreq,//Sound frequency
    3. DWORD dwduration//Sound duration
    4. }

If you write this prototype in C #, you need to convert the Win32 type to the appropriate C # type. Since the DWORD is a 4-byte integer, we can use an int or uint as the C # corresponding type.

Again, because int is a CLS-compliant type (which can be used in all. NET languages), it is more commonly used than uint and the bool type corresponds to BOOL.

Program implementation:


Successfully implemented a randomly generated beep sound playback ~

C # (8)--p/invoke

Related Article

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.