Archive

Archive for the ‘System’ Category

How to Find Out if a Program is Running

December 15th, 1999 m3Rlin 8 comments

I received tip from Mike Gajewski, one of our readers. This is the code you need to find out if a certain program is running. The IsProcess() routine checks if the specific filename is running. This tip can be useful when your application should or needs some other application running in order to run properly/at all.

OK, here are the sources of unit EnumProc:

uses
  SysUtils, Classes;
...
const
  MAX_PATH = 260;
  TH32CS_SNAPPROCESS = 2;
...
type
  TProcessEntry32 = packed record
    iSize,
    iUsage,
    iProcessID,
    iDefaultHeapId,
    iModuleId,
    iThreads,
    iParentProcessId,
    iPriClassBase,
    iFlags: Integer;
    aExeFile: array[0..MAX_PATH] of Char;
  end;

function CreateToolHelpSnapShot(lFlags, lProcessId: Longint): Longint; stdcall;
function ProcessFirst(hSnapshot: longint; var uProcess: TProcessEntry32): Longint; stdcall;
function ProcessNext(hSnapshot: Longint; var uProcess: TProcessEntry32): Longint; stdcall;
procedure CloseHandle(hPass: Longint); StdCall;

function IsProcess(sExe: string): Boolean;
procedure GetProcesses(var ProcessList: TStringList);

implementation

function CreateToolHelpSnapShot; external 'kernel32.dll' name 'CreateToolhelp32Snapshot';
function ProcessFirst; external 'kernel32.dll' name 'Process32First';
function ProcessNext; external 'kernel32.dll' name 'Process32Next';
procedure CloseHandle; external 'kernel32.dll';

function IsProcess(sExe: string): Boolean;
(*
** This routine examines Windows processes currently running to see if a
** certain program is running.
**
** sExe  : The executable name of the program to check.
** Result: True if the program is running, False otherwise.
*)
var
  liI, lSnapShot: Longint;
  rProcess: TProcessEntry32;
begin
  Result := False;
  ExeName := UpperCase(ExeName);
  lSnapShot := CreateToolHelpSnapShot(TH32CS_SNAPPROCESS, 0);
  if lSnapShot <> 0 then begin
    rProcess.lSize := SizeOf(rProcess);
    lI := ProcessFirst(lSnapShot, rProcess);
    while lI <> 0 do begin
      if Pos(ExeName, UpperCase(rProcess.sExe)) <> 0 then begin
        Result := True;
        Break;
      end;
      lI := ProcessNext(lSnapShot, rProcess);
    end;
    CloseHandle(lSnapShot);
  end;
end;

procedure GetProcesses(var ProcessList: TStringList);
(* This procedure will retrieve a list of Windows processes currently running.
**
** ProcessList: TStringList variable to fill with the running process names.
**
** !!Note: ProcessList will be empty if an error occurs
*)
var
  lI, lSnapShot: Longint;
  rProcess     : TProcessEntry32;
begin
  ProcessList.Clear;
  lSnapShot := CreateToolHelpSnapShot(TH32CS_SNAPPROCESS, 0);
  if lSnapShot <> 0 then begin
    rProcess.lSize := SizeOf(rProcess);
    lI := ProcessFirst(lSnapShot, rProcess);
    while r <> 0 do begin
      ProcessList.Add(rProcess.sExeFile);
      lI := ProcessNext(lSnapShot, rProcess);
    end;
    CloseHandle(lSnapShot);
  end;
end;

Example:

IsProcess('notepad.exe'); // Will return True if Notepad is running

How to Create Elliptic Forms

December 15th, 1999 m3Rlin No comments

Ever wanted to create forms that had a cool shape? How about an elliptic form? This would make a nice splash screen, wouldn’t it? Sure. But how can I create one of these? Hmmmm… Well, Windows 32 bit has all we need. CreateEllipticRgn() allows you to create an elliptic region. After we do so all we have to do is assign the region to our form. This can be done using the SetWindowRgn() function determines the area of a window that can be drawn on. The area not included in this region will not be displayed. You can use the first function a couple of times to create “weird” shapes :-) Then assign the created region using the second routine.

Add this code to your form’s OnCreate event:

var
  rgnNew: Hrgn; // The region variable
  ...
begin
  ...
  rgnNew := CreateEllipticRgn(1, 1, 100, 100); // A small circle
  { Work on the region here if you want to change it a bit more }
  SetWindowRgn(Handle, rgnNew, True);
  ...
end;