Do I need to call Coinitialize
in the main/VCL thread in Delphi
before using ShellExecuteEx?
For a thread, yes but for the VCL thread ?
In the RTL/VCL source, COM is initialized in the following ways:
OleInitialize
made from Forms.TApplication.Create
. So this call will be made for all VCL forms applications, but not, for example, for service applications.CoInitialize
or CoInitializeEx
in ComObj.InitComObj
. This is registered as an InitProc
in the initialization
section of the ComObj
unit. In turn, the call to Application.Initialize
in your project .dpr file's code will invoke ComObj.InitComObj
.Now, of these various COM initializations, the ones that count are 1 and 2. In any standard VCL forms application, both of these will run at startup in the main thread. Item 1 runs first and so gets to initialize COM first. That's the initialization that counts. Item 2 runs after and returns S_FALSE
meaning that COM was already initialized.
So, to your question:
Do I need to call Coinitialize in the main/VCL thread?
No you do not. You can be sure that COM has already been initialized in a VCL application's main thread.