DLL 동적 로딩
특정 폴더 내에 다수의 DLL 라이브러리 파일들을 로딩하기 위한 코드다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#include <iostream>
class DLLLoad { public: DLLLoad() {} ~DLLLoad() {}
bool LoadLibrary(); bool FreeLibrary();
private: const std::string DLL_DIR;
std::list<std::string> fileList; }
|
C++ 파일
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
#include "dllload.h" #include <iostream> #include <windows.h>
const std::string DLLLoad::DLL_DIR = "C:\\dll\\";
DLLLoad::DLLLoad() {} DLLLoad::~DLLLoad() {}
bool DLLLoad::LoadLibrary() { if (fileList.size() == 0) { std::string input = DLL_DIR + "*.dll";
WIN32_FIND_DATA FindData; HANDLE hFind = FindFirstFile(input.c_str(), &FindData); if (hFind == INVALID_HANDLE_VALUE) { std::cout << "Error - Can't find a file : " << FindData.cFileName << std::endl; return false; }
do { if (FindData.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) { std::string dir = CSM_DLL_DIR + FindData.cFileName; HINSTANCE hmodule = LoadLibrary(dir.c_str()); if (hmodule != NULL) { fileList.push_back(dir); }
} } while (FindNextFile(hFind, &FindData) != 0);
FindClose(hFind); }
return true; }
bool DLLLoad::FreeLibrary() { for (std::string file : fileList) { HINSTANCE hmodule = GetModuleHandle(file.c_str()); if (hmodule != NULL) { FreeLibrary(hmodule); } } return true; }
|
Main 함수
1 2 3 4 5 6
| int main() { DLLLoad dllLoad = new DLLLoad(); dllLoad->LoadLibrary(); dllLoad->FreeLibrary(); }
|