【正文】
ject and the application. Using these handles,an application can easily refer to any of these objects, and the operating system instantly knows which object a piece of code wants to manipulate.ConstantsThe Windows API functions declare literally thousands upon thousands of different constants to be used as parameter values. Constants for everything from color values to return values have been defined in the , , and files. The constants that are defined for each API function are listed with that function within the text. However, the file may yield more information concerning the constants forany particular function, and it is a good rule of thumb to check this Delphi source code file when using plicated functions.StringsAll Windows API functions that use strings require a pointer to a nullterminated string type. Windows is written in C, which does not have the Pascal string type. Earlier versions of Delphi required the application to allocate a string buffer and convert the String type to a PChar. However, Delphi 3 introduced a string conversion mechanism that allows a stringto be used as a PChar by simply typecasting it (., PChar(MyString), where MyString is declared as MyString: string). For the most part, this conversion will work with almost all Windows API functions that require a string parameter.Importing Windows FunctionsThe Windows API is huge. It defines functions for almost every kind of utility or parison or action that a programmer could think of. Due to the sheer volume of Windows API functions, some functions simply fell through the cracks and were not imported by the Delphi source code. Since all Windows API functions are simply functions exported from a DLL, importing a new Windows API function is a relatively simple process if the function parameters are known.Importing a new Windows API function is exactly like importing any other function from a DLL. For example, in earlier versions of Delphi, the BroadcastSystemMessage function described in Chapter 3 was not imported by the Delphi source code. In order to import this function for use within an application, it is simply declared as a function from within a DLL as:function BroadcastSystemMessage(Flags: DWORD。 Recipients: PDWORD。uiMessage: UINT。 wParam: WPARAM。 lParam: LPARAM): Longint。 stdcall。implementationfunction BroadcastSystemMessage。 external user32 name 39。BroadcastSystemMessage39。As long as the parameters required by the function and the DLL containing the function are known, any Windows API function can be imported and used by a Delphi application.It is important to note that the stdcall directive must be appended to the prototype for the function, as this defines the standard mechanism by which Windows passes parameters to a function on the stack.Incorrectly Imported FunctionsSome functions have been incorrectly imported by the Delphi source code. These exceptions are noted in the individual function descriptions. For the most part, the functions that have been imported incorrectly deal with the ability to pass NIL as a value to a pointer parameter, usually to retrieve the required size of a buffer so the buffer can be dynamically allocated to the exact length before calling the function to retrieve the real data. InDelphi, some of these functions have been imported with parameters defined as VAR or CONST. These types of parameters can accept a pointer to a buffer, but can never be set to NIL, thus limiting the use of the function within the Delphi environment. As is the case with almost anything in Delphi, it is a simple matter to fix. Simply reimport the function as if it did not exist, as outlined in the previous section. Functions that have been importedincorrectly are identified in their individual function descriptions throughout the book.Callback FunctionsAnother very important concept in Windows programming is that of a callback callback function is a function within the developer’sapplication that is never called directly by any other function or procedure within that application, but is instead called by the Windows operating system. This allows Windows to municate directly with the application, passing it various parameters as defined by the individual callback function.Most of the enumeration functions require some form of applicationdefined callback function that receives the enumerated information.Individual callback functions have specific parameters that must be declared exactly by the application. This is required so that Windows passes the correct information to the application in the correct order. A good example of a function that uses a callback function is EnumWindows. The EnumWindows function parses through all toplevel windows on the screen, passing the handle of each window to an applicationdefined callback function. This continues until all toplevel windows have been enumerated or the callbackfunction returns FALSE. The callback function used by EnumWindows is defined as:EnumWindowsProc(hWnd: HWND。 {a handle to a toplevel window}lParam: LPARAM {the applicationdefined data}): BOOL。 {returns TRUE or FALSE}A function matching this function prototype is created within the application, and a pointer to the function is passed as one of the parameters to the EnumWindows function. The Windows operating system calls this callback function for each toplevel window, passing the window’s handle in one of the callback function’s parameters. It is important to note that the stdcall directive must be appended to the prototype for the callback function,as this defines the standard mechanism by which Windows passes parameters to a function on the stack. Without the stdcall directive, Windows will not be able to access the callback powerful software mechanism, in many cases, allows an application to retrieve information about the system that is only stored internally by Windows and would otherwise be unreachable. For