我是 C++ 的新手。 在我当前的项目中,我已经包含了
#include <iostream>
#include <Windows.h>
#include <TlHelp32.h>
我只需要在 main() 的最开始快速检查一下,看看我的程序目录中是否存在所需的 dll。 那么对我来说最好的方法是什么?
最佳答案
因此,假设可以简单地检查同一目录中是否存在具有正确名称的文件:
#include <fstream>
...
void check_if_dll_exists()
{
std::ifstream dllfile(".\\myname.dll", std::ios::binary);
if (!dllfile)
{
... DLL doesn't exist...
}
}
如果您想知道它实际上是一个真正的 DLL(而不是有人打开命令提示符并执行 type NUL: > myname.dll
来创建一个空文件),您可以使用:
HMODULE dll = LoadLibrary(".\\myname.dll");
if (!dll)
{
... dll doesn't exist or isn't a real dll....
}
else
{
FreeLibrary(dll);
}
关于c++ - C++ 检查文件是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17005006/