【发布时间】:2019-09-28 13:52:48
【问题描述】:
喂!我需要在 C++ 中查询应用程序完整路径,例如“meshlab”->“C:\Program Files\VCG\MeshLab\meshlab.exe”
此信息存在于 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App 路径中,但我不想直接使用注册表,所以我使用 AssocQueryString 如下:
#include "pch.h"
#include <iostream>
#include <Windows.h>
#include <Shlwapi.h>
int main()
{
char* executablePath = nullptr;
DWORD executablePathLen = 0;
std::string shortName = "mspaint";
HRESULT res = AssocQueryStringA(ASSOCF_OPEN_BYEXENAME,
ASSOCSTR_EXECUTABLE,
shortName.c_str(),
NULL,
executablePath,
&executablePathLen);
executablePath = new char[executablePathLen];
res = AssocQueryStringA(ASSOCF_OPEN_BYEXENAME,
ASSOCSTR_EXECUTABLE,
shortName.c_str(),
NULL,
executablePath,
&executablePathLen);
std::cout << executablePath; // prints: C:\Windows\system32\mspaint.exe
delete[] executablePath;
std::cin.get();
}
对于 mspaint,它按预期工作,但对于 meshlab,它没有。 HRESULT 是 ERROR_NO_ASSOCIATION
有什么我错过的想法吗?
更新: 也适用于 C:\Program Files (x86)\foobar2000\foobar2000.exe 中的 foobar200 我怀疑它一定与 32/64 位注册表有关。我使用的是 Windows 10 64 位,而我的应用程序是 64 位
【问题讨论】:
-
AssocQueryStringA 的返回值是多少,函数调用后
&executablePathLen的结果是什么? -
并非每个程序都必须在注册表中列出自己。你确定meshlab可以吗?搜索您的本地注册表并查看确切位置。键名可能不是“meshlab”。一般来说,在 Windows 上查找第三方可执行文件并不是一门精确的科学,尤其是在构建它们时没有首先考虑编程访问。
-
@SevaAlekseyev 因为我很遗憾注册表中存在信息
-
ERROR_NO_ASSOCIATION 是一个非常强烈的信号
-
用 Procmon 运行 ShellExecuteEx,看看它读到了什么。 OBTW,meshlab 是否有可能在 PATH 中?
标签: c++ windows winapi registry