Chui-Wen Chiu
2007.06.23
將動態載入 DLL 並取得函數指標的動作封裝成專屬類別,透過這個類別可以省去許多瑣碎步驟又可避免忘記釋放 DLL 的問題。
原始碼
LibraryMgr.h |
#include <windows.h> #include <string> #ifndef LIBRARYMGR_H_INCLUDED class LibraryMgr_t ~LibraryMgr_t(void); bool LoadLibrary( const TCHAR* ptcFileName ); FARPROC GetProcAddress( const TCHAR* ptcProcName ); HMODULE GetModuleHandle(); bool GetModuleFileName(std::basic_string<TCHAR>& sName); bool FreeLibraryAndExitThread( DWORD dwExitCode ); void SetAutoFree(bool bFree) { m_bAutoFree = bFree; } private: }; #endif //LIBRARYMGR_H_INCLUDED |
LibraryMgr.cpp |
#include "LibraryMgr.h"
LibraryMgr_t::LibraryMgr_t(void) /** /** /** if ( m_hModule ) m_sFileNAme.assign( ptcFileName ); return m_hModule != NULL; /** ::FreeLibrary( m_hModule ); return true; /** return ::GetProcAddress( m_hModule, ptcProcName ); /** return ::GetModuleHandle( m_sFileNAme.c_str() ); /** sName.assign( m_sFileNAme ); /** ::FreeLibraryAndExitThread( m_hModule, dwExitCode ); m_hModule = 0; return true; |
範例
// 引入 LibraryMrg 類別標頭檔 |
參考資料
[1] Yet another DLL manager class
發表迴響