/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

  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_SetAPIError("SetJob");
  1231. }
  1232. if (buf)
  1233. free(buf);
  1234. Py_INCREF(Py_None);
  1235. return Py_None;
  1236. }
  1237. // @pymethod int|win32print|DocumentProperties|Changes printer configuration for a printer
  1238. // @rdesc If DM_IN_PROMPT is specified, returned value will be IDOK or IDCANCEL
  1239. static PyObject *PyDocumentProperties(PyObject *self, PyObject *args)
  1240. {
  1241. long rc;
  1242. HANDLE hprinter;
  1243. HWND hwnd;
  1244. TCHAR *devicename=NULL;
  1245. PDEVMODE dmoutput, dminput;
  1246. PyObject *obdmoutput, *obdminput, *obhwnd, *obdevicename, *ret=NULL;
  1247. DWORD mode;
  1248. // @pyparm <o PyHANDLE>|HWnd||Parent window handle to use if DM_IN_PROMPT is specified to display printer dialog
  1249. // @pyparm <o PyPrinterHANDLE>|hPrinter||Printer handle as returned by <om win32print.OpenPrinter>
  1250. // @pyparm string|DeviceName||Name of printer
  1251. // @pyparm <o PyDEVMODE>|DevModeOutput||PyDEVMODE object that receives modified info, can be None if DM_OUT_BUFFER not specified
  1252. // @pyparm <o PyDEVMODE>|DevModeInput||PyDEVMODE that specifies initial configuration, can be None if DM_IN_BUFFER not specified
  1253. // @pyparm int|Mode||A combination of DM_IN_BUFFER, DM_OUT_BUFFER, and DM_IN_PROMPT - pass 0 to retrieve driver data size
  1254. if (!PyArg_ParseTuple(args,"OO&OOOk:DocumentProperties", &obhwnd,
  1255. PyWinObject_AsPrinterHANDLE, &hprinter,
  1256. &obdevicename, &obdmoutput, &obdminput, &mode))
  1257. return NULL;
  1258. if (PyWinObject_AsTCHAR(obdevicename, &devicename, FALSE)
  1259. &&PyWinObject_AsHANDLE(obhwnd, (HANDLE *)&hwnd)
  1260. &&PyWinObject_AsDEVMODE(obdmoutput, &dmoutput, TRUE)
  1261. &&PyWinObject_AsDEVMODE(obdminput, &dminput, TRUE)){
  1262. rc=DocumentProperties(hwnd, hprinter, devicename, dmoutput, dminput, mode);
  1263. if (ret < 0)
  1264. PyWin_SetAPIError("DocumentProperties");
  1265. else{
  1266. if (obdmoutput!=Py_None)
  1267. ((PyDEVMODE *)obdmoutput)->modify_in_place();
  1268. ret = PyInt_FromLong(rc);
  1269. }
  1270. }
  1271. PyWinObject_FreeTCHAR(devicename);
  1272. return ret;
  1273. }
  1274. // @pymethod (<o PyUnicode>,...)|win32print|EnumPrintProcessors|List printer processors for specified server and environment
  1275. static PyObject *PyEnumPrintProcessors(PyObject *self, PyObject *args)
  1276. {
  1277. PRINTPROCESSOR_INFO_1W *info=NULL; // currently only level that exists
  1278. LPBYTE buf=NULL;
  1279. WCHAR *servername=NULL, *environment=NULL;
  1280. PyObject *observername=Py_None, *obenvironment=Py_None;
  1281. DWORD level=1, bufsize=0, bytes_needed, return_cnt;
  1282. PyObject *ret=NULL, *tuple_item;
  1283. // @pyparm string/<o PyUnicode>|Server|None|Name of print server, use None for local machine
  1284. // @pyparm string/<o PyUnicode>|Environment|None|Environment - eg 'Windows NT x86' - use None for current client environment
  1285. if (!PyArg_ParseTuple(args,"|OO:EnumPrintProcessors", &observername, &obenvironment))
  1286. return NULL;
  1287. if (!PyWinObject_AsWCHAR(observername, &servername, TRUE))
  1288. goto done;
  1289. if (!PyWinObject_AsWCHAR(obenvironment, &environment, TRUE))
  1290. goto done;
  1291. if (EnumPrintProcessorsW(servername, environment, level, buf, bufsize, &bytes_needed, &return_cnt)){
  1292. ret=PyTuple_New(0);
  1293. goto done;
  1294. }
  1295. if (bytes_needed==0){
  1296. PyWin_SetAPIError("EnumPrintProcessors");
  1297. goto done;
  1298. }
  1299. buf=(LPBYTE)malloc(bytes_needed);
  1300. if (buf==NULL){
  1301. PyErr_Format(PyExc_MemoryError,"EnumPrintProcessors: unable to allocate buffer of size %d", bytes_needed);
  1302. goto done;
  1303. }
  1304. bufsize=bytes_needed;
  1305. if (!EnumPrintProcessorsW(servername, environment, level, buf, bufsize, &bytes_needed, &return_cnt))
  1306. PyWin_SetAPIError("EnumPrintProcessors");
  1307. else{
  1308. ret=PyTuple_New(return_cnt);
  1309. if (ret!=NULL){
  1310. info=(PRINTPROCESSOR_INFO_1W *)buf;
  1311. for (DWORD buf_ind=0; buf_ind<return_cnt; buf_ind++){
  1312. tuple_item=PyWinObject_FromWCHAR(info->pName);
  1313. if (tuple_item==NULL){
  1314. Py_DECREF(ret);
  1315. ret=NULL;
  1316. break;
  1317. }
  1318. PyTuple_SetItem(ret,buf_ind,tuple_item);
  1319. info++;
  1320. }
  1321. }
  1322. }
  1323. done:
  1324. if (buf!=NULL)
  1325. free(buf);
  1326. if (servername!=NULL)
  1327. PyWinObject_FreeWCHAR(servername);
  1328. if (environment!=NULL)
  1329. PyWinObject_FreeWCHAR(environment);
  1330. return ret;
  1331. }
  1332. // @pymethod (<o PyUnicode>,...)|win32print|EnumPrintProcessorDatatypes|List data types that specified print provider recognizes
  1333. static PyObject *PyEnumPrintProcessorDatatypes(PyObject *self, PyObject *args)
  1334. {
  1335. DATATYPES_INFO_1W *di1;
  1336. LPBYTE buf=NULL;
  1337. WCHAR *servername=NULL, *processorname=NULL;
  1338. PyObject *observername, *obprocessorname;
  1339. DWORD level=1, bufsize=0, bytes_needed, return_cnt, buf_ind;
  1340. PyObject *ret=NULL, *tuple_item;
  1341. // @pyparm string/<o PyUnicode>|ServerName||Name of print server, use None for local machine
  1342. // @pyparm string/<o PyUnicode>|PrintProcessorName||Name of print processor
  1343. if (!PyArg_ParseTuple(args,"OO:EnumPrintProcessorDatatypes", &observername, &obprocessorname))
  1344. return NULL;
  1345. if (!PyWinObject_AsWCHAR(observername, &servername, TRUE))
  1346. goto done;
  1347. if (!PyWinObject_AsWCHAR(obprocessorname, &processorname, FALSE))
  1348. goto done;
  1349. EnumPrintProcessorDatatypesW(servername, processorname, level, buf, bufsize, &bytes_needed, &return_cnt);
  1350. if (bytes_needed==0){
  1351. PyWin_SetAPIError("EnumPrintProcessorDatatypes");
  1352. goto done;
  1353. }
  1354. buf=(LPBYTE)malloc(bytes_needed);
  1355. if (buf==NULL){
  1356. PyErr_Format(PyExc_MemoryError,"EnumPrintProcessorDatatypes: unable to allocate buffer of size %d", bytes_needed);
  1357. goto done;
  1358. }
  1359. bufsize=bytes_needed;
  1360. if (!EnumPrintProcessorDatatypesW(servername, processorname, level, buf, bufsize, &bytes_needed, &return_cnt)){
  1361. PyWin_SetAPIError("EnumPrintProcessorDatatypes");
  1362. goto done;
  1363. }
  1364. ret=PyTuple_New(return_cnt);
  1365. if (ret==NULL)
  1366. goto done;
  1367. di1=(DATATYPES_INFO_1W *)buf;
  1368. for (buf_ind=0; buf_ind<return_cnt; buf_ind++){
  1369. tuple_item=PyWinObject_FromWCHAR(di1->pName);
  1370. if (tuple_item==NULL){
  1371. Py_DECREF(ret);
  1372. ret=NULL;
  1373. break;
  1374. }
  1375. PyTuple_SetItem(ret,buf_ind,tuple_item);
  1376. di1++;
  1377. }
  1378. done:
  1379. if (servername!=NULL)
  1380. PyWinObject_FreeWCHAR(servername);
  1381. if (processorname!=NULL)
  1382. PyWinObject_FreeWCHAR(processorname);
  1383. if (buf!=NULL)
  1384. free(buf);
  1385. return ret;
  1386. }
  1387. // @pymethod (dict,...)|win32print|EnumPrinterDrivers|Lists installed printer drivers
  1388. static PyObject *PyEnumPrinterDrivers(PyObject *self, PyObject *args)
  1389. {
  1390. DWORD level=1, bufsize=0, bytes_needed, return_cnt, i;
  1391. LPBYTE buf=NULL;
  1392. DRIVER_INFO_1W *di1;
  1393. DRIVER_INFO_2W *di2;
  1394. DRIVER_INFO_3W *di3;
  1395. DRIVER_INFO_4W *di4;
  1396. DRIVER_INFO_5W *di5;
  1397. DRIVER_INFO_6W *di6;
  1398. PyObject *ret=NULL, *tuple_item;
  1399. PyObject *observername=Py_None, *obenvironment=Py_None;
  1400. WCHAR *servername=NULL, *environment=NULL;
  1401. // @pyparm string/unicode|Server|None|Name of print server, use None for local machine
  1402. // @pyparm string/unicode|Environment|None|Environment - eg 'Windows NT x86' - use None for current client environment
  1403. // @pyparm int|Level|1|Level of information to return, 1-6 (not all levels are supported on all platforms)
  1404. // @rdesc Returns a sequence of dictionaries representing DRIVER_INFO_* structures
  1405. // @comm On Win2k and up, 'all' can be passed for environment
  1406. if (!PyArg_ParseTuple(args,"|OOk:EnumPrinterDrivers", &observername, &obenvironment, &level))
  1407. return NULL;
  1408. if (!PyWinObject_AsWCHAR(observername, &servername, TRUE))
  1409. goto done;
  1410. if (!PyWinObject_AsWCHAR(obenvironment, &environment, TRUE))
  1411. goto done;
  1412. if (EnumPrinterDriversW(servername, environment, level, buf, bufsize, &bytes_needed, &return_cnt)){
  1413. ret=PyTuple_New(0);
  1414. goto done;
  1415. }
  1416. if (bytes_needed==0){
  1417. PyWin_SetAPIError("EnumPrinterDrivers");
  1418. goto done;
  1419. }
  1420. buf=(LPBYTE)malloc(bytes_needed);
  1421. if (buf==NULL){
  1422. PyErr_Format(PyExc_MemoryError,"EnumPrinterDrivers: unable to allocate buffer of size %d", bytes_needed);
  1423. goto done;
  1424. }
  1425. bufsize=bytes_needed;
  1426. if (!EnumPrinterDriversW(servername, environment, level, buf, bufsize, &bytes_needed, &return_cnt)){
  1427. PyWin_SetAPIError("EnumPrinterDrivers");
  1428. goto done;
  1429. }
  1430. ret=PyTuple_New(return_cnt);
  1431. if (ret==NULL)
  1432. goto done;
  1433. switch (level)
  1434. case 1:{
  1435. di1=(DRIVER_INFO_1W *)buf;
  1436. for (i=0; i<return_cnt; i++){
  1437. tuple_item=Py_BuildValue("{s:u}","Name",di1->pName);
  1438. if (tuple_item==NULL){
  1439. Py_DECREF(ret);
  1440. ret=NULL;
  1441. break;
  1442. }
  1443. PyTuple_SetItem(ret, i, tuple_item);
  1444. di1++;
  1445. }
  1446. break;
  1447. case 2:
  1448. di2=(DRIVER_INFO_2W *)buf;
  1449. for (i=0; i<return_cnt; i++){
  1450. tuple_item=Py_BuildValue("{s:l,s:u,s:u,s:u,s:u,s:u}",
  1451. "Version",di2->cVersion,
  1452. "Name",di2->pName,
  1453. "Environment",di2->pEnvironment,
  1454. "DriverPath",di2->pDriverPath,
  1455. "DataFile",di2->pDataFile,
  1456. "ConfigFile",di2->pConfigFile);
  1457. if (tuple_item==NULL){
  1458. Py_DECREF(ret);
  1459. ret=NULL;
  1460. break;
  1461. }
  1462. PyTuple_SetItem(ret, i, tuple_item);
  1463. di2++;
  1464. }
  1465. break;
  1466. case 3:
  1467. di3=(DRIVER_INFO_3W *)buf;
  1468. for (i=0; i<return_cnt; i++){
  1469. tuple_item=Py_BuildValue("{s:l,s:u,s:u,s:u,s:u,s:u,s:u,s:N,s:u,s:u}",
  1470. "Version",di3->cVersion,
  1471. "Name",di3->pName,
  1472. "Environment",di3->pEnvironment,
  1473. "DriverPath",di3->pDriverPath,
  1474. "DataFile",di3->pDataFile,
  1475. "ConfigFile",di3->pConfigFile,
  1476. "HelpFile", di3->pHelpFile,
  1477. "DependentFiles",PyWinObject_FromMultipleString(di3->pDependentFiles),
  1478. "MonitorName",di3->pMonitorName,
  1479. "DefaultDataType",di3->pDefaultDataType);
  1480. if (tuple_item==NULL){
  1481. Py_DECREF(ret);
  1482. ret=NULL;
  1483. break;
  1484. }
  1485. PyTuple_SetItem(ret, i, tuple_item);
  1486. di3++;
  1487. }
  1488. break;
  1489. case 4:
  1490. di4=(DRIVER_INFO_4W *)buf;
  1491. for (i=0; i<return_cnt; i++){
  1492. tuple_item=Py_BuildValue("{s:l,s:u,s:u,s:u,s:u,s:u,s:u,s:N,s:u,s:u,s:u}",
  1493. "Version",di4->cVersion,
  1494. "Name",di4->pName,
  1495. "Environment",di4->pEnvironment,
  1496. "DriverPath",di4->pDriverPath,
  1497. "DataFile",di4->pDataFile,
  1498. "ConfigFile",di4->pConfigFile,
  1499. "HelpFile", di4->pHelpFile,
  1500. "DependentFiles",PyWinObject_FromMultipleString(di4->pDependentFiles),
  1501. "MonitorName",di4->pMonitorName,
  1502. "DefaultDataType",di4->pDefaultDataType,
  1503. "PreviousNames",di4->pszzPreviousNames);
  1504. if (tuple_item==NULL){
  1505. Py_DECREF(ret);
  1506. ret=NULL;
  1507. break;
  1508. }
  1509. PyTuple_SetItem(ret, i, tuple_item);
  1510. di4++;
  1511. }
  1512. break;
  1513. case 5:
  1514. di5=(DRIVER_INFO_5W *)buf;
  1515. for (i=0; i<return_cnt; i++){
  1516. tuple_item=Py_BuildValue("{s:l,s:u,s:u,s:u,s:u,s:u,s:l,s:l,s:l}",
  1517. "Version",di5->cVersion,
  1518. "Name",di5->pName,
  1519. "Environment",di5->pEnvironment,
  1520. "DriverPath",di5->pDriverPath,
  1521. "DataFile",di5->pDataFile,
  1522. "ConfigFile",di5->pConfigFile,
  1523. "DriverAttributes", di5->dwDriverAttributes,
  1524. "DriverVersion",di5->dwDriverVersion,
  1525. "ConfigVersion",di5->dwConfigVersion);
  1526. if (tuple_item==NULL){
  1527. Py_DECREF(ret);
  1528. ret=NULL;
  1529. break;
  1530. }
  1531. PyTuple_SetItem(ret, i, tuple_item);
  1532. di5++;
  1533. }
  1534. break;
  1535. case 6:
  1536. di6=(DRIVER_INFO_6W *)buf;
  1537. for (i=0; i<return_cnt; i++){
  1538. tuple_item=Py_BuildValue("{s:l,s:u,s:u,s:u,s:u,s:u,s:u,s:N,s:u,s:u,s:u,s:N,s:L,s:u,s:u,s:u}",
  1539. "Version",di6->cVersion,
  1540. "Name",di6->pName,
  1541. "Environment",di6->pEnvironment,
  1542. "DriverPath",di6->pDriverPath,
  1543. "DataFile",di6->pDataFile,
  1544. "ConfigFile",di6->pConfigFile,
  1545. "HelpFile", di6->pHelpFile,
  1546. "DependentFiles",PyWinObject_FromMultipleString(di6->pDependentFiles),
  1547. "MonitorName",di6->pMonitorName,
  1548. "DefaultDataType",di6->pDefaultDataType,
  1549. "PreviousNames",di6->pszzPreviousNames,
  1550. "DriverDate", PyWinObject_FromFILETIME(di6->ftDriverDate),
  1551. "DriverVersion",di6->dwlDriverVersion,
  1552. "MfgName",di6->pszMfgName,
  1553. "OEMUrl",di6->pszOEMUrl,
  1554. "Provider",di6->pszProvider
  1555. );
  1556. if (tuple_item==NULL){
  1557. Py_DECREF(ret);
  1558. ret=NULL;
  1559. break;
  1560. }
  1561. PyTuple_SetItem(ret, i, tuple_item);
  1562. di6++;
  1563. }
  1564. break;
  1565. default:
  1566. PyErr_Format(PyExc_ValueError,"EnumPrinterDrivers: Level %d is not supported", level);
  1567. Py_DECREF(ret);
  1568. ret=NULL;
  1569. }
  1570. done:
  1571. if (buf!=NULL)
  1572. free(buf);
  1573. if (servername!=NULL)
  1574. PyWinObject_FreeWCHAR(servername);
  1575. if (environment!=NULL)
  1576. PyWinObject_FreeWCHAR(environment);
  1577. return ret;
  1578. }
  1579. PyObject *PyWin_Object_FromFORM_INFO_1(FORM_INFO_1W *fi1)
  1580. {
  1581. if (fi1==NULL){
  1582. Py_INCREF(Py_None);
  1583. return Py_None;
  1584. }
  1585. return Py_BuildValue("{s:k,s:u,s:{s:l,s:l},s:{s:l,s:l,s:l,s:l}}",
  1586. "Flags", fi1->Flags,
  1587. "Name", fi1->pName,
  1588. "Size",
  1589. "cx", fi1->Size.cx, "cy", fi1->Size.cy,
  1590. "ImageableArea",
  1591. "left", fi1->ImageableArea.left, "top", fi1->ImageableArea.top,
  1592. "right", fi1->ImageableArea.right, "bottom", fi1->ImageableArea.bottom);
  1593. }
  1594. // @pymethod (<o FORM_INFO_1>,...)|win32print|EnumForms|Lists forms for a printer
  1595. static PyObject *PyEnumForms(PyObject *self, PyObject *args)
  1596. {
  1597. // @pyparm <o PyPrinterHANDLE>|hprinter||Printer handle as returned by <om win32print.OpenPrinter>
  1598. // @rdesc Returns a sequence of dictionaries representing FORM_INFO_1 structures
  1599. PyObject *ret=NULL, *tuple_item;
  1600. HANDLE hprinter;
  1601. DWORD level=1, bufsize=0, bytes_needed=0, return_cnt, buf_ind;
  1602. FORM_INFO_1W *fi1;
  1603. LPBYTE buf=NULL;
  1604. CHECK_PFN(EnumForms);
  1605. if (!PyArg_ParseTuple(args,"O&:EnumForms", PyWinObject_AsPrinterHANDLE, &hprinter))
  1606. return NULL;
  1607. (*pfnEnumForms)(hprinter, level, buf, bufsize, &bytes_needed, &return_cnt);
  1608. if (bytes_needed==0){
  1609. PyWin_SetAPIError("EnumForms");
  1610. goto done;
  1611. }
  1612. buf=(LPBYTE)malloc(bytes_needed);
  1613. if (buf==NULL){
  1614. PyErr_Format(PyExc_MemoryError,"EnumForms: unable to allocate buffer of size %d", bytes_needed);
  1615. goto done;
  1616. }
  1617. bufsize=bytes_needed;
  1618. if (!(*pfnEnumForms)(hprinter, level, buf, bufsize, &bytes_needed, &return_cnt)){
  1619. PyWin_SetAPIError("EnumForms");
  1620. goto done;
  1621. }
  1622. ret=PyTuple_New(return_cnt);
  1623. if (ret==NULL)
  1624. goto done;
  1625. fi1=(FORM_INFO_1W *)buf;
  1626. for (buf_ind=0; buf_ind<return_cnt; buf_ind++){
  1627. tuple_item=PyWin_Object_FromFORM_INFO_1(fi1);
  1628. if (tuple_item==NULL){
  1629. Py_DECREF(ret);
  1630. ret=NULL;
  1631. break;
  1632. }
  1633. PyTuple_SetItem(ret,buf_ind,tuple_item);
  1634. fi1++;
  1635. }
  1636. done:
  1637. if (buf!=NULL)
  1638. free(buf);
  1639. return ret;
  1640. }
  1641. BOOL PyWinObject_AsRECTL(PyObject *obrectl, RECTL *rectl)
  1642. {
  1643. static char *rectl_keys[]={"left","top","right","bottom",0};
  1644. static char* err_msg="RECTL must be a dictionary containing {left:int, top:int, right:int, bottom:int}";
  1645. if (obrectl->ob_type!=&PyDict_Type){
  1646. PyErr_SetString(PyExc_TypeError,err_msg);
  1647. return FALSE;
  1648. }
  1649. if (PyArg_ParseTupleAndKeywords(dummy_tuple, obrectl, "llll", rectl_keys,
  1650. &rectl->left, &rectl->top, &rectl->right, &rectl->bottom))
  1651. return TRUE;
  1652. PyErr_Clear();
  1653. PyErr_SetString(PyExc_TypeError, err_msg);
  1654. return FALSE;
  1655. }
  1656. BOOL PyWinObject_AsSIZEL(PyObject *obsizel, SIZEL *sizel)
  1657. {
  1658. static char *sizel_keys[]={"cx","cy",0};
  1659. static char* err_msg="SIZEL must be a dictionary containing {cx:int, cy:int}";
  1660. if (obsizel->ob_type!=&PyDict_Type){
  1661. PyErr_SetString(PyExc_TypeError,err_msg);
  1662. return FALSE;
  1663. }
  1664. if (PyArg_ParseTupleAndKeywords(dummy_tuple, obsizel, "ll", sizel_keys, &sizel->cx, &sizel->cy))
  1665. return TRUE;
  1666. PyErr_Clear();
  1667. PyErr_SetString(PyExc_TypeError, err_msg);
  1668. return FALSE;
  1669. }
  1670. // @object FORM_INFO_1|A dictionary containing FORM_INFO_1W data
  1671. // @prop int|Flags|FORM_USER, FORM_BUILTIN, or FORM_PRINTER
  1672. // @prop <o PyUnicode>|Name|Name of form
  1673. // @prop dict|Size|A dictionary representing a SIZEL structure {'cx':int,'cy':int}
  1674. // @prop dict|ImageableArea|A dictionary representing a RECTL structure {'left':int, 'top':int, 'right':int, 'bottom':int}
  1675. BOOL PyWinObject_AsFORM_INFO_1(PyObject *obform, FORM_INFO_1W *fi1)
  1676. {
  1677. static char *form_keys[]={"Flags","Name","Size","ImageableArea",0};
  1678. static char* err_msg="FORM_INFO_1 must be a dictionary containing {Flags:int, Name:unicode, Size:dict, ImageableArea:dict}";
  1679. if (obform->ob_type!=&PyDict_Type){
  1680. PyErr_SetString(PyExc_TypeError,err_msg);
  1681. return FALSE;
  1682. }
  1683. return PyArg_ParseTupleAndKeywords(dummy_tuple, obform, "kuO&O&:FORM_INFO_1", form_keys, &fi1->Flags, &fi1->pName,
  1684. PyWinObject_AsSIZEL, &fi1->Size, PyWinObject_AsRECTL, &fi1->ImageableArea);
  1685. }
  1686. // @pymethod |win32print|AddForm|Adds a form for a printer
  1687. static PyObject *PyAddForm(PyObject *self, PyObject *args)
  1688. {
  1689. // @pyparm <o PyPrinterHANDLE>|hprinter||Printer handle as returned by <om win32print.OpenPrinter>
  1690. // @pyparm dict|Form||<o FORM_INFO_1> dictionary
  1691. // @rdesc Returns None on success, throws an exception otherwise
  1692. FORM_INFO_1W fi1;
  1693. HANDLE hprinter;
  1694. CHECK_PFN(AddForm);
  1695. if (!PyArg_ParseTuple(args, "O&O&:AddForm",
  1696. PyWinObject_AsPrinterHANDLE, &hprinter,
  1697. PyWinObject_AsFORM_INFO_1, &fi1))
  1698. return NULL;
  1699. if (!(*pfnAddForm)(hprinter, 1, (LPBYTE)&fi1))
  1700. return PyWin_SetAPIError("AddForm");
  1701. Py_INCREF(Py_None);
  1702. return Py_None;
  1703. }
  1704. // @pymethod |win32print|DeleteForm|Deletes a form defined for a printer
  1705. static PyObject *PyDeleteForm(PyObject *self, PyObject *args)
  1706. {
  1707. // @pyparm <o PyPrinterHANDLE>|hprinter||Printer handle as returned by <om win32print.OpenPrinter>
  1708. // @pyparm <o PyUnicode>|FormName||Name of form to be deleted
  1709. // @rdesc Returns None on success, throws an exception otherwise
  1710. HANDLE hprinter;
  1711. WCHAR *formname;
  1712. CHECK_PFN(DeleteForm);
  1713. if (!PyArg_ParseTuple(args, "O&u:DeleteForm", PyWinObject_AsPrinterHANDLE, &hprinter, &formname))
  1714. return NULL;
  1715. if (!(*pfnDeleteForm)(hprinter, formname))
  1716. return PyWin_SetAPIError("DeleteForm");
  1717. Py_INCREF(Py_None);
  1718. return Py_None;
  1719. }
  1720. // @pymethod |win32print|GetForm|Retrieves information about a form defined for a printer
  1721. static PyObject *PyGetForm(PyObject *self, PyObject *args)
  1722. {
  1723. // @pyparm <o PyPrinterHANDLE>|hprinter||Printer handle as returned by <om win32print.OpenPrinter>
  1724. // @pyparm <o PyUnicode>|FormName||Name of form for which to retrieve info
  1725. // @rdesc Returns a <o FORM_INFO_1> dict
  1726. HANDLE hprinter;
  1727. WCHAR *formname;
  1728. DWORD level=1, bufsize=0, bytes_needed=0;
  1729. FORM_INFO_1W *fi1=NULL;
  1730. LPBYTE buf=NULL;
  1731. PyObject *ret=NULL;
  1732. CHECK_PFN(GetForm);
  1733. if (!PyArg_ParseTuple(args,"O&u:GetForm", PyWinObject_AsPrinterHANDLE, &hprinter, &formname))
  1734. return NULL;
  1735. (*pfnGetForm)(hprinter, formname, level, buf, bufsize, &bytes_needed);
  1736. if (bytes_needed==0)
  1737. return PyWin_SetAPIError("GetForm");
  1738. buf=(LPBYTE)malloc(bytes_needed);
  1739. if (buf==NULL)
  1740. return PyErr_Format(PyExc_MemoryError,"GetForm: Unable to allocate %d bytes",bytes_needed);
  1741. bufsize=bytes_needed;
  1742. if (!(*pfnGetForm)(hprinter, formname, level, buf, bufsize, &bytes_needed))
  1743. PyWin_SetAPIError("GetForm");
  1744. else{
  1745. fi1=(FORM_INFO_1W *)buf;
  1746. ret=PyWin_Object_FromFORM_INFO_1(fi1);
  1747. }
  1748. free(buf);
  1749. return ret;
  1750. }
  1751. // @pymethod |win32print|SetForm|Change information for a form
  1752. static PyObject *PySetForm(PyObject *self, PyObject *args)
  1753. {
  1754. // @pyparm <o PyPrinterHANDLE>|hprinter||Printer handle as returned by <om win32print.OpenPrinter>
  1755. // @pyparm <o PyUnicode>|FormName||Name of form
  1756. // @pyparm dict|Form||<o FORM_INFO_1> dictionary
  1757. // @rdesc Returns None on success
  1758. FORM_INFO_1W fi1;
  1759. HANDLE hprinter;
  1760. WCHAR *formname;
  1761. CHECK_PFN(SetForm);
  1762. if (!PyArg_ParseTuple(args, "O&uO&:SetForm",
  1763. PyWinObject_AsPrinterHANDLE, &hprinter,
  1764. &formname,
  1765. PyWinObject_AsFORM_INFO_1, &fi1))
  1766. return NULL;
  1767. if (!(*pfnSetForm)(hprinter, formname, 1, (LPBYTE)&fi1))
  1768. return PyWin_SetAPIError("SetForm");
  1769. Py_INCREF(Py_None);
  1770. return Py_None;
  1771. }
  1772. // @pymethod |win32print|AddJob|Add a job to be spooled to a printer queue
  1773. static PyObject *PyAddJob(PyObject *self, PyObject *args)
  1774. {
  1775. // @rdesc Returns the file name to which data should be written and the job id of the new job
  1776. // @pyparm <o PyPrinterHANDLE>|hprinter||Printer handle as returned by <om win32print.OpenPrinter>
  1777. HANDLE hprinter;
  1778. DWORD level=1, bufsize, bytes_needed;
  1779. LPBYTE buf=NULL;
  1780. PyObject *ret=NULL;
  1781. BOOL bsuccess;
  1782. CHECK_PFN(AddJob);
  1783. if (!PyArg_ParseTuple(args,"O&:AddJob", PyWinObject_AsPrinterHANDLE, &hprinter))
  1784. return NULL;
  1785. bufsize=sizeof(ADDJOB_INFO_1)+ (MAX_PATH*sizeof(WCHAR));
  1786. buf=(LPBYTE)malloc(bufsize);
  1787. if (buf==NULL)
  1788. return PyErr_Format(PyExc_MemoryError,"AddJob: unable to allocate %d bytes",bufsize);
  1789. bsuccess=(*pfnAddJob)(hprinter, level, buf, bufsize, &bytes_needed);
  1790. if (!bsuccess)
  1791. if (bytes_needed > bufsize){
  1792. free(buf);
  1793. buf=(LPBYTE)malloc(bytes_needed);
  1794. if (buf==NULL)
  1795. return PyErr_Format(PyExc_MemoryError,"AddJob: unable to allocate %d bytes",bytes_needed);
  1796. bufsize=bytes_needed;
  1797. bsuccess=(*pfnAddJob)(hprinter, level, buf, bufsize, &bytes_needed);
  1798. }
  1799. if (!bsuccess)
  1800. PyWin_SetAPIError("AddJob");
  1801. else
  1802. ret=Py_BuildValue("uk",((ADDJOB_INFO_1 *)buf)->Path,((ADDJOB_INFO_1 *)buf)->JobId);
  1803. if (buf!=NULL)
  1804. free(buf);
  1805. return ret;
  1806. }
  1807. // @pymethod |win32print|ScheduleJob|Schedules a spooled job to be printed
  1808. static PyObject *PyScheduleJob(PyObject *self, PyObject *args)
  1809. {
  1810. // @pyparm <o PyPrinterHANDLE>|hprinter||Printer handle as returned by <om win32print.OpenPrinter>
  1811. // @pyparm int|JobId||Job Id as returned by <om win32print.AddJob>
  1812. HANDLE hprinter;
  1813. DWORD jobid;
  1814. CHECK_PFN(ScheduleJob);
  1815. if (!PyArg_ParseTuple(args,"O&k:ScheduleJob", PyWinObject_AsPrinterHANDLE, &hprinter, &jobid))
  1816. return NULL;
  1817. if (!(*pfnScheduleJob)(hprinter, jobid)){
  1818. PyWin_SetAPIError("ScheduleJob");
  1819. return NULL;
  1820. }
  1821. Py_INCREF(Py_None);
  1822. return Py_None;
  1823. }
  1824. // @pymethod |win32print|DeviceCapabilities|Queries a printer for its capabilities
  1825. static PyObject *PyDeviceCapabilities(PyObject *self, PyObject *args)
  1826. {
  1827. // @pyparm string|Device||Name of printer
  1828. // @pyparm string|Port||Port that printer is using
  1829. // @pyparm int|Capability||Type of capability to return - DC_* constant
  1830. // @pyparm <o PyDEVMODE>|DEVMODE|None|If present, function returns values from it, otherwise the printer defaults are used
  1831. TCHAR *device=NULL, *port=NULL;
  1832. PyObject *obdevice, *obport;
  1833. WORD capability;
  1834. LPTSTR buf=NULL;
  1835. PDEVMODE pdevmode;
  1836. PyObject *obdevmode=Py_None, *ret=NULL, *tuple_item;
  1837. DWORD result, bufsize, bufindex;
  1838. static DWORD papernamesize=64; // same for DC_PAPERNAMES, DC_MEDIATYPENAMES, DC_MEDIAREADY, DC_FILEDEPENDENCIES
  1839. static DWORD binnamesize=24; // DC_BINNAMES
  1840. static DWORD personalitysize=32; // DC_PERSONALITY
  1841. DWORD retsize;
  1842. if (!PyArg_ParseTuple(args,"OOh|O:DeviceCapabilities", &obdevice, &obport, &capability, &obdevmode))
  1843. return NULL;
  1844. if (!PyWinObject_AsTCHAR(obdevice, &device, FALSE))
  1845. goto done;
  1846. if (!PyWinObject_AsTCHAR(obport, &port, FALSE))
  1847. goto done;
  1848. if (!PyWinObject_AsDEVMODE(obdevmode, &pdevmode, TRUE))
  1849. goto done;
  1850. result=DeviceCapabilities(device,port,capability,buf,pdevmode);
  1851. if (result==-1){
  1852. PyWin_SetAPIError("DeviceCapabilities");
  1853. goto done;
  1854. }
  1855. // @flagh Capability|Returned value
  1856. switch (capability){
  1857. // none of these use the output pointer, just the returned DWORD
  1858. case DC_BINADJUST:
  1859. case DC_COLLATE:
  1860. case DC_COPIES:
  1861. case DC_COLORDEVICE:
  1862. case DC_DUPLEX:
  1863. case DC_DRIVER:
  1864. case DC_EMF_COMPLIANT:
  1865. case DC_EXTRA:
  1866. case DC_FIELDS:
  1867. case DC_ORIENTATION:
  1868. case DC_PRINTRATE:
  1869. case DC_PRINTRATEPPM:
  1870. case DC_PRINTRATEUNIT:
  1871. case DC_PRINTERMEM:
  1872. case DC_SIZE:
  1873. case DC_STAPLE:
  1874. case DC_TRUETYPE:
  1875. case DC_VERSION:
  1876. ret=Py_BuildValue("k",result);
  1877. break;
  1878. // @flag DC_MINEXTENT|Dictionary containing minimum paper width and height
  1879. // @flag DC_MAXEXTENT|Dictionary containing maximum paper width and height
  1880. case DC_MINEXTENT:
  1881. case DC_MAXEXTENT:
  1882. ret=Py_BuildValue("{s:h,s:h}","Width",LOWORD(result),"Length",HIWORD(result));
  1883. break;
  1884. // @flag DC_ENUMRESOLUTIONS|Sequence of dictionaries containing x and y resolutions in DPI
  1885. case DC_ENUMRESOLUTIONS:{
  1886. // output is pairs of LONGs, result indicates number of pairs
  1887. PLONG presolutions;
  1888. LONG xres, yres;
  1889. bufsize=result*2*sizeof(LONG);
  1890. buf=(LPTSTR)malloc(bufsize);
  1891. if (buf==NULL){
  1892. PyErr_Format(PyExc_MemoryError,"DeviceCapabilites: Unable to allocate %d bytes",bufsize);
  1893. break;
  1894. }
  1895. result=DeviceCapabilities(device,port,capability,buf,pdevmode);
  1896. if (result==-1)
  1897. break;
  1898. ret=PyTuple_New(result);
  1899. if (ret==NULL)
  1900. break;
  1901. presolutions=(PLONG)buf;
  1902. for (bufindex=0;bufindex<result;bufindex++){
  1903. xres=*presolutions++;
  1904. yres=*presolutions++;
  1905. tuple_item=Py_BuildValue("{s:l,s:l}", "xdpi", xres, "ydpi", yres);
  1906. if (tuple_item==NULL){
  1907. Py_DECREF(ret);
  1908. ret=NULL;
  1909. break;
  1910. }
  1911. PyTuple_SET_ITEM(ret,bufindex,tuple_item);
  1912. }
  1913. break;
  1914. }
  1915. // @flag DC_PAPERS|Returns a sequence of ints, DMPAPER_* constants
  1916. // @flag DC_BINS|Returns a sequence of ints, DMBIN_* constants
  1917. case DC_PAPERS:
  1918. case DC_BINS:{
  1919. // output is an array of WORDs
  1920. WORD *pword;
  1921. retsize=sizeof(WORD);
  1922. bufsize=result*retsize;
  1923. buf=(LPTSTR)malloc(bufsize);
  1924. if (buf==NULL){
  1925. PyErr_Format(PyExc_MemoryError,"DeviceCapabilites: Unable to allocate %d bytes",bufsize);
  1926. break;
  1927. }
  1928. result=DeviceCapabilities(device,port,capability,buf,pdevmode);
  1929. if (result==-1)
  1930. break;
  1931. ret=PyTuple_New(result);
  1932. if (ret==NULL)
  1933. break;
  1934. pword=(WORD *)buf;
  1935. for (bufindex=0;bufindex<result;bufindex++){
  1936. tuple_item=Py_BuildValue("h", *pword++);
  1937. if (tuple_item==NULL){
  1938. Py_DECREF(ret);
  1939. ret=NULL;
  1940. break;
  1941. }
  1942. PyTuple_SET_ITEM(ret,bufindex,tuple_item);
  1943. }
  1944. break;
  1945. }
  1946. // @flag DC_NUP|Sequence of ints containing supported logical page per physical page settings
  1947. case DC_NUP:
  1948. // @flag DC_MEDIATYPES|Sequence of ints, DMMEDIA_* constants
  1949. case DC_MEDIATYPES:{
  1950. DWORD *pdword;
  1951. retsize=sizeof(DWORD);
  1952. bufsize=result*retsize;
  1953. buf=(LPTSTR)malloc(bufsize);
  1954. if (buf==NULL){
  1955. PyErr_Format(PyExc_MemoryError,"DeviceCapabilites: Unable to allocate %d bytes",bufsize);
  1956. break;
  1957. }
  1958. result=DeviceCapabilities(device,port,capability,buf,pdevmode);
  1959. if (result==-1)
  1960. break;
  1961. ret=PyTuple_New(result);
  1962. if (ret==NULL)
  1963. break;
  1964. pdword=(DWORD *)buf;
  1965. for (bufindex=0;bufindex<result;bufindex++){
  1966. tuple_item=PyLong_FromUnsignedLong(*pdword++);
  1967. if (tuple_item==NULL){
  1968. Py_DECREF(ret);
  1969. ret=NULL;
  1970. break;
  1971. }
  1972. PyTuple_SET_ITEM(ret,bufindex,tuple_item);
  1973. }
  1974. break;
  1975. }
  1976. // @flag DC_PAPERNAMES|Sequence of strings
  1977. // @flag DC_MEDIATYPENAMES|Sequence of strings
  1978. // @flag DC_MEDIAREADY|Sequence of strings
  1979. // @flag DC_FILEDEPENDENCIES|Sequence of strings
  1980. // @flag DC_PERSONALITY|Sequence of strings
  1981. // @flag DC_BINNAMES|Sequence of strings
  1982. case DC_PAPERNAMES:
  1983. case DC_MEDIATYPENAMES:
  1984. case DC_MEDIAREADY:
  1985. case DC_FILEDEPENDENCIES: // first 4 return array of 64-char strings
  1986. case DC_PERSONALITY: // returns 32-char strings
  1987. case DC_BINNAMES:{ // returns array of 24-char strings
  1988. TCHAR *retname;
  1989. if (capability==DC_BINNAMES)
  1990. retsize=binnamesize;
  1991. else if (capability==DC_PERSONALITY)
  1992. retsize=personalitysize;
  1993. else
  1994. retsize=papernamesize;
  1995. bufsize=result*retsize*sizeof(TCHAR);
  1996. buf=(LPTSTR)malloc(bufsize);
  1997. if (buf==NULL){
  1998. PyErr_Format(PyExc_MemoryError,"DeviceCapabilites: Unable to allocate %d bytes",bufsize);
  1999. break;
  2000. }
  2001. ZeroMemory(buf,bufsize);
  2002. result=DeviceCapabilities(device,port,capability,buf,pdevmode);
  2003. if (result==-1)
  2004. break;
  2005. ret=PyTuple_New(result);
  2006. if (ret==NULL)
  2007. break;
  2008. retname=(TCHAR *)buf;
  2009. for (bufindex=0;bufindex<result;bufindex++){
  2010. if (*(retname+retsize-1)==0)
  2011. tuple_item=PyWinObject_FromTCHAR(retname);
  2012. else // won't be null-terminated if string occupies entire space
  2013. tuple_item=PyWinObject_FromTCHAR(retname,retsize);
  2014. if (tuple_item==NULL){
  2015. Py_DECREF(ret);
  2016. ret=NULL;
  2017. break;
  2018. }
  2019. PyTuple_SET_ITEM(ret,bufindex,tuple_item);
  2020. retname+=retsize;
  2021. }
  2022. break;
  2023. }
  2024. // @flag DC_PAPERSIZE|Sequence of dicts containing paper sizes, in 1/10 millimeter units
  2025. // @flag All others|Output is an int
  2026. case DC_PAPERSIZE:{
  2027. // output is an array of POINTs
  2028. POINT *ppoint;
  2029. retsize=sizeof(POINT);
  2030. bufsize=result*retsize;
  2031. buf=(LPTSTR)malloc(bufsize);
  2032. if (buf==NULL){
  2033. PyErr_Format(PyExc_MemoryError,"DeviceCapabilites: Unable to allocate %d bytes",bufsize);
  2034. break;
  2035. }
  2036. result=DeviceCapabilities(device,port,capability,buf,pdevmode);
  2037. if (result==-1)
  2038. break;
  2039. ret=PyTuple_New(result);
  2040. if (ret==NULL)
  2041. break;
  2042. ppoint=(POINT *)buf;
  2043. for (bufindex=0;bufindex<result;bufindex++){
  2044. tuple_item=Py_BuildValue("{s:l,s:l}", "x",ppoint->x, "y",ppoint->y);
  2045. if (tuple_item==NULL){
  2046. Py_DECREF(ret);
  2047. ret=NULL;
  2048. break;
  2049. }
  2050. PyTuple_SET_ITEM(ret,bufindex,tuple_item);
  2051. ppoint++;
  2052. }
  2053. break;
  2054. }
  2055. // last 3 are 95/98/Me only
  2056. case DC_DATATYPE_PRODUCED:
  2057. case DC_MANUFACTURER:
  2058. case DC_MODEL:
  2059. default:
  2060. PyErr_Format(PyExc_NotImplementedError,"Type %d is not supported", capability);
  2061. }
  2062. if (result==-1)
  2063. PyWin_SetAPIError("DeviceCapabilities");
  2064. done:
  2065. if (buf!=NULL)
  2066. free(buf);
  2067. PyWinObject_FreeTCHAR(device);
  2068. PyWinObject_FreeTCHAR(port);
  2069. return ret;
  2070. }
  2071. // @pymethod int|win32print|GetDeviceCaps|Retrieves device-specific parameters and settings
  2072. // @comm Can also be used for Display DCs in addition to printer DCs
  2073. // @pyseeapi GetDeviceCaps
  2074. static PyObject *PyGetDeviceCaps(PyObject *self, PyObject *args)
  2075. {
  2076. PyObject *obdc;
  2077. DWORD index;
  2078. int ret;
  2079. HDC hdc;
  2080. if (!PyArg_ParseTuple(args, "Ok",
  2081. &obdc, // @pyparm <o PyHANDLE>|hdc||Handle to a printer or display device context
  2082. &index)) // @pyparm int|Index||The capability to return. See MSDN for valid values.
  2083. return NULL;
  2084. if (!PyWinObject_AsHANDLE(obdc, (HANDLE *)&hdc))
  2085. return NULL;
  2086. ret=GetDeviceCaps(hdc, index);
  2087. return Py_BuildValue("i", ret);
  2088. }
  2089. // @pymethod (dict,...)|win32print|EnumMonitors|Lists installed printer port monitors
  2090. static PyObject *PyEnumMonitors(PyObject *self, PyObject *args)
  2091. {
  2092. // @pyparm str/<o PyUnicode>|Name||Name of server, use None for local machine
  2093. // @pyparm int|Level||Level of information to return, 1 and 2 supported
  2094. // @rdesc Returns a sequence of dicts representing MONITOR_INFO_* structures depending on level
  2095. PyObject *ret=NULL, *tuple_item, *observer_name;
  2096. WCHAR *server_name=NULL;
  2097. DWORD level, bufsize=0, bytes_needed=0, return_cnt, buf_ind;
  2098. LPBYTE buf=NULL;
  2099. CHECK_PFN(EnumMonitors);
  2100. if (!PyArg_ParseTuple(args,"Ok:EnumMonitors", &observer_name, &level))
  2101. return NULL;
  2102. if (!PyWinObject_AsWCHAR(observer_name, &server_name, TRUE))
  2103. return NULL;
  2104. (*pfnEnumMonitors)(server_name, level, buf, bufsize, &bytes_needed, &return_cnt);
  2105. if (bytes_needed==0){
  2106. PyWin_SetAPIError("EnumMonitors");
  2107. goto done;
  2108. }
  2109. buf=(LPBYTE)malloc(bytes_needed);
  2110. if (buf==NULL){
  2111. PyErr_Format(PyExc_MemoryError,"EnumMonitors: unable to allocate buffer of size %d", bytes_needed);
  2112. goto done;
  2113. }
  2114. bufsize=bytes_needed;
  2115. if (!(*pfnEnumMonitors)(server_name, level, buf, bufsize, &bytes_needed, &return_cnt)){
  2116. PyWin_SetAPIError("EnumMonitors");
  2117. goto done;
  2118. }
  2119. ret=PyTuple_New(return_cnt);
  2120. if (ret==NULL)
  2121. goto done;
  2122. switch (level){
  2123. case 1:{
  2124. MONITOR_INFO_1W *mi1;
  2125. mi1=(MONITOR_INFO_1W *)buf;
  2126. for (buf_ind=0; buf_ind<return_cnt; buf_ind++){
  2127. tuple_item=Py_BuildValue("{s:u}","Name",mi1->pName);
  2128. if (tuple_item==NULL){
  2129. Py_DECREF(ret);
  2130. ret=NULL;
  2131. break;
  2132. }
  2133. PyTuple_SetItem(ret,buf_ind,tuple_item);
  2134. mi1++;
  2135. }
  2136. break;
  2137. }
  2138. case 2:{
  2139. MONITOR_INFO_2W *mi2;
  2140. mi2=(MONITOR_INFO_2W *)buf;
  2141. for (buf_ind=0; buf_ind<return_cnt; buf_ind++){
  2142. tuple_item=Py_BuildValue("{s:u,s:u,s:u}", "Name",mi2->pName,
  2143. "Environment",mi2->pEnvironment, "DLLName",mi2->pDLLName);
  2144. if (tuple_item==NULL){
  2145. Py_DECREF(ret);
  2146. ret=NULL;
  2147. break;
  2148. }
  2149. PyTuple_SetItem(ret,buf_ind,tuple_item);
  2150. mi2++;
  2151. }
  2152. break;
  2153. }
  2154. default:
  2155. PyErr_Format(PyExc_NotImplementedError,"EnumMonitors: Level %d is not supported", level);
  2156. }
  2157. done:
  2158. if (server_name!=NULL)
  2159. PyWinObject_FreeWCHAR(server_name);
  2160. if (buf!=NULL)
  2161. free(buf);
  2162. return ret;
  2163. }
  2164. // @pymethod (dict,...)|win32print|EnumPorts|Lists printer port on a server
  2165. static PyObject *PyEnumPorts(PyObject *self, PyObject *args)
  2166. {
  2167. // @pyparm str/<o PyUnicode>|Name||Name of server, use None for local machine
  2168. // @pyparm int|Level||Level of information to return, 1 and 2 supported
  2169. // @rdesc Returns a sequence of dicts representing PORT_INFO_* structures depending on level
  2170. PyObject *ret=NULL, *tuple_item, *observer_name;
  2171. WCHAR *server_name=NULL;
  2172. DWORD level, bufsize=0, bytes_needed=0, return_cnt, buf_ind;
  2173. LPBYTE buf=NULL;
  2174. CHECK_PFN(EnumPorts);
  2175. if (!PyArg_ParseTuple(args,"Ok:EnumPorts", &observer_name, &level))
  2176. return NULL;
  2177. if (!PyWinObject_AsWCHAR(observer_name, &server_name, TRUE))
  2178. return NULL;
  2179. (*pfnEnumPorts)(server_name, level, buf, bufsize, &bytes_needed, &return_cnt);
  2180. if (bytes_needed==0){
  2181. PyWin_SetAPIError("EnumPorts");
  2182. goto done;
  2183. }
  2184. buf=(LPBYTE)malloc(bytes_needed);
  2185. if (buf==NULL){
  2186. PyErr_Format(PyExc_MemoryError,"EnumPorts: unable to allocate buffer of size %d", bytes_needed);
  2187. goto done;
  2188. }
  2189. bufsize=bytes_needed;
  2190. if (!(*pfnEnumPorts)(server_name, level, buf, bufsize, &bytes_needed, &return_cnt)){
  2191. PyWin_SetAPIError("EnumPorts");
  2192. goto done;
  2193. }
  2194. ret=PyTuple_New(return_cnt);
  2195. if (ret==NULL)
  2196. goto done;
  2197. switch (level){
  2198. case 1:{
  2199. PORT_INFO_1W *pi1;
  2200. pi1=(PORT_INFO_1W *)buf;
  2201. for (buf_ind=0; buf_ind<return_cnt; buf_ind++){
  2202. tuple_item=Py_BuildValue("{s:u}","Name",pi1->pName);
  2203. if (tuple_item==NULL){
  2204. Py_DECREF(ret);
  2205. ret=NULL;
  2206. break;
  2207. }
  2208. PyTuple_SetItem(ret,buf_ind,tuple_item);
  2209. pi1++;
  2210. }
  2211. break;
  2212. }
  2213. case 2:{
  2214. PORT_INFO_2W *pi2;
  2215. pi2=(PORT_INFO_2W *)buf;
  2216. for (buf_ind=0; buf_ind<return_cnt; buf_ind++){
  2217. tuple_item=Py_BuildValue("{s:u,s:u,s:u,s:l,s:l}", "Name",pi2->pPortName,
  2218. "MonitorName",pi2->pMonitorName, "Description",pi2->pDescription,
  2219. "PortType",pi2->fPortType, "Reserved",pi2->Reserved);
  2220. if (tuple_item==NULL){
  2221. Py_DECREF(ret);
  2222. ret=NULL;
  2223. break;
  2224. }
  2225. PyTuple_SetItem(ret,buf_ind,tuple_item);
  2226. pi2++;
  2227. }
  2228. break;
  2229. }
  2230. default:
  2231. PyErr_Format(PyExc_NotImplementedError,"EnumPorts: Level %d is not supported", level);
  2232. }
  2233. done:
  2234. if (server_name!=NULL)
  2235. PyWinObject_FreeWCHAR(server_name);
  2236. if (buf!=NULL)
  2237. free(buf);
  2238. return ret;
  2239. }
  2240. // @pymethod <o PyUnicode>|win32print|GetPrintProcessorDirectory|Returns the directory where print processor files reside
  2241. static PyObject *PyGetPrintProcessorDirectory(PyObject *self, PyObject *args)
  2242. {
  2243. // @pyparm str/<o PyUnicode>|Name||Name of server, use None for local machine
  2244. // @pyparm str/<o PyUnicode>|Environment||Environment - eg 'Windows NT x86' - use None for current client environment
  2245. PyObject *ret=NULL, *observer_name=Py_None, *obenvironment=Py_None;
  2246. WCHAR *server_name=NULL, *environment=NULL;
  2247. DWORD level=1, bufsize=0, bytes_needed=0, bytes_returned=0;
  2248. LPBYTE buf=NULL;
  2249. CHECK_PFN(GetPrintProcessorDirectory);
  2250. if (!PyArg_ParseTuple(args,"|OO:GetPrintProcessorDirectory", &observer_name, &obenvironment))
  2251. return NULL;
  2252. if (!PyWinObject_AsWCHAR(observer_name, &server_name, TRUE))
  2253. return NULL;
  2254. if (!PyWinObject_AsWCHAR(obenvironment, &environment, TRUE))
  2255. return NULL;
  2256. (*pfnGetPrintProcessorDirectory)(server_name, environment, level, buf, bufsize, &bytes_needed);
  2257. if (bytes_needed==0){
  2258. PyWin_SetAPIError("GetPrintProcessorDirectory");
  2259. goto done;
  2260. }
  2261. buf=(LPBYTE)malloc(bytes_needed);
  2262. if (buf==NULL){
  2263. PyErr_Format(PyExc_MemoryError,"GetPrintProcessorDirectory: unable to allocate buffer of size %d", bytes_needed);
  2264. goto done;
  2265. }
  2266. bufsize=bytes_needed;
  2267. if (!(*pfnGetPrintProcessorDirectory)(server_name, environment, level, buf, bufsize, &bytes_needed))
  2268. PyWin_SetAPIError("GetPrintProcessorDirectory");
  2269. else
  2270. ret=PyWinObject_FromWCHAR((WCHAR *)buf);
  2271. done:
  2272. if (server_name!=NULL)
  2273. PyWinObject_FreeWCHAR(server_name);
  2274. if (environment!=NULL)
  2275. PyWinObject_FreeWCHAR(environment);
  2276. if (buf!=NULL)
  2277. free(buf);
  2278. return ret;
  2279. }
  2280. // @pymethod <o PyUnicode>|win32print|GetPrinterDriverDirectory|Returns the directory where printer drivers are installed
  2281. static PyObject *PyGetPrinterDriverDirectory(PyObject *self, PyObject *args)
  2282. {
  2283. // @pyparm str/<o PyUnicode>|Name||Name of server, use None for local machine
  2284. // @pyparm str/<o PyUnicode>|Environment||Environment - eg 'Windows NT x86' - use None for current client environment
  2285. PyObject *ret=NULL, *observer_name=Py_None, *obenvironment=Py_None;
  2286. WCHAR *server_name=NULL, *environment=NULL;
  2287. DWORD level=1, bufsize=0, bytes_needed=0, bytes_returned=0;
  2288. LPBYTE buf=NULL;
  2289. CHECK_PFN(GetPrinterDriverDirectory);
  2290. if (!PyArg_ParseTuple(args,"|OO:GetPrinterDriverDirectory", &observer_name, &obenvironment))
  2291. return NULL;
  2292. if (!PyWinObject_AsWCHAR(observer_name, &server_name, TRUE))
  2293. return NULL;
  2294. if (!PyWinObject_AsWCHAR(obenvironment, &environment, TRUE))
  2295. return NULL;
  2296. (*pfnGetPrinterDriverDirectory)(server_name, environment, level, buf, bufsize, &bytes_needed);
  2297. if (bytes_needed==0){
  2298. PyWin_SetAPIError("GetPrinterDriverDirectory");
  2299. goto done;
  2300. }
  2301. buf=(LPBYTE)malloc(bytes_needed);
  2302. if (buf==NULL){
  2303. PyErr_Format(PyExc_MemoryError,"GetPrinterDriverDirectory: unable to allocate buffer of size %d", bytes_needed);
  2304. goto done;
  2305. }
  2306. bufsize=bytes_needed;
  2307. if (!(*pfnGetPrinterDriverDirectory)(server_name, environment, level, buf, bufsize, &bytes_needed))
  2308. PyWin_SetAPIError("GetPrinterDriverDirectory");
  2309. else
  2310. ret=PyWinObject_FromWCHAR((WCHAR *)buf);
  2311. done:
  2312. if (server_name!=NULL)
  2313. PyWinObject_FreeWCHAR(server_name);
  2314. if (environment!=NULL)
  2315. PyWinObject_FreeWCHAR(environment);
  2316. if (buf!=NULL)
  2317. free(buf);
  2318. return ret;
  2319. }
  2320. // @pymethod <o PyPrinterHANDLE>|win32print|AddPrinter|Installs a printer on a server
  2321. // @rdesc Returns a handle to the new printer
  2322. static PyObject *PyAddPrinter(PyObject *self, PyObject *args)
  2323. {
  2324. HANDLE hprinter;
  2325. LPBYTE buf=NULL;
  2326. DWORD level;
  2327. PyObject *obinfo;
  2328. TCHAR *server_name=NULL;
  2329. PyObject *observer_name, *ret=NULL;
  2330. // @pyparm string|Name||Name of server on which to install printer, None indicates local machine
  2331. // @pyparm int|Level||Level of data contained in pPrinter, only level 2 currently supported
  2332. // @pyparm dict|pPrinter||PRINTER_INFO_2 dict as returned by <om win32print.GetPrinter>
  2333. // @comm pPrinterName, pPortName, pDriverName, and pPrintProcessor are required
  2334. if (!PyArg_ParseTuple(args, "OkO:AddPrinter", &observer_name, &level, &obinfo))
  2335. return NULL;
  2336. if (level!=2){
  2337. PyErr_SetString(PyExc_ValueError,"AddPrinter only accepts level 2");
  2338. return NULL;
  2339. }
  2340. if (PyWinObject_AsPRINTER_INFO(level, obinfo, &buf)
  2341. &&PyWinObject_AsTCHAR(observer_name, &server_name, TRUE)){
  2342. hprinter=AddPrinter(server_name, level, buf);
  2343. if (hprinter==NULL)
  2344. PyWin_SetAPIError("AddPrinter");
  2345. else
  2346. ret = PyWinObject_FromPrinterHANDLE(hprinter);
  2347. }
  2348. PyWinObject_FreePRINTER_INFO(level, buf);
  2349. PyWinObject_FreeTCHAR(server_name);
  2350. return ret;
  2351. }
  2352. // @pymethod |win32print|DeletePrinter|Deletes an existing printer
  2353. // @comm Printer handle must be opened for PRINTER_ACCESS_ADMINISTER
  2354. // If there are any pending print jobs for the printer, actual deletion does not happen until they are done
  2355. static PyObject *PyDeletePrinter(PyObject *self, PyObject *args)
  2356. {
  2357. // @pyparm <o PyPrinterHANDLE>|hPrinter||Handle to printer as returned by <om win32print.OpenPrinter> or <om win32print.AddPrinter>
  2358. HANDLE hprinter;
  2359. if (!PyArg_ParseTuple(args, "O&:DeletePrinter", PyWinObject_AsPrinterHANDLE, &hprinter))
  2360. return NULL;
  2361. if (!DeletePrinter(hprinter)){
  2362. PyWin_SetAPIError("DeletePrinter");
  2363. return NULL;
  2364. }
  2365. Py_INCREF(Py_None);
  2366. return Py_None;
  2367. }
  2368. // @pymethod |win32print|DeletePrinterDriver|Removes the specified printer driver from a server
  2369. static PyObject *PyDeletePrinterDriver(PyObject *self, PyObject *args)
  2370. {
  2371. PyObject *ret=NULL;
  2372. PyObject *observername, *obenvironment, *obdrivername;
  2373. WCHAR *servername=NULL, *environment=NULL, *drivername=NULL;
  2374. // @pyparm string/<o PyUnicode>|Server||Name of print server, use None for local machine
  2375. // @pyparm string/<o PyUnicode>|Environment||Environment - eg 'Windows NT x86' - use None for current client environment
  2376. // @pyparm string/<o PyUnicode>|DriverName||Name of driver to remove
  2377. // @comm Does not delete associated driver files - use <om win32print.DeletePrinterDriverEx> if this is required
  2378. if (PyArg_ParseTuple(args,"OOO:DeletePrinterDriver", &observername, &obenvironment, &obdrivername)
  2379. &&PyWinObject_AsWCHAR(observername, &servername, TRUE)
  2380. &&PyWinObject_AsWCHAR(obenvironment, &environment, TRUE)
  2381. &&PyWinObject_AsWCHAR(obdrivername, &drivername, FALSE))
  2382. if (DeletePrinterDriverW(servername, environment, drivername)){
  2383. Py_INCREF(Py_None);
  2384. ret=Py_None;
  2385. }
  2386. else
  2387. PyWin_SetAPIError("DeletePrinterDriver");
  2388. if (servername!=NULL)
  2389. PyWinObject_FreeWCHAR(servername);
  2390. if (environment!=NULL)
  2391. PyWinObject_FreeWCHAR(environment);
  2392. if (drivername!=NULL)
  2393. PyWinObject_FreeWCHAR(drivername);
  2394. return ret;
  2395. }
  2396. // @pymethod |win32print|DeletePrinterDriverEx|Deletes a printer driver and its associated files
  2397. static PyObject *PyDeletePrinterDriverEx(PyObject *self, PyObject *args)
  2398. {
  2399. PyObject *ret=NULL;
  2400. PyObject *observername, *obenvironment, *obdrivername;
  2401. WCHAR *servername=NULL, *environment=NULL, *drivername=NULL;
  2402. DWORD deleteflag, versionflag;
  2403. CHECK_PFN(DeletePrinterDriverEx);
  2404. // @pyparm string/<o PyUnicode>|Server||Name of print server, use None for local machine
  2405. // @pyparm string/<o PyUnicode>|Environment||Environment - eg 'Windows NT x86' - use None for current client environment
  2406. // @pyparm string/<o PyUnicode>|DriverName||Name of driver to remove
  2407. // @pyparm int|DeleteFlag||Combination of DPD_DELETE_SPECIFIC_VERSION, DPD_DELETE_UNUSED_FILES, and DPD_DELETE_ALL_FILES
  2408. // @pyparm int|VersionFlag||Can be 0,1,2, or 3. Only used if DPD_DELETE_SPECIFIC_VERSION is specified in DeleteFlag
  2409. if (PyArg_ParseTuple(args,"OOOll:DeletePrinterDriverEx", &observername, &obenvironment, &obdrivername,
  2410. &deleteflag, &versionflag)
  2411. &&PyWinObject_AsWCHAR(observername, &servername, TRUE)
  2412. &&PyWinObject_AsWCHAR(obenvironment, &environment, TRUE)
  2413. &&PyWinObject_AsWCHAR(obdrivername, &drivername, FALSE))
  2414. if ((*pfnDeletePrinterDriverEx)(servername, environment, drivername, deleteflag, versionflag)){
  2415. Py_INCREF(Py_None);
  2416. ret=Py_None;
  2417. }
  2418. else
  2419. PyWin_SetAPIError("DeletePrinterDriverEx");
  2420. if (servername!=NULL)
  2421. PyWinObject_FreeWCHAR(servername);
  2422. if (environment!=NULL)
  2423. PyWinObject_FreeWCHAR(environment);
  2424. if (drivername!=NULL)
  2425. PyWinObject_FreeWCHAR(drivername);
  2426. return ret;
  2427. }
  2428. // @pymethod int|win32print|FlushPrinter|Clears printer from error state if WritePrinter fails
  2429. // @rdesc Returns the number of bytes actually written to the printer
  2430. static PyObject *PyFlushPrinter(PyObject *self, PyObject *args)
  2431. {
  2432. CHECK_PFN(FlushPrinter);
  2433. HANDLE hprinter;
  2434. PyObject *obbuf;
  2435. void *buf;
  2436. Py_ssize_t bufsize;
  2437. DWORD bytes_written=0, sleep_ms;
  2438. if (!PyArg_ParseTuple(args, "O&Ok",
  2439. PyWinObject_AsPrinterHANDLE, &hprinter, // @pyparm <o PyPrinterHANDLE>|Printer||Handle to a printer
  2440. &obbuf, // @pyparm str|Buf||Data to be sent to printer
  2441. &sleep_ms)) // @pyparm int|Sleep||Number of milliseconds to suspend printer
  2442. return NULL;
  2443. if (PyString_AsStringAndSize(obbuf, (char **)&buf, &bufsize)==-1)
  2444. return NULL;
  2445. if (!(*pfnFlushPrinter)(hprinter, buf,
  2446. PyWin_SAFE_DOWNCAST(bufsize, Py_ssize_t, DWORD),
  2447. &bytes_written, sleep_ms))
  2448. return PyWin_SetAPIError("FlushPrinter");
  2449. return PyLong_FromUnsignedLong(bytes_written);
  2450. }
  2451. /* List of functions exported by this module */
  2452. // @module win32print|A module encapsulating the Windows printing API.
  2453. static struct PyMethodDef win32print_functions[] = {
  2454. {"OpenPrinter", PyOpenPrinter, 1}, // @pymeth OpenPrinter|Retrieves a handle to a printer.
  2455. {"GetPrinter", PyGetPrinter ,1}, // @pymeth GetPrinter|Retrieves information about a printer
  2456. {"SetPrinter", PySetPrinter, 1}, // @pymeth SetPrinter|Changes printer configuration and status
  2457. {"ClosePrinter", PyClosePrinter, 1}, // @pymeth ClosePrinter|Closes a handle to a printer.
  2458. {"AddPrinterConnection", PyAddPrinterConnection, 1}, // @pymeth AddPrinterConnection|Connects to a network printer.
  2459. {"DeletePrinterConnection", PyDeletePrinterConnection, 1}, // @pymeth DeletePrinterConnection|Disconnects from a network printer.
  2460. {"EnumPrinters", PyEnumPrinters, 1}, // @pymeth EnumPrinters|Enumerates printers, print servers, domains and print providers.
  2461. {"GetDefaultPrinter", PyGetDefaultPrinter, METH_NOARGS}, // @pymeth GetDefaultPrinter|Returns the default printer.
  2462. {"GetDefaultPrinterW", PyGetDefaultPrinterW, METH_NOARGS}, // @pymeth GetDefaultPrinterW|Returns the default printer.
  2463. {"SetDefaultPrinter", PySetDefaultPrinter, 1}, // @pymeth SetDefaultPrinter|Sets the default printer.
  2464. {"SetDefaultPrinterW", PySetDefaultPrinterW, 1}, // @pymeth SetDefaultPrinterW|Sets the default printer.
  2465. {"StartDocPrinter", PyStartDocPrinter, 1}, // @pymeth StartDocPrinter|Notifies the print spooler that a document is to be spooled for printing. Returns the Jobid of the started job.
  2466. {"EndDocPrinter", PyEndDocPrinter, 1}, // @pymeth EndDocPrinter|The EndDocPrinter function ends a print job for the specified printer.
  2467. {"AbortPrinter", PyAbortPrinter, 1}, // @pymeth AbortPrinter|Deletes spool file for printer
  2468. {"StartPagePrinter", PyStartPagePrinter, 1}, // @pymeth StartPagePrinter|Notifies the print spooler that a page is to be printed on specified printer
  2469. {"EndPagePrinter", PyEndPagePrinter, 1}, // @pymeth EndPagePrinter|Ends a page in a print job
  2470. {"StartDoc", PyStartDoc, 1}, // @pymeth StartDoc|Starts spooling a print job on a printer device context
  2471. {"EndDoc", PyEndDoc, 1}, // @pymeth EndDoc|Stops spooling a print job on a printer device context
  2472. {"AbortDoc", PyAbortDoc, 1}, // @pymeth AbortDoc|Cancels print job on a printer device context
  2473. {"StartPage", PyStartPage, 1}, // @pymeth StartPage|Starts a page on a printer device context
  2474. {"EndPage", PyEndPage, 1}, // @pymeth EndPage|Ends a page on a printer device context
  2475. {"WritePrinter", PyWritePrinter, 1}, // @pymeth WritePrinter|Copies the specified bytes to the specified printer. StartDocPrinter and EndDocPrinter should be called before and after. Returns number of bytes written to printer.
  2476. {"EnumJobs", PyEnumJobs, 1}, // @pymeth EnumJobs|Enumerates print jobs on specified printer.
  2477. {"GetJob", PyGetJob, 1}, // @pymeth GetJob|Returns dictionary of information about a specified print job.
  2478. {"SetJob", PySetJob, 1}, // @pymeth SetJob|Pause, cancel, resume, set priority levels on a print job.
  2479. {"DocumentProperties", PyDocumentProperties, 1}, //@pymeth DocumentProperties|Changes printer configuration
  2480. {"EnumPrintProcessors", PyEnumPrintProcessors, 1}, //@pymeth EnumPrintProcessors|List printer providers for specified server and environment
  2481. {"EnumPrintProcessorDatatypes", PyEnumPrintProcessorDatatypes, 1}, //@pymeth EnumPrintProcessorDatatypes|Lists data types that specified print provider supports
  2482. {"EnumPrinterDrivers", PyEnumPrinterDrivers, 1}, //@pymeth EnumPrinterDrivers|Lists installed printer drivers
  2483. {"EnumForms", PyEnumForms, 1}, //@pymeth EnumForms|Lists forms for a printer
  2484. {"AddForm", PyAddForm, 1}, //@pymeth AddForm|Adds a form for a printer
  2485. {"DeleteForm", PyDeleteForm, 1}, //@pymeth DeleteForm|Deletes a form defined for a printer
  2486. {"GetForm", PyGetForm, 1}, //@pymeth GetForm|Retrieves information about a defined form
  2487. {"SetForm", PySetForm, 1}, //@pymeth SetForm|Change information for a form
  2488. {"AddJob", PyAddJob, 1}, //@pymeth AddJob|Adds a job to be spooled to a printer queue
  2489. {"ScheduleJob", PyScheduleJob, 1}, //@pymeth ScheduleJob|Schedules a spooled job to be printed
  2490. {"DeviceCapabilities", PyDeviceCapabilities, 1}, //@pymeth DeviceCapabilities|Queries a printer for its capabilities
  2491. {"GetDeviceCaps", PyGetDeviceCaps, METH_VARARGS}, //@pymeth GetDeviceCaps|Retrieves device-specific parameters and settings
  2492. {"EnumMonitors", PyEnumMonitors, 1}, //@pymeth EnumMonitors|Lists installed printer port monitors
  2493. {"EnumPorts", PyEnumPorts, 1}, //@pymeth EnumPorts|Lists printer ports on a server
  2494. {"GetPrintProcessorDirectory", PyGetPrintProcessorDirectory, 1}, //@pymeth GetPrintProcessorDirectory|Returns the directory where print processor files reside
  2495. {"GetPrinterDriverDirectory", PyGetPrinterDriverDirectory, 1}, //@pymeth GetPrinterDriverDirectory|Returns the directory where printer drivers are installed
  2496. {"AddPrinter", PyAddPrinter, 1}, //@pymeth AddPrinter|Adds a new printer on a server
  2497. {"DeletePrinter", PyDeletePrinter, 1}, //@pymeth DeletePrinter|Deletes an existing printer
  2498. {"DeletePrinterDriver", PyDeletePrinterDriver,1}, //@pymeth DeletePrinterDriver|Deletes the specified driver from a server
  2499. {"DeletePrinterDriverEx", PyDeletePrinterDriverEx,1}, //@pymeth DeletePrinterDriverEx|Deletes a printer driver and associated files
  2500. {"FlushPrinter", PyFlushPrinter,1}, //@pymeth FlushPrinter|Clears printer from error state if WritePrinter fails
  2501. { NULL }
  2502. };
  2503. static void AddConstant(PyObject *dict, char *name, long val)
  2504. {
  2505. PyObject *nv = PyInt_FromLong(val);
  2506. PyDict_SetItemString(dict, name, nv );
  2507. Py_XDECREF(nv);
  2508. }
  2509. PYWIN_MODULE_INIT_FUNC(win32print)
  2510. {
  2511. PYWIN_MODULE_INIT_PREPARE(win32print, win32print_functions,
  2512. "A module encapsulating the Windows printing API.")
  2513. AddConstant(dict, "PRINTER_INFO_1", 1);
  2514. AddConstant(dict, "PRINTER_ENUM_LOCAL", PRINTER_ENUM_LOCAL);
  2515. AddConstant(dict, "PRINTER_ENUM_NAME", PRINTER_ENUM_NAME);
  2516. AddConstant(dict, "PRINTER_ENUM_SHARED", PRINTER_ENUM_SHARED);
  2517. AddConstant(dict, "PRINTER_ENUM_DEFAULT", PRINTER_ENUM_DEFAULT);
  2518. AddConstant(dict, "PRINTER_ENUM_CONNECTIONS", PRINTER_ENUM_CONNECTIONS);
  2519. AddConstant(dict, "PRINTER_ENUM_NETWORK", PRINTER_ENUM_NETWORK);
  2520. AddConstant(dict, "PRINTER_ENUM_REMOTE", PRINTER_ENUM_REMOTE);
  2521. AddConstant(dict, "PRINTER_ENUM_EXPAND", PRINTER_ENUM_EXPAND);
  2522. AddConstant(dict, "PRINTER_ENUM_CONTAINER", PRINTER_ENUM_CONTAINER);
  2523. AddConstant(dict, "PRINTER_ENUM_ICON1", PRINTER_ENUM_ICON1);
  2524. AddConstant(dict, "PRINTER_ENUM_ICON2", PRINTER_ENUM_ICON2);
  2525. AddConstant(dict, "PRINTER_ENUM_ICON3", PRINTER_ENUM_ICON3);
  2526. AddConstant(dict, "PRINTER_ENUM_ICON4", PRINTER_ENUM_ICON4);
  2527. AddConstant(dict, "PRINTER_ENUM_ICON5", PRINTER_ENUM_ICON5);
  2528. AddConstant(dict, "PRINTER_ENUM_ICON6", PRINTER_ENUM_ICON6);
  2529. AddConstant(dict, "PRINTER_ENUM_ICON7", PRINTER_ENUM_ICON7);
  2530. AddConstant(dict, "PRINTER_ENUM_ICON8", PRINTER_ENUM_ICON8);
  2531. // Status member of JOB_INFO_1 and JOB_INFO_2
  2532. AddConstant(dict, "JOB_STATUS_DELETING", JOB_STATUS_DELETING);
  2533. AddConstant(dict, "JOB_STATUS_ERROR", JOB_STATUS_ERROR);
  2534. AddConstant(dict, "JOB_STATUS_OFFLINE", JOB_STATUS_OFFLINE);
  2535. AddConstant(dict, "JOB_STATUS_PAPEROUT", JOB_STATUS_PAPEROUT);
  2536. AddConstant(dict, "JOB_STATUS_PAUSED", JOB_STATUS_PAUSED);
  2537. AddConstant(dict, "JOB_STATUS_PRINTED", JOB_STATUS_PRINTED);
  2538. AddConstant(dict, "JOB_STATUS_PRINTING", JOB_STATUS_PRINTING);
  2539. AddConstant(dict, "JOB_STATUS_SPOOLING", JOB_STATUS_SPOOLING);
  2540. AddConstant(dict, "JOB_STATUS_DELETED", JOB_STATUS_DELETED);
  2541. AddConstant(dict, "JOB_STATUS_BLOCKED_DEVQ", JOB_STATUS_BLOCKED_DEVQ);
  2542. AddConstant(dict, "JOB_STATUS_USER_INTERVENTION", JOB_STATUS_USER_INTERVENTION);
  2543. AddConstant(dict, "JOB_STATUS_RESTART", JOB_STATUS_RESTART);
  2544. AddConstant(dict, "JOB_STATUS_COMPLETE", JOB_STATUS_COMPLETE);
  2545. AddConstant(dict, "MIN_PRIORITY", MIN_PRIORITY);
  2546. AddConstant(dict, "MAX_PRIORITY", MAX_PRIORITY);
  2547. AddConstant(dict, "DEF_PRIORITY", DEF_PRIORITY);
  2548. AddConstant(dict, "JOB_INFO_1", 1);
  2549. // Job control codes used with SetJob
  2550. AddConstant(dict, "JOB_CONTROL_CANCEL", JOB_CONTROL_CANCEL);
  2551. AddConstant(dict, "JOB_CONTROL_PAUSE", JOB_CONTROL_PAUSE);
  2552. AddConstant(dict, "JOB_CONTROL_RESTART", JOB_CONTROL_RESTART);
  2553. AddConstant(dict, "JOB_CONTROL_RESUME", JOB_CONTROL_RESUME);
  2554. AddConstant(dict, "JOB_CONTROL_DELETE", JOB_CONTROL_DELETE);
  2555. AddConstant(dict, "JOB_CONTROL_SENT_TO_PRINTER", JOB_CONTROL_SENT_TO_PRINTER);
  2556. AddConstant(dict, "JOB_CONTROL_LAST_PAGE_EJECTED", JOB_CONTROL_LAST_PAGE_EJECTED);
  2557. AddConstant(dict, "JOB_POSITION_UNSPECIFIED", JOB_POSITION_UNSPECIFIED);
  2558. AddConstant(dict, "DI_APPBANDING", DI_APPBANDING);
  2559. AddConstant(dict, "DI_ROPS_READ_DESTINATION", DI_ROPS_READ_DESTINATION);
  2560. AddConstant(dict, "FORM_USER", FORM_USER);
  2561. AddConstant(dict, "FORM_BUILTIN", FORM_BUILTIN);
  2562. AddConstant(dict, "FORM_PRINTER", FORM_PRINTER);
  2563. // Printer, print server, and print job access rights
  2564. AddConstant(dict, "SERVER_ACCESS_ADMINISTER",SERVER_ACCESS_ADMINISTER);
  2565. AddConstant(dict, "SERVER_ACCESS_ENUMERATE",SERVER_ACCESS_ENUMERATE);
  2566. AddConstant(dict, "PRINTER_ACCESS_ADMINISTER",PRINTER_ACCESS_ADMINISTER);
  2567. AddConstant(dict, "PRINTER_ACCESS_USE",PRINTER_ACCESS_USE);
  2568. AddConstant(dict, "JOB_ACCESS_ADMINISTER",JOB_ACCESS_ADMINISTER);
  2569. AddConstant(dict, "JOB_ACCESS_READ",JOB_ACCESS_READ);
  2570. AddConstant(dict, "SERVER_ALL_ACCESS",SERVER_ALL_ACCESS);
  2571. AddConstant(dict, "SERVER_READ",SERVER_READ);
  2572. AddConstant(dict, "SERVER_WRITE",SERVER_WRITE);
  2573. AddConstant(dict, "SERVER_EXECUTE",SERVER_EXECUTE);
  2574. AddConstant(dict, "PRINTER_ALL_ACCESS",PRINTER_ALL_ACCESS);
  2575. AddConstant(dict, "PRINTER_READ",PRINTER_READ);
  2576. AddConstant(dict, "PRINTER_WRITE",PRINTER_WRITE);
  2577. AddConstant(dict, "PRINTER_EXECUTE",PRINTER_EXECUTE);
  2578. AddConstant(dict, "JOB_ALL_ACCESS",JOB_ALL_ACCESS);
  2579. AddConstant(dict, "JOB_READ",JOB_READ);
  2580. AddConstant(dict, "JOB_WRITE",JOB_WRITE);
  2581. AddConstant(dict, "JOB_EXECUTE",JOB_EXECUTE);
  2582. // Command values for SetPrinter
  2583. AddConstant(dict, "PRINTER_CONTROL_PAUSE",PRINTER_CONTROL_PAUSE);
  2584. AddConstant(dict, "PRINTER_CONTROL_PURGE",PRINTER_CONTROL_PURGE);
  2585. AddConstant(dict, "PRINTER_CONTROL_SET_STATUS",PRINTER_CONTROL_SET_STATUS);
  2586. AddConstant(dict, "PRINTER_CONTROL_RESUME",PRINTER_CONTROL_RESUME);
  2587. // printer status constants
  2588. AddConstant(dict, "PRINTER_STATUS_PAUSED",PRINTER_STATUS_PAUSED);
  2589. AddConstant(dict, "PRINTER_STATUS_ERROR",PRINTER_STATUS_ERROR);
  2590. AddConstant(dict, "PRINTER_STATUS_PENDING_DELETION",PRINTER_STATUS_PENDING_DELETION);
  2591. AddConstant(dict, "PRINTER_STATUS_PAPER_JAM",PRINTER_STATUS_PAPER_JAM);
  2592. AddConstant(dict, "PRINTER_STATUS_PAPER_OUT",PRINTER_STATUS_PAPER_OUT);
  2593. AddConstant(dict, "PRINTER_STATUS_MANUAL_FEED",PRINTER_STATUS_MANUAL_FEED);
  2594. AddConstant(dict, "PRINTER_STATUS_PAPER_PROBLEM",PRINTER_STATUS_PAPER_PROBLEM);
  2595. AddConstant(dict, "PRINTER_STATUS_OFFLINE",PRINTER_STATUS_OFFLINE);
  2596. AddConstant(dict, "PRINTER_STATUS_IO_ACTIVE",PRINTER_STATUS_IO_ACTIVE);
  2597. AddConstant(dict, "PRINTER_STATUS_BUSY",PRINTER_STATUS_BUSY);
  2598. AddConstant(dict, "PRINTER_STATUS_PRINTING",PRINTER_STATUS_PRINTING);
  2599. AddConstant(dict, "PRINTER_STATUS_OUTPUT_BIN_FULL",PRINTER_STATUS_OUTPUT_BIN_FULL);
  2600. AddConstant(dict, "PRINTER_STATUS_NOT_AVAILABLE",PRINTER_STATUS_NOT_AVAILABLE);
  2601. AddConstant(dict, "PRINTER_STATUS_WAITING",PRINTER_STATUS_WAITING);
  2602. AddConstant(dict, "PRINTER_STATUS_PROCESSING",PRINTER_STATUS_PROCESSING);
  2603. AddConstant(dict, "PRINTER_STATUS_INITIALIZING",PRINTER_STATUS_INITIALIZING);
  2604. AddConstant(dict, "PRINTER_STATUS_WARMING_UP",PRINTER_STATUS_WARMING_UP);
  2605. AddConstant(dict, "PRINTER_STATUS_TONER_LOW",PRINTER_STATUS_TONER_LOW);
  2606. AddConstant(dict, "PRINTER_STATUS_NO_TONER",PRINTER_STATUS_NO_TONER);
  2607. AddConstant(dict, "PRINTER_STATUS_PAGE_PUNT",PRINTER_STATUS_PAGE_PUNT);
  2608. AddConstant(dict, "PRINTER_STATUS_USER_INTERVENTION",PRINTER_STATUS_USER_INTERVENTION);
  2609. AddConstant(dict, "PRINTER_STATUS_OUT_OF_MEMORY",PRINTER_STATUS_OUT_OF_MEMORY);
  2610. AddConstant(dict, "PRINTER_STATUS_DOOR_OPEN",PRINTER_STATUS_DOOR_OPEN);
  2611. AddConstant(dict, "PRINTER_STATUS_SERVER_UNKNOWN",PRINTER_STATUS_SERVER_UNKNOWN);
  2612. AddConstant(dict, "PRINTER_STATUS_POWER_SAVE",PRINTER_STATUS_POWER_SAVE);
  2613. // attribute flags for PRINTER_INFO_2
  2614. AddConstant(dict, "PRINTER_ATTRIBUTE_QUEUED",PRINTER_ATTRIBUTE_QUEUED);
  2615. AddConstant(dict, "PRINTER_ATTRIBUTE_DIRECT",PRINTER_ATTRIBUTE_DIRECT);
  2616. AddConstant(dict, "PRINTER_ATTRIBUTE_DEFAULT",PRINTER_ATTRIBUTE_DEFAULT);
  2617. AddConstant(dict, "PRINTER_ATTRIBUTE_SHARED",PRINTER_ATTRIBUTE_SHARED);
  2618. AddConstant(dict, "PRINTER_ATTRIBUTE_NETWORK",PRINTER_ATTRIBUTE_NETWORK);
  2619. AddConstant(dict, "PRINTER_ATTRIBUTE_HIDDEN",PRINTER_ATTRIBUTE_HIDDEN);
  2620. AddConstant(dict, "PRINTER_ATTRIBUTE_LOCAL",PRINTER_ATTRIBUTE_LOCAL);
  2621. AddConstant(dict, "PRINTER_ATTRIBUTE_ENABLE_DEVQ",PRINTER_ATTRIBUTE_ENABLE_DEVQ);
  2622. AddConstant(dict, "PRINTER_ATTRIBUTE_KEEPPRINTEDJOBS",PRINTER_ATTRIBUTE_KEEPPRINTEDJOBS);
  2623. AddConstant(dict, "PRINTER_ATTRIBUTE_DO_COMPLETE_FIRST",PRINTER_ATTRIBUTE_DO_COMPLETE_FIRST);
  2624. AddConstant(dict, "PRINTER_ATTRIBUTE_WORK_OFFLINE",PRINTER_ATTRIBUTE_WORK_OFFLINE);
  2625. AddConstant(dict, "PRINTER_ATTRIBUTE_ENABLE_BIDI",PRINTER_ATTRIBUTE_ENABLE_BIDI);
  2626. AddConstant(dict, "PRINTER_ATTRIBUTE_RAW_ONLY",PRINTER_ATTRIBUTE_RAW_ONLY);
  2627. AddConstant(dict, "PRINTER_ATTRIBUTE_PUBLISHED",PRINTER_ATTRIBUTE_PUBLISHED);
  2628. AddConstant(dict, "PRINTER_ATTRIBUTE_FAX",PRINTER_ATTRIBUTE_FAX);
  2629. AddConstant(dict, "PRINTER_ATTRIBUTE_TS",PRINTER_ATTRIBUTE_TS);
  2630. // directory service contants for Action member of PRINTER_INFO_7
  2631. AddConstant(dict, "DSPRINT_PUBLISH",DSPRINT_PUBLISH);
  2632. AddConstant(dict, "DSPRINT_UNPUBLISH",DSPRINT_UNPUBLISH);
  2633. AddConstant(dict, "DSPRINT_UPDATE",DSPRINT_UPDATE);
  2634. AddConstant(dict, "DSPRINT_PENDING",DSPRINT_PENDING);
  2635. AddConstant(dict, "DSPRINT_REPUBLISH",DSPRINT_REPUBLISH);
  2636. // port types from PORT_INFO_2
  2637. AddConstant(dict, "PORT_TYPE_WRITE",PORT_TYPE_WRITE);
  2638. AddConstant(dict, "PORT_TYPE_READ",PORT_TYPE_READ);
  2639. AddConstant(dict, "PORT_TYPE_REDIRECTED",PORT_TYPE_REDIRECTED);
  2640. AddConstant(dict, "PORT_TYPE_NET_ATTACHED",PORT_TYPE_NET_ATTACHED);
  2641. // DeletePrinterDriverEx DeleteFlag
  2642. AddConstant(dict, "DPD_DELETE_SPECIFIC_VERSION",DPD_DELETE_SPECIFIC_VERSION);
  2643. AddConstant(dict, "DPD_DELETE_UNUSED_FILES",DPD_DELETE_UNUSED_FILES);
  2644. AddConstant(dict, "DPD_DELETE_ALL_FILES",DPD_DELETE_ALL_FILES);
  2645. // Port status and severity used in PORT_INFO_3
  2646. AddConstant(dict, "PORT_STATUS_OFFLINE",PORT_STATUS_OFFLINE);
  2647. AddConstant(dict, "PORT_STATUS_PAPER_JAM",PORT_STATUS_PAPER_JAM);
  2648. AddConstant(dict, "PORT_STATUS_PAPER_OUT",PORT_STATUS_PAPER_OUT);
  2649. AddConstant(dict, "PORT_STATUS_OUTPUT_BIN_FULL",PORT_STATUS_OUTPUT_BIN_FULL);
  2650. AddConstant(dict, "PORT_STATUS_PAPER_PROBLEM",PORT_STATUS_PAPER_PROBLEM);
  2651. AddConstant(dict, "PORT_STATUS_NO_TONER",PORT_STATUS_NO_TONER);
  2652. AddConstant(dict, "PORT_STATUS_DOOR_OPEN",PORT_STATUS_DOOR_OPEN);
  2653. AddConstant(dict, "PORT_STATUS_USER_INTERVENTION",PORT_STATUS_USER_INTERVENTION);
  2654. AddConstant(dict, "PORT_STATUS_OUT_OF_MEMORY",PORT_STATUS_OUT_OF_MEMORY);
  2655. AddConstant(dict, "PORT_STATUS_TONER_LOW",PORT_STATUS_TONER_LOW);
  2656. AddConstant(dict, "PORT_STATUS_WARMING_UP",PORT_STATUS_WARMING_UP);
  2657. AddConstant(dict, "PORT_STATUS_POWER_SAVE",PORT_STATUS_POWER_SAVE);
  2658. AddConstant(dict, "PORT_STATUS_TYPE_ERROR",PORT_STATUS_TYPE_ERROR);
  2659. AddConstant(dict, "PORT_STATUS_TYPE_WARNING",PORT_STATUS_TYPE_WARNING);
  2660. AddConstant(dict, "PORT_STATUS_TYPE_INFO",PORT_STATUS_TYPE_INFO);
  2661. HMODULE hmodule=LoadLibrary(TEXT("winspool.drv"));
  2662. if (hmodule!=NULL){
  2663. pfnEnumForms=(EnumFormsfunc)GetProcAddress(hmodule,"EnumFormsW");
  2664. pfnAddForm=(AddFormfunc)GetProcAddress(hmodule,"AddFormW");
  2665. pfnDeleteForm=(DeleteFormfunc)GetProcAddress(hmodule,"DeleteFormW");
  2666. pfnGetForm=(GetFormfunc)GetProcAddress(hmodule,"GetFormW");
  2667. pfnSetForm=(SetFormfunc)GetProcAddress(hmodule,"SetFormW");
  2668. pfnAddJob=(AddJobfunc)GetProcAddress(hmodule,"AddJobW");
  2669. pfnScheduleJob=(ScheduleJobfunc)GetProcAddress(hmodule,"ScheduleJob");
  2670. pfnEnumPorts=(EnumPortsfunc)GetProcAddress(hmodule,"EnumPortsW");
  2671. pfnEnumMonitors=(EnumPortsfunc)GetProcAddress(hmodule,"EnumMonitorsW");
  2672. pfnGetPrintProcessorDirectory=(GetPrintProcessorDirectoryfunc)GetProcAddress(hmodule,"GetPrintProcessorDirectoryW");
  2673. pfnGetPrinterDriverDirectory=(GetPrintProcessorDirectoryfunc)GetProcAddress(hmodule,"GetPrinterDriverDirectoryW");
  2674. pfnDeletePrinterDriverEx=(DeletePrinterDriverExfunc)GetProcAddress(hmodule,"DeletePrinterDriverExW");
  2675. pfnFlushPrinter=(FlushPrinterfunc)GetProcAddress(hmodule, "FlushPrinter");
  2676. pfnGetDefaultPrinter=(GetDefaultPrinterfunc)GetProcAddress(hmodule, "GetDefaultPrinterW");
  2677. pfnSetDefaultPrinter=(SetDefaultPrinterfunc)GetProcAddress(hmodule, "SetDefaultPrinterW");
  2678. }
  2679. dummy_tuple=PyTuple_New(0);
  2680. PYWIN_MODULE_INIT_RETURN_SUCCESS;
  2681. }