| 1 | /* | 
|---|
| 2 | *  Set printer capabilities in DsDriver Keys on remote printer | 
|---|
| 3 | *  Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2002. | 
|---|
| 4 | * | 
|---|
| 5 | *  This program is free software; you can redistribute it and/or modify | 
|---|
| 6 | *  it under the terms of the GNU General Public License as published by | 
|---|
| 7 | *  the Free Software Foundation; either version 2 of the License, or | 
|---|
| 8 | *  (at your option) any later version. | 
|---|
| 9 | * | 
|---|
| 10 | *  This program is distributed in the hope that it will be useful, | 
|---|
| 11 | *  but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 12 | *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 13 | *  GNU General Public License for more details. | 
|---|
| 14 | * | 
|---|
| 15 | *  You should have received a copy of the GNU General Public License | 
|---|
| 16 | *  along with this program; if not, write to the Free Software | 
|---|
| 17 | *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 
|---|
| 18 | */ | 
|---|
| 19 |  | 
|---|
| 20 | /* This needs to be defined for certain compilers */ | 
|---|
| 21 | #define WINVER 0x0500 | 
|---|
| 22 |  | 
|---|
| 23 | #include <tchar.h> | 
|---|
| 24 | #include <windows.h> | 
|---|
| 25 | #include <stdio.h> | 
|---|
| 26 |  | 
|---|
| 27 | #define SAMBA_PORT _T("Samba") | 
|---|
| 28 |  | 
|---|
| 29 | TCHAR *PrintLastError(void) | 
|---|
| 30 | { | 
|---|
| 31 | static TCHAR msgtxt[1024*sizeof(TCHAR)]; | 
|---|
| 32 |  | 
|---|
| 33 | FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, | 
|---|
| 34 | NULL, GetLastError(), | 
|---|
| 35 | 0, msgtxt, 0, NULL); | 
|---|
| 36 |  | 
|---|
| 37 | return msgtxt; | 
|---|
| 38 | } | 
|---|
| 39 |  | 
|---|
| 40 | void map_orientation(HANDLE ph, TCHAR *printer, TCHAR *port) | 
|---|
| 41 | { | 
|---|
| 42 | DWORD rot; | 
|---|
| 43 | TCHAR portrait_only[] = _T("PORTRAIT\0"); | 
|---|
| 44 | TCHAR both[] = _T("LANDSCAPE\0PORTRAIT\0"); | 
|---|
| 45 |  | 
|---|
| 46 | /* orentation of 90 or 270 indicates landscape supported, 0 means it isn't */ | 
|---|
| 47 | rot = DeviceCapabilities(printer, port, DC_BINNAMES, NULL, NULL); | 
|---|
| 48 |  | 
|---|
| 49 | printf("printOrientationsSupported:\n"); | 
|---|
| 50 |  | 
|---|
| 51 | if (rot) { | 
|---|
| 52 | printf("\tPORTRAIT\n\tLANDSCAPE\n"); | 
|---|
| 53 | SetPrinterDataEx(ph, SPLDS_DRIVER_KEY, _T("printOrientationsSupported"), REG_MULTI_SZ, | 
|---|
| 54 | both, sizeof(both)); | 
|---|
| 55 | } else { | 
|---|
| 56 | printf("\tPORTRAIT\n"); | 
|---|
| 57 | SetPrinterDataEx(ph, SPLDS_DRIVER_KEY, _T("printOrientationsSupported"), REG_MULTI_SZ, | 
|---|
| 58 | portrait_only, sizeof(portrait_only)); | 
|---|
| 59 | } | 
|---|
| 60 | } | 
|---|
| 61 |  | 
|---|
| 62 | void map_resolution(HANDLE ph, TCHAR *printer, TCHAR *port) | 
|---|
| 63 | { | 
|---|
| 64 | DWORD num, *res, maxres = 0, i; | 
|---|
| 65 |  | 
|---|
| 66 | num = DeviceCapabilities(printer, port, DC_ENUMRESOLUTIONS, NULL, NULL); | 
|---|
| 67 | if ((DWORD) -1 == num) | 
|---|
| 68 | return; | 
|---|
| 69 | res = malloc(num*2*sizeof(DWORD)); | 
|---|
| 70 | num = DeviceCapabilities(printer, port, DC_ENUMRESOLUTIONS, (BYTE *) res, NULL); | 
|---|
| 71 | for (i=0; i < num*2; i++) { | 
|---|
| 72 | maxres = (res[i] > maxres) ? res[i] : maxres; | 
|---|
| 73 | } | 
|---|
| 74 | printf("printMaxResolutionSupported: %d\n", maxres); | 
|---|
| 75 | SetPrinterDataEx(ph, SPLDS_DRIVER_KEY, _T("printMaxResolutionSupported"), REG_DWORD, | 
|---|
| 76 | (BYTE *) &maxres, sizeof(maxres)); | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | void map_extents(HANDLE ph, TCHAR *printer, TCHAR *port) | 
|---|
| 80 | { | 
|---|
| 81 | DWORD extentval, xval, yval; | 
|---|
| 82 |  | 
|---|
| 83 | extentval = DeviceCapabilities(printer, port, DC_MINEXTENT, NULL, NULL); | 
|---|
| 84 | xval = (DWORD) (LOWORD(extentval)); | 
|---|
| 85 | yval = (DWORD) (HIWORD(extentval)); | 
|---|
| 86 | printf("printMinXExtent: %d\n", xval); | 
|---|
| 87 | printf("printMinYExtent: %d\n", yval); | 
|---|
| 88 | SetPrinterDataEx(ph, SPLDS_DRIVER_KEY, _T("printMinXExtent"), REG_DWORD, (BYTE *) &xval, sizeof(xval)); | 
|---|
| 89 | SetPrinterDataEx(ph, SPLDS_DRIVER_KEY, _T("printMinYExtent"), REG_DWORD, (BYTE *) &yval, sizeof(yval)); | 
|---|
| 90 | extentval = DeviceCapabilities(printer, port, DC_MAXEXTENT, NULL, NULL); | 
|---|
| 91 | xval = (DWORD) (LOWORD(extentval)); | 
|---|
| 92 | yval = (DWORD) (HIWORD(extentval)); | 
|---|
| 93 | printf("printMaxXExtent: %d\n", xval); | 
|---|
| 94 | printf("printMaxYExtent: %d\n", yval); | 
|---|
| 95 | SetPrinterDataEx(ph, SPLDS_DRIVER_KEY, _T("printMaxXExtent"), REG_DWORD, (BYTE *) &xval, sizeof(xval)); | 
|---|
| 96 | SetPrinterDataEx(ph, SPLDS_DRIVER_KEY, _T("printMaxYExtent"), REG_DWORD, (BYTE *) &yval, sizeof(yval)); | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | void map_printrateunit(HANDLE ph, TCHAR *printer, TCHAR *port) | 
|---|
| 100 | { | 
|---|
| 101 | DWORD unit; | 
|---|
| 102 | TCHAR ppm[] = _T("PagesPerMinute"); | 
|---|
| 103 | TCHAR ipm[] = _T("InchesPerMinute"); | 
|---|
| 104 | TCHAR lpm[] = _T("LinesPerMinute"); | 
|---|
| 105 | TCHAR cps[] = _T("CharactersPerSecond"); | 
|---|
| 106 |  | 
|---|
| 107 | unit = DeviceCapabilities(printer, port, DC_PRINTRATEUNIT, NULL, NULL); | 
|---|
| 108 | switch(unit) { | 
|---|
| 109 | case PRINTRATEUNIT_PPM: | 
|---|
| 110 | printf("printRateUnit: %s\n", ppm); | 
|---|
| 111 | SetPrinterDataEx(ph, SPLDS_DRIVER_KEY, _T("printRateUnit"), REG_SZ, ppm, sizeof(ppm)); | 
|---|
| 112 | break; | 
|---|
| 113 | case PRINTRATEUNIT_IPM: | 
|---|
| 114 | printf("printRateUnit: %s\n", ipm); | 
|---|
| 115 | SetPrinterDataEx(ph, SPLDS_DRIVER_KEY, _T("printRateUnit"), REG_SZ, ipm, sizeof(ipm)); | 
|---|
| 116 | break; | 
|---|
| 117 | case PRINTRATEUNIT_LPM: | 
|---|
| 118 | printf("printRateUnit: %s\n", lpm); | 
|---|
| 119 | SetPrinterDataEx(ph, SPLDS_DRIVER_KEY, _T("printRateUnit"), REG_SZ, lpm, sizeof(lpm)); | 
|---|
| 120 | break; | 
|---|
| 121 | case PRINTRATEUNIT_CPS: | 
|---|
| 122 | printf("printRateUnit: %s\n", cps); | 
|---|
| 123 | SetPrinterDataEx(ph, SPLDS_DRIVER_KEY, _T("printRateUnit"), REG_SZ, cps, sizeof(cps)); | 
|---|
| 124 | break; | 
|---|
| 125 | default: | 
|---|
| 126 | printf("printRateUnit: unknown value %d\n", unit); | 
|---|
| 127 | } | 
|---|
| 128 | } | 
|---|
| 129 |  | 
|---|
| 130 | void map_generic_boolean(HANDLE ph, TCHAR *printer, TCHAR *port, WORD cap, TCHAR *key) | 
|---|
| 131 | { | 
|---|
| 132 | BYTE boolval; | 
|---|
| 133 | /* DeviceCapabilities doesn't always return 1 for true...just nonzero */ | 
|---|
| 134 | boolval = (BYTE) (DeviceCapabilities(printer, port, cap, NULL, NULL) ? 1 : 0); | 
|---|
| 135 | printf("%s: %s\n", key, boolval ? "TRUE" : "FALSE"); | 
|---|
| 136 | SetPrinterDataEx(ph, SPLDS_DRIVER_KEY, key, REG_BINARY, &boolval, sizeof(boolval)); | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | void map_generic_dword(HANDLE ph, TCHAR *printer, TCHAR *port, WORD cap, TCHAR *key) | 
|---|
| 140 | { | 
|---|
| 141 | DWORD dword; | 
|---|
| 142 |  | 
|---|
| 143 | dword = DeviceCapabilities(printer, port, cap, NULL, NULL); | 
|---|
| 144 | if ((DWORD) -1 == dword) | 
|---|
| 145 | return; | 
|---|
| 146 |  | 
|---|
| 147 | printf("%s: %d\n", key, dword); | 
|---|
| 148 | SetPrinterDataEx(ph, SPLDS_DRIVER_KEY, key, REG_DWORD, (BYTE *) &dword, sizeof(dword)); | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | void map_generic_multi_sz(HANDLE ph, TCHAR *printer, TCHAR *port, WORD cap, TCHAR *key, int size) | 
|---|
| 152 | { | 
|---|
| 153 | TCHAR *strings_in; | 
|---|
| 154 | TCHAR *strings_out, *strings_cur; | 
|---|
| 155 | DWORD num_items, i; | 
|---|
| 156 |  | 
|---|
| 157 | num_items = DeviceCapabilities(printer, port, cap, NULL, NULL); | 
|---|
| 158 | if ((DWORD) -1 == num_items) | 
|---|
| 159 | return; | 
|---|
| 160 | strings_in = malloc(num_items * size); | 
|---|
| 161 | strings_out = calloc(num_items, size); | 
|---|
| 162 | num_items = DeviceCapabilities(printer, port, cap, strings_in, NULL); | 
|---|
| 163 | printf("%s:\n", key); | 
|---|
| 164 | for (i=0, strings_cur = strings_out; i < num_items; i++) { | 
|---|
| 165 | _tcsncpy(strings_cur, &strings_in[i*size], size); | 
|---|
| 166 | printf("\t%s\n", strings_cur); | 
|---|
| 167 | strings_cur += _tcslen(strings_cur) + 1; | 
|---|
| 168 | } | 
|---|
| 169 |  | 
|---|
| 170 | SetPrinterDataEx(ph, SPLDS_DRIVER_KEY, key, REG_MULTI_SZ, strings_out, | 
|---|
| 171 | (strings_cur - strings_out + 1) * sizeof(TCHAR)); | 
|---|
| 172 |  | 
|---|
| 173 | free(strings_in); | 
|---|
| 174 | free(strings_out); | 
|---|
| 175 | } | 
|---|
| 176 |  | 
|---|
| 177 | int main(int argc, char *argv[]) | 
|---|
| 178 | { | 
|---|
| 179 | HANDLE ph; | 
|---|
| 180 | BYTE *driver_info; | 
|---|
| 181 | DWORD needed; | 
|---|
| 182 | TCHAR *printer; | 
|---|
| 183 | TCHAR *port = SAMBA_PORT; | 
|---|
| 184 | PRINTER_DEFAULTS admin_access = {NULL, NULL, PRINTER_ACCESS_ADMINISTER}; | 
|---|
| 185 | PRINTER_INFO_7 publish = {NULL, DSPRINT_PUBLISH}; | 
|---|
| 186 |  | 
|---|
| 187 | if (argc < 2) { | 
|---|
| 188 | printf("Usage: %s <printername>\n", argv[0]); | 
|---|
| 189 | return -1; | 
|---|
| 190 | } | 
|---|
| 191 |  | 
|---|
| 192 | printer = argv[1]; | 
|---|
| 193 |  | 
|---|
| 194 | if (!(OpenPrinter(printer, &ph, &admin_access))) { | 
|---|
| 195 | printf("OpenPrinter failed, error = %s\n", PrintLastError()); | 
|---|
| 196 | return -1; | 
|---|
| 197 | } | 
|---|
| 198 |  | 
|---|
| 199 | GetPrinterDriver(ph, NULL, 1, NULL, 0, &needed); | 
|---|
| 200 | if (!needed) { | 
|---|
| 201 | printf("GetPrinterDriver failed, error = %s\n", PrintLastError()); | 
|---|
| 202 | ClosePrinter(ph); | 
|---|
| 203 | return -1; | 
|---|
| 204 | } | 
|---|
| 205 | driver_info = malloc(needed); | 
|---|
| 206 | if (!(GetPrinterDriver(ph, NULL, 1, driver_info, needed, &needed))) { | 
|---|
| 207 | printf("GetPrinterDriver failed, error = %s\n", PrintLastError()); | 
|---|
| 208 | ClosePrinter(ph); | 
|---|
| 209 | return -1; | 
|---|
| 210 | } | 
|---|
| 211 |  | 
|---|
| 212 | map_generic_multi_sz(ph, printer, port, DC_BINNAMES, _T("printBinNames"), 24); | 
|---|
| 213 | map_generic_boolean(ph, printer, port, DC_COLLATE, _T("printCollate")); | 
|---|
| 214 | map_generic_dword(ph, printer, port, DC_COPIES, _T("printMaxCopies")); | 
|---|
| 215 | map_generic_dword(ph, printer, port, DC_DRIVER, _T("driverVersion")); | 
|---|
| 216 | map_generic_boolean(ph, printer, port, DC_DUPLEX, _T("printDuplexSupported")); | 
|---|
| 217 | map_extents(ph, printer, port); | 
|---|
| 218 | map_resolution(ph, printer, port); | 
|---|
| 219 | map_orientation(ph, printer, port); | 
|---|
| 220 | map_generic_multi_sz(ph, printer, port, DC_PAPERNAMES, _T("printMediaSupported"), 64); | 
|---|
| 221 | #if (WINVER >= 0x0500) | 
|---|
| 222 | map_generic_boolean(ph, printer, port, DC_COLORDEVICE, _T("printColor")); | 
|---|
| 223 | map_generic_multi_sz(ph, printer, port, DC_PERSONALITY, _T("printLanguage"), 64); | 
|---|
| 224 | map_generic_multi_sz(ph, printer, port, DC_MEDIAREADY, _T("printMediaReady"),64); | 
|---|
| 225 | map_generic_dword(ph, printer, port, DC_PRINTERMEM, _T("printMemory")); | 
|---|
| 226 | map_generic_dword(ph, printer, port, DC_PRINTRATE, _T("printRate")); | 
|---|
| 227 | map_printrateunit(ph, printer, port); | 
|---|
| 228 | #ifdef DC_STAPLE | 
|---|
| 229 | map_generic_boolean(ph, printer, port, DC_STAPLE, _T("printStaplingSupported")); | 
|---|
| 230 | #endif | 
|---|
| 231 | #ifdef DC_PRINTRATEPPM | 
|---|
| 232 | map_generic_dword(ph, printer, port, DC_PRINTRATEPPM, _T("printPagesPerMinute")); | 
|---|
| 233 | #endif | 
|---|
| 234 | #endif | 
|---|
| 235 | SetPrinter(ph, 7, (BYTE *) &publish, 0); | 
|---|
| 236 | ClosePrinter(ph); | 
|---|
| 237 | return 0; | 
|---|
| 238 | } | 
|---|