Skip to content

Commit c2acc8f

Browse files
committed
refactor: move version detection to heidisql.lpr and exception handler to crashdialog unit
1 parent 588655c commit c2acc8f

9 files changed

Lines changed: 68 additions & 78 deletions

File tree

‎heidisql.lpr‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
SysUtils, Dialogs,
1111
Forms, printer4lazarus, datetimectrls, LCLTranslator, Translations,
1212
{ you can add units after this }
13-
main, apphelpers, dbconnection, generic_types
13+
main, apphelpers, dbconnection, generic_types, crashdialog, FileInfo
1414
{$if defined(LINUX) and (defined(LCLQt5) or defined(LCLQt6))}
1515
, platformtheme
1616
{$endif};
@@ -20,7 +20,22 @@
2020
var
2121
AppLanguage: String;
2222
WasDarkMode, IsDarkMode, FirstRun: Boolean;
23+
VI: TVersionInfo;
2324
begin
25+
// Detect version of the executable file, early so that crash reports can use it.
26+
// Create is intentionally outside the try block: an exception there (e.g. out of
27+
// memory) must not be swallowed by the except below.
28+
VI := TVersionInfo.Create;
29+
try
30+
// Load can fail on exotic executables; treat that as "no version info"
31+
VI.Load(HINSTANCE);
32+
AppVerRevision := VI.FixedInfo.FileVersion[3];
33+
AppVersion := Format('%d.%d.%d.%d', [VI.FixedInfo.FileVersion[0], VI.FixedInfo.FileVersion[1], VI.FixedInfo.FileVersion[2], VI.FixedInfo.FileVersion[3]]);
34+
except
35+
// keep the defaults initialized in apphelpers
36+
end;
37+
VI.Free;
38+
2439
PostponedLogItems := TDBLogItems.Create(True);
2540
Application.{%H-}MainFormOnTaskBar := True; // hide warning: Symbol "MainFormOnTaskBar" is not portable
2641

@@ -102,8 +117,10 @@
102117
ApplyPlatformTheme(AppSettings.ReadInt(asThemeMode));
103118
{$endif}
104119

120+
// Set the crash dialog handler before creating the main form, so that exceptions
121+
// in TMainForm.FormCreate are caught as well (Application.MainForm is still nil then)
122+
Application.OnException := TfrmCrashDialog.ShowCrashDialog;
105123
Application.CreateForm(TMainForm, MainForm);
106-
Application.OnException := MainForm.ApplicationException;
107124
MainForm.AfterFormCreate;
108125
Application.OnDeactivate := MainForm.ApplicationDeActivate;
109126
Application.OnIdle := MainForm.ApplicationIdle;

‎source/about.pas‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ procedure TAboutBox.FormShow(Sender: TObject);
119119
// Assign text
120120
Caption := f_('About %s', [APPNAME]);
121121
lblAppName.Caption := APPNAME;
122-
lblAppVersion.Caption := _('Version') + ' ' + Mainform.AppVersion;
122+
lblAppVersion.Caption := _('Version') + ' ' + AppVersion;
123123
lblAppCompiled.Caption := _('Compiled on:') + ' ' + DateTimeToStr(GetFileModTime(Application.ExeName)) + ' with';
124124
lnklblCompiler.Caption := GetCompilerVersion;
125125
lnklblCompiler.Hint := 'https://www.lazarus-ide.org/?utm_source='+APPNAME;

‎source/apphelpers.pas‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,10 @@ TAppSettings = class(TObject)
473473
procedure ApplySynEditWordWrap(Editor: TCustomSynEdit; Enable: Boolean);
474474

475475
var
476+
// Version of the executable file, detected in the main program (heidisql.lpr).
477+
// Initialized here so that crash reports have a sane default even if detection fails.
478+
AppVersion: String = '0.0.0.0';
479+
AppVerRevision: Integer = 0;
476480
AppSettings: TAppSettings;
477481
AppLanguageMoFile: TMOFile;
478482
AppLanguageMoBasePath: String;
@@ -3102,7 +3106,7 @@ function GetOS: String;
31023106

