使用 Windows API 获取文件关联

发布于 2024-09-15 13:13:40 字数 705 浏览 4 评论 0原文

我正在使用 C++ 为 Windows 开发一个基于控制台的文件浏览器,并且很难组合一个上下文菜单来列出与文件关联的操作并调用它们的命令。目前最大的问题是将操作与文件类型相关联。

我知道打开和调整 HKEY_CLASSES_ROOT 中注册表项的过程,但我找不到实际获取操作及其命令的方法,因此我可以从中构建上下文菜单。

注册表中这些关联的一般结构是:

HKEY_CLASSES_ROOT\(扩展名)\(默认)- 文件类型
HKEY_CLASSES_ROOT\filetype\(默认) - 文件类型的描述
HKEY_CLASSES_ROOT\filetype\shell\action\(默认) - 操作描述
HKEY_CLASSES_ROOT\filetype\shell\action\command\(默认) - 在文件上调用的命令

我想知道是否有一种方法(希望使用 Windows API)可以获取与文件类型关联的所有操作。至少这样我可以在注册表中检查这些操作的命令...

此外,这种方法似乎不适用于我系统上的某些常见文件类型(例如 mp3),因为默认键留空,而另一个键( “PercievedType”)设置为音频...我怎样才能获得类似这样的操作?

最后,如果有更好的方法来做到这一点,我很想听听,我通常讨厌处理注册表。我更愿意有一个简单的 Windows 调用来获取操作和命令......

I'm working on a console based file browser for Windows in C++ and am having difficulties getting together a context menu that lists actions associated with a file and calls commands on them. The biggest issue right now is getting the actions tied to the file types.

I know of the process to open and tweak the registry keys in HKEY_CLASSES_ROOT but I can't find a way to actually get the actions and their commands so I can build a context menu out of it.

The general structure of these associations in the registry is:

HKEY_CLASSES_ROOT\(extension)\(default) - filetype
HKEY_CLASSES_ROOT\filetype\(default) - description of the filetype
HKEY_CLASSES_ROOT\filetype\shell\action\(default) - description of the action
HKEY_CLASSES_ROOT\filetype\shell\action\command\(default) - command called on file

I'm wondering if there is a way (hopefully using the Windows API) that I can get all of the actions associated with a file type. At least then I can check those actions for their commands in the registry...

Also, this approach doesn't seem to work with some common file types (e.g. mp3) on my system as the default key is left blank and another key ("PercievedType") is set to audio... How can I get the actions for something like this?

Lastly, if there is a better way to do this in general I would love to hear it, I generally hate dealing with the registry. I would much rather have a simple windows call that would get me the actions and commands...

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

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

发布评论

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

评论(2

金兰素衣 2024-09-22 13:13:40

试试这个(为简洁起见,省略了错误处理):

TCHAR szBuf[1000];
DWORD cbBufSize = sizeof(szBuf);
HRESULT hr = AssocQueryString(0, ASSOCSTR_FRIENDLYAPPNAME,
   argv[1], NULL, szBuf, &cbBufSize);
if (FAILED(hr)) { /* handle error */ }
CStringA strFriendlyProgramName(szBuf, cbBufSize);

cbBufSize = sizeof(szBuf);
hr = AssocQueryString(0, ASSOCSTR_EXECUTABLE, 
   argv[1], NULL, szBuf, &cbBufSize);
if (FAILED(hr)) { /* handle error */ }
CStringA strExe(szBuf, cbBufSize);

std::cout << strFriendlyProgramName << " (" << strExe << ")" << std::endl;

Try this (error handling omitted for brevity):

TCHAR szBuf[1000];
DWORD cbBufSize = sizeof(szBuf);
HRESULT hr = AssocQueryString(0, ASSOCSTR_FRIENDLYAPPNAME,
   argv[1], NULL, szBuf, &cbBufSize);
if (FAILED(hr)) { /* handle error */ }
CStringA strFriendlyProgramName(szBuf, cbBufSize);

cbBufSize = sizeof(szBuf);
hr = AssocQueryString(0, ASSOCSTR_EXECUTABLE, 
   argv[1], NULL, szBuf, &cbBufSize);
if (FAILED(hr)) { /* handle error */ }
CStringA strExe(szBuf, cbBufSize);

std::cout << strFriendlyProgramName << " (" << strExe << ")" << std::endl;
╄→承喏 2024-09-22 13:13:40

考虑使用 IContextMenu。 IContextMenu 是 Windows 资源管理器访问文件和项目的上下文菜单的方式。

Raymond Chen 的这篇文章提供了如何访问给定 IContextMenu 的示例代码文件路径,并使用它用可用命令集填充 HMENU。这是 系列文章,提供了不错的概述以及示例代码。

Consider using IContextMenu. IContextMenu is how Windows Explorer accesses the context menu for files and items.

This article by Raymond Chen has sample code for how to access IContextMenu for a given file path and use it to fill an HMENU with the set of available commands. It's the first of a series of articles that give a decent overview along with sample code.

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