Make Vista + file dialogs before creating the main form

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.

+4
source share
1 answer

It seems that the problem is that COM is not initializing. This usually happens in Application.Initialize due to the initialization procedure, which is added using ComObj . But you are reporting that InitProc is nil inside Application.Initialize , which indicates that ComObj not included in your project.

So, you can easily solve the problem by including ComObj in your project. Or, if you want to be explicit, just call CoInitilize(nil) at the very beginning of your .dpr file.

+3
source

Source: https://habr.com/ru/post/1482102/


All Articles