31033107
function UserAgent(OwnerComponent: TComponent): String;
31043108
begin
3105-
Result := APPNAME+'/'+MainForm.AppVersion+' ('+GetOS+'; '+ExtractFilename(Application.ExeName)+'; '+OwnerComponent.Name+')';
3109+
Result := APPNAME+'/'+AppVersion+' ('+GetOS+'; '+ExtractFilename(Application.ExeName)+'; '+OwnerComponent.Name+')';
31063110
end;
31073111

31083112

‎source/connections.pas‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ implementation
262262

263263
function Tconnform.GetWindowCaption: String;
264264
begin
265-
Result := APPNAME + ' ' + MainForm.AppVersion + ' - ' + _('Session manager');
265+
Result := APPNAME + ' ' + AppVersion + ' - ' + _('Session manager');
266266
if not SelectedSessionPath.IsEmpty then
267267
Result := Result + ': ' + SelectedSessionPath;
268268
end;

‎source/crashdialog.pas‎

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
interface
66

77
uses
8-
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls, Buttons;
8+
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls, Buttons, LazVersion;
99

1010
type
1111

@@ -26,10 +26,10 @@ TfrmCrashDialog = class(TForm)
2626
private
2727

2828
public
29-
procedure SetDetails(AValue: String);
29+
// Global exception handler, assigned to Application.OnException in heidisql.lpr
30+
class procedure ShowCrashDialog(Sender: TObject; E: Exception);
3031
end;
3132

32-
3333
implementation
3434

3535
uses
@@ -102,9 +102,34 @@ procedure TfrmCrashDialog.FormShow(Sender: TObject);
102102
btnCopy.SetFocus;
103103
end;
104104

105-
procedure TfrmCrashDialog.SetDetails(AValue: String);
105+
class procedure TfrmCrashDialog.ShowCrashDialog(Sender: TObject; E: Exception);
106+
var
107+
I: Integer;
108+
Frames: PPointer;
109+
Header, Report: string;
110+
CrashDlg: TfrmCrashDialog;
106111
begin
107-
memoDetails.Text := AValue;
112+
Report := 'Callstack:' + LineEnding + BackTraceStrFunc(ExceptAddr);
113+
Frames := ExceptFrames;
114+
for I := 0 to ExceptFrameCount - 1 do
115+
Report := Report + LineEnding + BackTraceStrFunc(Frames[I]);
116+
Header := '';
117+
if E <> nil then begin
118+
Header := Header + 'Exception class: ' + E.ClassName + LineEnding +
119+
'Message: ' + E.Message + LineEnding;
120+
end;
121+
Header := Header +
122+
'OS: ' + GetOS + LineEnding +
123+
APPNAME + ' version: ' + AppVersion + LineEnding +
124+
'Lazarus version: ' + LazVersion.laz_version + LineEnding +
125+
'FreePascal version: ' + {$I %FPCVERSION%} + LineEnding +
126+
LineEnding;
127+
CrashDlg := TfrmCrashDialog.Create(Application.MainForm);
128+
CrashDlg.memoDetails.Text := Header + Report;
129+
if CrashDlg.ShowModal = mrAbort then begin
130+
Halt; // End of program execution
131+
end;
132+
CrashDlg.Free;
108133
end;
109134

110135

‎source/exportgrid.pas‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ procedure TfrmExportGrid.btnOKClick(Sender: TObject);
751751
'<html>' + sLineBreak +
752752
CodeIndent + '<head>' + sLineBreak +
753753
CodeIndent(2) + '<title>' + TableName + '</title>' + sLineBreak +
754-
CodeIndent(2) + '<meta name="GENERATOR" content="'+ APPNAME+' '+Mainform.AppVersion + '">' + sLineBreak +
754+
CodeIndent(2) + '<meta name="GENERATOR" content="'+ APPNAME+' '+AppVersion + '">' + sLineBreak +
755755
CodeIndent(2) + '<meta http-equiv="Content-Type" content="text/html; charset='+GetHTMLCharsetByEncoding(Encoding)+'" />' + sLineBreak +
756756
CodeIndent(2) + '<meta name="color-scheme" content="light dark">' + sLineBreak +
757757
CodeIndent(2) + '<style type="text/css">' + sLineBreak +
@@ -1200,7 +1200,7 @@ procedure TfrmExportGrid.btnOKClick(Sender: TObject);
12001200
CodeIndent(2) + '</table>' + sLineBreak + sLineBreak +
12011201
CodeIndent(2) + '<p>' + sLineBreak +
12021202
CodeIndent(3) + '<em>generated ' + DateToStr(now) + ' ' + TimeToStr(now) +
1203-
CodeIndent(3) + 'by <a href="'+APPDOMAIN+'">' + APPNAME + ' ' + Mainform.AppVersion + '</a></em>' + sLineBreak +
1203+
CodeIndent(3) + 'by <a href="'+APPDOMAIN+'">' + APPNAME + ' ' + AppVersion + '</a></em>' + sLineBreak +
12041204
CodeIndent(2) + '</p>' + sLineBreak + sLineBreak +
12051205
CodeIndent + '</body>' + sLineBreak +
12061206
'</html>' + sLineBreak;

