Preventing DLLHost Process Shutdown or Keeping Explorer Window Responsive during Long-Running IExplorerCommand::Invoke Operation in C++

96 views Asked by At

I have created a class that implements the IExplorerCommand interface. The Invoke() method in this class is being called from another process (DllHost.exe). In my case DLLHost.exe is called by Explorer.exe when I click on a Windows 11 context menu item I added. However, since my Invoke() method involves a time-consuming operation, executing it on the caller thread causes the Windows Explorer window to become unresponsive. To mitigate this, I have offloaded the actual job to a separate thread using std::thread.

The challenge I'm facing is that, approximately 5 seconds after IExplorerCommand::Invoke() is triggered, the DLLHost.exe process gets shut down, even while the background thread is still running. I've attempted to retain a reference count to the IExplorerCommand instance within the thread and also maintained a process handle using SHGetInstanceExplorer(), but these measures haven't prevented the premature shutdown of the process.

I also tried to start my thread with the SHCreateThread() function, and to pass the CTF_COINIT_STA | CTF_PROCESS_REF as flags to it, instead of std::thread, also without success.

Here are my queries:

  • How can I prevent the DLLHost.exe process from shutting down before my background thread completes its job?
  • Alternatively, is there a way to keep the Explorer window responsive while the Invoke() method is blocking due to its ongoing operation?

Any insights or suggestions on tackling this issue would be greatly appreciated.

--- ADDITIONAL NOTES ---

I also tried to replace my class by the CExplorerCommandVerb class from the Windows Classic Samples ExplorerCommandVerb project (available here: https://github.com/microsoft/Windows-classic-samples/blob/main/Samples/Win7Samples/winui/shell/appshellintegration/ExplorerCommandVerb/ExplorerCommandVerb.cpp), I got the same result.

The only notable difference is in the manner I declare my class and register it to the COM/WinRT. Below is what I use:

class __declspec(uuid("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")) MyExplorerCommand final : public Microsoft::WRL::RuntimeClass
    <Microsoft::WRL::RuntimeClassFlags<Microsoft::WRL::ClassicCom>, IExplorerCommand, IObjectWithSite>
{
    ...
}

CoCreatableClass(MyExplorerCommand)
CoCreatableClassWrlCreatorMapInclude(MyExplorerCommand)

May the issue comes from a such registration?

0

There are 0 answers