Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cWinOSWrap.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cWinOSWrap_H
7 #define _INC_cWinOSWrap_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "../WinAPI/WinTypes.h"
14 
15 #ifdef _WIN32
16 namespace GrayLib
17 {
18  enum WINOSD_TYPE
19  {
23  WINOSD_Kernel32,
24  WINOSD_User32,
25  WINOSD_PSAPI,
26  WINOSD_ADVAPI32,
27  WINOSD_IPHLPAPI, // GetAdaptersAddresses
28  WINOSD_QTY,
29  };
30 
31  class GRAYLIB_LINK cWinOSWrap : public cSingleton < cWinOSWrap >
32  {
38 
39  friend class cSingleton < cWinOSWrap >;
41 
42  public:
43  //typedef DWORD(WINAPI* FUNC_GetProcessId_t)(HANDLE hProcess);
44  //typedef BOOL(WINAPI* FUNC_CancelIo_t)(HANDLE h); // todo CancelIoEx?
45  typedef BOOL(WINAPI* FUNC_ChangeWindowMessageFilter_t)(UINT message, DWORD dwFlag);
46  typedef BOOL(WINAPI* FUNC_EnumProcessModulesEx)(HANDLE hProcess, OUT HMODULE *lphModule, DWORD cb, OUT LPDWORD lpcbNeeded, DWORD dwFilterFlag);
47  typedef LONG(WINAPI* FUNC_RegDeleteKeyExW)(HKEY hKey, LPCWSTR lpSubKey, REGSAM samDesired, DWORD Reserved);
48  typedef LONG(WINAPI* FUNC_RegDeleteKeyExA)(HKEY hKey, LPCSTR lpSubKey, REGSAM samDesired, DWORD Reserved);
49 
50  public:
51  // cOSModuleFunc<FUNC_CancelIo_t> m_pfnCancelIo; // m_modKernel32 Vista
52  cOSModuleFunc<FUNC_ChangeWindowMessageFilter_t> m_funcChangeWindowMessageFilter; // m_modUser32 Vista
53  cOSModuleFunc<FUNC_EnumProcessModulesEx> m_funcEnumProcessModulesEx; // m_modPSAPI Vista, Win7
54  cOSModuleFunc<FUNC_RegDeleteKeyExA> m_funcRegDeleteKeyExA; // m_modAdvapi, XP Pro x64, Vista
55 
56  public:
57  // static const char* k_ModuleName[WINOSD_QTY + 1];
58  cOSModule m_mod[WINOSD_QTY + 1];
59 
60  protected:
61  cWinOSWrap()
62  : cSingleton<cWinOSWrap>(this, typeid(cWinOSWrap))
63  {
64  }
65  ~cWinOSWrap()
66  {
67  }
68 
69  public:
70  HRESULT BindModule(cOSModule& mod, const FILECHAR_t* pszName)
71  {
73  if (mod.isValidModule()) // already done.
74  return S_OK;
75  FILECHAR_t szPath[_MAX_PATH];
76  StrLen_t nLen = cSystemInfo::GetSystemDir(szPath, STRMAX(szPath));
77  nLen = cFilePath::CombineFilePathA(szPath, STRMAX(szPath), nLen, pszName);
78  HRESULT hRes = mod.LoadModule(szPath);
79  if (FAILED(hRes))
80  return hRes;
81  ASSERT(mod.isValidModule());
82  return nLen;
83  }
84 
85  HRESULT BindKernel32()
86  {
87  HRESULT hRes = BindModule(m_mod[WINOSD_Kernel32], _FN("kernel32.dll"));
88  if (FAILED(hRes) || hRes == S_OK)
89  {
90  return hRes;
91  }
92  return S_OK;
93  }
94  HRESULT BindUser32()
95  {
96  HRESULT hRes = BindModule(m_mod[WINOSD_User32], _FN("user32.dll"));
97  if (FAILED(hRes) || hRes == S_OK)
98  {
99  return hRes;
100  }
101  // Vista
102  m_funcChangeWindowMessageFilter.put_FuncGeneric(m_mod[WINOSD_User32].GetSymbolAddress("ChangeWindowMessageFilter"));
103  return S_OK;
104  }
105  HRESULT BindPSAPI()
106  {
108  HRESULT hRes = BindModule(m_mod[WINOSD_PSAPI], _FN("psapi.dll"));
109  if (FAILED(hRes) || hRes == S_OK)
110  {
111  return hRes;
112  }
113  // Vista and Win7
114  m_funcEnumProcessModulesEx.put_FuncGeneric(m_mod[WINOSD_PSAPI].GetSymbolAddress("EnumProcessModulesEx"));
115  return S_OK;
116  }
117  HRESULT BindAdvapi()
118  {
119  HRESULT hRes = BindModule(m_mod[WINOSD_ADVAPI32], _FN("Advapi32.dll"));
120  if (FAILED(hRes) || hRes == S_OK)
121  {
122  return hRes;
123  }
124  m_funcRegDeleteKeyExA.put_FuncGeneric(m_mod[WINOSD_ADVAPI32].GetSymbolAddress("RegDeleteKeyExA"));
125  return S_OK;
126  }
127  };
128 };
129 #endif // _WIN32
130 #endif
#define _FN(x)
like _T(x) macro for static text file names.
Definition: FileName.h:23
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
#define FAILED(x)
Definition: HResult.h:30
#define STRMAX(x)
Get Max size of static string space. minus the '\0' terminator character.
Definition: StrConst.h:33
#define _MAX_PATH
Definition: SysTypes.h:423
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#define ASSERT(exp)
Definition: cDebugAssert.h:87
#define CHEAPOBJECT_IMPL
Definition: cHeapObject.h:32
static StrLen_t __stdcall CombineFilePathA(FILECHAR_t *pszOut, StrLen_t iLenMax, StrLen_t iLen, const FILECHAR_t *pszName, FILECHAR_t chSep=k_DirSep)
Definition: cFilePath.cpp:552
static StrLen_t __stdcall GetSystemDir(FILECHAR_t *pszDir, StrLen_t iLenMax)
Definition: cSystemInfo.cpp:183
Definition: cMesh.h:22
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