【问题标题】:What's the best way to resolve a filepath?解决文件路径的最佳方法是什么?
【发布时间】:2011-01-30 03:48:33
【问题描述】:

我有一系列看起来像这样的文件路径:

  • C:\Windows\System32\svchost.exe -k LocalSystemNetworkRestricted
  • C:\Windows\System32\svchost
  • C:\Program Files (x86)\Common Files\Steam\SteamService.exe /RunAsService
  • "C:\Program Files (x86)\Common Files\Steam\SteamService.exe" /RunAsService

我需要找到这些路径的实际位置。所以,上面分别是:

  • C:\Windows\System32\svchost.exe
  • C:\Windows\System32\svchost.exe
  • C:\Program Files (x86)\Common Files\Steam\SteamService.exe
  • C:\Program Files (x86)\Common Files\Steam\SteamService.exe

执行此操作的最佳方法是什么? windows是否有API函数来完成它?我本质上是想弄清楚如果我将路径传递给 CreateProcess 会调用什么可执行文件。

谢谢!

比利3

编辑:这是我现在确定的代码:

#include <algorithm>
#include <vector>
#include <string>
#include <boost/algorithm/string.hpp>
#include <Windows.h>

namespace Path {

bool Exists(const std::wstring& path)
{
    DWORD result = GetFileAttributesW(path.c_str());
    return result != INVALID_FILE_ATTRIBUTES;
}

#define PATH_PREFIX_RESOLVE(path, prefix, environment) \
if (boost::algorithm::istarts_with(path, prefix)) { \
    ExpandEnvironmentStringsW(environment, buffer, MAX_PATH); \
    path.replace(0, (sizeof(prefix)/sizeof(wchar_t)) - 1, buffer); \
    if (Exists(path)) return path; \
}

std::wstring Resolve(std::wstring path)
{
    using namespace boost::algorithm;
    wchar_t buffer[MAX_PATH];
    trim(path);
    if (path.empty() || Exists(path)) return path;

    //Start by trying to see if we have a quoted path
    if (path[0] == L'"') {
        return std::wstring(path.begin() + 1, std::find(path.begin() + 1, path.end(), L'"'));
    }

    //Check for those nasty cases where the beginning of the path has no root
    PATH_PREFIX_RESOLVE(path, L"\\", L"");
    PATH_PREFIX_RESOLVE(path, L"?\?\\", L"");
    PATH_PREFIX_RESOLVE(path, L"\\?\\", L"");
    PATH_PREFIX_RESOLVE(path, L"globalroot\\", L"");
    PATH_PREFIX_RESOLVE(path, L"system32\\", L"%systemroot%\\System32\\");
    PATH_PREFIX_RESOLVE(path, L"systemroot\\", L"%systemroot%\\");

    static std::vector<std::wstring> pathExts;
    if (pathExts.empty()) {
        #define MAX_ENVVAR 32767
        wchar_t pathext[MAX_ENVVAR];
        DWORD length = GetEnvironmentVariableW(L"PATHEXT", pathext, MAX_ENVVAR);
        if (!length) WindowsApiException::ThrowFromLastError();
        split(pathExts, pathext, std::bind2nd(std::equal_to<wchar_t>(), L';'));
        pathExts.insert(pathExts.begin(), std::wstring());
    }
    std::wstring::iterator currentSpace = path.begin();
    do {
        currentSpace = std::find(currentSpace, path.end(), L' ');
        std::wstring currentPath(path.begin(), currentSpace);
        std::wstring::size_type currentPathLength = currentPath.size();
        typedef std::vector<std::wstring>::const_iterator ExtIteratorType;
        for(ExtIteratorType it = pathExts.begin(); it != pathExts.end(); it++) {
            currentPath.replace(currentPathLength, currentPath.size() - currentPathLength, *it);
            if (Exists(currentPath)) return currentPath;
        }
        if (currentSpace != path.end())
            currentSpace++;
    } while (currentSpace != path.end());

    return path;
}

}

【问题讨论】:

  • 那些不是路径,那些是恰好以路径开头的命令行。
  • @bmargulies:我想这是真的——我想我需要上述命令行的路径部分。
  • #3 是个大问题。你如何确定你已经完成了路径(并且不要忘记一半的时间路径是用正斜杠指定的)和斜杠分隔的参数?至少用引号是可行的......
  • @MPelletier:这就是问题的原因。 CreateProcess 似乎对这些路径没问题,尽管我认为它们格式不正确。
  • 我感受到了你的痛苦,但我不知道内核之外的 API 实现了 CreateProcess 实现的规则。

标签: c++ windows path


【解决方案1】:

4 号应该相对容易。如果路径以 " 字符开头,请阅读直到下一个 " ,这就是路径。与其他的相比,它稍微有点棘手,但 Windows 的做法是将命令行简单地分成几部分,一次尝试一个,所以看看 #3,它将它分解成这样的数组:

["C:\Program", "Files", "(x86)\Common", "Files\Steam\SteamService.exe", "/RunAsService"]

然后它只是从最左边的元素开始寻找文件:

  1. C:\程序
  2. C:\Program 文件
  3. C:\Program Files (x86)\Common
  4. C:\Program Files (x86)\Common Files\Stream\StreamService.exe
  5. C:\Program Files (x86)\Common Files\Steam\SteamService.exe /RunAsService

每一步,它都会检查是否存在具有该名称的文件。如果是这样,那就是它选择的那个。它还尝试将“.exe”附加到名称中。所以在第一步中,它会检查是否有一个名为“C:\Program.exe”的文件,如果有,那就是第一个。如果没有,它会移动到第二步并尝试“C:\Program Files.exe”。如果不存在,则移动到下一个,依此类推。

过去,该算法的工作方式存在问题,例如,see here。

【讨论】:

  • 这不可能完全正确——我相信 CreateProcess 正在解析 PATHEXT 环境变量。如果我创建一个运行密钥 (HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\\Runme = "C:\Program") 并将可执行文件作为 C:\program.com,它会在启动时执行。
  • 是的,我相信你是对的。它将尝试 C:\Program.exe、C:\Program.com、C:\Program.bat 等 - 将尝试来自 PATHEXT 的所有扩展。
  • 看来我得自己写了grr...希望我不要搞砸了! :P
【解决方案2】:

请参阅 shlwapi.h 中的 Shell Path Handling Functions。 您的示例应使用::PathRemoveArgs(sPath) 后跟::PathMatchSpec(sPath, _T("*.exe"))。

【讨论】:

  • 对,他也应该调用 ::PathUnquoteSpaces() :-)
  • 您还假设该文件是一个 EXE 文件,但事实并非如此。另外,像C:\Windows\System32\svchost -k something 这样的东西是一个有效的路径,需要返回C:\Windows\System32\svchost.exe 或C:\Windows\System32\svchost.com 或%PATHEXT% 指定的任何内容。
猜你喜欢
  • 1970-01-01
  • 2010-11-08
  • 1970-01-01
  • 2019-10-05
  • 2020-08-17
  • 2010-09-14
  • 2013-12-29
相关资源
最近更新 更多