⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 module.hpp

📁 新版本TR1的stl
💻 HPP
📖 第 1 页 / 共 2 页
字号:
    /// \brief Looks up a symbol by ordinal into a typed function pointer variable.
    ///
    /// \return A pointer to the named symbol, or NULL if not found.
    template <ss_typename_param_k F>
    proc_pointer_type   get_symbol(ws_uint32_t symbolOrdinal, F &f)
    {
        return class_type::get_symbol(m_hmodule, symbolOrdinal, f);
    }
#endif /* STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT */
/// @}

/// \name Accessors
/// @{
public:
    /// \brief Provides access to the underlying module handle
    module_handle_type  get_module_handle() const;

    /// \brief Provides access to the underlying module handle
    module_handle_type  get() const;
/// @}

/// \name Implementation
/// @{
private:
    static module_handle_type   load(ws_char_a_t const* moduleName, void (*pfn)(ws_char_a_t const*, handle_type, void*), void* param);
    static module_handle_type   load(ws_char_w_t const* moduleName, void (*pfn)(ws_char_w_t const*, handle_type, void*), void* param);
    static void                 unload(module_handle_type hmodule, degenerate_feedback_proc_type, void* param) /* stlsoft_throw_0() */;
/// @}

/// \name Member Variables
/// @{
private:
    module_handle_type                  m_hmodule;
    void* const                         m_param;
    const degenerate_feedback_proc_type m_proc;
/// @}

/// \name Not to be implemented
/// @{
private:
    class_type& operator =(class_type const&);
/// @}
};

/* /////////////////////////////////////////////////////////////////////////
 * Access shims
 */

/** \brief Returns the module handle for the given module
 *
 * \ingroup group__concept__shim__module_attribute
 */
inline HINSTANCE get_module_handle(winstl_ns_qual(module) const& m)
{
    return m.get_module_handle();
}

/* /////////////////////////////////////////////////////////////////////////
 * Unit-testing
 */

#ifdef STLSOFT_UNITTEST
# include "./unittest/module_unittest_.h"
#endif /* STLSOFT_UNITTEST */

/* /////////////////////////////////////////////////////////////////////////
 * Implementation
 */

#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION

inline module::module(ws_char_a_t const* moduleName)
    : m_hmodule(load(moduleName))
    , m_param(NULL)
    , m_proc(NULL)
{
#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
    if(NULL == m_hmodule)
    {
        STLSOFT_THROW_X(windows_exception("Cannot load module", ::GetLastError()));
    }
#endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
}

inline module::module(ws_char_a_t const* moduleName, void (*pfn)(ws_char_a_t const*, handle_type, void*), void* param)
    : m_hmodule(load(moduleName, pfn, param))
    , m_param(param)
    , m_proc(reinterpret_cast<degenerate_feedback_proc_type>(pfn))
{
#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
    if(NULL == m_hmodule)
    {
        STLSOFT_THROW_X(windows_exception("Cannot load module", ::GetLastError()));
    }
#endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
}

inline module::module(ws_char_w_t const* moduleName)
    : m_hmodule(load(moduleName))
    , m_param(NULL)
    , m_proc(NULL)
{
#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
    if(NULL == m_hmodule)
    {
        STLSOFT_THROW_X(windows_exception("Cannot load module", ::GetLastError()));
    }
#endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
}

inline module::module(ws_char_w_t const* moduleName, void (*pfn)(ws_char_w_t const*, handle_type, void*), void* param)
    : m_hmodule(load(moduleName, pfn, param))
    , m_param(param)
    , m_proc(reinterpret_cast<degenerate_feedback_proc_type>(pfn))
{
#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
    if(NULL == m_hmodule)
    {
        STLSOFT_THROW_X(windows_exception("Cannot load module", ::GetLastError()));
    }
#endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
}

inline module::module(module::module_handle_type hmodule)
    : m_hmodule(hmodule)
    , m_param(NULL)
    , m_proc(NULL)
{
#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
    if(NULL == m_hmodule)
    {
        STLSOFT_THROW_X(windows_exception("Cannot load module", ::GetLastError()));
    }
#endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
}

