Gradual code migration to ATL and object-oriented approach:

- Configuration, user and session data are now internally stored as objects.
- Object wrappers for many structs and primitives added.
- Code partially rewritten to work with objects and benefit out-of-scope clean-up using object destructors.
- …And a lot more code internal changes.

Configuration, user and other data is now passed as compact BLOBs in inter-process communication greatly reducing communication payload.
This commit is contained in:
Simon Rozman
2015-04-09 08:50:24 +00:00
parent 8fcf315329
commit 45a741d7db
6 changed files with 331 additions and 13 deletions

View File

@@ -104,6 +104,7 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="atlcrypt.h" />
<ClInclude Include="atleap.h" />
<ClInclude Include="atlex.h" />
<ClInclude Include="atlmsi.h" />
<ClInclude Include="atlshlwapi.h" />

View File

@@ -37,5 +37,8 @@
<ClInclude Include="atlex.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="atleap.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@@ -102,7 +102,7 @@ namespace ATL
//
// CCertContext
//
class CCertContext : public ATL::CObjectWithHandleT<PCCERT_CONTEXT>
class CCertContext : public ATL::CObjectWithHandleDuplT<PCCERT_CONTEXT>
{
public:
virtual ~CCertContext() throw()
@@ -126,6 +126,46 @@ namespace ATL
{
CertFreeCertificateContext(m_h);
}
virtual HANDLE InternalDuplicate(HANDLE h) const
{
return CertDuplicateCertificateContext(h);
}
};
//
// CCertChainContext
//
class CCertChainContext : public ATL::CObjectWithHandleDuplT<PCCERT_CHAIN_CONTEXT>
{
public:
virtual ~CCertChainContext() throw()
{
if (m_h)
CertFreeCertificateChain(m_h);
}
inline BOOL Create(__in_opt HCERTCHAINENGINE hChainEngine, __in PCCERT_CONTEXT pCertContext, __in_opt LPFILETIME pTime, __in_opt HCERTSTORE hAdditionalStore, __in PCERT_CHAIN_PARA pChainPara, __in DWORD dwFlags, __reserved LPVOID pvReserved) throw()
{
HANDLE h;
if (CertGetCertificateChain(hChainEngine, pCertContext, pTime, hAdditionalStore, pChainPara, dwFlags, pvReserved, &h)) {
Attach(h);
return TRUE;
} else
return FALSE;
}
protected:
virtual void InternalFree()
{
CertFreeCertificateChain(m_h);
}
virtual HANDLE InternalDuplicate(HANDLE h) const
{
return CertDuplicateCertificateChain(h);
}
};

46
atleap.h Normal file
View File

