1. Technology
Listing Windows processes
Getting a list of Window processes and setting memory for return values using Delphi.
 Win prizes by sharing code!
Do you have some Delphi code you want to share? Are you interested in winning a prize for your work?
Delphi Programming Quickies Contest
 More of this Feature
• DOWNLOAD SAMPLE CODE
 Join the Discussion
"Post your views, comments, questions and doubts to this article."
Discuss!
 Related Resources
• Implementing function callbacks with Delphi
• Get current URL from IE
• API programming

Article submitted by Kenneth Todd for the Delphi Programming Quickies Contest.

Process's
Do you know what programs are running on your computer? There are multiple programs running invisibly in the back ground to monitor and manage how the operating system is working; these programs can also be called upon to report on the current state of the operating system. For example, EnumWindows is a window (API) function that will integrate these programs and deliver a report consisting of the handle of each top level window to a requested address of a callback function.
EnumWindows Definition
The EnumWindows function passes the handle of each top level window, in turn, to a defined callback function.

BOOL EnumWindows(
WNDENUMPROC lpEnumFunc, // pointer to callback function
LPARAM lParam // application-defined value
);

The first parameter is a callback function that is defined in the application to use the HWND handle that Windows passes. This callback function, which the developer creates, is registered by passing its address. The other parameter has no pre-defined value and takes an integer value that can be used as a flag by the developer. This tag should be declared in the main class so as to give it a more globally defined memory. If the function succeeds it returns a non-zero value (true) and a zero value (false) if it fails.

The code can be presented in a application with only 2 required controls; a button to trigger the call back and list box to display the results. Place a button on the form, rename it and change the Caption to Get Process and then click on it to create a click event procedure. Add the code as shown below to first clear the list box and then to call the EnumWindows function with the address of the call back function.

procedure TProcessForm.GetProcessButtonClick(Sender: TObject);
begin 
//Clear any previous calls
if ProcessListBox.Count > 0 then
  ProcessListBox.Clear;
//define the tag flag
lp := 0; //globally declared integer
//call the windows function with the address 
//of handling function and show an error message if it fails
if EnumWindows(@EnumProcess,lp) = false then
  ShowMessage('Error: Could not obtain 
               process window hook from system.');
end;

Callback Function
The callback function should be place above the 'Get Process' button click event as pascal requires that all function and procedures be declared before they are called. Using a forward declaration or the placing a function declaration in the forms class declaration will usually fail due to the window function requiring a simple fixed address to which the process handles are sent.
The requirement of windows for a properly set memory can also be seen by the necessary use of SetLength on the strings used with other function to gain additional information about the process's. If SetLength is not used to set up a fixed memory area then these functions would fail even though a Delphi string has multiple class over-rides for the default setting of a string.
This is some thing to note for future reverence, as from personal experience, 9 times of 10 the reason for a failed connection with windows is a memory setting that windows does not recognize.

function EnumProcess(hHwnd: HWND; lParam : integer): boolean; stdcall;
var
  pPid : DWORD;
  title, ClassName : string;
begin
  //if the returned value in null the 
  //callback has failed, so set to false and exit.
  if (hHwnd=NULL) then
  begin
    result := false;
  end
  else
  begin
    //additional functions to get more 
    //information about a process.
    //get the Process Identification number.
    GetWindowThreadProcessId(hHwnd,pPid);
    //set a memory area to receive 
    //the process class name
    SetLength(ClassName, 255);
    //get the class name and reset the 
    //memory area to the size of the name
    SetLength(ClassName, 
              GetClassName(hHwnd, 
                           PChar(className), 
                           Length(className)));
    SetLength(title, 255);
    //get the process title; usually displayed 
    //on the top bar in visible process
    SetLength(title, GetWindowText(hHwnd, PChar(title), Length(title)));
    //Display the process information 
    //by adding it to a list box
    ProcessForm.ProcessListBox.Items.Add
      ('Class Name = ' + className + 
       '; Title = ' + title + 
       '; HWND = ' IntToStr(hHwnd) + 
       '; Pid = ' + IntToStr(pPid));
    Result := true;
  end;
end;

Process list

More information
More info on these and other function can be found in the Win32 Programmer's Reference on the MS SDK Help Files menu which is installed with Delphi. These help files are not just for Delphi but are published by Microsoft as general programming help and certain techniques have to be applied for them to work correctly in a Pascal based code. Many of these functions are used in the Delphi source code and a better understanding of how to use them can be found by using a text search for a function name in the Delphi source code.

Related Video
Windows Movie Maker: Importing
Windows Movie Maker Capture

©2014 About.com. All rights reserved.