Vb. NET and C # call APIs

Source: Internet
Author: User
Tags function prototype

C # Call API:


[DllImport ("user32.dll")]
public static extern int GetWindow (int hwnd,int wcmd);
Write the API's * in parentheses. DLL file name, and write the API function prototype on the next line:
Can be written on the function prototype is the permissions, such as public, private, after the permission is static[static], followed by the extern[outside], and then the function of the prototype
such as the following:
[DllImport ("user32.dll")]
public static extern int FindWindowEx (int hwndparent,int hwndchildafter,string lpszclass,string lpszwindow);
[DllImport ("user32.dll")]
public static extern int SendMessage (int hwnd,int wmsg,string wparam,string lParam);
[DllImport ("user32.dll")]
public static extern int FindWindow (string lpclassname,string lpwindowname);

Call the WINDOWSAPI function with visualc#
API functions are the cornerstone of building WINDWS applications, and each of the Windows Application development tools, which provide the underlying functions, indirectly or directly invoke Windows API functions, and generally provide an interface to invoke the WINDOWSAPI function in order to achieve functional expansion. This means that you have the ability to invoke dynamic connection libraries. Visual C #, like other development tools, can also invoke the API functions of a dynamic-link library. NET Framework itself provides a service that allows the governed code to invoke the unmanaged functions implemented in the dynamic-link library, including the Windows API functions provided by the operating system. It is able to locate and invoke the output function, organizing its parameters (integer, String type, array, and structure, etc.) across the interoperation boundary as needed.

The following is an example of C # to briefly describe the basic process of invoking the API:
Declaration of dynamic Link library functions
Dynamic link library functions must be declared before use, relative to the vb,c# function declaration is more verbose, the former through the Api viewer paste, you can directly use, and the latter need to make some additional changes to the parameters of the work.

The Dynamic Link library function Declaration section generally consists of the following two parts, one is the function name or the index number, and the other is the file name of the dynamic link library.
For example, you want to call the MessageBox function in User32.DLL, we have to specify the name of the function MessageBoxA or MessageBoxW, and the name of the library User32.dll, we know Win32 The API typically has two versions of each function involving strings and characters, an ANSI version of Single-byte characters, and a Unicode version of Double-byte characters.

The following is an example of invoking 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);

Where the entry point entrypoint the entry location of the function in the dynamic link library, in a governed project, the original name and ordinal entry point of the objective function not only identifies a function that crosses the interop boundary. Also, you can map the entry point to a different name, which is to rename the function. Renaming can bring convenience to calling functions, by renaming, on the one hand, we don't have to break the brain for the size of a function, and it also guarantees consistency with existing naming conventions, allowing functions with different parameter types to coexist, and more importantly simplifying calls to ANSI and Unicode versions. CharSet is used to identify a function call in Unicode or ANSI version, Exactspelling=false will tell the compiler to let the compiler decide to use Unicode or an ANSI version. Please refer to the MSDN Online Help for additional parameters.

In C #, you can declare a dynamic-link library function in the entrypoint domain by name and ordinal, if the function name used in the method definition is the same as the DLL entry point, </

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.