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?

Comments

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

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

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

    ReplyDelete
  4. 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.

    ReplyDelete
  5. 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?

    ReplyDelete
  6. 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.

    ReplyDelete
  7. Did you test the different threading models?

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

    ReplyDelete
  9. 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.

    ReplyDelete
  10. 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?

    ReplyDelete
  11. Glad it'snot me then!

    I'd also be interested in any solution.

    ReplyDelete
  12. 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;

    ReplyDelete
  13. Tiger Jakt8:24 pm

    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.

    ReplyDelete
  14. Tiger Jakt8:26 pm

    Definitely from my showmessage experiments, I think the problem is with PChar() function as Jose points out. Let's see.

    ReplyDelete
  15. Anonymous3:33 pm

    nothing seems to work. resorted to use createprocess with commandline path to document.

    ReplyDelete

Post a Comment

Comments are very welcome, but please don't comment here if:

1) You have a query about, or a bug report for, one of my programs or libraries. Most of my posts contain a link to the relevant repository where there will be an issue tracker you can use.

2) You have a query about any 3rd party programs I feature, please address them to the program's developer(s) - there will be a link in the post.

3) You're one of the tiny, tiny minority who are aggressive or abusive - in the bin you go!

Thanks

Popular posts from this blog

New String Property Editor Planned For RAD Studio 12 Yukon 🤞

Multi-line String Literals Planned For Delphi 12 Yukon🤞

Call JavaScript in a TWebBrowser and get a result back