Tab restoring: avoid restoring tab file only if the owner is a *HeidiSQL* process. Fixes non-restored tabs when an old Heidi process id is re-assigned to another application. Happened here at least one time.

This commit is contained in:
Ansgar Becker
2020-11-09 18:08:06 +01:00
parent 13610e70f6
commit 66c50ce792
2 changed files with 33 additions and 5 deletions

View File

@ -367,7 +367,7 @@ type
function RunningAsUwp: Boolean; function RunningAsUwp: Boolean;
function GetThemeColor(Color: TColor): TColor; function GetThemeColor(Color: TColor): TColor;
function ThemeIsDark(ThemeName: String): Boolean; function ThemeIsDark(ThemeName: String): Boolean;
function ProcessExists(pid: Cardinal): Boolean; function ProcessExists(pid: Cardinal; ExeNameMatchesExpression: String=''): Boolean;
procedure ToggleCheckBoxWithoutClick(chk: TCheckBox; State: Boolean); procedure ToggleCheckBoxWithoutClick(chk: TCheckBox; State: Boolean);
var var
@ -2940,11 +2940,12 @@ begin
end; end;
function ProcessExists(pid: Cardinal): Boolean; function ProcessExists(pid: Cardinal; ExeNameMatchesExpression: String=''): Boolean;
var var
Proc: TProcessEntry32; Proc: TProcessEntry32;
SnapShot: THandle; SnapShot: THandle;
ContinueLoop: Boolean; ContinueLoop: Boolean;
rx: TRegExpr;
begin begin
// Check if a given process id exists // Check if a given process id exists
SnapShot := CreateToolhelp32Snapshot(TH32CS_SnapProcess, 0); SnapShot := CreateToolhelp32Snapshot(TH32CS_SnapProcess, 0);
@ -2952,7 +2953,26 @@ begin
Result := False; Result := False;
ContinueLoop := Process32First(SnapShot, Proc); ContinueLoop := Process32First(SnapShot, Proc);
while ContinueLoop do begin while ContinueLoop do begin
Result := Proc.th32ProcessID = pid; if Proc.th32ProcessID = pid then begin
// Found the process id, probably check if it matches a given pattern
if not ExeNameMatchesExpression.IsEmpty then begin
rx := TRegExpr.Create;
rx.ModifierI := True;
rx.Expression := ExeNameMatchesExpression;
try
Result := rx.Exec(Proc.szExeFile);
except
on E:Exception do begin
MainForm.LogSQL(E.Message, lcError);
Result := False;
end;
end;
rx.Free;
end else begin
// Pattern empty
Result := True;
end;
end;
if Result then if Result then
Break; Break;
ContinueLoop := Process32Next(SnapShot, Proc); ContinueLoop := Process32Next(SnapShot, Proc);

View File

@ -2361,6 +2361,7 @@ var
pid: Cardinal; pid: Cardinal;
EditorHeight, HelpersWidth: Integer; EditorHeight, HelpersWidth: Integer;
BindParams: String; BindParams: String;
ExeNameExpression: String;
begin begin
// Restore query tab setup from tabs.ini // Restore query tab setup from tabs.ini
Result := True; Result := True;
@ -2371,6 +2372,11 @@ begin
Sections := TStringList.Create; Sections := TStringList.Create;
TabsIni.ReadSections(Sections); TabsIni.ReadSections(Sections);
ExeNameExpression := '(' +
QuoteRegExprMetaChars(ExtractFileName(Application.ExeName)) +
'|' +
QuoteRegExprMetaChars(APPNAME) +
')';
for Section in Sections do begin for Section in Sections do begin
@ -2381,9 +2387,11 @@ begin
HelpersWidth := TabsIni.ReadInteger(Section, 'HelpersWidth', 0); HelpersWidth := TabsIni.ReadInteger(Section, 'HelpersWidth', 0);
BindParams := TabsIni.ReadString(Section, 'BindParams', ''); BindParams := TabsIni.ReadString(Section, 'BindParams', '');
// Don't restore this tab if it belongs to a different running process // Don't restore this tab if it belongs to a different running Heidi process
if (pid > 0) and (pid <> GetCurrentProcessId) and ProcessExists(pid) then if (pid > 0) and (pid <> GetCurrentProcessId) and ProcessExists(pid, ExeNameExpression) then begin
LogSQL(IfThen(BackupFilename.IsEmpty, Filename, BackupFilename)+' loaded in process #'+pid.ToString);
Continue; Continue;
end;
// Either we have a backup file, or a user stored file. // Either we have a backup file, or a user stored file.
// Both of them may not exist. // Both of them may not exist.