Fix a memory leak in X11DRV_GetTextExtentPoint.
[wine.git] / dlls / gdi / printdrv.c
blobade53907fafa88713b03171f102df9b8df388663
1 /*
2 * Implementation of some printer driver bits
3 *
4 * Copyright 1996 John Harvey
5 * Copyright 1998 Huw Davies
6 * Copyright 1998 Andreas Mohr
7 * Copyright 1999 Klaas van Gend
8 */
10 #include <stdlib.h>
11 #include <string.h>
12 #include <ctype.h>
13 #include <errno.h>
14 #include <unistd.h>
15 #include <fcntl.h>
16 #include "ldt.h"
17 #include "winbase.h"
18 #include "wine/wingdi16.h"
19 #include "winspool.h"
20 #include "winerror.h"
21 #include "winreg.h"
22 #include "debugtools.h"
23 #include "gdi.h"
24 #include "dc.h"
25 #include "callback.h"
26 #include "options.h"
27 #include "heap.h"
28 #include "file.h"
30 DEFAULT_DEBUG_CHANNEL(print)
32 static char PrinterModel[] = "Printer Model";
33 static char DefaultDevMode[] = "Default DevMode";
34 static char PrinterDriverData[] = "PrinterDriverData";
35 static char Printers[] = "System\\CurrentControlSet\\Control\\Print\\Printers\\";
37 /******************************************************************
38 * StartDoc16 [GDI.377]
41 INT16 WINAPI StartDoc16( HDC16 hdc, const DOCINFO16 *lpdoc )
43 DOCINFOA docA;
45 docA.cbSize = lpdoc->cbSize;
46 docA.lpszDocName = PTR_SEG_TO_LIN(lpdoc->lpszDocName);
47 docA.lpszOutput = PTR_SEG_TO_LIN(lpdoc->lpszOutput);
49 if(lpdoc->cbSize >= 14)
50 docA.lpszDatatype = PTR_SEG_TO_LIN(lpdoc->lpszDatatype);
51 else
52 docA.lpszDatatype = NULL;
54 if(lpdoc->cbSize >= 18)
55 docA.fwType = lpdoc->fwType;
56 else
57 docA.fwType = 0;
59 return StartDocA(hdc, &docA);
62 /******************************************************************
63 * StartDocA [GDI32.347]
65 * StartDoc calls the STARTDOC Escape with the input data pointing to DocName
66 * and the output data (which is used as a second input parameter).pointing at
67 * the whole docinfo structure. This seems to be an undocumented feature of
68 * the STARTDOC Escape.
70 INT WINAPI StartDocA(HDC hdc, const DOCINFOA* doc)
72 INT ret;
73 DC *dc = DC_GetDCPtr( hdc );
75 TRACE("DocName = '%s' Output = '%s' Datatype = '%s'\n",
76 doc->lpszDocName, doc->lpszOutput, doc->lpszDatatype);
78 if(!dc) return SP_ERROR;
80 if(dc->funcs->pStartDoc)
81 ret = dc->funcs->pStartDoc( dc, doc );
82 else
83 ret = Escape(hdc, STARTDOC, strlen(doc->lpszDocName),
84 doc->lpszDocName, (LPVOID)doc);
85 GDI_ReleaseObj( hdc );
86 return ret;
89 /*************************************************************************
90 * StartDocW [GDI32.348]
93 INT WINAPI StartDocW(HDC hdc, const DOCINFOW* doc)
95 DOCINFOA docA;
96 INT ret;
98 docA.cbSize = doc->cbSize;
99 docA.lpszDocName = doc->lpszDocName ?
100 HEAP_strdupWtoA( GetProcessHeap(), 0, doc->lpszDocName ) : NULL;
101 docA.lpszOutput = doc->lpszOutput ?
102 HEAP_strdupWtoA( GetProcessHeap(), 0, doc->lpszOutput ) : NULL;
103 docA.lpszDatatype = doc->lpszDatatype ?
104 HEAP_strdupWtoA( GetProcessHeap(), 0, doc->lpszDatatype ) : NULL;
105 docA.fwType = doc->fwType;
107 ret = StartDocA(hdc, &docA);
109 if(docA.lpszDocName)
110 HeapFree( GetProcessHeap(), 0, (LPSTR)docA.lpszDocName );
111 if(docA.lpszOutput)
112 HeapFree( GetProcessHeap(), 0, (LPSTR)docA.lpszOutput );
113 if(docA.lpszDatatype)
114 HeapFree( GetProcessHeap(), 0, (LPSTR)docA.lpszDatatype );
116 return ret;
119 /******************************************************************
120 * EndDoc16 [GDI.378]
123 INT16 WINAPI EndDoc16(HDC16 hdc)
125 return EndDoc(hdc);
128 /******************************************************************
129 * EndDoc [GDI32.76]
132 INT WINAPI EndDoc(HDC hdc)
134 INT ret;
135 DC *dc = DC_GetDCPtr( hdc );
136 if(!dc) return SP_ERROR;
138 if(dc->funcs->pEndDoc)
139 ret = dc->funcs->pEndDoc( dc );
140 else
141 ret = Escape(hdc, ENDDOC, 0, 0, 0);
142 GDI_ReleaseObj( hdc );
143 return ret;
146 /******************************************************************
147 * StartPage16 [GDI.379]
150 INT16 WINAPI StartPage16(HDC16 hdc)
152 return StartPage(hdc);
155 /******************************************************************
156 * StartPage [GDI32.349]
159 INT WINAPI StartPage(HDC hdc)
161 INT ret = 1;
162 DC *dc = DC_GetDCPtr( hdc );
163 if(!dc) return SP_ERROR;
165 if(dc->funcs->pStartPage)
166 ret = dc->funcs->pStartPage( dc );
167 else
168 FIXME("stub\n");
169 GDI_ReleaseObj( hdc );
170 return ret;
173 /******************************************************************
174 * EndPage16 [GDI.380]
177 INT16 WINAPI EndPage16( HDC16 hdc )
179 return EndPage(hdc);
182 /******************************************************************
183 * EndPage [GDI32.77]
186 INT WINAPI EndPage(HDC hdc)
188 INT ret;
189 DC *dc = DC_GetDCPtr( hdc );
190 if(!dc) return SP_ERROR;
192 if(dc->funcs->pEndPage)
193 ret = dc->funcs->pEndPage( dc );
194 else
195 ret = Escape(hdc, NEWFRAME, 0, 0, 0);
196 GDI_ReleaseObj( hdc );
197 return ret;
200 /******************************************************************************
201 * AbortDoc16 [GDI.382]
203 INT16 WINAPI AbortDoc16(HDC16 hdc)
205 return AbortDoc(hdc);
208 /******************************************************************************
209 * AbortDoc [GDI32.105]
211 INT WINAPI AbortDoc(HDC hdc)
213 INT ret;
214 DC *dc = DC_GetDCPtr( hdc );
215 if(!dc) return SP_ERROR;
217 if(dc->funcs->pAbortDoc)
218 ret = dc->funcs->pAbortDoc( dc );
219 else
220 ret = Escape(hdc, ABORTDOC, 0, 0, 0);
221 GDI_ReleaseObj( hdc );
222 return ret;
225 /**********************************************************************
226 * QueryAbort16 (GDI.155)
228 * Calls the app's AbortProc function if avail.
230 * RETURNS
231 * TRUE if no AbortProc avail or AbortProc wants to continue printing.
232 * FALSE if AbortProc wants to abort printing.
234 BOOL16 WINAPI QueryAbort16(HDC16 hdc, INT16 reserved)
236 BOOL ret = TRUE;
237 DC *dc = DC_GetDCPtr( hdc );
239 if(!dc) {
240 ERR("Invalid hdc %04x\n", hdc);
241 return FALSE;
244 if (dc->w.pAbortProc) ret = dc->w.pAbortProc(hdc, 0);
245 GDI_ReleaseObj( hdc );
246 return ret;
249 /* ### start build ### */
250 extern WORD CALLBACK PRTDRV_CallTo16_word_ww(FARPROC16,WORD,WORD);
251 /* ### stop build ### */
253 /**********************************************************************
254 * SetAbortProc16 (GDI.381)
257 INT16 WINAPI SetAbortProc16(HDC16 hdc, SEGPTR abrtprc)
259 ABORTPROC proc32 = (ABORTPROC)THUNK_Alloc((FARPROC16)abrtprc,
260 (RELAY)PRTDRV_CallTo16_word_ww);
261 return SetAbortProc(hdc, proc32);
264 /**********************************************************************
265 * SetAbortProc (GDI32.301)
268 INT WINAPI SetAbortProc(HDC hdc, ABORTPROC abrtprc)
270 DC *dc = DC_GetDCPtr( hdc );
272 if(dc->w.pAbortProc) THUNK_Free((FARPROC)dc->w.pAbortProc);
273 dc->w.pAbortProc = abrtprc;
274 GDI_ReleaseObj( hdc );
275 return TRUE;
279 /****************** misc. printer related functions */
282 * The following function should implement a queing system
284 struct hpq
286 struct hpq *next;
287 int tag;
288 int key;
291 static struct hpq *hpqueue;
293 /**********************************************************************
294 * CreatePQ (GDI.230)
297 HPQ16 WINAPI CreatePQ16(INT16 size)
299 #if 0
300 HGLOBAL16 hpq = 0;
301 WORD tmp_size;
302 LPWORD pPQ;
304 tmp_size = size << 2;
305 if (!(hpq = GlobalAlloc16(GMEM_SHARE|GMEM_MOVEABLE, tmp_size + 8)))
306 return 0xffff;
307 pPQ = GlobalLock16(hpq);
308 *pPQ++ = 0;
309 *pPQ++ = tmp_size;
310 *pPQ++ = 0;
311 *pPQ++ = 0;
312 GlobalUnlock16(hpq);
314 return (HPQ16)hpq;
315 #else
316 FIXME("(%d): stub\n",size);
317 return 1;
318 #endif
321 /**********************************************************************
322 * DeletePQ (GDI.235)
325 INT16 WINAPI DeletePQ16(HPQ16 hPQ)
327 return GlobalFree16((HGLOBAL16)hPQ);
330 /**********************************************************************
331 * ExtractPQ (GDI.232)
334 INT16 WINAPI ExtractPQ16(HPQ16 hPQ)
336 struct hpq *queue, *prev, *current, *currentPrev;
337 int key = 0, tag = -1;
338 currentPrev = prev = NULL;
339 queue = current = hpqueue;
340 if (current)
341 key = current->key;
343 while (current)
345 currentPrev = current;
346 current = current->next;
347 if (current)
349 if (current->key < key)
351 queue = current;
352 prev = currentPrev;
356 if (queue)
358 tag = queue->tag;
360 if (prev)
361 prev->next = queue->next;
362 else
363 hpqueue = queue->next;
364 HeapFree(GetProcessHeap(), 0, queue);
367 TRACE("%x got tag %d key %d\n", hPQ, tag, key);
369 return tag;
372 /**********************************************************************
373 * InsertPQ (GDI.233)
376 INT16 WINAPI InsertPQ16(HPQ16 hPQ, INT16 tag, INT16 key)
378 struct hpq *queueItem = HeapAlloc(GetProcessHeap(), 0, sizeof(struct hpq));
379 if(queueItem == NULL) {
380 ERR("Memory exausted!");
381 return FALSE;
383 queueItem->next = hpqueue;
384 hpqueue = queueItem;
385 queueItem->key = key;
386 queueItem->tag = tag;
388 FIXME("(%x %d %d): stub???\n", hPQ, tag, key);
389 return TRUE;
392 /**********************************************************************
393 * MinPQ (GDI.231)
396 INT16 WINAPI MinPQ16(HPQ16 hPQ)
398 FIXME("(%x): stub\n", hPQ);
399 return 0;
402 /**********************************************************************
403 * SizePQ (GDI.234)
406 INT16 WINAPI SizePQ16(HPQ16 hPQ, INT16 sizechange)
408 FIXME("(%x %d): stub\n", hPQ, sizechange);
409 return -1;
415 * The following functions implement part of the spooling process to
416 * print manager. I would like to see wine have a version of print managers
417 * that used LPR/LPD. For simplicity print jobs will be sent to a file for
418 * now.
420 typedef struct PRINTJOB
422 char *pszOutput;
423 char *pszTitle;
424 HDC16 hDC;
425 HANDLE16 hHandle;
426 int nIndex;
427 int fd;
428 } PRINTJOB, *PPRINTJOB;
430 #define MAX_PRINT_JOBS 1
431 #define SP_OK 1
433 PPRINTJOB gPrintJobsTable[MAX_PRINT_JOBS];
436 static PPRINTJOB FindPrintJobFromHandle(HANDLE16 hHandle)
438 return gPrintJobsTable[0];
441 static int CreateSpoolFile(LPCSTR pszOutput)
443 int fd=-1;
444 char psCmd[1024];
445 char *psCmdP = psCmd;
447 /* TTD convert the 'output device' into a spool file name */
449 if (pszOutput == NULL || *pszOutput == '\0')
450 return -1;
452 PROFILE_GetWineIniString( "spooler", pszOutput, "", psCmd, sizeof(psCmd) );
453 TRACE("Got printerSpoolCommand '%s' for output device '%s'\n",
454 psCmd, pszOutput);
455 if (!*psCmd)
456 psCmdP = (char *)pszOutput;
457 else
459 while (*psCmdP && isspace(*psCmdP))
461 psCmdP++;
463 if (!*psCmdP)
464 return -1;
466 if (*psCmdP == '|')
468 int fds[2];
469 if (pipe(fds))
470 return -1;
471 if (fork() == 0)
473 psCmdP++;
475 TRACE("In child need to exec %s\n",psCmdP);
476 close(0);
477 dup2(fds[0],0);
478 close (fds[1]);
479 system(psCmdP);
480 exit(0);
483 close (fds[0]);
484 fd = fds[1];
485 TRACE("Need to execute a cmnd and pipe the output to it\n");
487 else
489 DOS_FULL_NAME fullName;
491 TRACE("Just assume it's a file\n");
494 * The file name can be dos based, we have to find its
495 * Unix correspondant file name
497 DOSFS_GetFullName(psCmdP, FALSE, &fullName);
499 if ((fd = open(fullName.long_name, O_CREAT | O_TRUNC | O_WRONLY , 0600)) < 0)
501 ERR("Failed to create spool file %s (%s)\n",
502 fullName.long_name, strerror(errno));
505 return fd;
508 static int FreePrintJob(HANDLE16 hJob)
510 int nRet = SP_ERROR;
511 PPRINTJOB pPrintJob;
513 pPrintJob = FindPrintJobFromHandle(hJob);
514 if (pPrintJob != NULL)
516 gPrintJobsTable[pPrintJob->nIndex] = NULL;
517 HeapFree(GetProcessHeap(), 0, pPrintJob->pszOutput);
518 HeapFree(GetProcessHeap(), 0, pPrintJob->pszTitle);
519 if (pPrintJob->fd >= 0) close(pPrintJob->fd);
520 HeapFree(GetProcessHeap(), 0, pPrintJob);
521 nRet = SP_OK;
523 return nRet;
526 /**********************************************************************
527 * OpenJob (GDI.240)
530 HPJOB16 WINAPI OpenJob16(LPCSTR lpOutput, LPCSTR lpTitle, HDC16 hDC)
532 HPJOB16 hHandle = (HPJOB16)SP_ERROR;
533 PPRINTJOB pPrintJob;
535 TRACE("'%s' '%s' %04x\n", lpOutput, lpTitle, hDC);
537 pPrintJob = gPrintJobsTable[0];
538 if (pPrintJob == NULL)
540 int fd;
542 /* Try an create a spool file */
543 fd = CreateSpoolFile(lpOutput);
544 if (fd >= 0)
546 pPrintJob = HeapAlloc(GetProcessHeap(), 0, sizeof(PRINTJOB));
547 if(pPrintJob == NULL) {
548 WARN("Memory exausted!");
549 return hHandle;
552 hHandle = 1;
554 pPrintJob->pszOutput = HEAP_strdupA(GetProcessHeap(), 0, lpOutput);
555 if(lpTitle)
556 pPrintJob->pszTitle = HEAP_strdupA(GetProcessHeap(), 0, lpTitle);
557 pPrintJob->hDC = hDC;
558 pPrintJob->fd = fd;
559 pPrintJob->nIndex = 0;
560 pPrintJob->hHandle = hHandle;
561 gPrintJobsTable[pPrintJob->nIndex] = pPrintJob;
564 TRACE("return %04x\n", hHandle);
565 return hHandle;
568 /**********************************************************************
569 * CloseJob (GDI.243)
572 INT16 WINAPI CloseJob16(HPJOB16 hJob)
574 int nRet = SP_ERROR;
575 PPRINTJOB pPrintJob = NULL;
577 TRACE("%04x\n", hJob);
579 pPrintJob = FindPrintJobFromHandle(hJob);
580 if (pPrintJob != NULL)
582 /* Close the spool file */
583 close(pPrintJob->fd);
584 FreePrintJob(hJob);
585 nRet = 1;
587 return nRet;
590 /**********************************************************************
591 * WriteSpool (GDI.241)
594 INT16 WINAPI WriteSpool16(HPJOB16 hJob, LPSTR lpData, INT16 cch)
596 int nRet = SP_ERROR;
597 PPRINTJOB pPrintJob = NULL;
599 TRACE("%04x %08lx %04x\n", hJob, (DWORD)lpData, cch);
601 pPrintJob = FindPrintJobFromHandle(hJob);
602 if (pPrintJob != NULL && pPrintJob->fd >= 0 && cch)
604 if (write(pPrintJob->fd, lpData, cch) != cch)
605 nRet = SP_OUTOFDISK;
606 else
607 nRet = cch;
608 if (pPrintJob->hDC == 0) {
609 TRACE("hDC == 0 so no QueryAbort\n");
611 else if (!(QueryAbort16(pPrintJob->hDC, (nRet == SP_OUTOFDISK) ? nRet : 0 )))
613 CloseJob16(hJob); /* printing aborted */
614 nRet = SP_APPABORT;
617 return nRet;
620 /**********************************************************************
621 * WriteDialog (GDI.242)
624 INT16 WINAPI WriteDialog16(HPJOB16 hJob, LPSTR lpMsg, INT16 cchMsg)
626 TRACE("%04x %04x '%s'\n", hJob, cchMsg, lpMsg);
628 return Callout.MessageBoxA(0, lpMsg, "Printing Error", MB_OKCANCEL);
632 /**********************************************************************
633 * DeleteJob (GDI.244)
636 INT16 WINAPI DeleteJob16(HPJOB16 hJob, INT16 nNotUsed)
638 int nRet;
640 TRACE("%04x\n", hJob);
642 nRet = FreePrintJob(hJob);
643 return nRet;
647 * The following two function would allow a page to be sent to the printer
648 * when it has been processed. For simplicity they havn't been implemented.
649 * This means a whole job has to be processed before it is sent to the printer.
652 /**********************************************************************
653 * StartSpoolPage (GDI.246)
656 INT16 WINAPI StartSpoolPage16(HPJOB16 hJob)
658 FIXME("StartSpoolPage GDI.246 unimplemented\n");
659 return 1;
664 /**********************************************************************
665 * EndSpoolPage (GDI.247)
668 INT16 WINAPI EndSpoolPage16(HPJOB16 hJob)
670 FIXME("EndSpoolPage GDI.247 unimplemented\n");
671 return 1;
675 /**********************************************************************
676 * GetSpoolJob (GDI.245)
679 DWORD WINAPI GetSpoolJob16(int nOption, LONG param)
681 DWORD retval = 0;
682 TRACE("In GetSpoolJob param 0x%lx noption %d\n",param, nOption);
683 return retval;
687 /******************************************************************
688 * DrvGetPrinterDataInternal
690 * Helper for DrvGetPrinterData
692 static DWORD DrvGetPrinterDataInternal(LPSTR RegStr_Printer,
693 LPBYTE lpPrinterData, int cbData, int what)
695 DWORD res = -1;
696 HKEY hkey;
697 DWORD dwType, cbQueryData;
699 if (!(RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey))) {
700 if (what == INT_PD_DEFAULT_DEVMODE) { /* "Default DevMode" */
701 if (!(RegQueryValueExA(hkey, DefaultDevMode, 0, &dwType, 0, &cbQueryData))) {
702 if (!lpPrinterData)
703 res = cbQueryData;
704 else if ((cbQueryData) && (cbQueryData <= cbData)) {
705 cbQueryData = cbData;
706 if (RegQueryValueExA(hkey, DefaultDevMode, 0,
707 &dwType, lpPrinterData, &cbQueryData))
708 res = cbQueryData;
711 } else { /* "Printer Driver" */
712 cbQueryData = 32;
713 RegQueryValueExA(hkey, "Printer Driver", 0,
714 &dwType, lpPrinterData, &cbQueryData);
715 res = cbQueryData;
718 if (hkey) RegCloseKey(hkey);
719 return res;
722 /******************************************************************
723 * DrvGetPrinterData [GDI.282]
726 DWORD WINAPI DrvGetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
727 LPDWORD lpType, LPBYTE lpPrinterData,
728 int cbData, LPDWORD lpNeeded)
730 LPSTR RegStr_Printer;
731 HKEY hkey = 0, hkey2 = 0;
732 DWORD res = 0;
733 DWORD dwType, PrinterAttr, cbPrinterAttr, SetData, size;
735 if (HIWORD(lpPrinter))
736 TRACE("printer %s\n",lpPrinter);
737 else
738 TRACE("printer %p\n",lpPrinter);
739 if (HIWORD(lpProfile))
740 TRACE("profile %s\n",lpProfile);
741 else
742 TRACE("profile %p\n",lpProfile);
743 TRACE("lpType %p\n",lpType);
745 if ((!lpPrinter) || (!lpProfile) || (!lpNeeded))
746 return ERROR_INVALID_PARAMETER;
748 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
749 strlen(Printers) + strlen(lpPrinter) + 2);
750 strcpy(RegStr_Printer, Printers);
751 strcat(RegStr_Printer, lpPrinter);
753 if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
754 (!strcmp(lpProfile, DefaultDevMode)))) {
755 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
756 INT_PD_DEFAULT_DEVMODE);
757 if (size+1) {
758 *lpNeeded = size;
759 if ((lpPrinterData) && (*lpNeeded > cbData))
760 res = ERROR_MORE_DATA;
762 else res = ERROR_INVALID_PRINTER_NAME;
764 else
765 if (((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
766 (!strcmp(lpProfile, PrinterModel)))) {
767 *lpNeeded = 32;
768 if (!lpPrinterData) goto failed;
769 if (cbData < 32) {
770 res = ERROR_MORE_DATA;
771 goto failed;
773 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
774 INT_PD_DEFAULT_MODEL);
775 if ((size+1) && (lpType))
776 *lpType = REG_SZ;
777 else
778 res = ERROR_INVALID_PRINTER_NAME;
780 else
782 if ((res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)))
783 goto failed;
784 cbPrinterAttr = 4;
785 if ((res = RegQueryValueExA(hkey, "Attributes", 0,
786 &dwType, (LPBYTE)&PrinterAttr, &cbPrinterAttr)))
787 goto failed;
788 if ((res = RegOpenKeyA(hkey, PrinterDriverData, &hkey2)))
789 goto failed;
790 *lpNeeded = cbData;
791 res = RegQueryValueExA(hkey2, lpProfile, 0,
792 lpType, lpPrinterData, lpNeeded);
793 if ((res != ERROR_CANTREAD) &&
794 ((PrinterAttr &
795 (PRINTER_ATTRIBUTE_ENABLE_BIDI|PRINTER_ATTRIBUTE_NETWORK))
796 == PRINTER_ATTRIBUTE_NETWORK))
798 if (!(res) && (*lpType == REG_DWORD) && (*(LPDWORD)lpPrinterData == -1))
799 res = ERROR_INVALID_DATA;
801 else
803 SetData = -1;
804 RegSetValueExA(hkey2, lpProfile, 0, REG_DWORD, (LPBYTE)&SetData, 4); /* no result returned */
808 failed:
809 if (hkey2) RegCloseKey(hkey2);
810 if (hkey) RegCloseKey(hkey);
811 HeapFree(GetProcessHeap(), 0, RegStr_Printer);
812 return res;
816 /******************************************************************
817 * DrvSetPrinterData [GDI.281]
820 DWORD WINAPI DrvSetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
821 DWORD lpType, LPBYTE lpPrinterData,
822 DWORD dwSize)
824 LPSTR RegStr_Printer;
825 HKEY hkey = 0;
826 DWORD res = 0;
828 if (HIWORD(lpPrinter))
829 TRACE("printer %s\n",lpPrinter);
830 else
831 TRACE("printer %p\n",lpPrinter);
832 if (HIWORD(lpProfile))
833 TRACE("profile %s\n",lpProfile);
834 else
835 TRACE("profile %p\n",lpProfile);
836 TRACE("lpType %08lx\n",lpType);
838 if ((!lpPrinter) || (!lpProfile) ||
839 ((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
840 (!strcmp(lpProfile, PrinterModel))))
841 return ERROR_INVALID_PARAMETER;
843 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
844 strlen(Printers) + strlen(lpPrinter) + 2);
845 strcpy(RegStr_Printer, Printers);
846 strcat(RegStr_Printer, lpPrinter);
848 if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
849 (!strcmp(lpProfile, DefaultDevMode)))) {
850 if ( RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)
851 != ERROR_SUCCESS ||
852 RegSetValueExA(hkey, DefaultDevMode, 0, REG_BINARY,
853 lpPrinterData, dwSize) != ERROR_SUCCESS )
854 res = ERROR_INVALID_PRINTER_NAME;
856 else
858 strcat(RegStr_Printer, "\\");
860 if( (res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)) ==
861 ERROR_SUCCESS ) {
863 if (!lpPrinterData)
864 res = RegDeleteValueA(hkey, lpProfile);
865 else
866 res = RegSetValueExA(hkey, lpProfile, 0, lpType,
867 lpPrinterData, dwSize);
871 if (hkey) RegCloseKey(hkey);
872 HeapFree(GetProcessHeap(), 0, RegStr_Printer);
873 return res;