@@ -21,23 +21,6 @@ Example how to print page.
2121#include < algorithm>
2222#include " Pdfix.h"
2323
24- #if defined (__ANDROID__) || defined (__APPLE__) || defined (__GNUC__)
25- typedef unsigned char BYTE;
26- #endif
27-
28- template <typename T>
29- struct MallocDeleter {
30- void operator () (T* ptr) {
31- if (ptr) {
32- free (ptr);
33- ptr = nullptr ;
34- }
35- }
36- };
37- typedef std::unique_ptr<char , MallocDeleter<char > > unique_charp;
38- typedef std::unique_ptr<wchar_t , MallocDeleter<wchar_t > > unique_wcharp;
39- typedef std::unique_ptr<BYTE, MallocDeleter<BYTE> > unique_BYTEp;
40-
4124void PrintPage (
4225 std::wstring email, // authorization email
4326 std::wstring license_key, // authorization license key
@@ -56,9 +39,14 @@ void PrintPage(
5639
5740 // find the printer
5841 DWORD sz = 0 ;
59-
6042 GetDefaultPrinter (NULL , &sz);
61- unique_wcharp name ((wchar_t *)malloc (sz * sizeof (wchar_t ) + 1 ));
43+
44+ auto malloc_deleter = [=](void * buf) { free (buf); };
45+ std::unique_ptr<wchar_t , decltype (malloc_deleter)>
46+ name ((wchar_t *)malloc (sz * sizeof (wchar_t ) + 1 ), malloc_deleter);
47+ if (!name)
48+ throw std::runtime_error (" Memory allocation error" );
49+
6250 GetDefaultPrinter (name.get (), &sz);
6351
6452 PRINTER_DEFAULTS pd;
@@ -72,7 +60,11 @@ void PrintPage(
7260 throw std::runtime_error (" Error OpenPrinter" );
7361
7462 GetPrinter (handle, 2 , 0 , 0 , &sz);
75- unique_BYTEp data_2 ((BYTE*)malloc (sz));
63+ std::unique_ptr<unsigned char , decltype (malloc_deleter)>
64+ data_2 ((unsigned char *)malloc (sz * sizeof (unsigned char ) + 1 ), malloc_deleter);
65+ if (!data_2)
66+ throw std::runtime_error (" Memory allocation error" );
67+
7668 PRINTER_INFO_2* pi_2 = (PRINTER_INFO_2*)data_2.get ();
7769 GetPrinter (handle, 2 , (LPBYTE)pi_2, sz, &sz);
7870
0 commit comments