function RunThis(command, path:
string): THandle;
var si: TStartupInfo;
pi: TProcessInformation;
begin
FillChar(si, SizeOf(si), 0);
si.cb := SizeOf(si);
FillChar(pi, SizeOf(pi), 0);
GetStartupInfo(si);
si.wShowWindow := SW_HIDE;
si.dwFlags := si.dwFlags
or STARTF_USESHOWWINDOW;
si.dwFlags := si.dwFlags
and not (STARTF_USEPOSITION
or STARTF_USESIZE
or STARTF_USECOUNTCHARS
or STARTF_USEFILLATTRIBUTE);
if not CreateProcess(
nil, PChar(command),
nil,
nil, TRUE,
NORMAL_PRIORITY_CLASS,
nil, PChar(path), si, pi)
then begin
Result := 0;
end else begin
CloseHandle(pi.hThread);
Result := pi.hProcess;
end;
end;
function CheckProcess(
var hp: THandle): Boolean;
var exc: Cardinal;
begin
Result := False;
if hp <> 0
then begin
exc := STILL_ACTIVE;
if GetExitCodeProcess(hp, exc)
then
if exc <> STILL_ACTIVE
then begin
CloseHandle(hp);
hp := 0;
Result := True;
end;
end;
end;
procedure TfrmMain.DbServer_Start;
var pth, cmd:
string;
begin
CheckProcess(DbServer_Handle);
if DbServer_Handle <> 0
then exit;
try
Screen.Cursor := crHourglass;
pth := BasePath + '
mysql\bin';
cmd := pth + '
\mysqld-nt.exe --defaults-file=my.cnf --standalone';
DbServer_Handle := RunThis(cmd, pth);
if DbServer_Handle = 0
then
raise Exception.Create('
Fehler beim Starten des Datenbankservers!');
finally
Screen.Cursor := crDefault;
end;
end;
procedure TfrmMain.DbServer_Stop;
var pth, cmd:
string;
hdl: THandle;
cnt: Integer;
begin
CheckProcess(DbServer_Handle);
if DbServer_Handle = 0
then exit;
try
Screen.Cursor := crHourglass;
pth := BasePath + '
mysql\bin';
cmd := pth + '
\mysqladmin.exe --defaults-file=my.cnf --user=root --password=root shutdown';
hdl := RunThis(cmd, pth);
if hdl <> 0
then CloseHandle(hdl)
else raise Exception.Create('
Fehler beim Anhalten des Datenbankservers!');
cnt := 50;
repeat
dec(cnt);
CheckProcess(DbServer_Handle);
if DbServer_Handle <> 0
then
Sleep(100);
until (cnt < 1)
or (DbServer_Handle = 0);
finally
Screen.Cursor := crDefault;
end;
end;