‎source/main.pas‎

Lines changed: 5 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,19 +1247,11 @@ TMainForm = class(TExtForm)
12471247
procedure menuAlwaysGenerateFilterClick(Sender: TObject);
12481248
procedure SynMemoQueryShowHint(Sender: TObject; HintInfo: PHintInfo);
12491249
procedure actCopyGridNodesExecute(Sender: TObject);
1250-
procedure ApplicationException(Sender: TObject; E: Exception);
12511250
procedure actQueryTableExecute(Sender: TObject);
12521251
procedure actDisplayLogPanelExecute(Sender: TObject);
12531252
procedure actDisplayTreeFiltersExecute(Sender: TObject);
12541253
procedure actCopyFormattedExecute(Sender: TObject);
12551254
private
1256-
// Executable file details
1257-
FAppVerMajor: Integer;
1258-
FAppVerMinor: Integer;
1259-
FAppVerRelease: Integer;
1260-
FAppVerRevision: Integer;
1261-
FAppVersion: String;
1262-
12631255
FDelimiter: String;
12641256
FLogToFile: Boolean;
12651257
FFileNameSessionLog: String;
@@ -1395,8 +1387,6 @@ TMainForm = class(TExtForm)
13951387
TaskbarList3: ITaskbarList3;
13961388
TaskbarList4: ITaskbarList4;}
13971389

1398-
property AppVerRevision: Integer read FAppVerRevision;
1399-
property AppVersion: String read FAppVersion;
14001390
property Connections: TDBConnectionList read FConnections;
14011391
property Delimiter: String read FDelimiter write SetDelimiter;
14021392
property FocusedTables: TDBObjectList read FFocusedTables;
@@ -1478,10 +1468,10 @@ TMainForm = class(TExtForm)
14781468
implementation
14791469

14801470
uses
1481-
FileInfo, winpeimagereader, elfreader, machoreader, About, data_sorting, column_selection, loaddata, editvar,
1471+
About, data_sorting, column_selection, loaddata, editvar,
14821472
copytable, csv_detector, exportgrid, usermanager, rolemanagerpg, rolemanagermssql, reformatter, connections, sqlhelp, updatecheck,
14831473
insertfiles, texteditor, preferences, table_editor, view, routine_editor, trigger_editor, event_editor, grideditlinks,
1484-
crashdialog, selectdbobject;
1474+
selectdbobject;
14851475

14861476
{$R *.lfm}
14871477

@@ -1891,7 +1881,6 @@ procedure TMainForm.FormCreate(Sender: TObject);
18911881
QueryTab: TQueryTab;
18921882
Action, CopyAsAction: TAction;
18931883
ExportFormat: TGridExportFormat;
1894-
VI: TVersionInfo;
18951884
CopyAsMenu, CommandMenu, ColorSchemeItem: TMenuItem;
18961885
dti: TDBDatatypeCategoryIndex;
18971886
EditorCommand: TSynEditorCommand;
@@ -1934,23 +1923,6 @@ procedure TMainForm.FormCreate(Sender: TObject);
19341923
DatatypeCategories[dti].Name := _(DatatypeCategories[dti].Name);
19351924
end;
19361925

