带稀疏包的WPF应用首次加载Shell扩展时被终止问题求助
问题概述
我开发了一个WPF应用,为使其具备身份以调用UWP APIs,添加了sparse package支持。该功能可随WPF应用正常安装、卸载和更新(未使用MSIX),应用运行时已获得身份,在任务管理器进程中可见对应的Package Name。
目前我正按照微软文档添加右键菜单支持,基于官方示例实现了一个Shell扩展,可在所有文件和文件夹的右键菜单中显示,仅修改了IExplorerCommand的GetIcon()、GetTitle()和Invoke()实现。
AppxManifest.xml配置
我在AppxManifest.xml中进行了如下配置(值已匿名处理):
<desktop4:Extension Category="windows.fileExplorerContextMenus"> <desktop4:FileExplorerContextMenus> <desktop5:ItemType Type="*"> <desktop5:Verb Id="MyFileCommand" Clsid="file-guid"/> </desktop5:ItemType> <desktop5:ItemType Type="Directory"> <desktop5:Verb Id="MyFolderCommand" Clsid="folder-guid"/> </desktop5:ItemType> </desktop4:FileExplorerContextMenus> </desktop4:Extension> <com:Extension Category="windows.comServer"> <com:ComServer> <com:SurrogateServer DisplayName="SSVerbHandler"> <com:Class Id="file-guid" Path="my-shell-extension.dll" ThreadingModel="STA"/> </com:SurrogateServer> <com:SurrogateServer DisplayName="SSVerbHandler"> <com:Class Id="folder-guid" Path="my-shell-extension.dll" ThreadingModel="STA"/> </com:SurrogateServer> </com:ComServer> </com:Extension>
该配置可正常工作,右键菜单条目能正常显示并执行操作,但存在一个问题:每次首次打开右键菜单(即重启explorer.exe后右键点击文件或文件夹)时,已运行的WPF应用实例会被终止。
我推测这与UWP相关,因为最初每次右键点击都会终止应用,后来通过在AppxManifest.xml中配置多实例支持解决了部分问题:
<Package ... xmlns:desktop4="http://schemas.microsoft.com/appx/manifest/desktop/windows10/4" xmlns:iot2="http://schemas.microsoft.com/appx/manifest/iot/windows10/2" IgnorableNamespaces="uap mp desktop4 iot2"> ... <Applications> <Application Id="App" ... desktop4:SupportsMultipleInstances="true" iot2:SupportsMultipleInstances="true"> ... </Application> </Applications> ... </Package>
已尝试的排查操作
- 在Shell扩展的
DllMain、DllCanUnloadNow和DllGetClassObject中添加MessageBox,但首次加载时无弹窗,也不显示右键菜单,应用仍被终止; - 查看事件查看器(包括Appx*和AppModel-Runtime日志),未发现相关错误;
- 通过PowerShell获取退出码为1,但应用的崩溃日志无记录,仅在一处调用了
Environment.Exit(1); - 尝试用GFlags的静默退出监控,未触发;
- 用Process Monitor观察到大量ThreadExit后出现ProcessExit,退出码为1,似乎是应用自行返回1退出。
注:我近十年主要使用托管/.NET技术,对C++(或非托管语言)、UWP和Shell扩展开发经验较少。
内容的提问来源于stack exchange,提问作者DJH
相关产品推荐
相关产品推荐

