printing_info_win.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef PRINTING_BACKEND_PRINTING_INFO_WIN_H_
  5. #define PRINTING_BACKEND_PRINTING_INFO_WIN_H_
  6. #include <objidl.h>
  7. #include <stdint.h>
  8. #include <winspool.h>
  9. #include <memory>
  10. #include "base/component_export.h"
  11. namespace printing {
  12. namespace internal {
  13. COMPONENT_EXPORT(PRINT_BACKEND)
  14. std::unique_ptr<uint8_t[]> GetDriverInfo(HANDLE printer, int level);
  15. COMPONENT_EXPORT(PRINT_BACKEND)
  16. std::unique_ptr<uint8_t[]> GetPrinterInfo(HANDLE printer, int level);
  17. // This class is designed to work with PRINTER_INFO_X structures
  18. // and calls GetPrinter internally with correctly allocated buffer.
  19. template <typename PrinterInfoType, int level>
  20. class PrinterInfo {
  21. public:
  22. bool Init(HANDLE printer) {
  23. buffer_ = GetPrinterInfo(printer, level);
  24. return buffer_ != nullptr;
  25. }
  26. const PrinterInfoType* get() const {
  27. return reinterpret_cast<const PrinterInfoType*>(buffer_.get());
  28. }
  29. private:
  30. std::unique_ptr<uint8_t[]> buffer_;
  31. };
  32. // This class is designed to work with DRIVER_INFO_X structures
  33. // and calls GetDriverInfo internally with correctly allocated buffer.
  34. template <typename DriverInfoType, int level>
  35. class DriverInfo {
  36. public:
  37. bool Init(HANDLE printer) {
  38. buffer_ = GetDriverInfo(printer, level);
  39. return buffer_ != nullptr;
  40. }
  41. const DriverInfoType* get() const {
  42. return reinterpret_cast<const DriverInfoType*>(buffer_.get());
  43. }
  44. private:
  45. std::unique_ptr<uint8_t[]> buffer_;
  46. };
  47. } // namespace internal
  48. using PrinterInfo2 = internal::PrinterInfo<PRINTER_INFO_2, 2>;
  49. using PrinterInfo5 = internal::PrinterInfo<PRINTER_INFO_5, 5>;
  50. using DriverInfo6 = internal::DriverInfo<DRIVER_INFO_6, 6>;
  51. } // namespace printing
  52. #endif // PRINTING_BACKEND_PRINTING_INFO_WIN_H_