如何在系统路径中搜索文件

How to search in the system path for a file?

本文关键字:搜索 文件 路径 系统      更新时间:2023-10-16

我有x.dll在某个文件夹,这是系统路径的一部分。我还有另外一个文件x.zzz在同一个文件夹里,这不是一个可执行文件。

从一个c++程序中,我想搜索x.zzz而不加载x.dll。但我希望这工作完全像LoadLibrary函数。也就是说,它应该按照与LoadLibrary相同的顺序进行搜索。

这可能吗?

PS:我检查了SearchPath()函数,但是在文档中有一个注释说这不应该用于此目的。

不建议使用SearchPath函数作为定位.dll文件的方法输出的预期用途是调用LoadLibrary函数。这可能导致查找错误的.dll文件,因为SearchPath函数的搜索顺序与LoadLibrary函数使用的搜索顺序不同。如果你需要定位加载一个。dll文件,使用LoadLibrary函数。

使用任何内置函数的问题是,它们将专门寻找可执行文件或dll。我认为最好的方法是解析path变量并手动遍历目录。这可以用C函数来实现目录迭代。下面的代码应该可以在大多数平台上运行。

#include <dirent.h>
#include <cstdlib>
#include <iostream>
#include <string>
...
std::string findInPath(const std::string &key, char delim = ';');
std::string findInDir(const std::string &key, const std::string &dir);
...
std::string findInDir(const std::string &key, const std::string &directory)
{
  DIR *dir = opendir(directory.c_str());
  if(!dir)
    return "";
  dirent *dirEntry;
  while(dirEntry = readdir(dir))
  {
    if(key == dirEntry->d_name) // Found!
      return directory+'/'+key;
  }
  return "";
}
std::string findInPath(const std::string &key, char delim)
{
  std::string path(std::getenv("PATH"));
  size_t posPrev = -1;
  size_t posCur;
  while((posCur = path.find(delim, posPrev+1)) != std::string::npos)
  {
    // Locate the next directory in the path
    std::string pathCurrent = path.substr(posPrev+1, posCur-posPrev-1);
    // Search the current directory
    std::string found = findInDir(key, pathCurrent);
    if(!found.empty())
      return found;
    posPrev = posCur;
  }
  // Locate the last directory in the path
  std::string pathCurrent = path.substr(posPrev+1, path.size()-posPrev-1);
  // Search the current directory
  std::string found = findInDir(key, pathCurrent);
  if(!found.empty())
    return found;
  return "";
}

如何使用LoadLibraryEx()与标志LOAD_LIBRARY_AS_IMAGE_RESOURCE?

来自LoadLibraryEx文档:

如果使用此值,系统将该文件作为镜像文件映射到进程的虚拟地址空间。但是,加载器不加载静态导入或执行其他通常的初始化步骤。当您想加载DLL只是为了从中提取消息或资源时,请使用此标志。

我知道你说的是"不加载"…但是使用这种技术可以防止.dll的函数和变量污染您的命名空间等。如果你有性能需求,或者其他特定的原因来指定"不加载",请对此进行扩展。