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?
I know that one but never tracked it down. It occurred even on Windows XP for me sometimes.
ReplyDeleteGlad someone else has seen it. I was thinking I'd broken something!
ReplyDeleteDo you call CoInitialize before ShellExecute?
ReplyDeleteCoInitializeEx(nil, COINIT_APARTMENTTHREADED or COINIT_DISABLE_OLE1DDE);
I'm not sure if I did call CoInitialize - I'll check and try it if not.
ReplyDeleteI know some parts of the code do call it, but there may (should) be a matching CoUnintialize to those calls.
Thanks.
The reason is that some shellexecutes may cause COM activation, and that will fail if you don't do CoInit/CoUnInit.
ReplyDeleteI will be curious to see if it helps for your problem?
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.
ReplyDeleteDid you test the different threading models?
ReplyDeleteThe problem is in ShellExecute or it do really need a TWebBrowser to reproduce?
ReplyDeleteProblem is in ShellExecute. I'm intercepting any links clicked in TWebBrowser and handling them myself.
ReplyDeleteHaven't had chance to try any other threading models yet.
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?
ReplyDeleteGlad it'snot me then!
ReplyDeleteI'd also be interested in any solution.
the problem is, Delphi no convertion String Type to PChar is Wrong
ReplyDeletethis 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;
Thx
ReplyDeleteI'll try that.
I am also facing a shellexecute glitch with XE5 in win7. Sometimes it executes sometimes crashes the application without executing an else error message block. I tried showmessage(path) each time. no problem with path each time but shellexecute becomes unpredictable.
ReplyDeleteDefinitely from my showmessage experiments, I think the problem is with PChar() function as Jose points out. Let's see.
ReplyDeletenothing seems to work. resorted to use createprocess with commandline path to document.
ReplyDelete