A brief discussion on Windows API invocation in Delphi

Source: Internet
Author: User

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;
 sTmp: string;
 result := ’’;
 GetSystemDirectory(PChar(sTmp),256); //这是一个API函数
 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:

procedure TForm1.ChangeAppIcon;
 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.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.