inline module::module(module const& rhs)
    : m_param(NULL)
    , m_proc(NULL)
{
    if(NULL == rhs.get_module_handle())
    {
        m_hmodule = NULL;
    }
    else
    {
        basic_file_path_buffer<ws_char_a_t> buffer;
        ws_size_t                           cch =   system_traits<ws_char_a_t>::get_module_filename(rhs.get_module_handle(), &buffer[0], buffer.size());

        if(0 == cch)
        {
#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
            STLSOFT_THROW_X(windows_exception("Cannot get module path", ::GetLastError()));
#else /* STLSOFT_CF_EXCEPTION_SUPPORT */
            m_hmodule = NULL;
#endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
        }
        else
        {
            m_hmodule = load(buffer.data());
        }
    }
}

inline module::~module() stlsoft_throw_0()
{
    unload();
}

inline void module::unload() stlsoft_throw_0()
{
    if(NULL != m_hmodule)
    {
        if(NULL != m_proc)
        {
            class_type::unload(m_hmodule, m_proc, m_param);
        }
        else
        {
            class_type::unload(m_hmodule);
        }
        m_hmodule = NULL;
    }
}

inline module::module_handle_type module::detach()
{
    module_handle_type  h;

    h = m_hmodule;
    m_hmodule = NULL;

    return h;
}

inline /* static */ module::module_handle_type module::load(ws_char_a_t const* moduleName)
{
    return ::LoadLibraryA(moduleName);
}

inline /* static */ module::module_handle_type module::load(ws_char_a_t const* moduleName, void (*pfn)(ws_char_a_t const*, handle_type, void*), void* param)
{
    HINSTANCE   hinst   =   ::LoadLibraryA(moduleName);

    if(NULL != pfn)
    {
        (*pfn)(moduleName, hinst, param);
    }

    return hinst;
}

inline /* static */ module::module_handle_type module::load(ws_char_w_t const* moduleName)
{
    return ::LoadLibraryW(moduleName);
}

inline /* static */ void module::unload(module::module_handle_type hmodule) stlsoft_throw_0()
{
    if(NULL != hmodule)
    {
        ::FreeLibrary(hmodule);
    }
}

inline /* static */ void module::unload(module::module_handle_type hmodule, module::degenerate_feedback_proc_type pfn, void* param) /* stlsoft_throw_0() */
{
    if(NULL != hmodule)
    {
        if(NULL != pfn)
        {
#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
            try
            {
#endif /* STLSOFT_CF_EXCEPTION_SUPPORT */

                untyped_feedback_proc_type  fn  =   reinterpret_cast<untyped_feedback_proc_type>(pfn);

                (*fn)(NULL, hmodule, param);

#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
            }
            catch(...)
            {
                STLSOFT_MESSAGE_ASSERT("Module feedback procedure threw an exception", 0);

                throw; // This will, hopefully, precipitate unexpected()
            }
#endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
        }

        ::FreeLibrary(hmodule);
    }
}


inline /* static */ module::proc_pointer_type module::get_symbol(module::module_handle_type hmodule, ws_char_a_t const* symbolName)
{
    return reinterpret_cast<proc_pointer_type>(GetProcAddress(hmodule, symbolName));
}

inline /* static */ module::proc_pointer_type module::get_symbol(module::module_handle_type hmodule, ws_uint32_t symbolOrdinal)
{
    ws_char_a_t const* s = MAKEINTRESOURCEA(symbolOrdinal);

    return get_symbol(hmodule, s);
}

inline module::proc_pointer_type module::get_symbol(ws_char_a_t const* symbolName)
{
    return get_symbol(m_hmodule, symbolName);
}

inline module::proc_pointer_type module::get_symbol(ws_uint32_t symbolOrdinal)
{
    return get_symbol(m_hmodule, symbolOrdinal);
}

inline module::module_handle_type module::get_module_handle() const
{
    return m_hmodule;
}

inline module::module_handle_type module::get() const
{
    return m_hmodule;
}

#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */

/* ////////////////////////////////////////////////////////////////////// */

#ifndef _WINSTL_NO_NAMESPACE
# if defined(_STLSOFT_NO_NAMESPACE) || \
     defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
} // namespace winstl
# else
} // namespace winstl_project
} // namespace stlsoft
# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_WINSTL_NO_NAMESPACE */

/* ////////////////////////////////////////////////////////////////////// */

#endif /* WINSTL_INCL_WINSTL_DL_HPP_MODULE */

/* ////////////////////////////////////////////////////////////////////// */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -