yolobs-studio/UI/win-update/win-update-helpers.hpp

72 lines
1.6 KiB
C++
Raw Permalink Normal View History

2017-04-19 19:54:15 +00:00
#pragma once
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <Wincrypt.h>
#include <cstdint>
#include <string>
/* ------------------------------------------------------------------------ */
template<typename T, void freefunc(T)> class CustomHandle {
T handle;
public:
inline CustomHandle() : handle(0) {}
inline CustomHandle(T in) : handle(in) {}
inline ~CustomHandle()
{
if (handle)
freefunc(handle);
}
2019-09-22 21:19:10 +00:00
inline T *operator&() { return &handle; }
inline operator T() const { return handle; }
inline T get() const { return handle; }
2017-04-19 19:54:15 +00:00
inline CustomHandle<T, freefunc> &operator=(T in)
{
if (handle)
freefunc(handle);
handle = in;
return *this;
}
2019-09-22 21:19:10 +00:00
inline bool operator!() const { return !handle; }
2017-04-19 19:54:15 +00:00
};
void FreeProvider(HCRYPTPROV prov);
void FreeHash(HCRYPTHASH hash);
void FreeKey(HCRYPTKEY key);
using CryptProvider = CustomHandle<HCRYPTPROV, FreeProvider>;
2019-09-22 21:19:10 +00:00
using CryptHash = CustomHandle<HCRYPTHASH, FreeHash>;
using CryptKey = CustomHandle<HCRYPTKEY, FreeKey>;
2017-04-19 19:54:15 +00:00
/* ------------------------------------------------------------------------ */
template<typename T> class LocalPtr {
T *ptr = nullptr;
public:
inline ~LocalPtr()
{
if (ptr)
LocalFree(ptr);
}
2019-09-22 21:19:10 +00:00
inline T **operator&() { return &ptr; }
inline operator T() const { return ptr; }
inline T *get() const { return ptr; }
2017-04-19 19:54:15 +00:00
2019-09-22 21:19:10 +00:00
inline bool operator!() const { return !ptr; }
2017-04-19 19:54:15 +00:00
2019-09-22 21:19:10 +00:00
inline T *operator->() { return ptr; }
2017-04-19 19:54:15 +00:00
};
/* ------------------------------------------------------------------------ */
std::string vstrprintf(const char *format, va_list args);
std::string strprintf(const char *format, ...);