API (application PRogramming Interface) application programming interface, which must be used in all computer languages. What is an API? API is a way for programs to use services provided by the operating system. Most of our programming does not directly operate the hardware, but calls these APIs, and then the operating system directly operates the hardware. The advantage is that we do not have to test filtering and hardware when programming. The compatibility issue, more importantly, is to implement code sharing from the operating system layer. Therefore, if you can use APIs to implement functions during programming, we try to use them.
l How to use the Windows API in Delphi
In daily development work, we often use Windows API functions, so where does the API functions exist? We can understand this way that the API function is encapsulated in the DLL system file provided by the Windows system. For example, the Beep procedure we often use is implemented by calling MessageBeep in user32.dll in the Windwos system directory; SendMessage (send message) is to directly call SendMessageA of user32.dll. DelphiThe most frequently used Dlls are: advapi32.dll, kernel32.dll, mpr.dll, version.dll, comctl32.dll, gdi32.dll, opengl32.dll, user32.dll, wintrust.dll, msimg32. dll.
So how does Delphi use this API function? Since the API function exists in the system DLL, we can call the API function just like we call the DLL we wrote. There are two ways to call DLL functions, one is the static method and the other is the dynamic method. The APIs that call Windows are all static, why? This is because these DLLs are the most basic services provided by the operating system. The operating system is loaded into memory when it is started, and the operating system also uses them in large quantities.
l API and daily programming
Delphi encapsulates most of Windows API functions (mainly in Windows.pas unit), which should be said to be able to accomplish most of our work. We generally do not call API functions directly. But sometimes when there are special requirements, we may have to call some APIs that have not been encapsulated by Delphi, and sometimes even call API functions that have not been published by Windows. So how do you call these API functions? As mentioned earlier, just use static method calls. For more detailed calls, please refer to the relevant information.
The key to calling these unencapsulated API functions of Delphi is to know the parameters. You can check the latest MSDN or related information.
l API and VCL
Microsoft's MFC has a large number of encapsulated Windows APIs, and VCL is no exception. Most of the implementation of VCL functions cannot be separated from the Windows API, either directly called or simply encapsulated and then called. For example, the implementation of Repaint of TControl (in the Control unit):
procedure TControl.Repaint;
var
DC: HDC;
Begin
if (Visible or (csDesigning in ComponentState) and not (
csNoDesignVisible in ControlStyle)) and (Parent <> nil) and
Parent.HandleAllocated then
if csOpaque in ControlStyle then
Begin
//Dll's GetDC is called directly
DC := GetDC(Parent.Handle);
Try
// Directly call gdi32.Dll's IntersectClipRect
IntersectClipRect(DC, Left, Top, Left + Width, Top +
Height);
// Parent.PaintControls calls a large number of APIs
Parent.PaintControls(DC, Self);
Finally
// Directly call user32.Dll's ReleaseDC
ReleaseDC(Parent.Handle, DC);
end;
end else
Begin
//The following two are called by encapsulation
Invalidate;
Update;
end;
end;
It can be seen that there are APIs everywhere in VCL. We understand VCL from another perspective: VCL is a class library that encapsulates API functions in large quantities. The result is that it makes it easier for us to use APIs and do not have to care about those annoying API parameters.
What is COM? COM (Component Object Model), a component object model, is a software component model based on the Windows platform that allows different independent objects to communicate with each other without any computing language constraints. It defines a set of APIs and a binary standard. This definition is relatively abstract. First of all, it is a component model that defines a component object specification. The object that implements this COM model is a COM object. COM objects are accessed through an interface. A COM object can contain one or more interfaces. These interfaces constitute the function of COM objects. You can access the interface methods of COM objects just like accessing VCL object methods. The purpose of COM object is to realize resource sharing. It implements sharing at the binary code level, so it can be implemented in different programming languages or called by different programming languages. This is similar to DLL (actually the source of COM's idea. DLL).
l VCL and COM
COM is something that Microsoft has strongly praised before, so it is everywhere in the Windows operating system. Similarly, Delphi's VCL also calls Windows COM services. The most obvious example is all ADO components in the component bar ADO page, such as TADOQuery, which is from TCustomADODataSet Inherited, the definition of 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), a collection of COM objects that access the database through the Microsoft OLE DB provider. For example, we will study the implementation process of TADOQuery's First method:
TADOQuery.First->TDataSet.First->TdataSet.InternalFirst->TCustomADODataSet.InternalFirst->Recordset15. MoveFirst
TADOQuery inherits from TCustomADODataSet, and TCustomADODataSet inherits from TdataSet, TdataSet.InternalFirst defines a virtual method, and subclass TCustomADODataSet.InternalFirst overrides it. TCustomADODataSet. InternalFirs calls the MoveFirst method of interface Recordset15.
It is not difficult to find that TADOQuery.First is ultimately implemented through the interface calling the COM object.
l Delphi and Windows shell
What is a Windows shell? The Windows shell is an environment for Windows interface operation, and it also provides us with powerful scalability for programming. We use Windows shells to program certain functions, called shell extensions. For example, if your machine has WinRAR installed, you will see the WinRAR compressed menu in the right-click menu in the folder. These functions are implemented through Windows shell extensions.
The Windows shell is COM-based, so all shell extensions must be implemented through interfaces. Delphi also defines many shell extension interfaces, in the Source/rtl/Win/ShlObj.pas unit in the Delphi7 installation directory.
There is an example of Virtual Listview in Delphi's Demo directory, which is to realize disk browsing through Windows shell extensions. Interested readers can study it.
Embedding assembly language in code is also a major feature of Delphi. For example, the implementation of VCL's root class Tobjce embeds assembly statements in large quantities.
Assembly language is a relatively low-level computer language that is closely related to hardware. Therefore, we try not to use it in daily programming, but in some special occasions (such as high performance requirements and direct hardware manipulation), it can still play a big role.
The end.
(If you need to reprint, please indicate the source and author http://haitian.myrice.com)