xref: /third_party/lzma/CPP/Windows/DLL.h (revision 370b324c)
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
9typedef void * HMODULE;
10// typedef int (*FARPROC)();
11// typedef void *FARPROC;
12void *GetProcAddress(HMODULE module, LPCSTR procName);
13#endif
14
15namespace NWindows {
16namespace 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
32class CLibrary
33{
34  HMODULE _module;
35
36  // Z7_CLASS_NO_COPY(CLibrary);
37  // copy constructor is required here
38public:
39  CLibrary(): _module(NULL) {}
40  ~CLibrary() { Free(); }
41
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
52  HMODULE Get_HMODULE() const { return _module; }
53  // operator HMODULE() const { return _module; }
54  // HMODULE* operator&() { return &_module; }
55  bool IsLoaded() const { return (_module != NULL); }
56
57  void Attach(HMODULE m)
58  {
59    Free();
60    _module = m;
61  }
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
78class CLibrary
79{
80  HMODULE _module;
81
82  // Z7_CLASS_NO_COPY(CLibrary);
83public:
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
97bool MyGetModuleFileName(FString &path);
98
99FString GetModuleDirPrefix();
100
101}}
102
103#endif
104