Added missing GDI_ReleaseObj() to GetDCBrushColor & GetDCPenColor.
[wine.git] / dlls / gdi / printdrv.c
blobb84d0ed3ddeb6e7cecd9c03cc3edeebea66dd831
1 /*
2 * Implementation of some printer driver bits
4 * Copyright 1996 John Harvey
5 * Copyright 1998 Huw Davies
6 * Copyright 1998 Andreas Mohr
7 * Copyright 1999 Klaas van Gend
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "config.h"
25 #include "wine/port.h"
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <signal.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include <errno.h>
34 #ifdef HAVE_IO_H
35 # include <io.h>
36 #endif
37 #ifdef HAVE_UNISTD_H
38 # include <unistd.h>
39 #endif
40 #include <fcntl.h>
41 #include "windef.h"
42 #include "winbase.h"
43 #include "winuser.h"
44 #include "wine/winbase16.h"
45 #include "wine/wingdi16.h"
46 #include "winspool.h"
47 #include "winerror.h"
48 #include "winreg.h"
49 #include "wownt32.h"
50 #include "wine/debug.h"
51 #include "gdi.h"
52 #include "heap.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(print);
56 static char PrinterModel[] = "Printer Model";
57 static char DefaultDevMode[] = "Default DevMode";
58 static char PrinterDriverData[] = "PrinterDriverData";
59 static char Printers[] = "System\\CurrentControlSet\\Control\\Print\\Printers\\";
62 /******************************************************************
63 * StartDocA [GDI32.@]
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 * Note: we now do it the other way, with the STARTDOC Escape calling StartDoc.
72 INT WINAPI StartDocA(HDC hdc, const DOCINFOA* doc)
74 INT ret = 0;
75 DC *dc = DC_GetDCPtr( hdc );
77 TRACE("DocName = '%s' Output = '%s' Datatype = '%s'\n",
78 doc->lpszDocName, doc->lpszOutput, doc->lpszDatatype);
80 if(!dc) return SP_ERROR;
82 if (dc->funcs->pStartDoc) ret = dc->funcs->pStartDoc( dc->physDev, doc );
83 GDI_ReleaseObj( hdc );
84 return ret;
87 /*************************************************************************
88 * StartDocW [GDI32.@]
91 INT WINAPI StartDocW(HDC hdc, const DOCINFOW* doc)
93 DOCINFOA docA;
94 INT ret;
96 docA.cbSize = doc->cbSize;
97 docA.lpszDocName = doc->lpszDocName ?
98 HEAP_strdupWtoA( GetProcessHeap(), 0, doc->lpszDocName ) : NULL;
99 docA.lpszOutput = doc->lpszOutput ?
100 HEAP_strdupWtoA( GetProcessHeap(), 0, doc->lpszOutput ) : NULL;
101 docA.lpszDatatype = doc->lpszDatatype ?
102 HEAP_strdupWtoA( GetProcessHeap(), 0, doc->lpszDatatype ) : NULL;
103 docA.fwType = doc->fwType;
105 ret = StartDocA(hdc, &docA);
107 if(docA.lpszDocName)
108 HeapFree( GetProcessHeap(), 0, (LPSTR)docA.lpszDocName );
109 if(docA.lpszOutput)
110 HeapFree( GetProcessHeap(), 0, (LPSTR)docA.lpszOutput );
111 if(docA.lpszDatatype)
112 HeapFree( GetProcessHeap(), 0, (LPSTR)docA.lpszDatatype );
114 return ret;
118 /******************************************************************
119 * EndDoc [GDI32.@]
122 INT WINAPI EndDoc(HDC hdc)
124 INT ret = 0;
125 DC *dc = DC_GetDCPtr( hdc );
126 if(!dc) return SP_ERROR;
128 if (dc->funcs->pEndDoc) ret = dc->funcs->pEndDoc( dc->physDev );
129 GDI_ReleaseObj( hdc );
130 return ret;
134 /******************************************************************
135 * StartPage [GDI32.@]
138 INT WINAPI StartPage(HDC hdc)
140 INT ret = 1;
141 DC *dc = DC_GetDCPtr( hdc );
142 if(!dc) return SP_ERROR;
144 if(dc->funcs->pStartPage)
145 ret = dc->funcs->pStartPage( dc->physDev );
146 else
147 FIXME("stub\n");
148 GDI_ReleaseObj( hdc );
149 return ret;
153 /******************************************************************
154 * EndPage [GDI32.@]
157 INT WINAPI EndPage(HDC hdc)
159 ABORTPROC abort_proc;
160 INT ret = 0;
161 DC *dc = DC_GetDCPtr( hdc );
162 if(!dc) return SP_ERROR;
164 if (dc->funcs->pEndPage) ret = dc->funcs->pEndPage( dc->physDev );
165 abort_proc = dc->pAbortProc;
166 GDI_ReleaseObj( hdc );
167 if (abort_proc && !abort_proc( hdc, 0 ))
169 EndDoc( hdc );
170 ret = 0;
172 return ret;
176 /******************************************************************************
177 * AbortDoc [GDI32.@]
179 INT WINAPI AbortDoc(HDC hdc)
181 INT ret = 0;
182 DC *dc = DC_GetDCPtr( hdc );
183 if(!dc) return SP_ERROR;
185 if (dc->funcs->pAbortDoc) ret = dc->funcs->pAbortDoc( dc->physDev );
186 GDI_ReleaseObj( hdc );
187 return ret;
190 /**********************************************************************
191 * QueryAbort (GDI.155)
193 * Calls the app's AbortProc function if avail.
195 * RETURNS
196 * TRUE if no AbortProc avail or AbortProc wants to continue printing.
197 * FALSE if AbortProc wants to abort printing.
199 BOOL16 WINAPI QueryAbort16(HDC16 hdc16, INT16 reserved)
201 BOOL ret = TRUE;
202 HDC hdc = HDC_32( hdc16 );
203 DC *dc = DC_GetDCPtr( hdc );
204 ABORTPROC abproc;
206 if(!dc) {
207 ERR("Invalid hdc %p\n", hdc);
208 return FALSE;
211 abproc = dc->pAbortProc;
212 GDI_ReleaseObj( hdc );
214 if (abproc)
215 ret = abproc(hdc, 0);
216 return ret;
220 /**********************************************************************
221 * call_abort_proc16
223 static BOOL CALLBACK call_abort_proc16( HDC hdc, INT code )
225 ABORTPROC16 proc16;
226 DC *dc = DC_GetDCPtr( hdc );
228 if (!dc) return FALSE;
229 proc16 = dc->pAbortProc16;
230 GDI_ReleaseObj( hdc );
231 if (proc16)
233 WORD args[2];
234 DWORD ret;
236 args[1] = HDC_16(hdc);
237 args[0] = code;
238 WOWCallback16Ex( (DWORD)proc16, WCB16_PASCAL, sizeof(args), args, &ret );
239 return LOWORD(ret);
241 return TRUE;
245 /**********************************************************************
246 * SetAbortProc (GDI.381)
248 INT16 WINAPI SetAbortProc16(HDC16 hdc16, ABORTPROC16 abrtprc)
250 HDC hdc = HDC_32( hdc16 );
251 DC *dc = DC_GetDCPtr( hdc );
253 if (!dc) return FALSE;
254 dc->pAbortProc16 = abrtprc;
255 GDI_ReleaseObj( hdc );
256 return SetAbortProc( hdc, call_abort_proc16 );
259 /**********************************************************************
260 * SetAbortProc (GDI32.@)
263 INT WINAPI SetAbortProc(HDC hdc, ABORTPROC abrtprc)
265 DC *dc = DC_GetDCPtr( hdc );
267 if (!dc) return FALSE;
268 dc->pAbortProc = abrtprc;
269 GDI_ReleaseObj( hdc );
270 return TRUE;
274 /****************** misc. printer related functions */
277 * The following function should implement a queing system
279 struct hpq
281 struct hpq *next;
282 int tag;
283 int key;
286 static struct hpq *hpqueue;
288 /**********************************************************************
289 * CreatePQ (GDI.230)
292 HPQ16 WINAPI CreatePQ16(INT16 size)
294 #if 0
295 HGLOBAL16 hpq = 0;
296 WORD tmp_size;
297 LPWORD pPQ;
299 tmp_size = size << 2;
300 if (!(hpq = GlobalAlloc16(GMEM_SHARE|GMEM_MOVEABLE, tmp_size + 8)))
301 return 0xffff;
302 pPQ = GlobalLock16(hpq);
303 *pPQ++ = 0;
304 *pPQ++ = tmp_size;
305 *pPQ++ = 0;
306 *pPQ++ = 0;
307 GlobalUnlock16(hpq);
309 return (HPQ16)hpq;
310 #else
311 FIXME("(%d): stub\n",size);
312 return 1;
313 #endif
316 /**********************************************************************
317 * DeletePQ (GDI.235)
320 INT16 WINAPI DeletePQ16(HPQ16 hPQ)
322 return GlobalFree16((HGLOBAL16)hPQ);
325 /**********************************************************************
326 * ExtractPQ (GDI.232)
329 INT16 WINAPI ExtractPQ16(HPQ16 hPQ)
331 struct hpq *queue, *prev, *current, *currentPrev;
332 int key = 0, tag = -1;
333 currentPrev = prev = NULL;
334 queue = current = hpqueue;
335 if (current)
336 key = current->key;
338 while (current)
340 currentPrev = current;
341 current = current->next;
342 if (current)
344 if (current->key < key)
346 queue = current;
347 prev = currentPrev;
351 if (queue)
353 tag = queue->tag;
355 if (prev)
356 prev->next = queue->next;
357 else
358 hpqueue = queue->next;
359 HeapFree(GetProcessHeap(), 0, queue);
362 TRACE("%x got tag %d key %d\n", hPQ, tag, key);
364 return tag;
367 /**********************************************************************
368 * InsertPQ (GDI.233)
371 INT16 WINAPI InsertPQ16(HPQ16 hPQ, INT16 tag, INT16 key)
373 struct hpq *queueItem = HeapAlloc(GetProcessHeap(), 0, sizeof(struct hpq));
374 if(queueItem == NULL) {
375 ERR("Memory exausted!\n");
376 return FALSE;
378 queueItem->next = hpqueue;
379 hpqueue = queueItem;
380 queueItem->key = key;
381 queueItem->tag = tag;
383 FIXME("(%x %d %d): stub???\n", hPQ, tag, key);
384 return TRUE;
387 /**********************************************************************
388 * MinPQ (GDI.231)
391 INT16 WINAPI MinPQ16(HPQ16 hPQ)
393 FIXME("(%x): stub\n", hPQ);
394 return 0;
397 /**********************************************************************
398 * SizePQ (GDI.234)
401 INT16 WINAPI SizePQ16(HPQ16 hPQ, INT16 sizechange)
403 FIXME("(%x %d): stub\n", hPQ, sizechange);
404 return -1;
410 * The following functions implement part of the spooling process to
411 * print manager. I would like to see wine have a version of print managers
412 * that used LPR/LPD. For simplicity print jobs will be sent to a file for
413 * now.
415 typedef struct PRINTJOB
417 char *pszOutput;
418 char *pszTitle;
419 HDC16 hDC;
420 HANDLE16 hHandle;
421 int nIndex;
422 int fd;
423 } PRINTJOB, *PPRINTJOB;
425 #define MAX_PRINT_JOBS 1
426 #define SP_OK 1
428 PPRINTJOB gPrintJobsTable[MAX_PRINT_JOBS];
431 static PPRINTJOB FindPrintJobFromHandle(HANDLE16 hHandle)
433 return gPrintJobsTable[0];
436 static int CreateSpoolFile(LPCSTR pszOutput)
438 int fd=-1;
439 char psCmd[1024];
440 char *psCmdP = psCmd;
441 HKEY hkey;
443 /* TTD convert the 'output device' into a spool file name */
445 if (pszOutput == NULL || *pszOutput == '\0')
446 return -1;
448 psCmd[0] = 0;
449 if (!strncmp("LPR:",pszOutput,4))
451 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\spooler", &hkey))
453 DWORD type, count = sizeof(psCmd);
454 RegQueryValueExA(hkey, pszOutput, 0, &type, psCmd, &count);
455 RegCloseKey(hkey);
457 if(psCmd[0] == 0)
458 sprintf(psCmd,"|lpr -P%s",pszOutput+4);
460 else
462 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\spooler", &hkey))
464 DWORD type, count = sizeof(psCmd);
465 RegQueryValueExA(hkey, pszOutput, 0, &type, psCmd, &count);
466 RegCloseKey(hkey);
469 TRACE("Got printerSpoolCommand '%s' for output device '%s'\n",
470 psCmd, pszOutput);
471 if (!*psCmd)
472 psCmdP = (char *)pszOutput;
473 else
475 while (*psCmdP && isspace(*psCmdP))
477 psCmdP++;
479 if (!*psCmdP)
480 return -1;
482 TRACE("command: '%s'\n", psCmdP);
483 if (*psCmdP == '|')
485 int fds[2];
486 if (pipe(fds)) {
487 ERR("pipe() failed!\n");
488 return -1;
490 if (fork() == 0)
492 psCmdP++;
494 TRACE("In child need to exec %s\n",psCmdP);
495 close(0);
496 dup2(fds[0],0);
497 close (fds[1]);
499 /* reset signals that we previously set to SIG_IGN */
500 signal( SIGPIPE, SIG_DFL );
501 signal( SIGCHLD, SIG_DFL );
503 system(psCmdP);
504 exit(0);
507 close (fds[0]);
508 fd = fds[1];
509 TRACE("Need to execute a cmnd and pipe the output to it\n");
511 else
513 char buffer[MAX_PATH];
515 TRACE("Just assume it's a file\n");
518 * The file name can be dos based, we have to find its
519 * Unix correspondant file name
521 wine_get_unix_file_name(psCmdP, buffer, sizeof(buffer));
523 if ((fd = open(buffer, O_CREAT | O_TRUNC | O_WRONLY , 0600)) < 0)
525 ERR("Failed to create spool file '%s' ('%s'). (error %s)\n",
526 buffer, psCmdP, strerror(errno));
529 return fd;
532 static int FreePrintJob(HANDLE16 hJob)
534 int nRet = SP_ERROR;
535 PPRINTJOB pPrintJob;
537 pPrintJob = FindPrintJobFromHandle(hJob);
538 if (pPrintJob != NULL)
540 gPrintJobsTable[pPrintJob->nIndex] = NULL;
541 HeapFree(GetProcessHeap(), 0, pPrintJob->pszOutput);
542 HeapFree(GetProcessHeap(), 0, pPrintJob->pszTitle);
543 if (pPrintJob->fd >= 0) close(pPrintJob->fd);
544 HeapFree(GetProcessHeap(), 0, pPrintJob);
545 nRet = SP_OK;
547 return nRet;
550 /**********************************************************************
551 * OpenJob (GDI.240)
554 HPJOB16 WINAPI OpenJob16(LPCSTR lpOutput, LPCSTR lpTitle, HDC16 hDC)
556 HPJOB16 hHandle = (HPJOB16)SP_ERROR;
557 PPRINTJOB pPrintJob;
559 TRACE("'%s' '%s' %04x\n", lpOutput, lpTitle, hDC);
561 pPrintJob = gPrintJobsTable[0];
562 if (pPrintJob == NULL)
564 int fd;
566 /* Try and create a spool file */
567 fd = CreateSpoolFile(lpOutput);
568 if (fd >= 0)
570 pPrintJob = HeapAlloc(GetProcessHeap(), 0, sizeof(PRINTJOB));
571 if(pPrintJob == NULL) {
572 WARN("Memory exausted!\n");
573 return hHandle;
576 hHandle = 1;
578 pPrintJob->pszOutput = HeapAlloc(GetProcessHeap(), 0, strlen(lpOutput)+1);
579 strcpy( pPrintJob->pszOutput, lpOutput );
580 if(lpTitle)
582 pPrintJob->pszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(lpTitle)+1);
583 strcpy( pPrintJob->pszTitle, lpTitle );
585 pPrintJob->hDC = hDC;
586 pPrintJob->fd = fd;
587 pPrintJob->nIndex = 0;
588 pPrintJob->hHandle = hHandle;
589 gPrintJobsTable[pPrintJob->nIndex] = pPrintJob;
592 TRACE("return %04x\n", hHandle);
593 return hHandle;
596 /**********************************************************************
597 * CloseJob (GDI.243)
600 INT16 WINAPI CloseJob16(HPJOB16 hJob)
602 int nRet = SP_ERROR;
603 PPRINTJOB pPrintJob = NULL;
605 TRACE("%04x\n", hJob);
607 pPrintJob = FindPrintJobFromHandle(hJob);
608 if (pPrintJob != NULL)
610 /* Close the spool file */
611 close(pPrintJob->fd);
612 FreePrintJob(hJob);
613 nRet = 1;
615 return nRet;
618 /**********************************************************************
619 * WriteSpool (GDI.241)
622 INT16 WINAPI WriteSpool16(HPJOB16 hJob, LPSTR lpData, INT16 cch)
624 int nRet = SP_ERROR;
625 PPRINTJOB pPrintJob = NULL;
627 TRACE("%04x %08lx %04x\n", hJob, (DWORD)lpData, cch);
629 pPrintJob = FindPrintJobFromHandle(hJob);
630 if (pPrintJob != NULL && pPrintJob->fd >= 0 && cch)
632 if (write(pPrintJob->fd, lpData, cch) != cch)
633 nRet = SP_OUTOFDISK;
634 else
635 nRet = cch;
636 #if 0
637 /* FIXME: We just cannot call 16 bit functions from here, since we
638 * have acquired several locks (DC). And we do not really need to.
640 if (pPrintJob->hDC == 0) {
641 TRACE("hDC == 0 so no QueryAbort\n");
643 else if (!(QueryAbort16(pPrintJob->hDC, (nRet == SP_OUTOFDISK) ? nRet : 0 )))
645 CloseJob16(hJob); /* printing aborted */
646 nRet = SP_APPABORT;
648 #endif
650 return nRet;
653 typedef INT (WINAPI *MSGBOX_PROC)( HWND, LPCSTR, LPCSTR, UINT );
655 /**********************************************************************
656 * WriteDialog (GDI.242)
659 INT16 WINAPI WriteDialog16(HPJOB16 hJob, LPSTR lpMsg, INT16 cchMsg)
661 HMODULE mod;
662 MSGBOX_PROC pMessageBoxA;
663 INT16 ret = 0;
665 TRACE("%04x %04x '%s'\n", hJob, cchMsg, lpMsg);
667 if ((mod = GetModuleHandleA("user32.dll")))
669 if ((pMessageBoxA = (MSGBOX_PROC)GetProcAddress( mod, "MessageBoxA" )))
670 ret = pMessageBoxA(0, lpMsg, "Printing Error", MB_OKCANCEL);
672 return ret;
676 /**********************************************************************
677 * DeleteJob (GDI.244)
680 INT16 WINAPI DeleteJob16(HPJOB16 hJob, INT16 nNotUsed)
682 int nRet;
684 TRACE("%04x\n", hJob);
686 nRet = FreePrintJob(hJob);
687 return nRet;
691 * The following two function would allow a page to be sent to the printer
692 * when it has been processed. For simplicity they havn't been implemented.
693 * This means a whole job has to be processed before it is sent to the printer.
696 /**********************************************************************
697 * StartSpoolPage (GDI.246)
700 INT16 WINAPI StartSpoolPage16(HPJOB16 hJob)
702 FIXME("StartSpoolPage GDI.246 unimplemented\n");
703 return 1;
708 /**********************************************************************
709 * EndSpoolPage (GDI.247)
712 INT16 WINAPI EndSpoolPage16(HPJOB16 hJob)
714 FIXME("EndSpoolPage GDI.247 unimplemented\n");
715 return 1;
719 /**********************************************************************
720 * GetSpoolJob (GDI.245)
723 DWORD WINAPI GetSpoolJob16(int nOption, LONG param)
725 DWORD retval = 0;
726 TRACE("In GetSpoolJob param 0x%lx noption %d\n",param, nOption);
727 return retval;
731 /******************************************************************
732 * DrvGetPrinterDataInternal
734 * Helper for DrvGetPrinterData
736 static DWORD DrvGetPrinterDataInternal(LPSTR RegStr_Printer,
737 LPBYTE lpPrinterData, int cbData, int what)
739 DWORD res = -1;
740 HKEY hkey;
741 DWORD dwType, cbQueryData;
743 if (!(RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey))) {
744 if (what == INT_PD_DEFAULT_DEVMODE) { /* "Default DevMode" */
745 if (!(RegQueryValueExA(hkey, DefaultDevMode, 0, &dwType, 0, &cbQueryData))) {
746 if (!lpPrinterData)
747 res = cbQueryData;
748 else if ((cbQueryData) && (cbQueryData <= cbData)) {
749 cbQueryData = cbData;
750 if (RegQueryValueExA(hkey, DefaultDevMode, 0,
751 &dwType, lpPrinterData, &cbQueryData))
752 res = cbQueryData;
755 } else { /* "Printer Driver" */
756 cbQueryData = 32;
757 RegQueryValueExA(hkey, "Printer Driver", 0,
758 &dwType, lpPrinterData, &cbQueryData);
759 res = cbQueryData;
762 if (hkey) RegCloseKey(hkey);
763 return res;
766 /******************************************************************
767 * DrvGetPrinterData (GDI.282)
770 DWORD WINAPI DrvGetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
771 LPDWORD lpType, LPBYTE lpPrinterData,
772 int cbData, LPDWORD lpNeeded)
774 LPSTR RegStr_Printer;
775 HKEY hkey = 0, hkey2 = 0;
776 DWORD res = 0;
777 DWORD dwType, PrinterAttr, cbPrinterAttr, SetData, size;
779 if (HIWORD(lpPrinter))
780 TRACE("printer %s\n",lpPrinter);
781 else
782 TRACE("printer %p\n",lpPrinter);
783 if (HIWORD(lpProfile))
784 TRACE("profile %s\n",lpProfile);
785 else
786 TRACE("profile %p\n",lpProfile);
787 TRACE("lpType %p\n",lpType);
789 if ((!lpPrinter) || (!lpProfile) || (!lpNeeded))
790 return ERROR_INVALID_PARAMETER;
792 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
793 strlen(Printers) + strlen(lpPrinter) + 2);
794 strcpy(RegStr_Printer, Printers);
795 strcat(RegStr_Printer, lpPrinter);
797 if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
798 (!strcmp(lpProfile, DefaultDevMode)))) {
799 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
800 INT_PD_DEFAULT_DEVMODE);
801 if (size+1) {
802 *lpNeeded = size;
803 if ((lpPrinterData) && (*lpNeeded > cbData))
804 res = ERROR_MORE_DATA;
806 else res = ERROR_INVALID_PRINTER_NAME;
808 else
809 if (((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
810 (!strcmp(lpProfile, PrinterModel)))) {
811 *lpNeeded = 32;
812 if (!lpPrinterData) goto failed;
813 if (cbData < 32) {
814 res = ERROR_MORE_DATA;
815 goto failed;
817 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
818 INT_PD_DEFAULT_MODEL);
819 if ((size+1) && (lpType))
820 *lpType = REG_SZ;
821 else
822 res = ERROR_INVALID_PRINTER_NAME;
824 else
826 if ((res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)))
827 goto failed;
828 cbPrinterAttr = 4;
829 if ((res = RegQueryValueExA(hkey, "Attributes", 0,
830 &dwType, (LPBYTE)&PrinterAttr, &cbPrinterAttr)))
831 goto failed;
832 if ((res = RegOpenKeyA(hkey, PrinterDriverData, &hkey2)))
833 goto failed;
834 *lpNeeded = cbData;
835 res = RegQueryValueExA(hkey2, lpProfile, 0,
836 lpType, lpPrinterData, lpNeeded);
837 if ((res != ERROR_CANTREAD) &&
838 ((PrinterAttr &
839 (PRINTER_ATTRIBUTE_ENABLE_BIDI|PRINTER_ATTRIBUTE_NETWORK))
840 == PRINTER_ATTRIBUTE_NETWORK))
842 if (!(res) && (*lpType == REG_DWORD) && (*(LPDWORD)lpPrinterData == -1))
843 res = ERROR_INVALID_DATA;
845 else
847 SetData = -1;
848 RegSetValueExA(hkey2, lpProfile, 0, REG_DWORD, (LPBYTE)&SetData, 4); /* no result returned */
852 failed:
853 if (hkey2) RegCloseKey(hkey2);
854 if (hkey) RegCloseKey(hkey);
855 HeapFree(GetProcessHeap(), 0, RegStr_Printer);
856 return res;
860 /******************************************************************
861 * DrvSetPrinterData (GDI.281)
864 DWORD WINAPI DrvSetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
865 DWORD lpType, LPBYTE lpPrinterData,
866 DWORD dwSize)
868 LPSTR RegStr_Printer;
869 HKEY hkey = 0;
870 DWORD res = 0;
872 if (HIWORD(lpPrinter))
873 TRACE("printer %s\n",lpPrinter);
874 else
875 TRACE("printer %p\n",lpPrinter);
876 if (HIWORD(lpProfile))
877 TRACE("profile %s\n",lpProfile);
878 else
879 TRACE("profile %p\n",lpProfile);
880 TRACE("lpType %08lx\n",lpType);
882 if ((!lpPrinter) || (!lpProfile) ||
883 ((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
884 (!strcmp(lpProfile, PrinterModel))))
885 return ERROR_INVALID_PARAMETER;
887 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
888 strlen(Printers) + strlen(lpPrinter) + 2);
889 strcpy(RegStr_Printer, Printers);
890 strcat(RegStr_Printer, lpPrinter);
892 if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
893 (!strcmp(lpProfile, DefaultDevMode)))) {
894 if ( RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)
895 != ERROR_SUCCESS ||
896 RegSetValueExA(hkey, DefaultDevMode, 0, REG_BINARY,
897 lpPrinterData, dwSize) != ERROR_SUCCESS )
898 res = ERROR_INVALID_PRINTER_NAME;
900 else
902 strcat(RegStr_Printer, "\\");
904 if( (res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)) ==
905 ERROR_SUCCESS ) {
907 if (!lpPrinterData)
908 res = RegDeleteValueA(hkey, lpProfile);
909 else
910 res = RegSetValueExA(hkey, lpProfile, 0, lpType,
911 lpPrinterData, dwSize);
915 if (hkey) RegCloseKey(hkey);
916 HeapFree(GetProcessHeap(), 0, RegStr_Printer);
917 return res;