@@ -0,0 +1,46 @@
/*
Copyright 1991-2015 Amebis
This file is part of libatl.
Setup is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Setup is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Setup. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <eaptypes.h>
namespace ATL
{
namespace EAP
{
class CEAPAttribute : public EAP_ATTRIBUTE
{
public:
CEAPAttribute()
{
eaType = eatReserved;
dwLength = 0;
pValue = NULL;
}
~CEAPAttribute()
{
if (pValue)
delete pValue;
}
};
}
}

179
atlex.h
View File

@@ -20,6 +20,7 @@
#pragma once
#include <atldef.h>
#include <atlstr.h>
namespace ATL
@@ -37,28 +38,28 @@ namespace ATL
{
}
inline CObjectWithHandleT(T h) throw() : m_h(h)
inline CObjectWithHandleT(HANDLE h) throw() : m_h(h)
{
}
inline operator T() const throw()
inline operator HANDLE() const throw()
{
return m_h;
}
inline T*& operator*() const
inline HANDLE*& operator*() const
{
ATLENSURE(m_h != NULL);
return *m_h;
}
inline T* operator&() throw()
inline HANDLE* operator&() throw()
{
ATLASSERT(m_h == NULL);
return &m_h;
}
inline T operator->() const throw()
inline HANDLE operator->() const throw()
{
ATLASSERT(m_h != NULL);
return m_h;
@@ -69,31 +70,31 @@ namespace ATL
return m_h == NULL;
}
inline bool operator<(_In_opt_ T h) const throw()
inline bool operator<(_In_opt_ HANDLE h) const throw()
{
return m_h < h;
}
inline bool operator!=(_In_opt_ T h) const
inline bool operator!=(_In_opt_ HANDLE h) const
{
return !operator==(h);
}
inline bool operator==(_In_opt_ T h) const throw()
inline bool operator==(_In_opt_ HANDLE h) const throw()
{
return m_h == h;
}
inline void Attach(_In_opt_ T h) throw()
inline void Attach(_In_opt_ HANDLE h) throw()
{
if (m_h)
InternalFree();
m_h = h;
}
inline T Detach() throw()
inline HANDLE Detach() throw()
{
T h = m_h;
HANDLE h = m_h;
m_h = NULL;
return h;
}
@@ -110,6 +111,160 @@ namespace ATL
virtual void InternalFree() = 0;
protected:
T m_h;
HANDLE m_h;
};
//
// CObjectWithHandleDuplT
//
template <class T>
class CObjectWithHandleDuplT : public CObjectWithHandleT<T>
{
public:
inline HANDLE GetDuplicate() const
{
return m_h ? InternalDuplicate(m_h) : NULL;
}
inline BOOL DuplicateAndAttach(_In_opt_ HANDLE h) throw()
{
if (m_h)
InternalFree();
return h ? (m_h = InternalDuplicate(h)) != NULL : (m_h = NULL, TRUE);
}
//
// Do not allow = operators. They are semantically ambigious:
// Do they attach the class to the existing instance of object, or do they duplicate it?
// To avoid confusion, user should use Attach() and Duplicate() methods explicitly.
//
//inline const CObjectWithHandleDuplT<T>& operator=(_In_ const HANDLE src)
//{
// Attach(src ? InternalDuplicate(src) : NULL);
// return *this;
//}
//inline const CObjectWithHandleDuplT<T>& operator=(_In_ const CObjectWithHandleDuplT<T> &src)
//{
// Attach(src.m_h ? InternalDuplicate(src.m_h) : NULL);
// return *this;
//}
protected:
virtual HANDLE InternalDuplicate(HANDLE h) const = 0;
};
//
// CStrFormatT, CStrFormatW, CStrFormatA, CStrFormat
//
template<typename BaseType, class StringTraits>
class CStrFormatT : public CStringT<BaseType, StringTraits>
{
public:
CStrFormatT(_In_z_ _FormatMessage_format_string_ PCXSTR pszFormat, ...)
{
ATLASSERT(AtlIsValidString(pszFormat));
va_list argList;
va_start(argList, pszFormat);
FormatV(pszFormat, argList);
va_end(argList);
}
CStrFormatT(_In_ _FormatMessage_format_string_ UINT nFormatID, ...)
{
CStringT strFormat(GetManager());
ATLENSURE(strFormat.LoadString(nFormatID));
va_list argList;
va_start(argList, nFormatID);
FormatV(strFormat, argList);
va_end(argList);
}
CStrFormatT(_In_ HINSTANCE hInstance, _In_ _FormatMessage_format_string_ UINT nFormatID, ...)
{
CStringT strFormat(GetManager());
ATLENSURE(strFormat.LoadString(hInstance, nFormatID));
va_list argList;
va_start(argList, nFormatID);
FormatV(strFormat, argList);
va_end(argList);
}
CStrFormatT(_In_ HINSTANCE hInstance, _In_ WORD wLanguageID, _In_ _FormatMessage_format_string_ UINT nFormatID, ...)
{
CStringT strFormat(GetManager());
ATLENSURE(strFormat.LoadString(hInstance, nFormatID, wLanguageID));
va_list argList;
va_start(argList, nFormatID);
FormatV(strFormat, argList);
va_end(argList);
}
};
typedef CStrFormatT< wchar_t, StrTraitATL< wchar_t, ChTraitsCRT< wchar_t > > > CStrFormatW;
typedef CStrFormatT< char, StrTraitATL< char, ChTraitsCRT< char > > > CStrFormatA;
typedef CStrFormatT< TCHAR, StrTraitATL< TCHAR, ChTraitsCRT< TCHAR > > > CStrFormat;
//
// CStrFormatMsgT, CStrFormatMsgW, CStrFormatMsgA, CStrFormatMsg
//
template<typename BaseType, class StringTraits>
class CStrFormatMsgT : public CStringT<BaseType, StringTraits>
{
public:
CStrFormatMsgT(_In_z_ _FormatMessage_format_string_ PCXSTR pszFormat, ...)
{
ATLASSERT(AtlIsValidString(pszFormat));
va_list argList;
va_start(argList, pszFormat);
FormatMessageV(pszFormat, &argList);
va_end(argList);
}
CStrFormatMsgT(_In_ _FormatMessage_format_string_ UINT nFormatID, ...)
{
CStringT strFormat(GetManager());
ATLENSURE(strFormat.LoadString(nFormatID));
va_list argList;
va_start(argList, nFormatID);
FormatMessageV(strFormat, &argList);
va_end(argList);
}
CStrFormatMsgT(_In_ HINSTANCE hInstance, _In_ _FormatMessage_format_string_ UINT nFormatID, ...)
{
CStringT strFormat(GetManager());
ATLENSURE(strFormat.LoadString(hInstance, nFormatID));
va_list argList;
va_start(argList, nFormatID);
FormatMessageV(strFormat, &argList);
va_end(argList);
}
CStrFormatMsgT(_In_ HINSTANCE hInstance, _In_ WORD wLanguageID, _In_ _FormatMessage_format_string_ UINT nFormatID, ...)
{
CStringT strFormat(GetManager());
ATLENSURE(strFormat.LoadString(hInstance, nFormatID, wLanguageID));
va_list argList;
va_start(argList, nFormatID);
FormatMessageV(strFormat, &argList);
va_end(argList);
}
};
typedef CStrFormatMsgT< wchar_t, StrTraitATL< wchar_t, ChTraitsCRT< wchar_t > > > CStrFormatMsgW;
typedef CStrFormatMsgT< char, StrTraitATL< char, ChTraitsCRT< char > > > CStrFormatMsgA;
typedef CStrFormatMsgT< TCHAR, StrTraitATL< TCHAR, ChTraitsCRT< TCHAR > > > CStrFormatMsg;
}

View File

@@ -19,6 +19,7 @@
#pragma once
#include "atlex.h"
#include <atlcoll.h>
#include <atlstr.h>
#include <Windows.h>
@@ -348,3 +349,75 @@ inline LSTATUS RegQueryValueExW(__in HKEY hKey, __in_opt LPCWSTR lpValueName, __
return lResult;
}
#if _WIN32_WINNT >= _WIN32_WINNT_VISTA
inline LSTATUS RegLoadMUIStringA(__in HKEY hKey, __in_opt LPCSTR pszValue, __out ATL::CAtlStringA &sOut, __in DWORD Flags, __in_opt LPCSTR pszDirectory)
{
LSTATUS lResult;
DWORD dwSize;
Flags &= ~REG_MUI_STRING_TRUNCATE;
lResult = RegLoadMUIStringA(hKey, pszValue, NULL, 0, &dwSize, Flags, pszDirectory);
if (lResult == ERROR_MORE_DATA) {
LPSTR szBuffer = sOut.GetBuffer(dwSize - 1);
if (!szBuffer) return ERROR_OUTOFMEMORY;
sOut.ReleaseBuffer((lResult = RegLoadMUIStringA(hKey, pszValue, szBuffer, dwSize, &dwSize, Flags, pszDirectory)) == ERROR_SUCCESS ? dwSize - 1 : 0);
} else if (lResult == ERROR_SUCCESS)
sOut.Empty();
return lResult;
}
inline LSTATUS RegLoadMUIStringW(__in HKEY hKey, __in_opt LPCWSTR pszValue, __out ATL::CAtlStringW &sOut, __in DWORD Flags, __in_opt LPCWSTR pszDirectory)
{
LSTATUS lResult;
DWORD dwSize;
Flags &= ~REG_MUI_STRING_TRUNCATE;
lResult = RegLoadMUIStringW(hKey, pszValue, NULL, 0, &dwSize, Flags, pszDirectory);
if (lResult == ERROR_MORE_DATA) {
LPWSTR szBuffer = sOut.GetBuffer(dwSize - 1);
if (!szBuffer) return ERROR_OUTOFMEMORY;
sOut.ReleaseBuffer((lResult = RegLoadMUIStringW(hKey, pszValue, szBuffer, dwSize, &dwSize, Flags, pszDirectory)) == ERROR_SUCCESS ? dwSize - 1 : 0);
} else if (lResult == ERROR_SUCCESS)
sOut.Empty();
return lResult;
}
#endif
namespace ATL
{
class CAtlLibrary : public CObjectWithHandleT<HMODULE>
{
public:
virtual ~CAtlLibrary() throw()
{
if (m_h)
FreeLibrary(m_h);
}
inline BOOL Load(__in LPCTSTR lpFileName, __reserved HANDLE hFile, __in DWORD dwFlags) throw()
{
HANDLE h = LoadLibraryEx(lpFileName, hFile, dwFlags);
if (h) {
Attach(h);
return TRUE;
} else
return FALSE;
}
protected:
virtual void InternalFree()
{
FreeLibrary(m_h);
}
};
}