This is the first of a series of tutorials aimed at Delphi programmers wishing to seek an understanding of the Win32 API. We will start with a brief introduction to the Win32 API and move on to writing our first Delphi program that uses the API. Useful links and suggestions for further reading are included at the end of the tutorial.
At the end of the tutorial, you will
An API is an application program interface. It prescribes the methods that a programmer can use to make requests of an operating system or another application. It is simply the collection of functions available to the programmer. The Win32 API is the application program interface provided by Microsoft for programmers who want to speak directly with the 32 bit Windows operating systems. Currently that list includes Windows 95, Windows 98, Windows ME, Windows NT, Windows 2000 and Windows XP.
One of the great things about Delphi is that you can accomplish many tasks quickly without using the Windows API. Many of the things that you do with the API are much easier if you can find a Delphi function to do the same thing. Delphi has done an excellent job of encapsulating many of the API functions for us so that we do not have to resort to the API. The tutorial Finding the Windows Desktop in Delphi suggests two methods for finding the location of the Windows Desktop, one using Delphi and another using the API.
The good news is that there are thousands of API function – it is rich in functionality. The bad news is that there are thousands of API functions – finding the function or functions that do what you want can be daunting. Fortunately there are many free resources on the Internet and some excellent books about using the API. The references section at the end of this document provides additional sources of information and some useful links.
Delphi ships with a copy of the Microsoft Win32 Programmers Reference as a Help File. It provides information about the thousands of available API functions. The next tutorial in this series entitled Finding and Understanding the Microsoft Win32 Programmers Reference will help you find and understand the reference.
The help file was written for programmers who use the C programming language. Delphi is based on Object Pascal. It employs different data type and header conventions than C. Fortunately the Borland developers made it easy for us to find the Object Pascal version of those C declarations by including a Win32 API Interface Unit called Windows.pas in the Borland Delphi Run-time Library.
Later tutorials in this series explain pointers, the Windows API data types and the C programming language in greater detail.
Create a new application by choosing New from Delphi’s File menu and choosing Application.
Drop a Label component on the form and set the properties as follows
Name: Label1
Autosize: True
Drop a second Label component on the form and set the properties to:
Name: Label2
Autosize: True
Drop a Button on the form and set the properties to:
Name: Button1
Make sure your Unit1 uses clause contains (it will)
Windows.pas
Add the following code to the On Click Event handler for Button1
|
procedure TForm1.Button1Click(Sender: TObject); var TZInfo : TTimeZoneInformation; // variable of Type TTimeZoneInformation, declared in windows.pas begin GetTimeZoneInformation(TZInfo); // Call the API function Label1.Caption := TZInfo.StandardName; // the result is returned in the TimeZoneInformation record // the offset is returned in TimeZoneInformation.Bias Label2.Caption := FormatFloat('"GMT -" #0 "minutes"', TZInfo.Bias); end; |
Now run your program and click Button1. The results should look something like this.

We have used the GetTimeZoneInformation Win32 API function to retrieve the time zone for our computer as a string as well as an offset in minutes from GMT.
This tutorial provided step-by-step instructions for creating a simple application that used the WIN32 API to retrieve the time zone as a string and an offset integer value in minutes. You can find many more examples in other tutorials in this series, at the Jedi Code Library, in various Delphi newsgroups and at the Borland Developer Network. The Microsoft Developer Network can also be a useful source of information, but it is intended primarily for those using Microsoft tools. An excellent book is the Tomes of Delphi: Win32 Core API by John Ayers, ISBN ISBN 1-55622-750-7 Wordware Publishing Inc.
The next tutorial in this series will make extensive use of the Microsoft Win32 Programmers Reference and Windows.pas. We will examine the GlobalMemoryStatus function in greater detail with the goal of developing a better understanding of the Win32 API.
Lets take a look at what we have learned.
|
Delphi 6 online help |
|
|
WIN32.HLP |
|
|
|
Borland Delphi Run-time Library - Win32 API Interface Unit. |
|
Borland Delphi Run-time Library - Win32 API Shell objects Interface Unit. |
|
|
Win32 common controls interface unit |
|
|
Other tutorials in this series |
|
|
An excellent cooperative effort among Delphi programmers with lots of free source code |
|
The MER System Database Search |
A free newsgroup search engine you can use to find Delphi newsgroup postings |
|
|
The Microsoft support site for developers |
|
|
Borland’s support site. |
|
Newsgroups |
Borland Delphi newsgroups. |
The Tomes of Delphi: Win32 Core API - Windows 2000 Edition by John Ayers, ISBN 1-55622-750-7 Wordware Publishing Inc
The Tomes of Delphi: Win32 Shell API – Windows 2000 Edition by John Ayers, ISBN 1-55622-749-3 Wordware Publishing Inc
Windows 2000 SYSTEMS PROGRAMMING Black Book by Al Williams, 2000, ISBN 1-57610-280-7, The Coriolis Group
The source code contained in this tutorial is provided free of charge.
You may incorporate this source code into your own program but you do so at
your own risk. Please remember to test your program and any source code copied
from this site. Please report errors and improvements to jpgriffiths.com.
Copyright © 2003 Paul Griffiths, www.jpgriffiths.com.
Last Updated: [January 2003]