📄 ncbidll.hpp
字号:
/* * =========================================================================== * PRODUCTION $Log: ncbidll.hpp,v $ * PRODUCTION Revision 1000.3 2003/12/02 20:27:57 gouriano * PRODUCTION PRODUCTION: UPGRADED [ORIGINAL] Dev-tree R1.21 * PRODUCTION * =========================================================================== */#ifndef CORELIB___NCBIDLL__HPP#define CORELIB___NCBIDLL__HPP/* $Id: ncbidll.hpp,v 1000.3 2003/12/02 20:27:57 gouriano Exp $ * =========================================================================== * * PUBLIC DOMAIN NOTICE * National Center for Biotechnology Information * * This software/database is a "United States Government Work" under the * terms of the United States Copyright Act. It was written as part of * the author's official duties as a United States Government employee and * thus cannot be copyrighted. This software/database is freely available * to the public for use. The National Library of Medicine and the U.S. * Government have not placed any restriction on its use or reproduction. * * Although all reasonable efforts have been taken to ensure the accuracy * and reliability of the software and data, the NLM and the U.S. * Government do not and cannot warrant the performance or results that * may be obtained by using this software or data. The NLM and the U.S. * Government disclaim all warranties, express or implied, including * warranties of performance, merchantability or fitness for any particular * purpose. * * Please cite the author in any work or product based on this material. * * =========================================================================== * * Author: Denis Vakatov, Vladimir Ivanov, Anatoliy Kuznetsov * * *//// @file ncbidll.hpp/// Define class Dll and for Portable DLL handling.#include <corelib/ncbistd.hpp>#include <corelib/ncbifile.hpp>BEGIN_NCBI_SCOPE/** @addtogroup Dll * * @{ */// Forward declaration of struct containing OS-specific DLL handle.struct SDllHandle;/////////////////////////////////////////////////////////////////////////////////// CDll --////// Define class for portable Dll handling.////// The DLL name is considered the basename if it does not contain embedded/// '/', '\', or ':' symbols. Also, in this case, if the DLL name does not/// match pattern "lib*.so", "lib*.so.*", or "*.dll" (and if eExactName flag/// not passed to the constructor), then it will be automagically transformed/// according to the following rules:/// - UNIX: <name> ---> lib<name>.so/// - MS Windows: <name> ---> <name>.dll////// If the DLL is specified by its basename, then it will be searched/// (after the transformation described above) in the following locations:////// UNIX:/// 1) the directories that are listed in the LD_LIBRARY_PATH environment/// variable (analyzed once at the process startup);/// 2) the directory from which the application loaded;/// 3) hard-coded (e.g. with `ldconfig' on Linux) paths.////// MS Windows:/// 1) the directory from which the application is loaded;/// 2) the current directory; /// 3) the Windows system directory;/// 4) the Windows directory;/// 5) the directories that are listed in the PATH environment variable.////// NOTE: All methods of this class except the destructor throw exception/// CCoreException::eDll on error.class CDll{public: /// When to load DLL. enum ELoad { eLoadNow, ///< Load DLL immediately in the constructor eLoadLater ///< Load DLL later, using method Load() }; /// Whether to unload DLL in the destructor. enum EAutoUnload { eAutoUnload, ///< Do unload DLL in the destructor eNoAutoUnload ///< Do not unload DLL in the destructor }; /// Whether to transform the DLL basename. /// /// Transformation is done according to the following: /// /// UNIX: <name> ---> lib<name>.so /// MS Windows: <name> ---> <name>.dll enum EBasename { eBasename, ///< Treat as basename (if it looks like one) eExactName ///< Use the name "as is" (no prefix/suffix adding) }; /// Constructor. /// /// @param name /// Can be either DLL basename or an absolute file path. /// @param when_to_load /// Choice to load now or later using Load(). /// @param auto_unload /// Choice to unload DLL in destructor. /// @param treat_as /// Choice to transform the DLL base name. /// @sa /// Basename discussion in CDll header, /// Eload, EAutoUnload, EBasename definition. NCBI_XNCBI_EXPORT CDll(const string& name, ELoad when_to_load = eLoadNow, EAutoUnload auto_unload = eNoAutoUnload, EBasename treate_as = eBasename); /// Constructor. /// /// The absolute file path to the DLL will be formed using the "path" /// and "name" parameters in the following way: /// - UNIX: <path>/lib<name>.so ; <path>/<name> if "name" is not basename /// - MS-Win: <path>\<name>.dll ; <path>\<name> if "name" is not basename /// /// @param path /// Path to DLL. /// @param name /// Name of DLL. /// @param when_to_load /// Choice to load now or later using Load(). /// @param auto_load /// Choice to unload DLL in destructor. /// @param treat_as /// Choice to transform the DLL base name. /// @sa /// Basename discussion in CDll header, /// Eload, EAutoUnload, EBasename definition. NCBI_XNCBI_EXPORT CDll(const string& path, const string& name, ELoad when_to_load = eLoadNow, EAutoUnload auto_unload = eNoAutoUnload, EBasename treate_as = eBasename); /// Destructor. /// /// Unload DLL if constructor was passed "eAutoUnload". /// Destructor does not throw any exceptions. NCBI_XNCBI_EXPORT ~CDll(void); /// Load DLL. /// /// Load the DLL using the name specified in the constructor's DLL "name". /// If Load() is called more than once without calling Unload() in between, /// then it will do nothing. NCBI_XNCBI_EXPORT void Load(void); /// Unload DLL. /// /// Do nothing and do not generate errors if the DLL is not loaded. NCBI_XNCBI_EXPORT void Unload(void); /// Get DLLs entry point (function). /// /// Get the entry point (a function) with name "name" in the DLL and /// return the entry point's address on success, or return NULL on error. /// If the DLL is not loaded yet, then this method will call Load(), /// which can result in throwing an exception if Load() fails. /// @sa /// GetEntryPoint_Data() template <class TFunc> TFunc GetEntryPoint_Func(const string& name, TFunc* func) { TEntryPoint ptr = GetEntryPoint(name); if ( func ) { *func = (TFunc)ptr.func; } return (TFunc)ptr.func; } /// Get DLLs entry point (data). /// /// Get the entry point (a data) with name "name" in the DLL and /// return the entry point's address on success, or return NULL on error. /// If the DLL is not loaded yet, then this method will call Load(), /// which can result in throwing an exception if Load() fails. /// @sa /// GetEntryPoint_Func() template <class TData> TData GetEntryPoint_Data(const string& name, TData* data) { TEntryPoint ptr = GetEntryPoint(name); if ( data ) { *data = static_cast<TData> (ptr.data); } return static_cast<TData> (ptr.data); } /// Fake, uncallable function pointer typedef void (*FEntryPoint)(char**** Do_Not_Call_This); /// Entry point -- pointer to either a function or a data union TEntryPoint { FEntryPoint func; ///< Do not call this func without type cast! void* data; }; /// Helper find method for getting a DLLs entry point. /// /// Get the entry point (e.g. a function) with name "name" in the DLL. /// @param name /// Name of DLL. /// @param pointer_size /// Size of pointer. /// @return /// The entry point's address on success, or return NULL on error. /// @sa /// GetEntryPoint_Func(), GetEntryPoint_Data() NCBI_XNCBI_EXPORT TEntryPoint GetEntryPoint(const string& name); /// Get the name of the DLL file NCBI_XNCBI_EXPORT const string& GetName() const { return m_Name; }private: /// Helper method to throw exception with system-specific error message. void x_ThrowException(const string& what); /// Helper method to initialize object.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -