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.