/win32/src/win32print/win32print.cpp

https://bitbucket.org/jaraco/pywin32 · C++ · 2829 lines · 2445 code · 144 blank · 240 comment · 352 complexity · e109620777c9a665812db8ce8dbe814a MD5 · raw file

Large files are truncated click here to view the full file

  1. /***********************************************************
  2. win32printmodule.cpp -- module for interface into printer API
  3. Note that this source file contains embedded documentation.
  4. This documentation consists of marked up text inside the
  5. C comments, and is prefixed with an '@' symbol. The source
  6. files are processed by a tool called "autoduck" which
  7. generates Windows .hlp files.
  8. @doc
  9. ******************************************************************/
  10. #include "PyWinTypes.h"
  11. #include "PyWinObjects.h"
  12. #include <stdarg.h>
  13. #define CHECK_PFN(fname)if (pfn##fname==NULL) return PyErr_Format(PyExc_NotImplementedError,"%s is not available on this platform", #fname);
  14. typedef BOOL (WINAPI *EnumFormsfunc)(HANDLE,DWORD,LPBYTE,DWORD,LPDWORD,LPDWORD);
  15. static EnumFormsfunc pfnEnumForms=NULL;
  16. typedef BOOL (WINAPI *AddFormfunc)(HANDLE,DWORD,LPBYTE);
  17. static AddFormfunc pfnAddForm=NULL;
  18. typedef BOOL (WINAPI *DeleteFormfunc)(HANDLE, LPWSTR);
  19. static DeleteFormfunc pfnDeleteForm=NULL;
  20. typedef BOOL (WINAPI *GetFormfunc)(HANDLE,LPWSTR,DWORD,LPBYTE,DWORD,LPDWORD);
  21. static GetFormfunc pfnGetForm=NULL;
  22. typedef BOOL (WINAPI *SetFormfunc)(HANDLE, LPWSTR, DWORD, LPBYTE);
  23. static SetFormfunc pfnSetForm=NULL;
  24. typedef BOOL (WINAPI *AddJobfunc)(HANDLE,DWORD,LPBYTE,DWORD,LPDWORD);
  25. static AddJobfunc pfnAddJob=NULL;
  26. typedef BOOL (WINAPI *ScheduleJobfunc)(HANDLE, DWORD);
  27. static ScheduleJobfunc pfnScheduleJob=NULL;
  28. typedef BOOL (WINAPI * EnumPortsfunc)(LPWSTR,DWORD,LPBYTE,DWORD,LPDWORD,LPDWORD);
  29. static EnumPortsfunc pfnEnumPorts=NULL;
  30. static EnumPortsfunc pfnEnumMonitors=NULL; // same args as EnumPorts
  31. typedef BOOL (WINAPI *GetPrintProcessorDirectoryfunc)(LPWSTR,LPWSTR,DWORD,LPBYTE,DWORD,LPDWORD);
  32. static GetPrintProcessorDirectoryfunc pfnGetPrintProcessorDirectory=NULL;
  33. static GetPrintProcessorDirectoryfunc pfnGetPrinterDriverDirectory=NULL; // same as GetPrintProcessorDirectory
  34. typedef BOOL (WINAPI *DeletePrinterDriverExfunc)(LPWSTR, LPWSTR, LPWSTR, DWORD, DWORD);
  35. static DeletePrinterDriverExfunc pfnDeletePrinterDriverEx=NULL;
  36. typedef BOOL (WINAPI *FlushPrinterfunc)(HANDLE, LPVOID, DWORD, LPDWORD, DWORD);
  37. static FlushPrinterfunc pfnFlushPrinter=NULL;
  38. typedef BOOL (WINAPI *GetDefaultPrinterfunc)(LPWSTR, LPDWORD);
  39. static GetDefaultPrinterfunc pfnGetDefaultPrinter=NULL;
  40. typedef BOOL (WINAPI *SetDefaultPrinterfunc)(LPWSTR);
  41. static SetDefaultPrinterfunc pfnSetDefaultPrinter=NULL;
  42. static PyObject *dummy_tuple=NULL;
  43. // To be used in PyArg_ParseTuple with O& format
  44. BOOL PyWinObject_AsPrinterHANDLE(PyObject *obhprinter, HANDLE *phprinter){
  45. return PyWinObject_AsHANDLE(obhprinter, phprinter);
  46. }
  47. // @object PyPrinterHANDLE|Handle to a printer or print server.
  48. // <nl>Created using <om win32print.OpenPrinter> or <om win32print.AddPrinter>
  49. // <nl>Inherits all methods and properties of <o PyHANDLE>.
  50. // <nl>When object is destroyed, handle is released using ClosePrinter.
  51. class PyPrinterHANDLE: public PyHANDLE
  52. {
  53. public:
  54. PyPrinterHANDLE(HANDLE hInit) : PyHANDLE(hInit) {}
  55. virtual BOOL Close(void){
  56. BOOL ret=ClosePrinter(m_handle);
  57. if (!ret)
  58. PyWin_SetAPIError("ClosePrinter");
  59. m_handle = 0;
  60. return ret;
  61. }
  62. virtual const char *GetTypeName(){
  63. return "PyPrinterHANDLE";
  64. }
  65. };
  66. PyObject *PyWinObject_FromPrinterHANDLE(HANDLE hprinter)
  67. {
  68. PyObject *ret=new PyPrinterHANDLE(hprinter);
  69. if (ret==NULL)
  70. PyErr_NoMemory();
  71. return ret;
  72. }
  73. void PyWinObject_FreePRINTER_DEFAULTS(PPRINTER_DEFAULTS pdefaults)
  74. {
  75. PyWinObject_FreeTCHAR(pdefaults->pDatatype);
  76. }
  77. // @object PRINTER_DEFAULTS|A dictionary representing a PRINTER_DEFAULTS structure
  78. // @prop string|pDatatype|Data type to be used for print jobs, see <om win32print.EnumPrintProcessorDatatypes>, optional, can be None
  79. // @prop <o PyDEVMODE>|pDevMode|A PyDEVMODE that specifies default printer parameters, optional, can be None
  80. // @prop int|DesiredAccess|An ACCESS_MASK specifying what level of access is needed, eg PRINTER_ACCESS_ADMINISTER, PRINTER_ACCESS_USE
  81. BOOL PyWinObject_AsPRINTER_DEFAULTS(PyObject *obdefaults, PPRINTER_DEFAULTS pdefaults)
  82. {
  83. static char *printer_default_keys[]={"DesiredAccess","pDataType","pDevMode",NULL};
  84. static char *printer_default_format="k|OO";
  85. ZeroMemory(pdefaults,sizeof(PRINTER_DEFAULTS));
  86. PyObject *obDataType=Py_None, *obdevmode=Py_None;
  87. if (!PyDict_Check(obdefaults)){
  88. PyErr_SetString(PyExc_TypeError, "PRINTER_DEFAULTS must be a dictionary");
  89. return FALSE;
  90. }
  91. return PyArg_ParseTupleAndKeywords(dummy_tuple,obdefaults,printer_default_format,printer_default_keys,
  92. &pdefaults->DesiredAccess, &pdefaults->pDatatype, &obdevmode)
  93. &&PyWinObject_AsDEVMODE(obdevmode, &pdefaults->pDevMode, TRUE)
  94. &&PyWinObject_AsTCHAR(obDataType, &pdefaults->pDatatype, TRUE);
  95. }
  96. // Printer stuff.
  97. // @pymethod <o PyPrinterHANDLE>|win32print|OpenPrinter|Retrieves a handle to a printer.
  98. static PyObject *PyOpenPrinter(PyObject *self, PyObject *args)
  99. {
  100. TCHAR *printer;
  101. HANDLE handle;
  102. PRINTER_DEFAULTS printer_defaults = {NULL, NULL, 0};
  103. PRINTER_DEFAULTS *pprinter_defaults=NULL;
  104. PyObject *obprinter, *obdefaults=Py_None, *ret=NULL;
  105. if (!PyArg_ParseTuple(args, "O|O:OpenPrinter",
  106. &obprinter, // @pyparm string|printer||Printer or print server name. Use None to open local print server.
  107. &obdefaults)) // @pyparm dict|Defaults|None|<o PRINTER_DEFAULTS> dict, or None
  108. return NULL;
  109. if (obdefaults!=Py_None){
  110. if (!PyWinObject_AsPRINTER_DEFAULTS(obdefaults, &printer_defaults))
  111. return NULL;
  112. pprinter_defaults=&printer_defaults;
  113. }
  114. if (PyWinObject_AsTCHAR(obprinter, &printer, TRUE)){
  115. if (OpenPrinter(printer, &handle, pprinter_defaults))
  116. ret=PyWinObject_FromPrinterHANDLE(handle);
  117. else
  118. PyWin_SetAPIError("OpenPrinter");
  119. }
  120. PyWinObject_FreePRINTER_DEFAULTS(&printer_defaults);
  121. PyWinObject_FreeTCHAR(printer);
  122. return ret;
  123. }
  124. // @pymethod |win32print|ClosePrinter|Closes a handle to a printer.
  125. static PyObject *PyClosePrinter(PyObject *self, PyObject *args)
  126. {
  127. PyObject *obhprinter;
  128. if (!PyArg_ParseTuple(args, "O:ClosePrinter",
  129. &obhprinter)) // @pyparm <o PyPrinterHANDLE>|hPrinter||handle to printer object
  130. return NULL;
  131. /* If the object is a PyPrinterHANDLE, its Close method must be called to ensure that the m_handle member is cleared.
  132. A second handle with the same value can be created as soon as the first handle is closed here, and if
  133. this happens between the time this function is executed and the first object is deref'ed, the original object's
  134. destruction would close a valid handle contained in the second object. */
  135. if (PyHANDLE_Check(obhprinter)){
  136. // Make sure we can't Close any other type of handle
  137. const char *handletype=((PyHANDLE *)obhprinter)->GetTypeName();
  138. if (strcmp(handletype, "PyPrinterHANDLE")!=0)
  139. return PyErr_Format(PyExc_TypeError, "ClosePrinter: Object must be a printer handle, not %s", handletype);
  140. if (((PyHANDLE *)obhprinter)->Close()){
  141. Py_INCREF(Py_None);
  142. return Py_None;
  143. }
  144. return NULL;
  145. }
  146. HANDLE hprinter;
  147. if (!PyWinObject_AsPrinterHANDLE(obhprinter, &hprinter))
  148. return NULL;
  149. if (!ClosePrinter(hprinter))
  150. return PyWin_SetAPIError("ClosePrinter");
  151. Py_INCREF(Py_None);
  152. return Py_None;
  153. }
  154. static PyObject *PyWinObject_FromPRINTER_INFO(LPBYTE printer_info, DWORD level)
  155. {
  156. switch (level){
  157. case 1:
  158. PRINTER_INFO_1 *pi1;
  159. pi1=(PRINTER_INFO_1 *)printer_info;
  160. return Py_BuildValue("{s:k,s:N,s:N,s:N}",
  161. "Flags",pi1->Flags,
  162. "pDescription",PyWinObject_FromTCHAR(pi1->pDescription),
  163. "pName",PyWinObject_FromTCHAR(pi1->pName),
  164. "pComment",PyWinObject_FromTCHAR(pi1->pComment));
  165. case 2:
  166. PRINTER_INFO_2 *pi2;
  167. pi2=(PRINTER_INFO_2 *)printer_info;
  168. return Py_BuildValue("{s:N,s:N,s:N,s:N,s:N,s:N,s:N,s:N,s:N,s:N,s:N,s:N,s:N,s:k,s:k,s:k,s:k,s:k,s:k,s:k,s:k}",
  169. "pServerName",PyWinObject_FromTCHAR(pi2->pServerName),
  170. "pPrinterName",PyWinObject_FromTCHAR(pi2->pPrinterName),
  171. "pShareName",PyWinObject_FromTCHAR(pi2->pShareName),
  172. "pPortName",PyWinObject_FromTCHAR(pi2->pPortName),
  173. "pDriverName",PyWinObject_FromTCHAR(pi2->pDriverName),
  174. "pComment",PyWinObject_FromTCHAR(pi2->pComment),
  175. "pLocation",PyWinObject_FromTCHAR(pi2->pLocation),
  176. "pDevMode",PyWinObject_FromDEVMODE(pi2->pDevMode),
  177. "pSepFile", PyWinObject_FromTCHAR(pi2->pSepFile),
  178. "pPrintProcessor",PyWinObject_FromTCHAR(pi2->pPrintProcessor),
  179. "pDatatype",PyWinObject_FromTCHAR(pi2->pDatatype),
  180. "pParameters",PyWinObject_FromTCHAR(pi2->pParameters),
  181. "pSecurityDescriptor",PyWinObject_FromSECURITY_DESCRIPTOR(pi2->pSecurityDescriptor),
  182. "Attributes",pi2->Attributes, "Priority",pi2->Priority,
  183. "DefaultPriority",pi2->DefaultPriority,
  184. "StartTime",pi2->StartTime, "UntilTime",pi2->UntilTime,
  185. "Status",pi2->Status, "cJobs",pi2->cJobs, "AveragePPM",pi2->AveragePPM);
  186. case 3:
  187. PRINTER_INFO_3 *pi3;
  188. pi3=(PRINTER_INFO_3 *)printer_info;
  189. return Py_BuildValue("{s:N}","pSecurityDescriptor",PyWinObject_FromSECURITY_DESCRIPTOR(pi3->pSecurityDescriptor));
  190. case 4:
  191. PRINTER_INFO_4 *pi4;
  192. pi4=(PRINTER_INFO_4 *)printer_info;
  193. return Py_BuildValue("{s:N,s:N,s:k}",
  194. "pPrinterName",PyWinObject_FromTCHAR(pi4->pPrinterName),
  195. "pServerName",PyWinObject_FromTCHAR(pi4->pServerName),
  196. "Attributes",pi4->Attributes);
  197. case 5:
  198. PRINTER_INFO_5 *pi5;
  199. pi5=(PRINTER_INFO_5 *)printer_info;
  200. return Py_BuildValue("{s:N,s:N,s:k,s:k,s:k}",
  201. "pPrinterName",PyWinObject_FromTCHAR(pi5->pPrinterName),
  202. "pPortName",PyWinObject_FromTCHAR(pi5->pPortName),
  203. "Attributes",pi5->Attributes,
  204. "DeviceNotSelectedTimeout",pi5->DeviceNotSelectedTimeout,
  205. "TransmissionRetryTimeout",pi5->TransmissionRetryTimeout);
  206. case 7:
  207. PRINTER_INFO_7 *pi7;
  208. pi7=(PRINTER_INFO_7 *)printer_info;
  209. return Py_BuildValue("{s:N,s:k}",
  210. "ObjectGUID",PyWinObject_FromTCHAR(pi7->pszObjectGUID),
  211. "Action",pi7->dwAction);
  212. case 8: // global printer defaults
  213. PRINTER_INFO_8 *pi8;
  214. pi8=(PRINTER_INFO_8 *)printer_info;
  215. return Py_BuildValue("{s:N}","pDevMode", PyWinObject_FromDEVMODE(pi8->pDevMode));
  216. case 9: // per user printer defaults
  217. PRINTER_INFO_9 *pi9;
  218. pi9=(PRINTER_INFO_9 *)printer_info;
  219. return Py_BuildValue("{s:N}","pDevMode", PyWinObject_FromDEVMODE(pi9->pDevMode));
  220. default:
  221. return PyErr_Format(PyExc_NotImplementedError,"Level %d is not supported",level);
  222. }
  223. }
  224. // @pymethod dict|win32print|GetPrinter|Retrieves information about a printer
  225. // @rdesc Returns a dictionary containing PRINTER_INFO_* data for level, or
  226. // returns a tuple of PRINTER_INFO_2 data if no level is passed in.
  227. static PyObject *PyGetPrinter(PyObject *self, PyObject *args)
  228. {
  229. HANDLE hprinter;
  230. DWORD needed, level;
  231. BOOL backward_compat;
  232. LPBYTE buf=NULL;
  233. PyObject *rc=NULL;
  234. PRINTER_INFO_2 *pi2;
  235. // @comm Original implementation used level 2 only and returned a tuple
  236. // Pass single arg as indicator to use old behaviour for backward compatibility
  237. if (PyArg_ParseTuple(args, "O&:GetPrinter",
  238. PyWinObject_AsPrinterHANDLE, &hprinter)){ // @pyparm <o PyPrinterHANDLE>|hPrinter||handle to printer object as returned by <om win32print.OpenPrinter>
  239. backward_compat=TRUE;
  240. level=2;
  241. }
  242. else{
  243. PyErr_Clear();
  244. if (!PyArg_ParseTuple(args, "O&k:GetPrinter",
  245. PyWinObject_AsPrinterHANDLE, &hprinter,
  246. &level)) // @pyparm int|Level|2|Level of data returned (1,2,3,4,5,7,8,9)
  247. return NULL;
  248. backward_compat=FALSE;
  249. }
  250. // first allocate memory.
  251. GetPrinter(hprinter, level, NULL, 0, &needed );
  252. if (GetLastError()!=ERROR_INSUFFICIENT_BUFFER)
  253. return PyWin_SetAPIError("GetPrinter");
  254. buf=(LPBYTE)malloc(needed);
  255. if (buf==NULL)
  256. return PyErr_Format(PyExc_MemoryError,"GetPrinter: Unable to allocate buffer of %d bytes", needed);
  257. if (!GetPrinter(hprinter, level, buf, needed, &needed )) {
  258. free(buf);
  259. return PyWin_SetAPIError("GetPrinter");
  260. }
  261. if (backward_compat){
  262. pi2=(PRINTER_INFO_2 *)buf;
  263. rc = Py_BuildValue("NNNNNNNONNNNOkkkkkkkk",
  264. PyWinObject_FromTCHAR(pi2->pServerName),
  265. PyWinObject_FromTCHAR(pi2->pPrinterName),
  266. PyWinObject_FromTCHAR(pi2->pShareName),
  267. PyWinObject_FromTCHAR(pi2->pPortName),
  268. PyWinObject_FromTCHAR(pi2->pDriverName),
  269. PyWinObject_FromTCHAR(pi2->pComment),
  270. PyWinObject_FromTCHAR(pi2->pLocation),
  271. Py_None,
  272. PyWinObject_FromTCHAR(pi2->pSepFile),
  273. PyWinObject_FromTCHAR(pi2->pPrintProcessor),
  274. PyWinObject_FromTCHAR(pi2->pDatatype),
  275. PyWinObject_FromTCHAR(pi2->pParameters),
  276. Py_None,
  277. pi2->Attributes, pi2->Priority, pi2->DefaultPriority, pi2->StartTime, pi2->UntilTime,
  278. pi2->Status, pi2->cJobs, pi2->AveragePPM);
  279. }
  280. else
  281. rc = PyWinObject_FromPRINTER_INFO(buf, level);
  282. free(buf);
  283. return rc;
  284. }
  285. void PyWinObject_FreePRINTER_INFO(DWORD level, LPBYTE pbuf)
  286. {
  287. if ((level==0) || (pbuf==NULL))
  288. return;
  289. switch(level){
  290. case 2:{
  291. PRINTER_INFO_2 *pi2 = (PRINTER_INFO_2 *)pbuf;
  292. PyWinObject_FreeTCHAR(pi2->pServerName);
  293. PyWinObject_FreeTCHAR(pi2->pPrinterName);
  294. PyWinObject_FreeTCHAR(pi2->pShareName);
  295. PyWinObject_FreeTCHAR(pi2->pPortName);
  296. PyWinObject_FreeTCHAR(pi2->pDriverName);
  297. PyWinObject_FreeTCHAR(pi2->pComment);
  298. PyWinObject_FreeTCHAR(pi2->pLocation);
  299. PyWinObject_FreeTCHAR(pi2->pSepFile);
  300. PyWinObject_FreeTCHAR(pi2->pPrintProcessor);
  301. PyWinObject_FreeTCHAR(pi2->pDatatype);
  302. PyWinObject_FreeTCHAR(pi2->pParameters);
  303. break;
  304. }
  305. case 4:{
  306. PRINTER_INFO_4 *pi4 = (PRINTER_INFO_4 *)pbuf;
  307. PyWinObject_FreeTCHAR(pi4->pPrinterName);
  308. PyWinObject_FreeTCHAR(pi4->pServerName);
  309. break;
  310. }
  311. case 5:{
  312. PRINTER_INFO_5 *pi5 = (PRINTER_INFO_5 *)pbuf;
  313. PyWinObject_FreeTCHAR(pi5->pPrinterName);
  314. PyWinObject_FreeTCHAR(pi5->pPortName);
  315. break;
  316. }
  317. case 7:{
  318. PRINTER_INFO_7 *pi7 = (PRINTER_INFO_7 *)pbuf;
  319. PyWinObject_FreeTCHAR(pi7->pszObjectGUID);
  320. break;
  321. }
  322. default:
  323. break;
  324. }
  325. free(pbuf);
  326. }
  327. BOOL PyWinObject_AsPRINTER_INFO(DWORD level, PyObject *obinfo, LPBYTE *pbuf)
  328. {
  329. BOOL ret=FALSE;
  330. size_t bufsize;
  331. *pbuf=NULL;
  332. if (level==0)
  333. if (obinfo==Py_None)
  334. return TRUE;
  335. else{
  336. *pbuf = (LPBYTE)PyInt_AsLong(obinfo);
  337. if ((*pbuf==(LPBYTE)-1)&&PyErr_Occurred()){
  338. PyErr_Clear();
  339. PyErr_SetString(PyExc_TypeError,"Info must be None or a PRINTER_STATUS_* integer when level is 0.");
  340. return FALSE;
  341. }
  342. return TRUE;
  343. }
  344. if (!PyDict_Check (obinfo)){
  345. PyErr_Format(PyExc_TypeError, "PRINTER_INFO_%d must be a dictionary", level);
  346. return FALSE;
  347. }
  348. switch(level){
  349. case 2:{
  350. static char *pi2_keys[]={"pServerName","pPrinterName","pShareName","pPortName",
  351. "pDriverName","pComment","pLocation","pDevMode","pSepFile","pPrintProcessor",
  352. "pDatatype","pParameters","pSecurityDescriptor","Attributes","Priority",
  353. "DefaultPriority","StartTime","UntilTime","Status","cJobs","AveragePPM", NULL};
  354. static char *pi2_format="OOOOOOOOOOOOOkkkkkkkk:PRINTER_INFO_2";
  355. PyObject *obServerName=Py_None, *obPrinterName=Py_None, *obShareName=Py_None,
  356. *obPortName=Py_None, *obDriverName=Py_None, *obComment=Py_None,
  357. *obLocation=Py_None, *obDevMode=Py_None,
  358. *obSepFile=Py_None, *obPrintProcessor=Py_None,
  359. *obDatatype=Py_None, *obParameters=Py_None, *obSecurityDescriptor=Py_None;
  360. PRINTER_INFO_2 *pi2;
  361. bufsize=sizeof(PRINTER_INFO_2);
  362. if (NULL == (*pbuf= (LPBYTE)malloc(bufsize))){
  363. PyErr_Format(PyExc_MemoryError, "Malloc failed for %d bytes", bufsize);
  364. break;
  365. }
  366. ZeroMemory(*pbuf,bufsize);
  367. pi2=(PRINTER_INFO_2 *)*pbuf;
  368. ret=PyArg_ParseTupleAndKeywords(dummy_tuple, obinfo, pi2_format, pi2_keys,
  369. &obServerName, &obPrinterName, &obShareName, &obPortName,
  370. &obDriverName, &obComment, &obLocation,
  371. &obDevMode,
  372. &obSepFile, &obPrintProcessor, &obDatatype, &obParameters,
  373. &obSecurityDescriptor,
  374. &pi2->Attributes, &pi2->Priority, &pi2->DefaultPriority, &pi2->StartTime,
  375. &pi2->UntilTime, &pi2->Status, &pi2->cJobs, &pi2->AveragePPM)
  376. &&PyWinObject_AsTCHAR(obServerName, &pi2->pServerName, TRUE)
  377. &&PyWinObject_AsTCHAR(obPrinterName, &pi2->pPrinterName, TRUE)
  378. &&PyWinObject_AsTCHAR(obShareName, &pi2->pShareName, TRUE)
  379. &&PyWinObject_AsTCHAR(obPortName, &pi2->pPortName, TRUE)
  380. &&PyWinObject_AsTCHAR(obDriverName, &pi2->pDriverName, TRUE)
  381. &&PyWinObject_AsTCHAR(obComment, &pi2->pComment, TRUE)
  382. &&PyWinObject_AsTCHAR(obLocation, &pi2->pLocation, TRUE)
  383. &&PyWinObject_AsDEVMODE(obDevMode, &pi2->pDevMode,FALSE)
  384. &&PyWinObject_AsTCHAR(obSepFile, &pi2->pSepFile, TRUE)
  385. &&PyWinObject_AsTCHAR(obPrintProcessor, &pi2->pPrintProcessor, TRUE)
  386. &&PyWinObject_AsTCHAR(obDatatype, &pi2->pDatatype, TRUE)
  387. &&PyWinObject_AsTCHAR(obParameters, &pi2->pParameters, TRUE)
  388. &&PyWinObject_AsSECURITY_DESCRIPTOR(obSecurityDescriptor, &pi2->pSecurityDescriptor, TRUE);
  389. break;
  390. }
  391. case 3:{
  392. static char *pi3_keys[]={"pSecurityDescriptor", NULL};
  393. static char *pi3_format="O:PRINTER_INFO_3";
  394. PyObject *obSecurityDescriptor;
  395. PRINTER_INFO_3 *pi3;
  396. bufsize=sizeof(PRINTER_INFO_3);
  397. if (NULL == (*pbuf=(LPBYTE)malloc(bufsize))){
  398. PyErr_Format(PyExc_MemoryError, "Malloc failed for %d bytes", bufsize);
  399. break;
  400. }
  401. ZeroMemory(*pbuf,bufsize);
  402. pi3=(PRINTER_INFO_3 *)*pbuf;
  403. ret=PyArg_ParseTupleAndKeywords(dummy_tuple, obinfo, pi3_format, pi3_keys, &obSecurityDescriptor)
  404. &&PyWinObject_AsSECURITY_DESCRIPTOR(obSecurityDescriptor, &pi3->pSecurityDescriptor, FALSE);
  405. break;
  406. }
  407. case 4:{
  408. static char *pi4_keys[]={"pPrinterName","pServerName","Attributes", NULL};
  409. static char *pi4_format="OOk:PRINTER_INFO_4";
  410. PyObject *obPrinterName=Py_None, *obServerName=Py_None;
  411. PRINTER_INFO_4 *pi4;
  412. bufsize=sizeof(PRINTER_INFO_4);
  413. if (NULL == (*pbuf=(LPBYTE)malloc(bufsize))){
  414. PyErr_Format(PyExc_MemoryError, "Malloc failed for %d bytes", bufsize);
  415. break;
  416. }
  417. ZeroMemory(*pbuf,bufsize);
  418. pi4=(PRINTER_INFO_4 *)*pbuf;
  419. ret=PyArg_ParseTupleAndKeywords(dummy_tuple, obinfo, pi4_format, pi4_keys,
  420. &obPrinterName, &obServerName, &pi4->Attributes)
  421. &&PyWinObject_AsTCHAR(obPrinterName, &pi4->pPrinterName, TRUE)
  422. &&PyWinObject_AsTCHAR(obServerName, &pi4->pServerName, TRUE);
  423. break;
  424. }
  425. case 5:{
  426. static char *pi5_keys[]={"pPrinterName","pPortName","Attributes",
  427. "DeviceNotSelectedTimeout","TransmissionRetryTimeout", NULL};
  428. static char *pi5_format="OOkkk:PRINTER_INFO_5";
  429. PyObject *obPrinterName=Py_None, *obPortName=Py_None;
  430. PRINTER_INFO_5 *pi5;
  431. bufsize=sizeof(PRINTER_INFO_5);
  432. if (NULL == (*pbuf=(LPBYTE)malloc(bufsize))){
  433. PyErr_Format(PyExc_MemoryError, "Malloc failed for %d bytes", bufsize);
  434. break;
  435. }
  436. ZeroMemory(*pbuf,bufsize);
  437. pi5=(PRINTER_INFO_5 *)*pbuf;
  438. ret=PyArg_ParseTupleAndKeywords(dummy_tuple, obinfo, pi5_format, pi5_keys,
  439. &obPrinterName, &obPortName, &pi5->Attributes,
  440. &pi5->DeviceNotSelectedTimeout, &pi5->TransmissionRetryTimeout)
  441. &&PyWinObject_AsTCHAR(obPrinterName, &pi5->pPrinterName, TRUE)
  442. &&PyWinObject_AsTCHAR(obPortName, &pi5->pPortName, TRUE);
  443. break;
  444. }
  445. case 7:{
  446. static char *pi7_keys[]={"ObjectGUID","Action", NULL};
  447. static char *pi7_format="Ok:PRINTER_INFO_7";
  448. PyObject *obObjectGUID=Py_None;
  449. PRINTER_INFO_7 *pi7;
  450. bufsize=sizeof(PRINTER_INFO_7);
  451. if (NULL == (*pbuf=(LPBYTE)malloc(bufsize))){
  452. PyErr_Format(PyExc_MemoryError, "Malloc failed for %d bytes", bufsize);
  453. break;
  454. }
  455. ZeroMemory(*pbuf,bufsize);
  456. pi7=(PRINTER_INFO_7 *)*pbuf;
  457. ret=PyArg_ParseTupleAndKeywords(dummy_tuple, obinfo, pi7_format, pi7_keys,
  458. &obObjectGUID, &pi7->dwAction)
  459. &&PyWinObject_AsTCHAR(obObjectGUID, &pi7->pszObjectGUID, TRUE);
  460. break;
  461. }
  462. case 8:
  463. case 9:{ //identical structs, 8 is for global defaults and 9 is for user defaults
  464. static char *pi8_keys[]={"pDevMode", NULL};
  465. static char *pi8_format="O:PRINTER_INFO_8";
  466. PyObject *obDevMode;
  467. PRINTER_INFO_8 *pi8;
  468. bufsize=sizeof(PRINTER_INFO_8);
  469. if (NULL == (*pbuf=(LPBYTE)malloc(bufsize))){
  470. PyErr_Format(PyExc_MemoryError, "Malloc failed for %d bytes", bufsize);
  471. break;
  472. }
  473. ZeroMemory(*pbuf,bufsize);
  474. pi8=(PRINTER_INFO_8 *)*pbuf;
  475. ret=PyArg_ParseTupleAndKeywords(dummy_tuple, obinfo, pi8_format, pi8_keys, &obDevMode)
  476. &&PyWinObject_AsDEVMODE(obDevMode,&pi8->pDevMode,FALSE);
  477. break;
  478. }
  479. default:
  480. PyErr_Format(PyExc_NotImplementedError,"Information level %d is not supported", level);
  481. }
  482. if (!ret){
  483. if ((*pbuf!=NULL) && (level!=0))
  484. free(*pbuf);
  485. *pbuf=NULL;
  486. }
  487. return ret;
  488. }
  489. // @pymethod |win32print|SetPrinter|Change printer configuration and status
  490. static PyObject *PySetPrinter(PyObject *self, PyObject *args)
  491. {
  492. HANDLE hprinter;
  493. LPBYTE buf=NULL;
  494. DWORD level, command;
  495. PyObject *obinfo=NULL, *ret=NULL;
  496. // @pyparm <o PyPrinterHANDLE>|hPrinter||Printer handle as returned by <om win32print.OpenPrinter>
  497. // @pyparm int|Level||Level of data contained in pPrinter
  498. // @pyparm dict|pPrinter||PRINTER_INFO_* dict as returned by <om win32print.GetPrinter>, can be None if level is 0
  499. // @pyparm int|Command||Command to send to printer - one of the PRINTER_CONTROL_* constants, or 0
  500. // @comm If Level is 0 and Command is PRINTER_CONTROL_SET_STATUS, pPrinter should be an integer,
  501. // and is interpreted as the new printer status to set (one of the PRINTER_STATUS_* constants).
  502. if (!PyArg_ParseTuple(args, "O&kOk:SetPrinter",
  503. PyWinObject_AsPrinterHANDLE, &hprinter, &level, &obinfo, &command))
  504. return NULL;
  505. if (!PyWinObject_AsPRINTER_INFO(level, obinfo, &buf))
  506. return NULL;
  507. if (!SetPrinter(hprinter, level, buf, command))
  508. PyWin_SetAPIError("SetPrinter");
  509. else{
  510. Py_INCREF(Py_None);
  511. ret=Py_None;
  512. }
  513. PyWinObject_FreePRINTER_INFO(level, buf);
  514. return ret;
  515. }
  516. // @pymethod None|win32print|AddPrinterConnection|Connects to remote printer
  517. static PyObject *PyAddPrinterConnection(PyObject *self, PyObject *args)
  518. {
  519. TCHAR *printer;
  520. PyObject *obprinter;
  521. if (!PyArg_ParseTuple(args, "O:AddPrinterConnection",
  522. &obprinter)) // @pyparm string|printer||printer to connect to (eg: \\server\printer).
  523. return NULL;
  524. if (!PyWinObject_AsTCHAR(obprinter, &printer, FALSE))
  525. return NULL;
  526. BOOL bsuccess=AddPrinterConnection(printer);
  527. PyWinObject_FreeTCHAR(printer);
  528. if (!bsuccess)
  529. return PyWin_SetAPIError("AddPrinterConnection");
  530. Py_INCREF(Py_None);
  531. return Py_None;
  532. }
  533. // @pymethod None|win32print|DeletePrinterConnection|Removes connection to remote printer
  534. static PyObject *PyDeletePrinterConnection(PyObject *self, PyObject *args)
  535. {
  536. TCHAR *printer;
  537. PyObject *obprinter;
  538. if (!PyArg_ParseTuple(args, "O:DeletePrinterConnection",
  539. &obprinter)) // @pyparm string|printer||printer to disconnect from (eg: \\server\printer).
  540. return NULL;
  541. if (!PyWinObject_AsTCHAR(obprinter, &printer, FALSE))
  542. return NULL;
  543. BOOL bsuccess=DeletePrinterConnection(printer);
  544. PyWinObject_FreeTCHAR(printer);
  545. if (!bsuccess)
  546. return PyWin_SetAPIError("DeletePrinterConnection");
  547. Py_INCREF(Py_None);
  548. return Py_None;
  549. }
  550. // @pymethod string|win32print|GetDefaultPrinter|Returns the default printer.
  551. static PyObject *PyGetDefaultPrinter(PyObject *self, PyObject *args)
  552. {
  553. TCHAR *printer, *s;
  554. int printer_size= 100;
  555. /* Windows < 2000 does not have a GetDefaultPrinter so the default printer
  556. must be retrieved from registry */
  557. if (NULL == (printer= (TCHAR *)malloc(printer_size * sizeof(TCHAR))))
  558. {
  559. PyErr_SetString(PyExc_MemoryError, "Malloc failed.");
  560. return NULL;
  561. }
  562. if (0 == GetProfileString(TEXT("Windows"), TEXT("Device"), TEXT(""), printer, printer_size))
  563. {
  564. PyErr_SetString(PyExc_RuntimeError, "The default printer was not found.");
  565. return NULL;
  566. }
  567. if (NULL == (s= _tcschr(printer, TEXT(','))))
  568. {
  569. PyErr_SetString(PyExc_RuntimeError, "The returned printer is malformed.");
  570. return NULL;
  571. }
  572. *s= 0;
  573. PyObject *ret= PyWinObject_FromTCHAR(printer);
  574. free(printer);
  575. return ret;
  576. }
  577. // @pymethod <o PyUnicode>|win32print|GetDefaultPrinterW|Returns the default printer.
  578. // @comm Unlike <om win32print.GetDefaultPrinter>, this method calls the GetDefaultPrinter API function.
  579. static PyObject *PyGetDefaultPrinterW(PyObject *self, PyObject *args)
  580. {
  581. CHECK_PFN(GetDefaultPrinter);
  582. WCHAR *printer=NULL;
  583. DWORD err, printer_size=100;
  584. PyObject *ret=NULL;
  585. printer= (WCHAR *)malloc(printer_size*sizeof(WCHAR));
  586. if (printer==NULL)
  587. return PyErr_Format(PyExc_MemoryError, "Unable to allocate %d bytes", printer_size*sizeof(WCHAR));
  588. if (!(*pfnGetDefaultPrinter)(printer, &printer_size)){
  589. err=GetLastError();
  590. if (err!=ERROR_INSUFFICIENT_BUFFER){
  591. PyWin_SetAPIError("GetDefaultPrinter");
  592. goto done;
  593. }
  594. free(printer);
  595. printer=(WCHAR *)malloc(printer_size*sizeof(WCHAR));
  596. if (printer==NULL)
  597. return PyErr_Format(PyExc_MemoryError, "Unable to allocate %d bytes", printer_size*sizeof(WCHAR));
  598. if (!(*pfnGetDefaultPrinter)(printer, &printer_size)){
  599. PyWin_SetAPIError("GetDefaultPrinter");
  600. goto done;
  601. }
  602. }
  603. ret=PyWinObject_FromWCHAR(printer);
  604. done:
  605. if (printer)
  606. free(printer);
  607. return ret;
  608. }
  609. // @pymethod None|win32print|SetDefaultPrinter|Sets the default printer.
  610. // @comm This function uses the pre-win2k method of WriteProfileString rather than the SetDefaultPrinter API function
  611. static PyObject *PySetDefaultPrinter(PyObject *self, PyObject *args)
  612. {
  613. TCHAR *printer=NULL, *info=NULL, *dprinter=NULL;
  614. int info_size= 100;
  615. PyObject *obprinter;
  616. /* Windows < 2000 does not have a SetDefaultPrinter so the default printer
  617. must be set in the registry */
  618. if (!PyArg_ParseTuple(args, "O:SetDefaultPrinter",
  619. &obprinter)) // @pyparm string|printer||printer to set as default
  620. return NULL;
  621. if (!PyWinObject_AsTCHAR(obprinter, &printer, FALSE))
  622. return NULL;
  623. if (NULL == (info= (TCHAR *)malloc(info_size *sizeof(TCHAR))))
  624. PyErr_NoMemory();
  625. else if (0 == GetProfileString(TEXT("Devices"), printer, TEXT(""), info, info_size))
  626. PyErr_SetString(PyExc_RuntimeError, "The printer was not found.");
  627. else if (NULL == (dprinter= (TCHAR *)malloc((_tcslen(printer) + _tcslen(info) + 3) * sizeof(TCHAR))))
  628. PyErr_NoMemory();
  629. else{
  630. _tcscpy(dprinter, printer);
  631. _tcscat(dprinter, TEXT(","));
  632. _tcscat(dprinter, info);
  633. WriteProfileString(TEXT("Windows"), TEXT("device"), dprinter);
  634. SendNotifyMessage(HWND_BROADCAST,WM_SETTINGCHANGE,0,0);
  635. }
  636. if (dprinter)
  637. free(dprinter);
  638. if (info)
  639. free(info);
  640. PyWinObject_FreeTCHAR(printer);
  641. Py_INCREF(Py_None);
  642. return Py_None;
  643. }
  644. // @pymethod None|win32print|SetDefaultPrinterW|Sets the default printer
  645. // @comm Unlike <om win32print.SetDefaultPrinter>, this method calls the SetDefaultPrinter API function.
  646. static PyObject *PySetDefaultPrinterW(PyObject *self, PyObject *args)
  647. {
  648. CHECK_PFN(SetDefaultPrinter);
  649. WCHAR *printer=NULL;
  650. PyObject *obprinter, *ret=NULL;
  651. // @pyparm <o PyUnicode>|Printer||Name of printer, can be None to use first available printer
  652. if (!PyArg_ParseTuple(args, "O:SetDefaultPrinter", &obprinter))
  653. return NULL;
  654. if (!PyWinObject_AsWCHAR(obprinter, &printer, TRUE))
  655. return NULL;
  656. if (!(*pfnSetDefaultPrinter)(printer))
  657. PyWin_SetAPIError("SetDefaultPrinter");
  658. else{
  659. Py_INCREF(Py_None);
  660. ret = Py_None;
  661. }
  662. PyWinObject_FreeWCHAR(printer);
  663. return ret;
  664. }
  665. // @pymethod tuple|win32print|EnumPrinters|Enumerates printers, print servers, domains and print providers.
  666. // @comm Use Flags=PRINTER_ENUM_NAME, Name=None, Level=1 to enumerate print providers.<nl>
  667. // Use Flags=PRINTER_ENUM_NAME, Name=\\servername, Level=2 or 5 to list printers on another server.<nl>
  668. // See MSDN docs for EnumPrinters for other specific combinations
  669. static PyObject *PyEnumPrinters(PyObject *self, PyObject *args)
  670. {
  671. DWORD flags;
  672. DWORD level= 1;
  673. BYTE *buf=NULL;
  674. DWORD bufsize;
  675. DWORD bufneeded;
  676. DWORD printersreturned;
  677. TCHAR *name= NULL;
  678. PyObject *obname=Py_None;
  679. DWORD i;
  680. PyObject *ret=NULL, *obprinter_info;
  681. static size_t printer_info_offset[]={
  682. sizeof(PRINTER_INFO_1),sizeof(PRINTER_INFO_2),sizeof(PRINTER_INFO_3),
  683. sizeof(PRINTER_INFO_4),sizeof(PRINTER_INFO_5),sizeof(PRINTER_INFO_6),
  684. sizeof(PRINTER_INFO_7),sizeof(PRINTER_INFO_8),sizeof(PRINTER_INFO_9)
  685. };
  686. if (!PyArg_ParseTuple(args, "k|Ok:EnumPrinters",
  687. &flags, // @pyparm int|flags||types of printer objects to enumerate (combination of PRINTER_ENUM_* constants).
  688. &obname, // @pyparm string|name|None|name of printer object.
  689. &level)) // @pyparm int|level|1|type of printer info structure (Levels 1,2,4,5 supported)
  690. return NULL;
  691. if (level<1 || level>9)
  692. return PyErr_Format(PyExc_ValueError,"Level %d is not supported", level);
  693. if (!PyWinObject_AsTCHAR(obname, &name, TRUE))
  694. return NULL; // last exit without cleanup
  695. // if call with NULL buffer succeeds, there's nothing to enumerate
  696. if (EnumPrinters(flags, name, level, NULL, 0, &bufneeded, &printersreturned)){
  697. ret = PyTuple_New(0);
  698. goto done;
  699. }
  700. if (GetLastError()!=ERROR_INSUFFICIENT_BUFFER){
  701. PyWin_SetAPIError("EnumPrinters");
  702. goto done;
  703. }
  704. bufsize= bufneeded;
  705. if (NULL == (buf= (BYTE *)malloc(bufsize))){
  706. PyErr_Format(PyExc_MemoryError,"EnumPrinters: unable to allocate %d bytes", bufsize);
  707. goto done;
  708. }
  709. // @rdesc Level 1 returns a tuple of tuples for backward compatibility.
  710. // Each individual element is a tuple of (flags, description, name, comment)<nl>
  711. // All other levels return a tuple of dictionaries representing PRINTER_INFO_* structures
  712. if (!EnumPrinters(flags, name, level, buf, bufsize, &bufneeded, &printersreturned))
  713. PyWin_SetAPIError("EnumPrinters");
  714. else{
  715. ret=PyTuple_New(printersreturned);
  716. if (ret!=NULL)
  717. for (i= 0; i < printersreturned; i++){
  718. if (level==1){
  719. PRINTER_INFO_1 *info;
  720. info= (PRINTER_INFO_1 *)(buf + i * sizeof(PRINTER_INFO_1));
  721. obprinter_info=Py_BuildValue("kNNN",
  722. info->Flags,
  723. PyWinObject_FromTCHAR(info->pDescription),
  724. PyWinObject_FromTCHAR(info->pName),
  725. PyWinObject_FromTCHAR(info->pComment));
  726. }
  727. else
  728. obprinter_info=PyWinObject_FromPRINTER_INFO(buf + i * printer_info_offset[level-1], level);
  729. if (obprinter_info==NULL){
  730. Py_DECREF(ret);
  731. ret=NULL;
  732. break;
  733. }
  734. PyTuple_SET_ITEM(ret, i, obprinter_info);
  735. }
  736. }
  737. done:
  738. PyWinObject_FreeTCHAR(name);
  739. if (buf)
  740. free(buf);
  741. return ret;
  742. }
  743. // @pymethod int|win32print|StartDocPrinter|Notifies the print spooler that a document is to be spooled for printing. To be used before using WritePrinter. Returns the Jobid of the started job.
  744. static PyObject *PyStartDocPrinter(PyObject *self, PyObject *args)
  745. {
  746. HANDLE hprinter;
  747. DWORD level= 1;
  748. TCHAR *pDocName=NULL, *pOutputFile=NULL, *pDatatype=NULL;
  749. PyObject *obDocName, *obOutputFile, *obDatatype, *ret=NULL;
  750. DOC_INFO_1 info;
  751. DWORD JobID;
  752. if (!PyArg_ParseTuple(args, "O&k(OOO):StartDocPrinter",
  753. PyWinObject_AsPrinterHANDLE, &hprinter, // @pyparm <o PyPrinterHANDLE>|hprinter||handle to printer (from <om win32print.OpenPrinter>)
  754. &level, // @pyparm int|level|1|type of docinfo structure (only docinfo level 1 supported)
  755. &obDocName, &obOutputFile, &obDatatype // @pyparm data|tuple||A tuple corresponding to the level parameter.
  756. ))
  757. return NULL;
  758. if (level != 1)
  759. {
  760. PyErr_SetString(PyExc_ValueError, "This information level is not supported");
  761. return NULL;
  762. }
  763. // @comm For level 1, the tuple is:
  764. // @tupleitem 0|string|docName|Specifies the name of the document.
  765. // @tupleitem 1|string|outputFile|Specifies the name of an output file. To print to a printer, set this to None.
  766. // @tupleitem 2|string|dataType|Identifies the type of data used to record the document, such
  767. // as "raw" or "emf", used to record the print job. This member can be None. If it is not None,
  768. // the StartDoc function passes it to the printer driver. Note that the printer driver might
  769. // ignore the requested data type.
  770. if (PyWinObject_AsTCHAR(obDocName, &pDocName, FALSE)
  771. &&PyWinObject_AsTCHAR(obOutputFile, &pOutputFile, TRUE)
  772. &&PyWinObject_AsTCHAR(obDatatype, &pDatatype, TRUE)){
  773. info.pDocName= pDocName;
  774. info.pOutputFile= pOutputFile;
  775. info.pDatatype= pDatatype;
  776. Py_BEGIN_ALLOW_THREADS
  777. JobID= StartDocPrinter(hprinter, level, (LPBYTE)&info);
  778. Py_END_ALLOW_THREADS
  779. if (0 == JobID)
  780. PyWin_SetAPIError("StartDocPrinter");
  781. else
  782. ret = PyLong_FromUnsignedLong(JobID);
  783. }
  784. PyWinObject_FreeTCHAR(pDocName);
  785. PyWinObject_FreeTCHAR(pOutputFile);
  786. PyWinObject_FreeTCHAR(pDatatype);
  787. return ret;
  788. }
  789. // @pymethod None|win32print|EndDocPrinter|The EndDocPrinter function ends a print job for the specified printer. To be used after using WritePrinter.
  790. static PyObject *PyEndDocPrinter(PyObject *self, PyObject *args)
  791. {
  792. HANDLE hprinter;
  793. if (!PyArg_ParseTuple(args, "O&:EndDocPrinter",
  794. PyWinObject_AsPrinterHANDLE, &hprinter)) // @pyparm <o PyPrinterHANDLE>|hPrinter||handle to printer (from <om win32print.OpenPrinter>)
  795. return NULL;
  796. if (!EndDocPrinter(hprinter))
  797. return PyWin_SetAPIError("EndDocPrinter");
  798. Py_INCREF(Py_None);
  799. return Py_None;
  800. }
  801. // @pymethod |win32print|AbortPrinter|Deletes spool file for a printer
  802. static PyObject *PyAbortPrinter(PyObject *self, PyObject *args)
  803. {
  804. // @pyparm <o PyPrinterHANDLE>|hPrinter||Handle to printer as returned by <om win32print.OpenPrinter>
  805. HANDLE hprinter;
  806. if (!PyArg_ParseTuple(args, "O&:AbortPrinter", PyWinObject_AsPrinterHANDLE, &hprinter))
  807. return NULL;
  808. if (!AbortPrinter(hprinter))
  809. return PyWin_SetAPIError("AbortPrinter");
  810. Py_INCREF(Py_None);
  811. return Py_None;
  812. }
  813. // @pymethod |win32print|StartPagePrinter|Notifies the print spooler that a page is to be printed on specified printer
  814. static PyObject *PyStartPagePrinter(PyObject *self, PyObject *args)
  815. {
  816. // @pyparm <o PyPrinterHANDLE>|hprinter||Printer handle as returned by <om win32print.OpenPrinter>
  817. HANDLE hprinter;
  818. if (!PyArg_ParseTuple(args, "O&:StartPagePrinter", PyWinObject_AsPrinterHANDLE, &hprinter))
  819. return NULL;
  820. if (!StartPagePrinter(hprinter))
  821. return PyWin_SetAPIError("StartPagePrinter");
  822. Py_INCREF(Py_None);
  823. return Py_None;
  824. }
  825. // @pymethod |win32print|EndPagePrinter|Ends a page in a print job
  826. static PyObject *PyEndPagePrinter(PyObject *self, PyObject *args)
  827. {
  828. // @pyparm <o PyPrinterHANDLE>|hprinter||Printer handle as returned by <om win32print.OpenPrinter>
  829. HANDLE hprinter;
  830. if (!PyArg_ParseTuple(args, "O&:EndPagePrinter", PyWinObject_AsPrinterHANDLE, &hprinter))
  831. return NULL;
  832. if (!EndPagePrinter(hprinter))
  833. return PyWin_SetAPIError("EndPagePrinter");
  834. Py_INCREF(Py_None);
  835. return Py_None;
  836. }
  837. void PyWinObject_FreeDOCINFO(DOCINFO *di)
  838. {
  839. PyWinObject_FreeTCHAR((TCHAR *)di->lpszDocName);
  840. PyWinObject_FreeTCHAR((TCHAR *)di->lpszOutput);
  841. PyWinObject_FreeTCHAR((TCHAR *)di->lpszDatatype);
  842. }
  843. // @object DOCINFO|A tuple of information representing a DOCINFO struct
  844. // @prop string/<o PyUnicode>|DocName|Name of document
  845. // @prop string/<o PyUnicode>|Output|Name of output file when printing to file. Use None for normal printing.
  846. // @prop string/<o PyUnicode>|DataType|Type of data to be sent to printer, eg RAW, EMF, TEXT. Use None for printer default.
  847. // @prop int|Type|Flag specifying mode of operation. Can be DI_APPBANDING, DI_ROPS_READ_DESTINATION, or 0
  848. BOOL PyWinObject_AsDOCINFO(PyObject *obdocinfo, DOCINFO *di)
  849. {
  850. PyObject *obDocName, *obOutput, *obDataType;
  851. ZeroMemory(di, sizeof(*di));
  852. if (!PyTuple_Check(obdocinfo)){
  853. PyErr_SetString(PyExc_TypeError,"DOCINFO must be a tuple");
  854. return FALSE;
  855. }
  856. di->cbSize=sizeof(DOCINFO);
  857. return PyArg_ParseTuple(obdocinfo, "OOOk", &obDocName, &obOutput, &obDataType, &di->fwType)
  858. &&PyWinObject_AsTCHAR(obDocName, (TCHAR **)&di->lpszDocName, TRUE)
  859. &&PyWinObject_AsTCHAR(obOutput, (TCHAR **)&di->lpszOutput, TRUE)
  860. &&PyWinObject_AsTCHAR(obDataType, (TCHAR **)&di->lpszDatatype, TRUE);
  861. }
  862. // @pymethod int|win32print|StartDoc|Starts spooling a print job on a printer device context
  863. static PyObject *PyStartDoc(PyObject *self, PyObject *args)
  864. {
  865. // @pyparm <o PyHANDLE>|hdc||Printer device context handle as returned by <om win32gui.CreateDC>
  866. // @pyparm tuple|docinfo||<o DOCINFO> tuple specifying print job parameters
  867. // @rdesc On success, returns the job id of the print job
  868. HDC hdc;
  869. DOCINFO docinfo={0};
  870. int jobid;
  871. PyObject *obdocinfo;
  872. if (!PyArg_ParseTuple(args, "O&O:StartDoc", PyWinObject_AsPrinterHANDLE, &hdc, &obdocinfo))
  873. return NULL;
  874. if (!PyWinObject_AsDOCINFO(obdocinfo, &docinfo))
  875. return NULL;
  876. jobid=StartDoc(hdc, &docinfo);
  877. PyWinObject_FreeDOCINFO(&docinfo);
  878. if (jobid > 0)
  879. return PyLong_FromUnsignedLong(jobid);
  880. return PyWin_SetAPIError("StartDoc");
  881. }
  882. // @pymethod |win32print|EndDoc|Stops spooling a print job on a printer device context
  883. static PyObject *PyEndDoc(PyObject *self, PyObject *args)
  884. {
  885. // @pyparm <o PyHANDLE>|hdc||Printer device context handle as returned by <om win32gui.CreateDC>
  886. HDC hdc;
  887. int err;
  888. if (!PyArg_ParseTuple(args, "O&:EndDoc", PyWinObject_AsPrinterHANDLE, &hdc))
  889. return NULL;
  890. err=EndDoc(hdc);
  891. if (err > 0){
  892. Py_INCREF(Py_None);
  893. return Py_None;
  894. }
  895. return PyWin_SetAPIError("EndDoc");
  896. }
  897. // @pymethod |win32print|AbortDoc|Cancels a print job
  898. static PyObject *PyAbortDoc(PyObject *self, PyObject *args)
  899. {
  900. // @pyparm <o PyHANDLE>|hdc||Printer device context handle as returned by <om win32gui.CreateDC>
  901. HDC hdc;
  902. int err;
  903. if (!PyArg_ParseTuple(args, "O&:AbortDoc", PyWinObject_AsPrinterHANDLE, &hdc))
  904. return NULL;
  905. err=AbortDoc(hdc);
  906. if (err > 0){
  907. Py_INCREF(Py_None);
  908. return Py_None;
  909. }
  910. return PyWin_SetAPIError("AbortDoc");
  911. }
  912. // @pymethod |win32print|StartPage|Starts a page on a printer device context
  913. static PyObject *PyStartPage(PyObject *self, PyObject *args)
  914. {
  915. // @pyparm <o PyHANDLE>|hdc||Printer device context handle as returned by <om win32gui.CreateDC>
  916. HDC hdc;
  917. int err;
  918. if (!PyArg_ParseTuple(args, "O&:StartPage", PyWinObject_AsPrinterHANDLE, &hdc))
  919. return NULL;
  920. err=StartPage(hdc);
  921. if (err > 0){
  922. Py_INCREF(Py_None);
  923. return Py_None;
  924. }
  925. return PyWin_SetAPIError("StartPage");
  926. }
  927. // @pymethod |win32print|EndPage|Ends a page on a printer device context
  928. static PyObject *PyEndPage(PyObject *self, PyObject *args)
  929. {
  930. // @pyparm <o PyHANDLE>|hdc||Printer device context handle as returned by <om win32gui.CreateDC>
  931. HDC hdc;
  932. int err;
  933. if (!PyArg_ParseTuple(args, "O&:EndPage", PyWinObject_AsPrinterHANDLE, &hdc))
  934. return NULL;
  935. err=EndPage(hdc);
  936. if (err > 0){
  937. Py_INCREF(Py_None);
  938. return Py_None;
  939. }
  940. return PyWin_SetAPIError("EndPage");
  941. }
  942. // @pymethod int|win32print|WritePrinter|Copies the specified bytes to the specified printer.
  943. // Suitable for copying raw Postscript or HPGL files to a printer.
  944. // StartDocPrinter and EndDocPrinter should be called before and after.
  945. // @rdesc Returns number of bytes written to printer.
  946. static PyObject *PyWritePrinter(PyObject *self, PyObject *args)
  947. {
  948. HANDLE hprinter;
  949. LPVOID buf;
  950. DWORD buf_size;
  951. DWORD bufwritten_size;
  952. PyObject *obbuf;
  953. if (!PyArg_ParseTuple(args, "O&O:WritePrinter",
  954. PyWinObject_AsPrinterHANDLE, &hprinter, // @pyparm <o PyPrinterHANDLE>|hprinter||Handle to printer as returned by <om win32print.OpenPrinter>.
  955. &obbuf)) // @pyparm string|buf||String or buffer containing data to send to printer. Embedded NULL bytes are allowed.
  956. return NULL;
  957. if (!PyWinObject_AsReadBuffer(obbuf, &buf, &buf_size, FALSE))
  958. return NULL;
  959. if (!WritePrinter(hprinter, buf, buf_size, &bufwritten_size))
  960. return PyWin_SetAPIError("WritePrinter");
  961. return PyLong_FromUnsignedLong(bufwritten_size);
  962. }
  963. // convert a job structure to python. only works for level 1
  964. PyObject *JobtoPy(DWORD level, LPBYTE buf)
  965. {
  966. JOB_INFO_1 *job1;
  967. JOB_INFO_2 *job2;
  968. JOB_INFO_3 *job3;
  969. PyObject *ret;
  970. switch (level){
  971. case 1:{
  972. job1= (JOB_INFO_1 *)buf;
  973. ret= Py_BuildValue("{s:k, s:N, s:N, s:N, s:N, s:N, s:N, s:k, s:k, s:k, s:k, s:k, s:N}",
  974. "JobId", job1->JobId,
  975. "pPrinterName", PyWinObject_FromTCHAR(job1->pPrinterName),
  976. "pMachineName", PyWinObject_FromTCHAR(job1->pMachineName),
  977. "pUserName", PyWinObject_FromTCHAR(job1->pUserName),
  978. "pDocument", PyWinObject_FromTCHAR(job1->pDocument),
  979. "pDatatype", PyWinObject_FromTCHAR(job1->pDatatype),
  980. "pStatus", PyWinObject_FromTCHAR(job1->pStatus),
  981. "Status", job1->Status,
  982. "Priority", job1->Priority,
  983. "Position", job1->Position,
  984. "TotalPages", job1->TotalPages,
  985. "PagesPrinted", job1->PagesPrinted,
  986. "Submitted", PyWinObject_FromSYSTEMTIME(job1->Submitted));
  987. return ret;
  988. }
  989. case 2:{
  990. job2=(JOB_INFO_2 *)buf;
  991. ret= Py_BuildValue("{s:k, s:N, s:N, s:N, s:N, s:N, s:N, s:N, s:N, s:N, s:N, s:N, s:N, s:k, s:k, s:k, s:k, s:k, s:k, s:k, s:N, s:k, s:k}",
  992. "JobId", job2->JobId,
  993. "pPrinterName", PyWinObject_FromTCHAR(job2->pPrinterName),
  994. "pMachineName", PyWinObject_FromTCHAR(job2->pMachineName),
  995. "pUserName", PyWinObject_FromTCHAR(job2->pUserName),
  996. "pDocument", PyWinObject_FromTCHAR(job2->pDocument),
  997. "pNotifyName", PyWinObject_FromTCHAR(job2->pNotifyName),
  998. "pDatatype", PyWinObject_FromTCHAR(job2->pDatatype),
  999. "pPrintProcessor", PyWinObject_FromTCHAR(job2->pPrintProcessor),
  1000. "pParameters", PyWinObject_FromTCHAR(job2->pParameters),
  1001. "pDriverName", PyWinObject_FromTCHAR(job2->pDriverName),
  1002. "pDevMode", PyWinObject_FromDEVMODE(job2->pDevMode),
  1003. "pStatus", PyWinObject_FromTCHAR(job2->pStatus),
  1004. "pSecurityDescriptor", PyWinObject_FromSECURITY_DESCRIPTOR(job2->pSecurityDescriptor),
  1005. "Status", job2->Status,
  1006. "Priority", job2->Priority,
  1007. "Position", job2->Position,
  1008. "StartTime", job2->StartTime,
  1009. "UntilTime", job2->UntilTime,
  1010. "TotalPages", job2->TotalPages,
  1011. "Size", job2->Size,
  1012. "Submitted", PyWinObject_FromSYSTEMTIME(job2->Submitted),
  1013. "Time", job2->Time,
  1014. "PagesPrinted", job2->PagesPrinted);
  1015. return ret;
  1016. }
  1017. case 3:{
  1018. job3=(JOB_INFO_3 *)buf;
  1019. ret=Py_BuildValue("{s:k, s:k, s:k}",
  1020. "JobId", job3->JobId,
  1021. "NextJobId",job3->NextJobId,
  1022. "Reserved",job3->Reserved);
  1023. return ret;
  1024. }
  1025. default:
  1026. return PyErr_Format(PyExc_NotImplementedError,"Job info level %d is not yet supported", level);
  1027. }
  1028. }
  1029. // @pymethod tuple|win32print|EnumJobs|Enumerates print jobs on specified printer.
  1030. // @rdesc Returns a sequence of dictionaries representing JOB_INFO_* structures, depending on level
  1031. static PyObject *PyEnumJobs(PyObject *self, PyObject *args)
  1032. {
  1033. HANDLE hprinter;
  1034. DWORD firstjob;
  1035. DWORD nojobs;
  1036. DWORD level= 1;
  1037. LPBYTE buf;
  1038. DWORD buf_size;
  1039. DWORD bufneeded_size;
  1040. DWORD jobsreturned;
  1041. size_t job_info_offset[]={sizeof(JOB_INFO_1),sizeof(JOB_INFO_2),sizeof(JOB_INFO_3)};
  1042. if (!PyArg_ParseTuple(args, "O&kk|k:EnumJobs",
  1043. PyWinObject_AsPrinterHANDLE, &hprinter, // @pyparm <o PyPrinterHANDLE>|hPrinter||Handle of printer.
  1044. &firstjob, // @pyparm int|FirstJob||location of first job in print queue to enumerate.
  1045. &nojobs, // @pyparm int|NoJobs||Number of jobs to enumerate.
  1046. &level // @pyparm int|Level|1|Level of information to return (JOB_INFO_1, JOB_INFO_2, JOB_INFO_3 supported).
  1047. ))
  1048. return NULL;
  1049. if ((level < 1)||(level > 3))
  1050. return PyErr_Format(PyExc_ValueError, "Information level %d is not supported", level);
  1051. if (EnumJobs(hprinter, firstjob, nojobs, level, NULL, 0, &bufneeded_size, &jobsreturned))
  1052. return PyTuple_New(0);
  1053. if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
  1054. return PyWin_SetAPIError("EnumJobs");
  1055. buf_size= bufneeded_size;
  1056. if (NULL == (buf= (LPBYTE)malloc(buf_size)))
  1057. return PyErr_Format(PyExc_MemoryError, "Malloc failed for %d bytes", buf_size);
  1058. if (!EnumJobs(hprinter, firstjob, nojobs, level, buf, buf_size, &bufneeded_size, &jobsreturned))
  1059. {
  1060. free(buf);
  1061. return PyWin_SetAPIError("EnumJobs");
  1062. }
  1063. DWORD i;
  1064. PyObject *job_info;
  1065. PyObject *ret = PyTuple_New(jobsreturned);
  1066. if (ret!=NULL)
  1067. for (i= 0; i < jobsreturned; i++)
  1068. {
  1069. job_info=JobtoPy(level, (buf + i * job_info_offset[level-1]));
  1070. if (job_info == NULL){
  1071. Py_DECREF(ret);
  1072. ret=NULL;
  1073. break;
  1074. }
  1075. PyTuple_SetItem(ret, i, job_info);
  1076. }
  1077. free(buf);
  1078. return ret;
  1079. }
  1080. // @pymethod dictionary|win32print|GetJob|Returns dictionary of information about a specified print job.
  1081. // @rdesc Returns a dict representing a JOB_INFO_* struct, depending on level
  1082. static PyObject *PyGetJob(PyObject *self, PyObject *args)
  1083. {
  1084. HANDLE hprinter;
  1085. DWORD jobid;
  1086. DWORD level= 1;
  1087. LPBYTE buf;
  1088. DWORD buf_size;
  1089. DWORD bufneeded_size;
  1090. if (!PyArg_ParseTuple(args, "O&k|k:GetJob",
  1091. PyWinObject_AsPrinterHANDLE, &hprinter, // @pyparm <o PyPrinterHANDLE>|hPrinter||Handle to a printer as returned by <om win32print.OpenPrinter>.
  1092. &jobid, // @pyparm int|JobID||Job Identifier.
  1093. &level // @pyparm int|Level|1|Level of information to return (JOB_INFO_1, JOB_INFO_2, JOB_INFO_3 supported).
  1094. ))
  1095. return NULL;
  1096. if ((level < 1)||(level > 3))
  1097. return PyErr_Format(PyExc_ValueError, "Information level %d is not supported", level);
  1098. GetJob(hprinter, jobid, level, NULL, 0, &bufneeded_size);
  1099. if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
  1100. return PyWin_SetAPIError("GetJob");
  1101. buf_size= bufneeded_size;
  1102. if (NULL == (buf= (LPBYTE)malloc(buf_size)))
  1103. {
  1104. PyErr_SetString(PyExc_MemoryError, "Malloc failed.");
  1105. return NULL;
  1106. }
  1107. if (!GetJob(hprinter, jobid, level, buf, buf_size, &bufneeded_size))
  1108. {
  1109. free(buf);
  1110. return PyWin_SetAPIError("GetJob");
  1111. }
  1112. PyObject *ret= JobtoPy(level, buf);
  1113. free(buf);
  1114. return ret;
  1115. }
  1116. // Convert a python dictionary to a JOB_INFO_* structure.
  1117. // Returned buffer must be freed.
  1118. BOOL PytoJob(DWORD level, PyObject *pyjobinfo, LPBYTE *pbuf)
  1119. {
  1120. static char *job1_keys[]={"JobId","pPrinterName","pMachineName","pUserName","pDocument","pDatatype",
  1121. "pStatus","Status","Priority","Position","TotalPages","PagesPrinted","Submitted", NULL};
  1122. static char *job1_format="kzzzzzzkkkkk|O:JOB_INFO_1";
  1123. static char *job2_keys[]={"JobId","pPrinterName","pMachineName","pUserName","pDocument","pNotifyName",
  1124. "pDatatype","pPrintProcessor","pParameters","pDriverName","pDevMode","pStatus","pSecurityDescriptor",
  1125. "Status","Priority","Position","StartTime","UntilTime","TotalPages","Size",
  1126. "Submitted","Time","PagesPrinted", NULL};
  1127. static char *job2_format="kzzzzzzzzzOzOkkkkkkkOkk:JOB_INFO_2";
  1128. static char *job3_keys[]={"JobId","NextJobId","Reserved", NULL};
  1129. static char *job3_format="kk|k:JOB_INFO_3";
  1130. PyObject *obdevmode, *obsecurity_descriptor, *obsubmitted=Py_None;
  1131. BOOL ret=FALSE;
  1132. *pbuf=NULL;
  1133. switch(level){
  1134. case 0:
  1135. if (pyjobinfo==Py_None)
  1136. ret=TRUE;
  1137. else
  1138. PyErr_SetString(PyExc_TypeError,"Info must be None when level is 0.");
  1139. break;
  1140. case 1:
  1141. if (!PyDict_Check (pyjobinfo)){
  1142. PyErr_SetString(PyExc_TypeError, "JOB_INFO_1 must be a dictionary");
  1143. break;
  1144. }
  1145. JOB_INFO_1 *job1;
  1146. if (NULL == (*pbuf= (LPBYTE)malloc(sizeof(JOB_INFO_1)))){
  1147. PyErr_Format(PyExc_MemoryError, "Malloc failed for %d bytes", sizeof(JOB_INFO_1));
  1148. break;
  1149. }
  1150. job1=(JOB_INFO_1 *)*pbuf;
  1151. ZeroMemory(job1,sizeof(JOB_INFO_1));
  1152. if (PyArg_ParseTupleAndKeywords(dummy_tuple, pyjobinfo, job1_format, job1_keys,
  1153. &job1->JobId, &job1->pPrinterName, &job1->pMachineName, &job1->pUserName, &job1->pDocument,
  1154. &job1->pDatatype, &job1->pStatus, &job1->Status, &job1->Priority, &job1->Position,
  1155. &job1->TotalPages, &job1->PagesPrinted, &obsubmitted)
  1156. &&((obsubmitted==Py_None)||PyWinObject_AsSYSTEMTIME(obsubmitted, &job1->Submitted)))
  1157. ret=TRUE;
  1158. break;
  1159. case 2:
  1160. if (!PyDict_Check (pyjobinfo)){
  1161. PyErr_SetString(PyExc_TypeError, "JOB_INFO_2 must be a dictionary");
  1162. break;
  1163. }
  1164. JOB_INFO_2 *job2;
  1165. if (NULL == (*pbuf=(LPBYTE)malloc(sizeof(JOB_INFO_2)))){
  1166. PyErr_Format(PyExc_MemoryError, "Malloc failed for %d bytes", sizeof(JOB_INFO_2));
  1167. break;
  1168. }
  1169. job2=(JOB_INFO_2 *)*pbuf;
  1170. ZeroMemory(job2,sizeof(JOB_INFO_2));
  1171. if (PyArg_ParseTupleAndKeywords(dummy_tuple, pyjobinfo, job2_format, job2_keys,
  1172. &job2->JobId, &job2->pPrinterName, &job2->pMachineName, &job2->pUserName, &job2->pDocument,
  1173. &job2->pNotifyName, &job2->pDatatype, &job2->pPrintProcessor, &job2->pParameters,
  1174. &job2->pDriverName, &obdevmode, &job2->pStatus, &obsecurity_descriptor, &job2->Status,
  1175. &job2->Priority, &job2->Position, &job2->StartTime, &job2->UntilTime,
  1176. &job2->TotalPages, &job2->Size, &obsubmitted, &job2->Time, &job2->PagesPrinted)
  1177. &&PyWinObject_AsDEVMODE(obdevmode, &job2->pDevMode, TRUE)
  1178. &&PyWinObject_AsSECURITY_DESCRIPTOR(obsecurity_descriptor, &job2->pSecurityDescriptor, TRUE)
  1179. &&((obsubmitted==Py_None)||PyWinObject_AsSYSTEMTIME(obsubmitted, &job2->Submitted)))
  1180. ret=TRUE;
  1181. break;
  1182. case 3:
  1183. if (!PyDict_Check (pyjobinfo)){
  1184. PyErr_SetString(PyExc_TypeError, "JOB_INFO_3 must be a dictionary");
  1185. break;
  1186. }
  1187. JOB_INFO_3 *job3;
  1188. if (NULL == (*pbuf=(LPBYTE)malloc(sizeof(JOB_INFO_3)))){
  1189. PyErr_Format(PyExc_MemoryError, "Malloc failed for %d bytes", sizeof(JOB_INFO_3));
  1190. break;
  1191. }
  1192. job3=(JOB_INFO_3 *)*pbuf;
  1193. ZeroMemory(job3,sizeof(JOB_INFO_3));
  1194. ret=PyArg_ParseTupleAndKeywords(dummy_tuple, pyjobinfo, job3_format, job3_keys,
  1195. &job3->JobId, &job3->NextJobId, &job3->Reserved);
  1196. break;
  1197. default:
  1198. PyErr_Format(PyExc_NotImplementedError,"Information level %d is not supported", level);
  1199. }
  1200. if (!ret)
  1201. if (*pbuf!=NULL)
  1202. free(*pbuf);
  1203. return ret;
  1204. }
  1205. // @pymethod None|win32print|SetJob|Pause, cancel, resume, set priority levels on a print job.
  1206. // @comm If printer is not opened with at least PRINTER_ACCESS_ADMINISTER access, 'Position' member of
  1207. // JOB_INFO_1 and JOB_INFO_2 must be set to JOB_POSITION_UNSPECIFIED
  1208. static PyObject *PySetJob(PyObject *self, PyObject *args)
  1209. {
  1210. HANDLE hprinter;
  1211. DWORD jobid;
  1212. DWORD level= 1;
  1213. PyObject *pyjobinfo;
  1214. DWORD command;
  1215. LPBYTE buf;
  1216. if (!PyArg_ParseTuple(args, "O&kkOk:SetJob",
  1217. PyWinObject_AsPrinterHANDLE, &hprinter, // @pyparm <o PyPrinterHANDLE>|hPrinter||Handle of printer.
  1218. &jobid, // @pyparm int|JobID||Job Identifier.
  1219. &level, // @pyparm int|Level||Level of information in JobInfo dict (0, 1, 2, and 3 are supported).
  1220. &pyjobinfo, // @pyparm dict|JobInfo||JOB_INFO_* Dictionary as returned by <om win32print.GetJob> or <om win32print.EnumJobs> (can be None if Level is 0).
  1221. &command // @pyparm int|Command||Job command value (JOB_CONTROL_*).
  1222. ))
  1223. return NULL;
  1224. if (!PytoJob(level, pyjobinfo, &buf))
  1225. return NULL;
  1226. if (!SetJob(hprinter, jobid, level, buf, command))
  1227. {
  1228. if (buf)
  1229. free(buf);
  1230. return PyWin_SetAPI