01 October 2015

Delphi XE and ShellExecute glitch

Just encountered a strange bug in one of my programs when running it from the Delphi XE IDE on Windows 10.

Triggering a TBrowseURL action with a valid URL property value causes the program to hang and the default browser (Chrome) is not displayed. The BeforeBrowse event is triggered, but AfterBrowse is not until the program is closed. Sometimes the program hangs and I have to close it with Ctrl+F2.

Running the same code outside the IDE works correctly, but running the program either with or without the debugger in the IDE makes no difference.

I've tracked this down to where TBrowseURL calls ShellExecute from the ShellAPI unit. If I call ShellExecute directly I get the same problem.

I never noticed this problem before when running Windows 7 on my old laptop - it just seems to have started on my new Windows 10 laptop. Still, it's possible the bug was still there on the old machine and I just never noticed it, but I doubt it.

Anyone else had this problem? Any ideas what may be causing it?

13 comments:

Thomas Mueller said...

I know that one but never tracked it down. It occurred even on Windows XP for me sometimes.

Peter Johnson said...

Glad someone else has seen it. I was thinking I'd broken something!

Lars Fosdal said...

Do you call CoInitialize before ShellExecute?
CoInitializeEx(nil, COINIT_APARTMENTTHREADED or COINIT_DISABLE_OLE1DDE);

Peter Johnson said...

I'm not sure if I did call CoInitialize - I'll check and try it if not.

I know some parts of the code do call it, but there may (should) be a matching CoUnintialize to those calls.

Thanks.

Lars Fosdal said...

The reason is that some shellexecutes may cause COM activation, and that will fail if you don't do CoInit/CoUnInit.
I will be curious to see if it helps for your problem?

Peter Johnson said...

Tried calling CoInitializeEz as suggested Lars, both in FormCreate and FormDestroy for the main form and in the BeforeBrowser and AfterBrowse events of a TBrowseURL component and no luck - the program still hangs in the IDE when the action is triggered while it works fine when run outside the IDE.

Lars Fosdal said...

Did you test the different threading models?

EMB said...

The problem is in ShellExecute or it do really need a TWebBrowser to reproduce?

Peter Johnson said...

Problem is in ShellExecute. I'm intercepting any links clicked in TWebBrowser and handling them myself.

Haven't had chance to try any other threading models yet.

KeeperWorld said...

Problem is in ShellExecute!!! This problem of SheelExecute - only in Windows 10. In XP, Windows7, Windows8 - everything is normal.... Who does that know how to correct it?

Peter Johnson said...

Glad it'snot me then!

I'd also be interested in any solution.

Jose Ozuna said...

the problem is, Delphi no convertion String Type to PChar is Wrong

this code work fine on Windows 10 with Compile with delphi XE8

var
s : WideString;
begin
s := VariablePathString;
ShellExecuteW(Handle, PWideChar('open'), PWideChar(s), nil, nil, SW_SHOWNORMAL);
end;

Peter Johnson said...

Thx

I'll try that.