I have a rather strange problem:
My program uses ShellExecuteEx to start another program. This works fine when my program runs stand alone, but fails when it gets started from the Delphi IDE where "Started from the Delphi IDE" means either:
- Run -> Run (inside the debugger)
- Run -> Run without Debugging
ShellExecuteEx returns false and RaiseLastOsError results in the following error message:
System Error. Code: -2146368396.
The COM+ registry database detected a system error.
The same program has another problem that is probably caused by the same issue: The TOpenDialog.Exeucte and TSaveDialog.Execute methods don't do anything. No dialog is shown and the functions return false. Again this works fine when the program runs stand alone. From googling I have found that this is a COM related issue as well.
My program does not contain any COM code, only those functions that the Delphi RTL/VCL automatically calls.
I have placed a breakpoint on CoInitialize and CoInitializeEx and found only one call to CoInitialize which comes from ComObj.InitComObj. There seems to be nothing wrong there.
Here is the code that fails:
function ShellExecEx(const Filename: string; const Parameters: string;
const Verb: string; CmdShow: Integer; _ShowAssociateDialog: Boolean = False): boolean;
var
Sei: TShellExecuteInfo;
begin
FillChar(Sei, SizeOf(Sei), #0);
Sei.cbSize := SizeOf(Sei);
Sei.FMask := SEE_MASK_DOENVSUBST;
if not _ShowAssociateDialog then
Sei.FMask := Sei.FMask or SEE_MASK_FLAG_NO_UI;
Sei.lpFile := PChar(Filename);
if Parameters <> '' then
Sei.lpParameters := PChar(Parameters)
else
Sei.lpParameters := nil;
if Verb <> '' then
Sei.lpVerb := PChar(Verb)
else
Sei.lpVerb := nil;
Sei.nShow := CmdShow;
Result := ShellExecuteEx(@Sei);
end;
// called as:
lEditorFilename := 'C:\Program Files (x86)\Notepad++\notepad++.exe';
lParameterStr := '"D:\source\EditorUi.dfm" -n1540';
if not ShellExecEx(lEditorFilename, lParameterStr, '', SW_SHOWNORMAL) then
RaiseLastOSError;
This is a 32 bit Delphi XE2 program running on Windows 8.1 64 bit.
Any hints what might cause this?
EDIT:
Following the question from David Heffernan regarding env substitution I removed the additional environment variable
lang=de
I had put into the Run -> Parameters dialog to test the German translations. And all of a sudden both effects described above went away. Putting it back, or adding just any environment variable (eg. test=test), reproduced them reliably.
WTF?