1 // Windows/DLL.h 2 3 #ifndef ZIP7_INC_WINDOWS_DLL_H 4 #define ZIP7_INC_WINDOWS_DLL_H 5 6 #include "../Common/MyString.h" 7 8 #ifndef _WIN32 9 typedef void * HMODULE; 10 // typedef int (*FARPROC)(); 11 // typedef void *FARPROC; 12 void *GetProcAddress(HMODULE module, LPCSTR procName); 13 #endif 14 15 namespace NWindows { 16 namespace NDLL { 17 18 #ifdef _WIN32 19 20 /* 21 #ifdef UNDER_CE 22 #define My_GetProcAddress(module, procName) (void *)::GetProcAddressA(module, procName) 23 #else 24 #define My_GetProcAddress(module, procName) (void *)::GetProcAddress(module, procName) 25 #endif 26 */ 27 28 /* Win32: Don't call CLibrary::Free() and FreeLibrary() from another 29 FreeLibrary() code: detaching code in DLL entry-point or in 30 destructors of global objects in DLL module. */ 31 32 class CLibrary 33 { 34 HMODULE _module; 35 36 // Z7_CLASS_NO_COPY(CLibrary); 37 // copy constructor is required here 38 public: CLibrary()39 CLibrary(): _module(NULL) {} ~CLibrary()40 ~CLibrary() { Free(); } 41 CLibrary(const CLibrary & c)42 CLibrary(const CLibrary &c): _module(NULL) 43 { 44 if (c._module) 45 { 46 // we need non const to reference from original item 47 // c._module = NULL; 48 throw 20230102; 49 } 50 } 51 Get_HMODULE()52 HMODULE Get_HMODULE() const { return _module; } 53 // operator HMODULE() const { return _module; } 54 // HMODULE* operator&() { return &_module; } IsLoaded()55 bool IsLoaded() const { return (_module != NULL); } 56 Attach(HMODULE m)57 void Attach(HMODULE m) 58 { 59 Free(); 60 _module = m; 61 } Detach()62 HMODULE Detach() 63 { 64 const HMODULE m = _module; 65 _module = NULL; 66 return m; 67 } 68 69 bool Free() throw(); 70 bool LoadEx(CFSTR path, DWORD flags = LOAD_LIBRARY_AS_DATAFILE) throw(); 71 bool Load(CFSTR path) throw(); 72 // FARPROC 73 // void *GetProc(LPCSTR procName) const { return My_GetProcAddress(_module, procName); } 74 }; 75 76 #else 77 78 class CLibrary 79 { 80 HMODULE _module; 81 82 // Z7_CLASS_NO_COPY(CLibrary); 83 public: 84 CLibrary(): _module(NULL) {} 85 ~CLibrary() { Free(); } 86 87 HMODULE Get_HMODULE() const { return _module; } 88 89 bool Free() throw(); 90 bool Load(CFSTR path) throw(); 91 // FARPROC 92 // void *GetProc(LPCSTR procName) const; // { return My_GetProcAddress(_module, procName); } 93 }; 94 95 #endif 96 97 bool MyGetModuleFileName(FString &path); 98 99 FString GetModuleDirPrefix(); 100 101 }} 102 103 #endif 104