如果 ShellExecute 由于 C++ 中没有文件关联而失败,如何打开窗口的默认对话框?

发布于 2024-11-16 04:01:34 字数 268 浏览 10 评论 0原文

我可以使用 Windows ShellExecute 函数打开一个文件,只要该文件具有正确的关联,就不会出现任何问题。

如果不存在关联,我想使用默认的 Windows 对话框打开文件:

image

这可能吗?如果是这样怎么办?

I can use the windows ShellExecute function to open a file with no problems so long as the file has a correct association.

If no association exists i would like to use the default windows dialog to open the file:

image

Is this possible? If so how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

少女净妖师 2024-11-23 04:01:34

记录 方式 显示该对话框是使用 openas动词。

CoInitializeEx(NULL, COINIT_APARTMENTTHREADED|COINIT_DISABLE_OLE1DDE);
SHELLEXECUTEINFO sei = { sizeof(sei) };
sei.fMask = SEE_MASK_NOASYNC;
sei.nShow = SW_SHOWNORMAL;
sei.lpVerb = "openas";
sei.lpFile = "C:\\yourfile.ext";
ShellExecuteEx(&sei);

如果您在 HKEY_CLASSES_ROOT\Unknown\shell\openas 下进行检查,您会发现这与在 shell32 中调用(未记录的)OpenAs_RunDLL 导出相同。

The documented way to show that dialog is to use the openas verb.

CoInitializeEx(NULL, COINIT_APARTMENTTHREADED|COINIT_DISABLE_OLE1DDE);
SHELLEXECUTEINFO sei = { sizeof(sei) };
sei.fMask = SEE_MASK_NOASYNC;
sei.nShow = SW_SHOWNORMAL;
sei.lpVerb = "openas";
sei.lpFile = "C:\\yourfile.ext";
ShellExecuteEx(&sei);

If you check under HKEY_CLASSES_ROOT\Unknown\shell\openas you see that this is the same as calling the (undocumented) OpenAs_RunDLL export in shell32.

噩梦成真你也成魔 2024-11-23 04:01:34

执行RUNDLL32 Shell32,OpenAs_RunDLL path/to/file/to/open

Execute RUNDLL32 Shell32,OpenAs_RunDLL path/to/file/to/open

榆西 2024-11-23 04:01:34

只是不要使用明确的动词。使用像“open”这样的特定动词是一个很大的错误:

  • “open”可能不是默认动词(例如,它可能是“play”,“edit”或“run”)
  • “open”可能不存在

它是一个更正确的方式是简单地将 NULL 作为动词传递。系统将自动选择最合适的动词:

  • 将使用默认动词,如果设置了
  • “open”动词,将使用“open”动词,如果未设置默认动词,
  • 将使用第一个动词,如果没有默认动词和“open”动词可用
  • < strong>如果没有指定动词 - 系统将带来“打开方式”对话框

换句话说,simple

ShellExecute(0, NULL, 'C:\MyFile.StrangeExt', ...);

将显示“打开方式”对话框。

仅当您想要特定操作时才使用特定动词。例如“打印”、“探索”、“运行”。否则 - 只需传递 nil。

Simply do not use explicit verb. Using a specific verb like 'open' is a big mistake:

  • 'open' may be not a default verb (for example, it may be 'play', 'edit' or 'run')
  • 'open' may not exists

It is a way more correct to simply pass NULL as verb. The system will automatically select most appropriate verb:

  • Default verb will be used, if it is set
  • 'open' verb will be used, if no default verb is set
  • first verb will be used, if no default and 'open' verbs are available
  • if no verbs are assigned - the system will bring "Open with" dialog

In other words, simple

ShellExecute(0, NULL, 'C:\MyFile.StrangeExt', ...);

will show "Open with" dialog.

Only use a specific verb if you want a specific action. E.g. 'print', 'explore', 'runas'. Otherwise - just pass nil.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文