1
0

helpers.hpp 802 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #pragma once
  2. #define WIN32_LEAN_AND_MEAN
  3. #include <windows.h>
  4. #include <Wincrypt.h>
  5. /* ------------------------------------------------------------------------ */
  6. template<typename T, void freefunc(T)> class CustomHandle {
  7. T handle;
  8. public:
  9. inline CustomHandle() : handle(0) {}
  10. inline CustomHandle(T in) : handle(in) {}
  11. inline ~CustomHandle()
  12. {
  13. if (handle)
  14. freefunc(handle);
  15. }
  16. inline T *operator&() { return &handle; }
  17. inline operator T() const { return handle; }
  18. inline T get() const { return handle; }
  19. inline CustomHandle<T, freefunc> &operator=(T in)
  20. {
  21. if (handle)
  22. freefunc(handle);
  23. handle = in;
  24. return *this;
  25. }
  26. inline bool operator!() const { return !handle; }
  27. };
  28. void FreeProvider(HCRYPTPROV prov);
  29. using CryptProvider = CustomHandle<HCRYPTPROV, FreeProvider>;