In order to be able to quickly develop applications under Windows, Delphi has a good encapsulation of Windows controls, so that we can directly use the Delphi control to write programs, rather than using the format, invoke the control of complex APIs to develop, Save a lot of time for us to develop.
But sometimes these controls don't solve our problems, and this time we have to use the API, which is the Windows application interface. In this way, the problem is followed, the API is written in C + + language, and Delphi scripting language is Pascal, the data types used are incompatible. So, how to call the data type incompatible API in Delphi, this is what I introduced to you in this article.
One of the most common data types we use in Delphi is the string type, which is not supported by the API, so what do we do when we need to get a string of values from the API? Look at the following program:
function GetSysPath: string;
result := ’’;
result := sTmp + ’\’;
In this function, the API function GetSystemDirectory is invoked, and its first argument is a pointer to a null-terminated string, in Delphi, the data type is Pchar, so the data type conversion must be written as Pchar (STMP). Let's look at another example:
icon: TIcon ;
icon := TIcon.Create;
icon.Handle := ExtractIcon(application.Handle,pchar(’D:\WINNT\regedit.exe’),0); //这是一个API函数
if icon.Handle <> 0 then
application.Icon := icon;
The process uses an API function to get an application icon to dynamically change the current program's icon. API function ExtractIcon Returns a HICON type, which is a pointer to an icon, if written as a application. Icon = ExtractIcon (application. Handle, Pchar (' D:\WINNT\regedit.exe '), 0); The compilation certainly does not pass, the data type is inconsistent! Application. Icon property is Ticon type, we can create a Ticon, the return value of the API to the Ticon.handle, and finally to assign the icon to Application.icon to solve our problem.
From the above example, we can see that Delphi does not directly support the data types in Windows, only in the form of encapsulation to support. To skillfully invoke API functions, we must be familiar with Delphi packaging mechanism, read more, write more, to practice, then we will be able to invoke the API function.