1937-
// Detect version
1938-
try
1939-
VI := TVersionInfo.Create;
1940-
VI.Load(HINSTANCE);
1941-
FAppVerMajor := VI.FixedInfo.FileVersion[0];
1942-
FAppVerMinor := VI.FixedInfo.FileVersion[1];
1943-
FAppVerRelease := VI.FixedInfo.FileVersion[2];
1944-
FAppVerRevision := VI.FixedInfo.FileVersion[3];
1945-
VI.Free;
1946-
except
1947-
FAppVerMajor := 0;
1948-
FAppVerMinor := 0;
1949-
FAppVerRelease := 0;
1950-
FAppVerRevision := 0;
1951-
end;
1952-
FAppVersion := Format('%d.%d.%d.%d', [FAppVerMajor, FAppVerMinor, FAppVerRelease, FAppVerRevision]);
1953-
19541926
// Load snippet filenames
19551927
SetSnippetFilenames;
19561928

@@ -2078,7 +2050,7 @@ procedure TMainForm.FormCreate(Sender: TObject);
20782050
// Force status bar position to below log memo
20792051
StatusBar.Top := SynMemoSQLLog.Top + SynMemoSQLLog.Height;
20802052
actDataShowNext.Hint := f_('Show next %s rows ...', [FormatNumber(AppSettings.ReadInt(asDatagridRowsPerStep))]);
2081-
actAboutBox.Caption := f_('About %s', [APPNAME+' '+FAppVersion]);
2053+
actAboutBox.Caption := f_('About %s', [APPNAME+' '+AppVersion]);
20822054
// Activate logging
20832055
LogToFile := AppSettings.ReadBool(asLogToFile);
20842056
if AppSettings.ReadBool(asLogHorizontalScrollbar) then
@@ -2291,7 +2263,7 @@ procedure TMainForm.AfterFormCreate;
22912263
Environment := Environment + 'Portable';
22922264

