In a small application, I want to show the open file dialog before creating the main form. I do it like this: * .dpr:
begin Application.Initialize; Init; // <========================================= Application.MainFormOnTaskbar := True; Application.CreateForm(TForm1, Form1); Application.Run; end.
When I use the following Init procedure:
procedure Init; var OpenDialog: TOpenDialog; begin TheFileName := '(nix)'; OpenDialog := TOpenDialog.Create(nil); try if OpenDialog.Execute then TheFileName := OpenDialog.FileName else Exit; finally OpenDialog.Free; end; end;
in Windows 7 there is no dialog box. I can fix this by setting UseLatestCommonDialogs to False, but I would like to avoid it. Change Init procedure for
procedure Init; var OpenDialog: TOpenDialog; begin TheFileName := '(nix)'; CoInitialize(nil); try OpenDialog := TOpenDialog.Create(nil); try if OpenDialog.Execute then TheFileName := OpenDialog.FileName else Exit; finally OpenDialog.Free; end; finally CoUninitialize; end; end;
works. However, I'm not sure if I get the CoInitialize / CoUninitialize right, for example:
- Am I ruining something by calling CoUninitialize so early?
- To “know” the fact that TOpenDialog internally (sometimes) uses COM, it smells like a close abstraction, which I would rather avoid.
Edit: I found a slightly better way: if I add ComObj to the dpr uses , I can omit the CoInitialize / CoUninitialize calls. Of course, the problem with fuzzy abstraction persists.
source share