C # and API

zhaozj2021-02-16  50

API (Application Programming Interface) is a set of commands, which interfaces the programs with the processors. The most commonly used set of external procedures are those that make up Microsoft Windows itself. The Windows API contains thousands of functions, structures, and constants that you can declare and use in your projects. Those functions are written in the C language, however, so they must be declared before you can use them. The declarations for DLL procedures can become fairly complex. Specifically to C # it is more complex than VB . You can use the API Viewer Tool To Get API Function Declaration But You Have To Keep In Mind The Type Of Parameter Which is Different In C #. API (Application Programming Interface) is a command set for the program and processor interface. The most commonly used process is called outside the Microsoft Windows internal process. Windows APIs include thousands of functions, structures, and constants that you can use. These functions are written in C, before using them, you must declare. Defining a DLL process will be quite complex, even complicated than VB. You can use the API Viewer tool to get a declaration of the API function, but you must note that its parameter type is different from C #. Most of the advanced languages ​​support API programming. The Microsoft Foundation Class Library (MFC) framework encapsulates a large portion of the Win32 (API). ODBC API Functions are useful for performing fast operations on database. With API your application can request lower-level services to perform on computer's operating system. As API supports thousands of functionality from simple Message Box to Encryption or Remote computing, developers should know how to implement API in their program. most of the high-level language support API, Microsoft Foundation Class library (MFC Most of the Win32 API packaged. The ODBC API is great to improve the speed of operation of the database. Using the API, you can request a more underlying system service. API provides support from a simple dialog to complex encryption.

Developers should know how to use the API API HAS MANY TYPES Depending On OS, Processor and FunctionAns Depending On OS, (For different operating systems, processors ..........) OS Specific API: Operating System Unique API : Each operating system has common set of API's and some special eg Windows NT supports MS-DOS, Win16, Win32, POSIX (Portable Operating System Interface), OS / 2 console API and Windows 95 supports MS-DOS, Win16 and Win32 APIs, Each operating system has a common API and a proprietary API. For example: Windows NT supports MS-DOS, WIN16, WIN32, POSIX (Portable Operating System Interface), OS / 2 Console API; and Windows 95 Supports MS-DOS, WIN16 and WIN32 API. Win16 and Win32 API: Win16 IS An API CREATED for 16-Bit Processor and Relies on 16 Bit Values. IT Has Platform Independent Nature EG You Can Tie Win16 Programs To MS-DOS Feature Like Tsr Program, Win16 is a 16-bit processor And use a 16-bit value, it is a separate platform. For example: You can run the TSR program in the MS-DOS environment. Win32 IS An API CREATED for 32-Bit Processor and Relies On 32 Bit Values. IT IS Portable To any Operating System, Wide Range of Processors and Platform Independent Nature. Win32 is a 32-bit processor and uses a 32-bit value. He can be used for any operating system, which is widely used. Win32 APRARY NAME E.G. Kernel32, the DLL of the USER32 ETC? Win32 API is generally 32 suffix, such as kernel32, USER32, etc. All Apis Are Implement Using 3 Libraries. All APIs are implemented in the 3 DLL below. KERNEL User GDI

Kernel

It is the library named kernel32.dll, Which supports Capabilities That Are Associated with Os Such AS

Its library name is: kernel32.dll, it is an API set for operating system management

Loading Process Context Switching. File I / O. File Operation Memory Management. Memory Management

E.G. The GlobalMemoryStatus Function Obtains Information About The System's Current USAGE OF Both Physical and Virtual Memory

For example, the GlobalMemoryStatus function gets information about the current system physical virtual memory.

2. User

This is the library named "User32.dll" in Win32. Under Win32, its library name is User32.dll

This allows managing the entire user interface, SUCH AS

It manages all user interfaces, such as:

Windows Window Menus Menu Dialog Boxes Dialog Icons ETC., Icon, etc.

E.G. The Drawicon Function Draws An Icon OR CURSOR INTO The Specified Device Context.

For example: Drawicon draws an icon on the specified device.

3. GRAPHICAL Device Interface

This is the library named "gdi32.dll" in Win32. It is graphic output library. Using GDI Windows Draws Windows, Menus and Dialog Boxes.

This DLL is GDI32.DLL, which is responsible for the output of the image, using GDI to draw the window, menu, dialog

