Use Visual C # to call Windows API functions

Source: Internet
Author: User
Tags versions
Api functions are the cornerstone of building Windws applications. For every Windows application development tool, the underlying functions provided by API functions call Windows Api functions indirectly or directly, at the same time, in order to achieve function expansion, interfaces for calling WindowsAPI functions are generally provided, that is, they can call dynamic connection libraries. Visual C # can call the API functions of the dynamic link library like other development tools .. NET Framework itself provides such a service that allows governed code to call non-governed functions implemented in the dynamic link library, including Windows API functions provided by the operating system. It can locate and call the output function, and organize its parameters (integer, string type, array, and structure) to span the interoperability boundary as needed.

The following uses C # as an example to briefly introduce the basic process of calling an API:
Dynamic library function declaration
The function of the dynamic link library must be declared before use. Compared with VB, C # function declaration is even more difficult. The former can be directly used after being pasted through the Api Viewer, the latter requires extra parameter changes.

The function declaration part of the dynamic link library is generally composed of the following two parts: one is the function name or index number, and the other is the file name of the dynamic link library.

For example, if you want to call the MessageBox function in User32.DLL, you must specify the function name MessageBoxA or MessageBoxW, and the library name User32.dll, we know that Win32 API generally has two versions for each function involving strings and characters, the ANSI version of single-byte characters and the UNICODE version of double-byte characters.

The following is an example of calling an API function:
[DllImport ("KERNEL32.DLL", EntryPoint = "MoveFileW", SetLastError = true,
CharSet = CharSet. Unicode, ExactSpelling = true,
CallingConvention = CallingConvention. StdCall)]
Public static extern bool MoveFile (String src, String dst );

The entry point EntryPoint identifies the function's entry position in the dynamic link library. In A governed project, the original name and serial number entry point of the target function not only identifies a function that spans the interoperability boundary. You can also map the entry point to a different name, that is, rename the function. Renaming can bring various conveniences to function calling. By renaming, on the one hand, we don't have to worry about the case sensitivity of the function, and it can also ensure that it is consistent with the existing naming rules, allows functions with different parameter types to coexist. More importantly, it simplifies calls to ANSI and Unicode versions. CharSet is used to identify the Unicode or ANSI version used for function calls. ExactSpelling = false tells the compiler to decide whether to use Unicode or Ansi. For other parameters, see the MSDN online help.
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.