4

我对 C++ 非常陌生。在我目前的项目中,我已经包括

#include <iostream>
#include <Windows.h>
#include <TlHelp32.h>

我只需要在我的 main() 的开头快速检查一下,看看我的程序目录中是否存在所需的 dll。那么对我来说最好的方法是什么?

4

2 回答 2

9

So, assuming it's OK to simply check that the file with the right name EXISTS in the same directory:

#include <fstream>

...

void check_if_dll_exists()
{
    std::ifstream dllfile(".\\myname.dll", std::ios::binary);
    if (!dllfile)
    {
         ... DLL doesn't exist... 
    }
}

If you want to know that it's ACTUALLY a real DLL (rather than someone opening a command prompt and doing type NUL: > myname.dll to create an empty file), you can use:

HMODULE dll = LoadLibrary(".\\myname.dll");

if (!dll)
{
   ... dll doesn't exist or isn't a real dll.... 
}
else
{
   FreeLibrary(dll);
}
于 2013-06-08T23:56:18.190 回答
6

有很多方法可以实现,但使用 boost 库始终是一个好方法。

#include <boost/filesystem.hpp>
using boost::filesystem;

if (!exists("lib.dll")) {
    std::cout << "dll does not exists." << std::endl;
}
于 2013-06-09T01:12:56.253 回答