Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cOSModule.h
Go to the documentation of this file.
1 //
6 //
7 
8 #ifndef _INC_cOSModule_H
9 #define _INC_cOSModule_H
10 #ifndef NO_PRAGMA_ONCE
11 #pragma once
12 #endif
13 
14 #include "cOSHandle.h"
15 #include "cFilePath.h"
16 #include "cMime.h"
17 
18 #ifdef _WIN32
19 #define MODULE_EXT MIME_EXT_dll
20 
21 #elif defined(__linux__)
22 #include <dlfcn.h>
23 #define MODULE_EXT MIME_EXT_so
24 typedef int (GRAYCALL * FARPROC)();
25 #else
26 #error NOOS
27 #endif
28 
29 namespace Gray
30 {
31 #define HMODULE_NULL ((HMODULE)nullptr) // This sometimes means the current process module.
32 
34  {
44 
45  private:
46  HMODULE m_hModule;
47  UINT32 m_uFlags;
48 #if defined(__linux__)
49  cStringF m_sModuleName;
50 #endif
51 
52  protected:
53  void FreeModuleLast();
54 
55  public:
56 #ifdef _WIN32
57 
58 #ifndef LOAD_LIBRARY_AS_IMAGE_RESOURCE
59 #define LOAD_LIBRARY_AS_IMAGE_RESOURCE 0x00000020 // __GNUC__
60 #endif
61  static const UINT k_Load_Normal = 0;
62  static const UINT k_Load_Preload = DONT_RESOLVE_DLL_REFERENCES;
63  static const UINT k_Load_Resource = LOAD_LIBRARY_AS_IMAGE_RESOURCE;
64 #elif defined(__linux__)
65  static const UINT k_Load_Normal = RTLD_NOW;
66  static const UINT k_Load_Preload = RTLD_LAZY;
67  static const UINT k_Load_Resource = RTLD_LAZY;
68 #endif
69  static const UINT k_Load_OSMask = 0x0FFFFFFF;
70  static const UINT k_Load_ByName = 0x40000000;
71  static const UINT k_Load_NoRefCount = 0x80000000;
72 
73  cOSModule(HMODULE hModule = HMODULE_NULL, UINT32 uFlags = k_Load_Normal);
74  cOSModule(const FILECHAR_t* pszModuleName, UINT32 uFlags);
75  ~cOSModule();
76 
77  static MIME_TYPE GRAYCALL CheckModuleTypeFile(const FILECHAR_t* pszPathName);
78 
79 #ifndef UNDER_CE
80  static HMODULE GRAYCALL GetModuleHandleForAddr(const void* pAddr);
81 #endif
82  FARPROC GetSymbolAddress(const char* pszSymbolName) const;
83 
84  bool isValidModule() const noexcept
85  {
86  return(m_hModule != HMODULE_NULL);
87  }
88  operator HMODULE() const noexcept
89  {
90  return m_hModule;
91  }
92  HMODULE get_ModuleHandle() const noexcept
93  {
94  return m_hModule;
95  }
96  UINT_PTR get_ModuleInt() const noexcept
97  {
99  return (UINT_PTR)m_hModule;
100  }
101  bool isResourceModule() const noexcept
102  {
103  // We cant call this. its not loaded as code.
104  return ( m_uFlags & (k_Load_Preload | k_Load_Resource));
105  }
106 
107  StrLen_t GetModulePath(FILECHAR_t* pszModuleName, StrLen_t nSizeMax) const;
108  cStringF get_Name() const;
109 
110  HRESULT GetLastErrorDef(HRESULT hResDef = E_FAIL) const
111  {
112 #ifdef _WIN32
113  return HResult::GetLastDef(hResDef);
114 #elif defined(__linux__)
115  // Just a string?
116  const char* pszError = ::dlerror();
117  UNREFERENCED_PARAMETER(pszError);
118  return E_NOTIMPL;
119 #endif
120  }
121 
122  void AttachModule(HMODULE hModule = HMODULE_NULL, UINT32 uFlags = k_Load_Normal)
123  {
124  FreeModuleLast();
125  m_hModule = hModule;
126  m_uFlags = uFlags;
127  }
128  void ClearModule() noexcept
129  {
130  m_hModule = HMODULE_NULL;
131  m_uFlags = k_Load_Normal;
132 #ifdef __linux__
133  m_sModuleName.Empty();
134 #endif
135  }
136  HMODULE DetachModule() noexcept
137  {
138  HMODULE hModule = m_hModule;
139  ClearModule();
140  return hModule;
141  }
142  void FreeThisModule();
143 
144  bool AttachModuleName(const FILECHAR_t* pszModuleName, UINT32 uFlags = k_Load_NoRefCount);
145  HRESULT LoadModule(const FILECHAR_t* pszModuleName, UINT32 uFlags = k_Load_Normal);
146  HRESULT LoadModuleWithSymbol(const FILECHAR_t* pszModuleName, const char* pszSymbolName);
147 
148 #ifdef _WIN32
149  // Load or enum resources ?? cWinResource, cAppRes
150 #endif
152  };
153 
154  template<class TYPE = FARPROC>
156  {
160  public:
162  public:
163  cOSModuleFunc(TYPE pFunc = nullptr)
164  : m_pFunc(pFunc)
165  {
166  }
168  {
169  }
171  {
172  m_pFunc = nullptr;
173  }
174  bool put_FuncAddress(TYPE pFunc)
175  {
176  m_pFunc = pFunc;
177  if (m_pFunc == nullptr)
178  return false;
179  return true;
180  }
181  bool put_FuncGeneric(FARPROC pFunc)
182  {
183  m_pFunc = (TYPE)pFunc;
184  if (m_pFunc == nullptr)
185  return false;
186  return true;
187  }
188  bool isValidFunc() const
189  {
190  return(m_pFunc != nullptr);
191  }
192  operator TYPE () const
193  {
194  return m_pFunc;
195  }
196  };
197 
199 
200 #ifdef GRAY_DLL // force implementation/instantiate for DLL/SO.
202 #endif
203 }
204 #endif // _INC_cOSModule_H
#define GRAYCORE_LINK
Definition: GrayCore.h:47
#define GRAYCALL
declare calling convention for static functions so everyone knows the arg passing scheme....
Definition: GrayCore.h:36
#define TYPE
Definition: StrT.cpp:38
#define UNREFERENCED_PARAMETER(P)
< _WIN32 type thing. get rid of stupid warning.
Definition: SysTypes.h:299
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#define HMODULE_NULL
Definition: cOSModule.h:31
#define UNITTEST_FRIEND(n)
Define this in the class body to be unit tested. Allow the unit test to access private/protected stuf...
Definition: cUnitTestDecl.h:17
void Empty()
Definition: cString.h:193
static HRESULT GetLastDef(HRESULT hResDef=E_FAIL) noexcept
Definition: HResult.h:242
Definition: cOSModule.h:156
bool put_FuncAddress(TYPE pFunc)
Definition: cOSModule.h:174
TYPE m_pFunc
FARPROC to typedef int (FAR WINAPI *FARPROC)();.
Definition: cOSModule.h:161
bool isValidFunc() const
Definition: cOSModule.h:188
~cOSModuleFunc()
Definition: cOSModule.h:167
bool put_FuncGeneric(FARPROC pFunc)
Definition: cOSModule.h:181
cOSModuleFunc(TYPE pFunc=nullptr)
Definition: cOSModule.h:163
void ClearFuncAddress()
Definition: cOSModule.h:170
Definition: cOSModule.h:34
void AttachModule(HMODULE hModule=((HMODULE) nullptr), UINT32 uFlags=k_Load_Normal)
Definition: cOSModule.h:122
bool isResourceModule() const noexcept
Definition: cOSModule.h:101
HRESULT GetLastErrorDef(HRESULT hResDef=E_FAIL) const
Definition: cOSModule.h:110
HMODULE DetachModule() noexcept
Definition: cOSModule.h:136
void ClearModule() noexcept
Definition: cOSModule.h:128
bool isValidModule() const noexcept
Definition: cOSModule.h:84
HMODULE get_ModuleHandle() const noexcept
Definition: cOSModule.h:92
UINT_PTR get_ModuleInt() const noexcept
Definition: cOSModule.h:96
< The main namespace for all Core functions.
Definition: GrayCore.cpp:14
MIME_TYPE
Definition: cMime.h:79
int StrLen_t
the length of a string in chars (bytes for UTF8, wchar_t for UNICODE). or offset in characters....
Definition: StrConst.h:32
char FILECHAR_t
a UTF8 char in a file name. like TCHAR
Definition: FileName.h:22
cOSModuleFunc< FARPROC > cOSModuleFuncGeneric
Definition: cOSModule.h:198