IT CAN CREATE GRAPHICAL OUTPUT.

Output image

IT can also use for storing graphical images.

Store image

E.G. The CreateBitmap Function Creates A Bitmap with The Specified Width, Height, And Color Format (Color Planes and Bits-Per-Pixel).

For example, the CreateBitMap function creates a bitmap specifying the width, height, and color format.

C # and API: Implementing API in C # is tuff job for beginners Before implementing API you should know how to implement structure in C #, type conversion, safe / unsafe code, managed / unmanaged code and lots more C # in the API tools early. Scholars are quite good. Before using the API in C #, you should first know how to use the structure, type conversion, security and unsafe code in C #. Before implementing complex API we will start with simple MessageBox API. To implement code for MessageBox API open new C # project and add one button. When button gets clicked the code will display Message Box. Before using complex api, we will start with a simple Messagebox API as a list. Open a C # project, add a button, in the click event, we will display a message. Increase the namespace using an external library: use system.Runtime.InteropServices;

Declaring the API below

[DLLIMPORT ("User32.dll")] Public Static Extern Int MessageBox (int H, String M, String C, INT TYPE);

Where DllImport attribute used for calling method from unmanaged code. "User32.dll" indicates library name. DllImport attribute specifies the dll location that contains the implementation of an extern method. The static modifier used to declare a static member, which belongs to the type itself rather than to a specific object, extern is used to indicate that the method is implemented externally. a method that is decorated with the DllImport attribute must have the extern modifier. DllImport attribute is used to specify dynamic link library method comprising external position. "User32.dll" points out the library name, Static indicates that it does not belong to a specific object. EXTERN indicates an external method. Methods with DLLIMPORT properties must have a modifier extern. MessageBox is Function Name, Which Returns Int and takes 4 parameters as shown in declaration. Messagebox is a Hankname with four parameters to return an INT value. Many API uses structure to pass and retrieve values, as it is less expensive. It also uses constant data type for passing constant data and simple data type for passing Built-in data type as seen in previous declaration of MessageBox function. Many API usage structure To pass, return parameters, which can reduce complexity. It also allows the use of fixed parameters as the Messagebox function. Add the following code in the click event: protected void button1_click (object sender, system.eventargs e) {

MessageBox (0, "API Message Box", "API Demo", 0);

}

Compile and run the program, after clicking the button, you can see a message box called by the API.

Using structure using a structured Working with API, which uses complex structure or structure in structure, is somewhat complex than using simple API. But once you understand the implementation then whole API world is yours. API is often used in complex structures. But once you understand them, it will be very simple. In Next Example We Will Use GetsystemInfo API Which Returns Information About The Current System. The following list, we use the GetSystemInfo API to get information about the current system. Step 1: Add a C # window and add a button above, add a namespace in the window code page: use system.Runtime.InterOpServices;

Disclaimer GetSystemInfo parameter structure:

[Structlayout (layoutkind.sequential)]

Public struct system_info {

Public uint dwoemid;

Public uint dwpagesize;

Public uint lpminimumApplicationAddress;

Public uint lpmaximuMapplicationAddress;

Public uint dwactiveprocessormask;

Public uint dwnumberofprocessors;

Public uint dwprocessortype;

Public uint dwallocationGranularity;

Public uint dwprocessorlevel;

Public uint dwprocessorrevision;

}

Declare API function:

[DLLIMPORT ("kernel32")]]

Static Extern void getSystemInfo (Ref System_INFO PSI);

WHERE REF IS Method Parameter Keyword Causes a Method To Refer to The Same Variable That Was Passed INTO The Method. REF is a keyword in the form of a logo parameter, which corrects the transferred variable points to the same variable (address delivery) Add Following Code In Button Click Event in Which First Create Struct Object and the pass it to function. Add the following code in the button.

Protected Void Button1_Click (Object Sender, System.EventArgs E) {

Try {

System_info psi = new system_info (); getSystemInfo (Ref psi); //////

ONCE You Retrieve The Structure Perform Operations on Required Parameter

such as:

Listbox1.items.insert (0, psi.dwactiveprocessormask.tostring ());

///////

}

Catch (Exception ER) {

Messagebox.show (Er.Message);

}

}

转载请注明原文地址:https://www.9cbs.com/read-28364.html

New Post(0)