22932265
StatsCall := THttpDownload.Create(Self);
2294-
StatsURL := APPDOMAIN + 'savestats.php?c=' + IntToStr(FAppVerRevision) +
2266+
StatsURL := APPDOMAIN + 'savestats.php?c=' + IntToStr(AppVerRevision) +
22952267
'&bits=' + IntToStr(GetExecutableBits) +
22962268
'&env=' + EncodeURLParam(Environment) +
22972269
'&winver=';
@@ -13695,7 +13667,7 @@ procedure TMainForm.SetWindowCaption;
1369513667
Cap := DBtree.Path(DBtree.FocusedNode, 0, ttStatic, '\') + ' - ' + APPNAME;
1369613668
if AppSettings.PortableMode then
1369713669
Cap := Cap + ' Portable';
13698-
Cap := Cap + ' ' + FAppVersion;
13670+
Cap := Cap + ' ' + AppVersion;
1369913671
Caption := Cap;
1370013672
Application.Title := Cap;
1370113673
end;
@@ -15347,34 +15319,6 @@ procedure TMainForm.ApplicationShowHint(var HintStr: string; var CanShow: Boolea
1534715319
end;
1534815320

1534915321

15350-
procedure TMainForm.ApplicationException(Sender: TObject; E: Exception);
15351-
var
15352-
I: Integer;
15353-
Frames: PPointer;
15354-
Report: string;
15355-
CrashDlg: TfrmCrashDialog;
15356-
begin
15357-
Report := '';
15358-
if E <> nil then begin
15359-
Report := Report + 'Exception class: ' + E.ClassName + LineEnding +
15360-
'Message: ' + E.Message + LineEnding +
15361-
APPNAME + ' ' + MainForm.AppVersion + LineEnding +
15362-
LineEnding;
15363-
end;
15364-
Report := Report + 'Callstack:' + LineEnding;
15365-
Report := Report + BackTraceStrFunc(ExceptAddr);
15366-
Frames := ExceptFrames;
15367-
for I := 0 to ExceptFrameCount - 1 do
15368-
Report := Report + LineEnding + BackTraceStrFunc(Frames[I]);
15369-
CrashDlg := TfrmCrashDialog.Create(Self);
15370-
CrashDlg.SetDetails(Report);
15371-
if CrashDlg.ShowModal = mrAbort then begin
15372-
Halt; // End of program execution
15373-
end;
15374-
CrashDlg.Free;
15375-
end;
15376-
15377-
1537815322
procedure TMainForm.actToggleCommentExecute(Sender: TObject);
1537915323
var
1538015324
Editor: TSynMemo;

‎source/tabletools.pas‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,7 +1911,7 @@ procedure TfrmTableTools.DoExport(DBObj: TDBObject);
19111911
Format('-- %-30s%s', [_('Host')+':', DBObj.Connection.Parameters.HostName]) + CRLF +
19121912
Format('-- %-30s%s', [_('Server version')+':', DBObj.Connection.ServerVersionUntouched]) + CRLF +
19131913
Format('-- %-30s%s', [_('Server OS')+':', DBObj.Connection.ServerOS]) + CRLF +
1914-
Format('-- %-30s%s', [APPNAME + ' ' + _('Version')+':', Mainform.AppVersion]) + CRLF +
1914+
Format('-- %-30s%s', [APPNAME + ' ' + _('Version')+':', AppVersion]) + CRLF +
19151915
'-- --------------------------------------------------------' + CRLF + CRLF;
19161916
end;
19171917
Header := Header +
@@ -2140,7 +2140,7 @@ procedure TfrmTableTools.DoExport(DBObj: TDBObject);
21402140
Data := DBObj.Connection.GetResults(
21412141
DBObj.Connection.ApplyLimitClause(
21422142
'SELECT',
2143-
'/* '+APPNAME+' '+MainForm.AppVersion+' */ ' + ColumnsForSelect + ' FROM '+DBObj.QuotedDbAndTableName + OrderBy,
2143+
'/* '+APPNAME+' '+AppVersion+' */ ' + ColumnsForSelect + ' FROM '+DBObj.QuotedDbAndTableName + OrderBy,
21442144
Limit,
21452145
Offset)
21462146
);

‎source/updatecheck.pas‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ procedure TfrmUpdateCheck.FormShow(Sender: TObject);
8686
ReadCheckFile;
8787
// Developer versions probably have "unknown" (0) as revision,
8888
// which makes it impossible to compare the revisions.
89-
if Mainform.AppVerRevision = 0 then
89+
if AppVerRevision = 0 then
9090
Status(_('Error: Cannot determine current revision. Using a developer version?'))
9191
else if groupRelease.Enabled then
9292
Status(_('Updates available.'))
@@ -122,7 +122,7 @@ procedure TfrmUpdateCheck.ReadCheckFile;
122122
// Prepare download
123123
CheckfileDownload := THttpDownload.Create(Self);
124124
CheckfileDownload.TimeOut := 5;
125-
CheckfileDownload.URL := APPDOMAIN+'updatecheck.php?r='+IntToStr(Mainform.AppVerRevision)+'&bits='+IntToStr(GetExecutableBits)+'&os='+EncodeURLParam(GetOS.ToLower)+'&t='+EncodeURLParam(DateTimeToStr(Now));
125+
CheckfileDownload.URL := APPDOMAIN+'updatecheck.php?r='+IntToStr(AppVerRevision)+'&bits='+IntToStr(GetExecutableBits)+'&os='+EncodeURLParam(GetOS.ToLower)+'&t='+EncodeURLParam(DateTimeToStr(Now));
126126
CheckFilename := GetTempDir + APPNAME + '_updatecheck.ini';
127127

128128
// Download the check file
@@ -138,7 +138,7 @@ procedure TfrmUpdateCheck.ReadCheckFile;
138138
ReleasePackage := 'portable'
139139
else
140140
ReleasePackage := {$IFDEF WINDOWS} 'installer' {$ELSE} 'package' {$ENDIF};
141-
memoRelease.Lines.Add(f_('Version %s (yours: %s)', [ReleaseVersion, Mainform.AppVersion]));
141+
memoRelease.Lines.Add(f_('Version %s (yours: %s)', [ReleaseVersion, AppVersion]));
142142
memoRelease.Lines.Add(f_('Released: %s', [Ini.ReadString(INISECT_RELEASE, 'Date', '')]));
143143
Note := Ini.ReadString(INISECT_RELEASE, 'Note', '');
144144
if Note <> '' then

0 commit comments

Comments
 (0)