About Calling Windows API functions in C # [Introduction]

Source: Internet
Author: User

Reference

Calling Windows API functions in C # [Reference]

 

For Windows system API function calls inProgramSometimes it is essential in design.Programming LanguageThe calling methods and interfaces are both standardized. The Calling methods in C # language are as follows (the following programming environment is Visual Studio. NET ):

1. Add a new class item to the project, open the class file, and add a reference to the following namespace in the file header:

Using system. runtime. interopservices;

In the class definition subject, the reference to the API is added to the static call method. The following API call is used as an example:

/// <Summary>

/// Open and close the CD tray.

/// </Summary>

[Dllimport ("winmm. dll", entrypoint = "mcisendstring", charset = charset. Auto)]

Public static extern int mcisendstring (string lpstrcommand, string lpstrreturnstring, int ureturnlength, int hwndcallback );

/// <Summary>

/// Display and hide the mouse pointer.

/// </Summary>

[Dllimport ("user32.dll", entrypoint = "showcursor", charset = charset. Auto)]

Public static extern int showcursor (INT bshow );

 

/// <Summary>

/// Clear the recycle bin.

/// </Summary>

[Dllimport ("shell32.dll", entrypoint = "shemptyrecyclebin", charset = charset. Auto)]

Public static extern long shemptyrecyclebin (intptr hwnd, string pszrotpath, long dwflags );

 

/// <Summary>

/// Open the browser

/// </Summary>

[Dllimport ("shell32.dll", entrypoint = "ShellExecute", charset = charset. Auto)]

Public static extern int ShellExecute (intptr hwnd, string lpoperation, string lpfile, string lpparameters, string lpdirectory, int nshowcmd );

 

/// <Summary>

/// Maximize the window, minimize the window, normal size window;

/// </Summary>

[Dllimport ("user32.dll", entrypoint = "showwindow", charset = charset. Auto)]

Public static extern int showwindow (intptr hwnd, int ncmdshow );

 

 

2. With the above file, you can call the above API in the event processing of your own form object. The method is as follows:

The following strreturn is a public variable of the string type. apicils refers to the class name created in the first step.

Open the CD tray:

Long lngreturn = apicils. mcisendstring ("set cdaudio door open", strreturn, 127, 0 );

Close the CD tray:

Long lngreturn = apicils. mcisendstring ("set cdaudio door closed", strreturn, 127, 0 );

Show the mouse pointer in the application form:

Apicwalls. showcursor (1 );

Hide the mouse pointer in the application form:

Apicwalls. showcursor (0 );

Clear the recycle bin:

Apicils. shemptyrecyclebin (Form. activeform. Handle, "", 0x00000000 );

Open the browser window. textbox1.text indicates the URL to be accessed:

Long lngreturn = apicils. ShellExecute (Form. activeform. Handle, "open", textbox1.text, "", "", 1 );

Maximum window:

Apicwalls. showwindow (Form. activeform. Handle, 3 );

Minimize window:

Apicwalls. showwindow (Form. activeform. Handle, 2 );

Restore normal size window:

Apicwalls. showwindow (Form. activeform. Handle, 1 );

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.