Monday, March 19, 2012

Check wheter or not Firebird is running

// Don't forget to include the unit WinSvc.
// Call IsFirebirdRunning Function in your program :

// ********************************************************************************** //
Procedure TMainForm.FormCreate(Sender: TOBject);
// ********************************************************************************** //
Begin
    If not (IsFirebirdRunning) then ShowMessage('Firebird is not running');
End;

// ********************************************************************************** //
Function IsFirebirdRunning:boolean;
// ********************************************************************************** //
begin
    Result:=(ServiceGetStatus('','FirebirdServerDefaultInstance') = SERVICE_RUNNING);
end;

// ********************************************************************************** //
Function ServiceGetStatus(sMachine, sService: string ): DWord;
// ********************************************************************************** //
var
    schm,
    schs : SC_Handle;
    ss : TServiceStatus;
    dwStat : DWord;
begin
    dwStat := 0;

    schm := OpenSCManager(PChar(sMachine), nil, SC_MANAGER_CONNECT);

    if (schm > 0) then
    begin
        schs := OpenService(schm, PChar(sService), SERVICE_QUERY_STATUS);

        if (schs > 0) then
        begin
            if (QueryServiceStatus(schs, ss)) then
            begin
                dwStat := ss.dwCurrentState;
            end;
            CloseServiceHandle(schs);
        end;
        CloseServiceHandle(schm);
    end;
    Result := dwStat;
end;