win-update-helpers.hpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #pragma once
  2. #define WIN32_LEAN_AND_MEAN
  3. #include <windows.h>
  4. #include <Wincrypt.h>
  5. #include <cstdint>
  6. #include <string>
  7. /* ------------------------------------------------------------------------ */
  8. template<typename T, void freefunc(T)> class CustomHandle {
  9. T handle;
  10. public:
  11. inline CustomHandle() : handle(0) {}
  12. inline CustomHandle(T in) : handle(in) {}
  13. inline ~CustomHandle()
  14. {
  15. if (handle)
  16. freefunc(handle);
  17. }
  18. inline T *operator&() { return &handle; }
  19. inline operator T() const { return handle; }
  20. inline T get() const { return handle; }
  21. inline CustomHandle<T, freefunc> &operator=(T in)
  22. {
  23. if (handle)
  24. freefunc(handle);
  25. handle = in;
  26. return *this;
  27. }
  28. inline bool operator!() const { return !handle; }
  29. };
  30. void FreeProvider(HCRYPTPROV prov);
  31. void FreeHash(HCRYPTHASH hash);
  32. void FreeKey(HCRYPTKEY key);
  33. using CryptProvider = CustomHandle<HCRYPTPROV, FreeProvider>;
  34. using CryptHash = CustomHandle<HCRYPTHASH, FreeHash>;
  35. using CryptKey = CustomHandle<HCRYPTKEY, FreeKey>;
  36. /* ------------------------------------------------------------------------ */
  37. template<typename T> class LocalPtr {
  38. T *ptr = nullptr;
  39. public:
  40. inline ~LocalPtr()
  41. {
  42. if (ptr)
  43. LocalFree(ptr);
  44. }
  45. inline T **operator&() { return &ptr; }
  46. inline operator T() const { return ptr; }
  47. inline T *get() const { return ptr; }
  48. inline bool operator!() const { return !ptr; }
  49. inline T *operator->() { return ptr; }
  50. };
  51. /* ------------------------------------------------------------------------ */
  52. std::string vstrprintf(const char *format, va_list args);
  53. std::string strprintf(const char *format, ...);