如何在系统路径中搜索文件
How to search in the system path for a file?
我有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的函数和变量污染您的命名空间等。如果你有性能需求,或者其他特定的原因来指定"不加载",请对此进行扩展。
相关文章:
- 按单词搜索文件,然后在C++中打印特定行数
- 在C++目录中搜索文件的函数输出中出错
- 按扩展名过滤搜索文件会返回太多结果
- 如何向 g++ 编译器解释如何读取用于从'list'文件中搜索 .h 文件的'include'目录列表
- C++,使用自己的条件搜索文件中的字符串
- C/C++ 从根位置搜索文件夹并返回绝对路径
- 在目录中搜索文件夹
- 搜索文件的特定位置
- 按文件名搜索文件
- 使用扩展功能在整个系统中搜索文件
- 在目录中搜索文件
- 在C++中搜索文件夹
- 生成文件应该在另一个目录中搜索 .o 文件
- 有没有更好的方法来搜索文件中的字符串
- 在 c++ 中搜索文件中的字符串以查找非常大的输入的有效方法
- 如何在子目录中搜索文件
- 如何指定在整个系统中搜索文件的目录
- 如何在visualstudio中快速搜索文件内容以查找大型代码库
- C++显示include搜索文件的位置
- 在计算机中递归搜索文件