Behind Delphi --- Beginner Reference 2 (2)

zhaozj2021-02-16  49

(After continuous Delphi back --- Beginner Reference 2 (1))

2 Windows API

API (Application Programming Interface) Applying Programming Interface, all computer languages ​​are used. What is the API? API is a way to serve the service provided by the program using the operating system. We most programmed all direct operating hardware, but call these APIs, then manipulate hardware by the operating system, this benefit is that we do not have to test and hardware when we program Compatible issues, more importantly, code sharing is implemented from the operating system layer. So, if you can use the API function when programming, we try to use it.

l Delphi How to use Windows API

In daily development, we often use the Windows API function, then where is the API letter? We can understand this, the API function is in the DLL system file packaged in the Windows system. As we often use the Beep process (bell), it is to call the MessageBeep implementation in the user32.dll in the WindWos system directory; sendMessage is the SendMessagea that directly calls USER32.DLL. Delphi has the most frequent frequency DLL is: Advapi32.dll, kernel32.dll, mpr.dll, version.dll, comctl32.dll, gdi32.dll, opengl32.dll, user32.dll, wintrust.dll, msimg32.dll.

So how do Delphi use this API function? Since the API function exists in the DLL of the system, we can call the API function like the DLL written by calling yourself. There are two ways to call the DLL function, one is a static method, one is a dynamic manner. Calling Windows API is a static way, why? This is because these DLLs are the most basic services provided by the operating system, and the operating system has been loaded into memory when starting, and the operating system is also used.

l API and daily programming

Delphi encapsulates most of the API functions of Windows (mainly in Windows.Pas unit), you should say that we can complete our work, we generally do not call the API function directly. But sometimes there is a special request, we may call some Delphi unpacking API, sometimes even call the API function that Windows is not announced. So how do you call these API functions? Just as mentioned earlier, it is good to use a static method. For more detailed calls, please refer to the relevant information.

The key to calling these Delphi unpacking API function is to know the parameters. You can see the latest MSDN or related information.

l API and VCL

Microsoft's MFC has a large number of packages Windows API, VCL is no exception. Most of the VCL function is inseparable from the Windows API, or it is directly called, or it is simply packaged. Such as TControl's Repaint Realization (in the Control unit):

Procedure tcontrol.repaint;

VAR

DC: HDC;

Begin

IF (Visible or (Csdesigning In ComponentState) and not

CSNODesignVisible in controlstyle)))))))))

Parent.handleAllocated then

IF csopaque in ControlStyle Then

Begin

// Call directly with user32.dll's getDCDC: = getd.handle;

Try

// Turn directly to GDI32.DLL INTERSECTCLIPRECT

INTERSECTCLIPRECT (DC, LEFT, TOP, LEFT WIDTH, TOP

HEIGHT);

// parent.paintControls calls a lot of API

Parent.PaintControls (DC, Self);

Finally

/ / Touch USER32.DLL directly to releasedc

ReleaseDC (Parent.handle, DC);

END;

END ELSE

Begin

// The following two packages are called

INVALIDATE;

Update;

END;

END;

It can be seen that there is an API everywhere in the VCL. We understand the VCL from another side is: VCL is a large number of encapsulated API functions class libraries. This result is to make us easier to use the API, do not care about those annoying API parameters.

3 Delphi and Windwos COM services

What COM is? Com (Component Object Model), the component object model, which is a software component model that is unlikely from any computational language-constrained software based on the Windows platform, which defines a set of APIs and a binary standard. This definition is abstract, first of all, it is a component model that defines a component object specification, and the object that implements this COM model is a COM object. The COM object is implemented by an interface (Interface), and a COM object can contain one or more interfaces that form the functionality of the COM object, you can access the interface method of the COM object like accessing the VCL object method. The purpose of the COM object is to achieve resource sharing. It implements sharing at the binary code level, so it can be implemented in different programming languages, or can be called by different programming languages, which is similar to the DLL (in fact COM's ideological source DLL).

l VCL and COM

COM is a very strong thing before Microsoft, so there is everywhere in the Windows operating system. The same Delphi's VCL also has a calling Windows COM service. The most obvious example is all ADO components of the component bar ADO page, such as TadoQuery, it is from TCUStomadodataset Inherited, TCUSTOMADODATASET is as follows:

Tcustomadodataset = Class (TDataSet, IUNKNOWN,

RecordseTeventsvt)

Private

FRECORDSETOBJECT: _Recordset;

FFINDCURSOR: _Recordset;

FLOOKUPCURSOR: _Recordset;

FLOCKCURSOR: _Recordset;

FROWSET: IROWSET;

Faccessor: Iaccessor;

FrowsetFind: iRowsetFind;

Fhaccessor: Haccessor;

FoleRecbufsize: integer;

...

END;

ADO (Microsoft ActiveX Data Objects), which is a set of COM objects accessed through the Microsoft OLE DB provider. As we study the implementation of TadoQuery's First method:

TADOQuery.First-> TDataSet.First-> TdataSet.InternalFirst-> TCustomADODataSet InternalFirst -..> Recordset15 MoveFirstTADOQuery inherited from TCustomADODataSet, inherited from the TCustomADODataSet TdataSet, TdataSet.InternalFirst virtual method is defined, and subclasses TCustomADODataSet InternalFirst covered. it. TCUSTOMADODATASET. INTERNALFIRS Call the MoveFirst method of the interface RECORDSET15.

It is not difficult to find that tadoQuery.first is ultimately to call COM objects through an interface.

l Delphi with Windows shell

What is a Windows shell? The Windows housing is an environment for Windows interface, which provides powerful scalability for our programming. We use the Windows shell to achieve some functions, called the housing extension. For example, if you have installed WinRar on your machine, you will see the WinRar's compressed menu in the folder. These features are implemented through Windows housing extension.

The Windows housing is COM based on COM, so all housing extensions must be implemented through the interface. Delphi also defines a lot of housing extension interfaces, in the Source / RTL / WIN / SHLOBJ.PAS unit in the installation directory of Delphi7.

An example of a Virtual ListView in DELPHI's DEMO directory is that the Windows housing extension implements disk browsing, interested readers can study it.

4 embedded assembly language

Embedding assembly languages ​​in the code is also a major feature of Delphi. If the root TOBJCE of the VCL is largely embedded in the assembled statement.

Assembly language is a relatively low-level computer language, close to hardware relationship. So we try not to use it in our usual program, but in some special occasions (such as high performance requirements, you need to manipulate hardware directly), use it or play a lot.

.

(If you need to reprint, please indicate the source and the author http://haitian.myrice.com)

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

New Post(0)