00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00049 #ifndef WINSTL_INCL_WINSTL_DL_HPP_MODULE
00050 #define WINSTL_INCL_WINSTL_DL_HPP_MODULE
00051
00052 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00053 # define WINSTL_VER_WINSTL_DL_HPP_MODULE_MAJOR 6
00054 # define WINSTL_VER_WINSTL_DL_HPP_MODULE_MINOR 5
00055 # define WINSTL_VER_WINSTL_DL_HPP_MODULE_REVISION 1
00056 # define WINSTL_VER_WINSTL_DL_HPP_MODULE_EDIT 225
00057 #endif
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 #ifndef WINSTL_INCL_WINSTL_H_WINSTL
00071 # include <winstl/winstl.h>
00072 #endif
00073 #ifndef WINSTL_INCL_WINSTL_ERROR_HPP_WINDOWS_EXCEPTIONS
00074 # include <winstl/error/exceptions.hpp>
00075 #endif
00076 #ifndef STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING
00077 # include <stlsoft/shims/access/string.hpp>
00078 #endif
00079 # ifndef WINSTL_INCL_WINSTL_FILESYSTEM_HPP_FILE_PATH_BUFFER
00080 # include <winstl/filesystem/file_path_buffer.hpp>
00081 # endif
00082 #ifndef WINSTL_INCL_WINSTL_SYSTEM_HPP_SYSTEM_TRAITS
00083 # include <winstl/system/system_traits.hpp>
00084 #endif
00085
00086
00087
00088
00089
00090 #ifndef _WINSTL_NO_NAMESPACE
00091 # if defined(_STLSOFT_NO_NAMESPACE) || \
00092 defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
00093
00094 namespace winstl
00095 {
00096 # else
00097
00098
00099 namespace stlsoft
00100 {
00101
00102 namespace winstl_project
00103 {
00104
00105 # endif
00106 #endif
00107
00108
00109
00110
00111
00116 class module
00117 {
00118 public:
00120 typedef HINSTANCE module_handle_type;
00125 typedef HINSTANCE handle_type;
00127 typedef module class_type;
00129 typedef void (*proc_pointer_type)();
00130 private:
00131 typedef void (*degenerate_feedback_proc_type)();
00132 typedef void (*untyped_feedback_proc_type)(void const*, handle_type, void*);
00133 public:
00134 typedef handle_type resource_type;
00135
00138 public:
00146 ss_explicit_k module(ws_char_a_t const* moduleName);
00147
00148 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00149 ss_explicit_k module(ws_char_a_t const* moduleName, void (*pfn)(ws_char_a_t const*, handle_type, void*), void* param);
00150 #endif
00151
00159 ss_explicit_k module(ws_char_w_t const* moduleName);
00160
00161 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00162 ss_explicit_k module(ws_char_w_t const* moduleName, void (*pfn)(ws_char_w_t const*, handle_type, void*), void* param);
00163 #endif
00164 #if defined(STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT) && \
00165 ( !defined(STLSOFT_COMPILER_IS_MSVC) || \
00166 _MSC_VER >= 1200)
00177 template <ss_typename_param_k S>
00178 ss_explicit_k module(S const& moduleName)
00179 : m_hmodule(load(moduleName))
00180 , m_param(NULL)
00181 , m_proc(NULL)
00182 {
00183 # ifdef STLSOFT_CF_EXCEPTION_SUPPORT
00184 if(NULL == m_hmodule)
00185 {
00186 STLSOFT_THROW_X(windows_exception("Cannot load module", ::GetLastError()));
00187 }
00188 # endif
00189 }
00190 #endif
00196 ss_explicit_k module(module_handle_type hmodule);
00200 module(class_type const& rhs);
00202 ~module() stlsoft_throw_0();
00204
00207 public:
00212 static module_handle_type load(ws_char_a_t const* moduleName);
00217 static module_handle_type load(ws_char_w_t const* moduleName);
00218 #if defined(STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT) && \
00219 ( !defined(STLSOFT_COMPILER_IS_MSVC) || \
00220 _MSC_VER >= 1200)
00230 template <ss_typename_param_k S>
00231 static module_handle_type load(S const& moduleName)
00232 {
00233 return class_type::load(stlsoft_ns_qual(c_str_ptr)(moduleName));
00234 }
00235 #endif
00237 static void unload(module_handle_type hmodule) stlsoft_throw_0();
00241 static proc_pointer_type get_symbol(module_handle_type hmodule, ws_char_a_t const* symbolName);
00245 static proc_pointer_type get_symbol(module_handle_type hmodule, ws_uint32_t symbolOrdinal);
00246
00247 #if defined(STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT) && \
00248 ( !defined(STLSOFT_COMPILER_IS_MSVC) || \
00249 _MSC_VER >= 1200)
00253 template <ss_typename_param_k F>
00254 static proc_pointer_type get_symbol(module_handle_type hmodule, ws_char_a_t const* symbolName, F &f)
00255 {
00256 proc_pointer_type proc = class_type::get_symbol(hmodule, symbolName);
00257
00258 f = reinterpret_cast<F>(proc);
00259
00260 return proc;
00261 }
00265 template <ss_typename_param_k F>
00266 static proc_pointer_type get_symbol(module_handle_type hmodule, ws_uint32_t symbolOrdinal, F &f)
00267 {
00268 proc_pointer_type proc = class_type::get_symbol(hmodule, symbolOrdinal);
00269
00270 f = reinterpret_cast<F>(proc);
00271
00272 return proc;
00273 }
00274 #endif
00276
00279 public:
00281 void unload() stlsoft_throw_0();
00282
00284 module_handle_type detach();
00286
00289 public:
00293 proc_pointer_type get_symbol(ws_char_a_t const* symbolName);
00297 proc_pointer_type get_symbol(ws_uint32_t symbolOrdinal);
00298
00299 #if defined(STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT) && \
00300 ( !defined(STLSOFT_COMPILER_IS_MSVC) || \
00301 _MSC_VER >= 1200)
00305 template <ss_typename_param_k F>
00306 proc_pointer_type get_symbol(ws_char_a_t const* symbolName, F &f)
00307 {
00308 return class_type::get_symbol(m_hmodule, symbolName, f);
00309 }
00313 template <ss_typename_param_k F>
00314 proc_pointer_type get_symbol(ws_uint32_t symbolOrdinal, F &f)
00315 {
00316 return class_type::get_symbol(m_hmodule, symbolOrdinal, f);
00317 }
00318 #endif
00320
00323 public:
00325 module_handle_type get_module_handle() const;
00326
00328 module_handle_type get() const;
00330
00333 private:
00334 static module_handle_type load(ws_char_a_t const* moduleName, void (*pfn)(ws_char_a_t const*, handle_type, void*), void* param);
00335 static module_handle_type load(ws_char_w_t const* moduleName, void (*pfn)(ws_char_w_t const*, handle_type, void*), void* param);
00336 static void unload(module_handle_type hmodule, degenerate_feedback_proc_type, void* param) ;
00338
00341 private:
00342 module_handle_type m_hmodule;
00343 void* const m_param;
00344 const degenerate_feedback_proc_type m_proc;
00346
00349 private:
00350 class_type& operator =(class_type const&);
00352 };
00353
00354
00355
00356
00357
00362 inline HINSTANCE get_module_handle(winstl_ns_qual(module) const& m)
00363 {
00364 return m.get_module_handle();
00365 }
00366
00367
00368
00369
00370
00371 #ifdef STLSOFT_UNITTEST
00372 # include "./unittest/module_unittest_.h"
00373 #endif
00374
00375
00376
00377
00378
00379 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00380
00381 inline module::module(ws_char_a_t const* moduleName)
00382 : m_hmodule(load(moduleName))
00383 , m_param(NULL)
00384 , m_proc(NULL)
00385 {
00386 #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
00387 if(NULL == m_hmodule)
00388 {
00389 STLSOFT_THROW_X(windows_exception("Cannot load module", ::GetLastError()));
00390 }
00391 #endif
00392 }
00393
00394 inline module::module(ws_char_a_t const* moduleName, void (*pfn)(ws_char_a_t const*, handle_type, void*), void* param)
00395 : m_hmodule(load(moduleName, pfn, param))
00396 , m_param(param)
00397 , m_proc(reinterpret_cast<degenerate_feedback_proc_type>(pfn))
00398 {
00399 #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
00400 if(NULL == m_hmodule)
00401 {
00402 STLSOFT_THROW_X(windows_exception("Cannot load module", ::GetLastError()));
00403 }
00404 #endif
00405 }
00406
00407 inline module::module(ws_char_w_t const* moduleName)
00408 : m_hmodule(load(moduleName))
00409 , m_param(NULL)
00410 , m_proc(NULL)
00411 {
00412 #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
00413 if(NULL == m_hmodule)
00414 {
00415 STLSOFT_THROW_X(windows_exception("Cannot load module", ::GetLastError()));
00416 }
00417 #endif
00418 }
00419
00420 inline module::module(ws_char_w_t const* moduleName, void (*pfn)(ws_char_w_t const*, handle_type, void*), void* param)
00421 : m_hmodule(load(moduleName, pfn, param))
00422 , m_param(param)
00423 , m_proc(reinterpret_cast<degenerate_feedback_proc_type>(pfn))
00424 {
00425 #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
00426 if(NULL == m_hmodule)
00427 {
00428 STLSOFT_THROW_X(windows_exception("Cannot load module", ::GetLastError()));
00429 }
00430 #endif
00431 }
00432
00433 inline module::module(module::module_handle_type hmodule)
00434 : m_hmodule(hmodule)
00435 , m_param(NULL)
00436 , m_proc(NULL)
00437 {
00438 #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
00439 if(NULL == m_hmodule)
00440 {
00441 STLSOFT_THROW_X(windows_exception("Cannot load module", ::GetLastError()));
00442 }
00443 #endif
00444 }
00445
00446 inline module::module(module const& rhs)
00447 : m_param(NULL)
00448 , m_proc(NULL)
00449 {
00450 if(NULL == rhs.get_module_handle())
00451 {
00452 m_hmodule = NULL;
00453 }
00454 else
00455 {
00456 basic_file_path_buffer<ws_char_a_t> buffer;
00457 ws_size_t cch = system_traits<ws_char_a_t>::get_module_filename(rhs.get_module_handle(), &buffer[0], buffer.size());
00458
00459 if(0 == cch)
00460 {
00461 #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
00462 STLSOFT_THROW_X(windows_exception("Cannot get module path", ::GetLastError()));
00463 #else
00464 m_hmodule = NULL;
00465 #endif
00466 }
00467 else
00468 {
00469 m_hmodule = load(buffer.data());
00470 }
00471 }
00472 }
00473
00474 inline module::~module() stlsoft_throw_0()
00475 {
00476 unload();
00477 }
00478
00479 inline void module::unload() stlsoft_throw_0()
00480 {
00481 if(NULL != m_hmodule)
00482 {
00483 if(NULL != m_proc)
00484 {
00485 class_type::unload(m_hmodule, m_proc, m_param);
00486 }
00487 else
00488 {
00489 class_type::unload(m_hmodule);
00490 }
00491 m_hmodule = NULL;
00492 }
00493 }
00494
00495 inline module::module_handle_type module::detach()
00496 {
00497 module_handle_type h;
00498
00499 h = m_hmodule;
00500 m_hmodule = NULL;
00501
00502 return h;
00503 }
00504
00505 inline module::module_handle_type module::load(ws_char_a_t const* moduleName)
00506 {
00507 return ::LoadLibraryA(moduleName);
00508 }
00509
00510 inline module::module_handle_type module::load(ws_char_a_t const* moduleName, void (*pfn)(ws_char_a_t const*, handle_type, void*), void* param)
00511 {
00512 HINSTANCE hinst = ::LoadLibraryA(moduleName);
00513
00514 if(NULL != pfn)
00515 {
00516 (*pfn)(moduleName, hinst, param);
00517 }
00518
00519 return hinst;
00520 }
00521
00522 inline module::module_handle_type module::load(ws_char_w_t const* moduleName)
00523 {
00524 return ::LoadLibraryW(moduleName);
00525 }
00526
00527 inline void module::unload(module::module_handle_type hmodule) stlsoft_throw_0()
00528 {
00529 if(NULL != hmodule)
00530 {
00531 ::FreeLibrary(hmodule);
00532 }
00533 }
00534
00535 inline void module::unload(module::module_handle_type hmodule, module::degenerate_feedback_proc_type pfn, void* param)
00536 {
00537 if(NULL != hmodule)
00538 {
00539 if(NULL != pfn)
00540 {
00541 #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
00542 try
00543 {
00544 #endif
00545
00546 untyped_feedback_proc_type fn = reinterpret_cast<untyped_feedback_proc_type>(pfn);
00547
00548 (*fn)(NULL, hmodule, param);
00549
00550 #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
00551 }
00552 catch(...)
00553 {
00554 STLSOFT_MESSAGE_ASSERT("Module feedback procedure threw an exception", 0);
00555
00556 throw;
00557 }
00558 #endif
00559 }
00560
00561 ::FreeLibrary(hmodule);
00562 }
00563 }
00564
00565
00566 inline module::proc_pointer_type module::get_symbol(module::module_handle_type hmodule, ws_char_a_t const* symbolName)
00567 {
00568 return reinterpret_cast<proc_pointer_type>(GetProcAddress(hmodule, symbolName));
00569 }
00570
00571 inline module::proc_pointer_type module::get_symbol(module::module_handle_type hmodule, ws_uint32_t symbolOrdinal)
00572 {
00573 ws_char_a_t const* s = MAKEINTRESOURCEA(symbolOrdinal);
00574
00575 return get_symbol(hmodule, s);
00576 }
00577
00578 inline module::proc_pointer_type module::get_symbol(ws_char_a_t const* symbolName)
00579 {
00580 return get_symbol(m_hmodule, symbolName);
00581 }
00582
00583 inline module::proc_pointer_type module::get_symbol(ws_uint32_t symbolOrdinal)
00584 {
00585 return get_symbol(m_hmodule, symbolOrdinal);
00586 }
00587
00588 inline module::module_handle_type module::get_module_handle() const
00589 {
00590 return m_hmodule;
00591 }
00592
00593 inline module::module_handle_type module::get() const
00594 {
00595 return m_hmodule;
00596 }
00597
00598 #endif
00599
00600
00601
00602 #ifndef _WINSTL_NO_NAMESPACE
00603 # if defined(_STLSOFT_NO_NAMESPACE) || \
00604 defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
00605 }
00606 # else
00607 }
00608 }
00609 # endif
00610 #endif
00611
00612
00613
00614 #endif
00615
00616