Real VC ++. Net Note 3-hosting C ++ to call API functions

Source: Internet
Author: User

Recently, I encountered a simple problem: how to use the. Net program to pop up the optical drive?
One search on the Internet is an answer:
Mcisendstring ("set cdaudio door open wait", null, 0, null );
What is MCI? Is a media control interface. So this is under the Win32 API
Function, which is directly referenced in. NET and cannot be compiled.
Is there any alternative function under. Net? I have not found it yet. Can you just stare at it?
How can I swallow this breath if it is clearly there but it cannot be used?

The solution is "platform call". In other words, it is to use the unmanaged C ++ under the hosted C ++.

// ================================================ ======================================




When a platform call calls an unmanaged function, it performs the following operations in sequence:

  1. Find the DLL that contains the function.

  2. Load the DLL to the memory.

  3. Find the address of the function in the memory and push its parameters to the stack to block the required data.

  4. Transfers control to an unmanaged function.

// ================================================ ====================================

The above is a detailed description of Platform calls in msdn. It doesn't matter if you have read it yet. Follow me!

To call an unmanaged function, perform the following four steps:

1. Identify the function in DLL.

At the minimum, you must specify the name of the function and the name of the DLL that contains the function.

This is the first task to be done. Find out the DLL name of the unmanaged function you want to use. Here there is a general rule for your reference: for example, the MessageBox of traditional C ++ The function is in the user32.lib library, so we can determine that the corresponding code is in user32.dll.

In this case, the mcisendstring function is in winmm. lib and we need to mark winmm. dll.

2. Create a class to accommodate DLL Functions.

You can use an existing class to create a separate class for each unmanaged function, or create a class that contains a group of related unmanaged functions.

3. Create the original type in the managed code.

[C ++] usageDllimportattributeIdentifies the DLL and function. UseExtern "C"Mark the packaging method or function.

4. Call the DLL Function.

Call methods on the Managed class just like any other managed method.
 

In fact, the actual code for the above four steps is as follows:

Using namespace system: runtime: interopservices; // namespace of the dllimportattribute class

[Dllimportattribute ("winmm. dll", charset = charset: auto)]

Extern "C" int mcisendstring (

String * lpszcommand,

String * lpszreturnstring,

Int cchreturn,

String * hwndcallback

);

The red part can be applied directly without modification. Pay attention to the following changes:

Int mcisendstring (// prototype created in the managed code

String * lpszcommand,

String * lpszreturnstring,

Int cchreturn,

String * hwndcallback

);

You can compare the changes in the Declaration between mcisendstring and the original API function, and find out the reasons for this change.

The following is the call code:

Mcisendstring ("set cdaudio door open wait", null, 0, null );

// Actually, it is called like a common function.

 

Of course, the actual header file must be included:

# Include "stdafx. H" // This is not just a header file.

# Include <windows. h>

 

Finally, I wish the compilation went smoothly !;)

 

[WQL]

 

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.