wined3d: Filter out some shader compilation spam.
[wine] / dlls / gdi32 / printdrv.c
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  *
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.
13  *
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.
18  *
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23
24 #include "config.h"
25 #include "wine/port.h"
26
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_private.h"
52
53 WINE_DEFAULT_DEBUG_CHANNEL(print);
54
55 static const char PrinterModel[]      = "Printer Model";
56 static const char DefaultDevMode[]    = "Default DevMode";
57 static const char PrinterDriverData[] = "PrinterDriverData";
58 static const char Printers[]          = "System\\CurrentControlSet\\Control\\Print\\Printers\\";
59
60
61 /******************************************************************
62  *                  StartDocA  [GDI32.@]
63  *
64  * StartDoc calls the STARTDOC Escape with the input data pointing to DocName
65  * and the output data (which is used as a second input parameter).pointing at
66  * the whole docinfo structure.  This seems to be an undocumented feature of
67  * the STARTDOC Escape.
68  *
69  * Note: we now do it the other way, with the STARTDOC Escape calling StartDoc.
70  */
71 INT WINAPI StartDocW(HDC hdc, const DOCINFOW* doc)
72 {
73     INT ret = 0;
74     DC *dc = get_dc_ptr( hdc );
75
76     TRACE("DocName = %s Output = %s Datatype = %s\n",
77           debugstr_w(doc->lpszDocName), debugstr_w(doc->lpszOutput),
78           debugstr_w(doc->lpszDatatype));
79
80     if(!dc) return SP_ERROR;
81
82     if (dc->funcs->pStartDoc) ret = dc->funcs->pStartDoc( dc->physDev, doc );
83     release_dc_ptr( dc );
84     return ret;
85 }
86
87 /*************************************************************************
88  *                  StartDocA [GDI32.@]
89  *
90  */
91 INT WINAPI StartDocA(HDC hdc, const DOCINFOA* doc)
92 {
93     LPWSTR szDocName = NULL, szOutput = NULL, szDatatype = NULL;
94     DOCINFOW docW;
95     INT ret, len;
96
97     docW.cbSize = doc->cbSize;
98     if (doc->lpszDocName)
99     {
100         len = MultiByteToWideChar(CP_ACP,0,doc->lpszDocName,-1,NULL,0);
101         szDocName = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
102         MultiByteToWideChar(CP_ACP,0,doc->lpszDocName,-1,szDocName,len);
103     }
104     if (doc->lpszOutput)
105     {
106         len = MultiByteToWideChar(CP_ACP,0,doc->lpszOutput,-1,NULL,0);
107         szOutput = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
108         MultiByteToWideChar(CP_ACP,0,doc->lpszOutput,-1,szOutput,len);
109     }
110     if (doc->lpszDatatype)
111     {
112         len = MultiByteToWideChar(CP_ACP,0,doc->lpszDatatype,-1,NULL,0);
113         szDatatype = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
114         MultiByteToWideChar(CP_ACP,0,doc->lpszDatatype,-1,szDatatype,len);
115     }
116
117     docW.lpszDocName = szDocName;
118     docW.lpszOutput = szOutput;
119     docW.lpszDatatype = szDatatype;
120     docW.fwType = doc->fwType;
121
122     ret = StartDocW(hdc, &docW);
123
124     HeapFree( GetProcessHeap(), 0, szDocName );
125     HeapFree( GetProcessHeap(), 0, szOutput );
126     HeapFree( GetProcessHeap(), 0, szDatatype );
127
128     return ret;
129 }
130
131
132 /******************************************************************
133  *                  EndDoc  [GDI32.@]
134  *
135  */
136 INT WINAPI EndDoc(HDC hdc)
137 {
138     INT ret = 0;
139     DC *dc = get_dc_ptr( hdc );
140     if(!dc) return SP_ERROR;
141
142     if (dc->funcs->pEndDoc) ret = dc->funcs->pEndDoc( dc->physDev );
143     release_dc_ptr( dc );
144     return ret;
145 }
146
147
148 /******************************************************************
149  *                  StartPage  [GDI32.@]
150  *
151  */
152 INT WINAPI StartPage(HDC hdc)
153 {
154     INT ret = 1;
155     DC *dc = get_dc_ptr( hdc );
156     if(!dc) return SP_ERROR;
157
158     if(dc->funcs->pStartPage)
159         ret = dc->funcs->pStartPage( dc->physDev );
160     else
161         FIXME("stub\n");
162     release_dc_ptr( dc );
163     return ret;
164 }
165
166
167 /******************************************************************
168  *                  EndPage  [GDI32.@]
169  *
170  */
171 INT WINAPI EndPage(HDC hdc)
172 {
173     INT ret = 0;
174     DC *dc = get_dc_ptr( hdc );
175     if(!dc) return SP_ERROR;
176
177     if (dc->funcs->pEndPage) ret = dc->funcs->pEndPage( dc->physDev );
178     if (dc->pAbortProc && !dc->pAbortProc( hdc, 0 ))
179     {
180         EndDoc( hdc );
181         ret = 0;
182     }
183     release_dc_ptr( dc );
184     return ret;
185 }
186
187
188 /******************************************************************************
189  *                 AbortDoc  [GDI32.@]
190  */
191 INT WINAPI AbortDoc(HDC hdc)
192 {
193     INT ret = 0;
194     DC *dc = get_dc_ptr( hdc );
195     if(!dc) return SP_ERROR;
196
197     if (dc->funcs->pAbortDoc) ret = dc->funcs->pAbortDoc( dc->physDev );
198     release_dc_ptr( dc );
199     return ret;
200 }
201
202 /**********************************************************************
203  *           QueryAbort   (GDI.155)
204  *
205  *  Calls the app's AbortProc function if avail.
206  *
207  * RETURNS
208  * TRUE if no AbortProc avail or AbortProc wants to continue printing.
209  * FALSE if AbortProc wants to abort printing.
210  */
211 BOOL16 WINAPI QueryAbort16(HDC16 hdc16, INT16 reserved)
212 {
213     BOOL ret = TRUE;
214     HDC hdc = HDC_32( hdc16 );
215     DC *dc = get_dc_ptr( hdc );
216
217     if(!dc) {
218         ERR("Invalid hdc %p\n", hdc);
219         return FALSE;
220     }
221     if (dc->pAbortProc) ret = dc->pAbortProc(hdc, 0);
222     release_dc_ptr( dc );
223     return ret;
224 }
225
226
227 /**********************************************************************
228  *           call_abort_proc16
229  */
230 static BOOL CALLBACK call_abort_proc16( HDC hdc, INT code )
231 {
232     ABORTPROC16 proc16;
233     DC *dc = get_dc_ptr( hdc );
234
235     if (!dc) return FALSE;
236     proc16 = dc->pAbortProc16;
237     release_dc_ptr( dc );
238     if (proc16)
239     {
240         WORD args[2];
241         DWORD ret;
242
243         args[1] = HDC_16(hdc);
244         args[0] = code;
245         WOWCallback16Ex( (DWORD)proc16, WCB16_PASCAL, sizeof(args), args, &ret );
246         return LOWORD(ret);
247     }
248     return TRUE;
249 }
250
251
252 /**********************************************************************
253  *           SetAbortProc   (GDI.381)
254  */
255 INT16 WINAPI SetAbortProc16(HDC16 hdc16, ABORTPROC16 abrtprc)
256 {
257     HDC hdc = HDC_32( hdc16 );
258     DC *dc = get_dc_ptr( hdc );
259
260     if (!dc) return FALSE;
261     dc->pAbortProc16 = abrtprc;
262     dc->pAbortProc   = call_abort_proc16;
263     release_dc_ptr( dc );
264     return TRUE;
265 }
266
267 /**********************************************************************
268  *           SetAbortProc   (GDI32.@)
269  *
270  */
271 INT WINAPI SetAbortProc(HDC hdc, ABORTPROC abrtprc)
272 {
273     DC *dc = get_dc_ptr( hdc );
274
275     if (!dc) return FALSE;
276     dc->pAbortProc = abrtprc;
277     release_dc_ptr( dc );
278     return TRUE;
279 }
280
281
282 /****************** misc. printer related functions */
283
284 /*
285  * The following function should implement a queing system
286  */
287 struct hpq
288 {
289     struct hpq  *next;
290     int          tag;
291     int          key;
292 };
293
294 static struct hpq *hpqueue;
295
296 /**********************************************************************
297  *           CreatePQ   (GDI.230)
298  *
299  */
300 HPQ16 WINAPI CreatePQ16(INT16 size)
301 {
302 #if 0
303     HGLOBAL16 hpq = 0;
304     WORD tmp_size;
305     LPWORD pPQ;
306
307     tmp_size = size << 2;
308     if (!(hpq = GlobalAlloc16(GMEM_SHARE|GMEM_MOVEABLE, tmp_size + 8)))
309        return 0xffff;
310     pPQ = GlobalLock16(hpq);
311     *pPQ++ = 0;
312     *pPQ++ = tmp_size;
313     *pPQ++ = 0;
314     *pPQ++ = 0;
315     GlobalUnlock16(hpq);
316
317     return (HPQ16)hpq;
318 #else
319     FIXME("(%d): stub\n",size);
320     return 1;
321 #endif
322 }
323
324 /**********************************************************************
325  *           DeletePQ   (GDI.235)
326  *
327  */
328 INT16 WINAPI DeletePQ16(HPQ16 hPQ)
329 {
330     return GlobalFree16((HGLOBAL16)hPQ);
331 }
332
333 /**********************************************************************
334  *           ExtractPQ   (GDI.232)
335  *
336  */
337 INT16 WINAPI ExtractPQ16(HPQ16 hPQ)
338 {
339     struct hpq *queue, *prev, *current, *currentPrev;
340     int key = 0, tag = -1;
341     currentPrev = prev = NULL;
342     queue = current = hpqueue;
343     if (current)
344         key = current->key;
345
346     while (current)
347     {
348         currentPrev = current;
349         current = current->next;
350         if (current)
351         {
352             if (current->key < key)
353             {
354                 queue = current;
355                 prev = currentPrev;
356             }
357         }
358     }
359     if (queue)
360     {
361         tag = queue->tag;
362
363         if (prev)
364             prev->next = queue->next;
365         else
366             hpqueue = queue->next;
367         HeapFree(GetProcessHeap(), 0, queue);
368     }
369
370     TRACE("%x got tag %d key %d\n", hPQ, tag, key);
371
372     return tag;
373 }
374
375 /**********************************************************************
376  *           InsertPQ   (GDI.233)
377  *
378  */
379 INT16 WINAPI InsertPQ16(HPQ16 hPQ, INT16 tag, INT16 key)
380 {
381     struct hpq *queueItem = HeapAlloc(GetProcessHeap(), 0, sizeof(struct hpq));
382     if(queueItem == NULL) {
383         ERR("Memory exausted!\n");
384         return FALSE;
385     }
386     queueItem->next = hpqueue;
387     hpqueue = queueItem;
388     queueItem->key = key;
389     queueItem->tag = tag;
390
391     FIXME("(%x %d %d): stub???\n", hPQ, tag, key);
392     return TRUE;
393 }
394
395 /**********************************************************************
396  *           MinPQ   (GDI.231)
397  *
398  */
399 INT16 WINAPI MinPQ16(HPQ16 hPQ)
400 {
401     FIXME("(%x): stub\n", hPQ);
402     return 0;
403 }
404
405 /**********************************************************************
406  *           SizePQ   (GDI.234)
407  *
408  */
409 INT16 WINAPI SizePQ16(HPQ16 hPQ, INT16 sizechange)
410 {
411     FIXME("(%x %d): stub\n", hPQ, sizechange);
412     return -1;
413 }
414
415
416
417 /*
418  * The following functions implement part of the spooling process to
419  * print manager.  I would like to see wine have a version of print managers
420  * that used LPR/LPD.  For simplicity print jobs will be sent to a file for
421  * now.
422  */
423 typedef struct PRINTJOB
424 {
425     char        *pszOutput;
426     char        *pszTitle;
427     HDC16       hDC;
428     HANDLE16    hHandle;
429     int         nIndex;
430     int         fd;
431 } PRINTJOB, *PPRINTJOB;
432
433 #define MAX_PRINT_JOBS 1
434 #define SP_OK 1
435
436 static PPRINTJOB gPrintJobsTable[MAX_PRINT_JOBS];
437
438
439 static PPRINTJOB FindPrintJobFromHandle(HANDLE16 hHandle)
440 {
441     return gPrintJobsTable[0];
442 }
443
444 static int CreateSpoolFile(LPCSTR pszOutput)
445 {
446     int fd=-1;
447     char psCmd[1024];
448     const char *psCmdP = psCmd;
449     HKEY hkey;
450
451     /* TTD convert the 'output device' into a spool file name */
452
453     if (pszOutput == NULL || *pszOutput == '\0')
454       return -1;
455
456     psCmd[0] = 0;
457     /* @@ Wine registry key: HKCU\Software\Wine\Printing\Spooler */
458     if(!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Printing\\Spooler", &hkey))
459     {
460         DWORD type, count = sizeof(psCmd);
461         RegQueryValueExA(hkey, pszOutput, 0, &type, (LPBYTE)psCmd, &count);
462         RegCloseKey(hkey);
463     }
464     if (!psCmd[0] && !strncmp("LPR:",pszOutput,4))
465         sprintf(psCmd,"|lpr -P'%s'",pszOutput+4);
466
467     TRACE("Got printerSpoolCommand '%s' for output device '%s'\n",
468           psCmd, pszOutput);
469     if (!*psCmd)
470         psCmdP = pszOutput;
471     else
472     {
473         while (*psCmdP && isspace(*psCmdP))
474         {
475             psCmdP++;
476         }
477         if (!*psCmdP)
478             return -1;
479     }
480     TRACE("command: '%s'\n", psCmdP);
481 #ifdef HAVE_FORK
482     if (*psCmdP == '|')
483     {
484         int fds[2];
485         if (pipe(fds)) {
486             ERR("pipe() failed!\n"); 
487             return -1;
488         }
489         if (fork() == 0)
490         {
491             psCmdP++;
492
493             TRACE("In child need to exec %s\n",psCmdP);
494             close(0);
495             dup2(fds[0],0);
496             close (fds[1]);
497
498             /* reset signals that we previously set to SIG_IGN */
499             signal( SIGPIPE, SIG_DFL );
500             signal( SIGCHLD, SIG_DFL );
501
502             execl("/bin/sh", "/bin/sh", "-c", psCmdP, (char*)0);
503             _exit(1);
504
505         }
506         close (fds[0]);
507         fd = fds[1];
508         TRACE("Need to execute a cmnd and pipe the output to it\n");
509     }
510     else
511 #endif
512     {
513         char *buffer;
514         WCHAR psCmdPW[MAX_PATH];
515
516         TRACE("Just assume it's a file\n");
517
518         /**
519          * The file name can be dos based, we have to find its
520          * Unix correspondant file name
521          */
522         MultiByteToWideChar(CP_ACP, 0, psCmdP, -1, psCmdPW, MAX_PATH);
523         if ((buffer = wine_get_unix_file_name(psCmdPW)))
524         {
525             if ((fd = open(buffer, O_CREAT | O_TRUNC | O_WRONLY, 0666)) < 0)
526             {
527                 ERR("Failed to create spool file '%s' ('%s'). (error %s)\n",
528                     buffer, psCmdP, strerror(errno));
529             }
530             HeapFree(GetProcessHeap(), 0, buffer);
531         }
532     }
533     return fd;
534 }
535
536 static int FreePrintJob(HANDLE16 hJob)
537 {
538     int nRet = SP_ERROR;
539     PPRINTJOB pPrintJob;
540
541     pPrintJob = FindPrintJobFromHandle(hJob);
542     if (pPrintJob != NULL)
543     {
544         gPrintJobsTable[pPrintJob->nIndex] = NULL;
545         HeapFree(GetProcessHeap(), 0, pPrintJob->pszOutput);
546         HeapFree(GetProcessHeap(), 0, pPrintJob->pszTitle);
547         if (pPrintJob->fd >= 0) close(pPrintJob->fd);
548         HeapFree(GetProcessHeap(), 0, pPrintJob);
549         nRet = SP_OK;
550     }
551     return nRet;
552 }
553
554 /**********************************************************************
555  *           OpenJob   (GDI.240)
556  *
557  */
558 HPJOB16 WINAPI OpenJob16(LPCSTR lpOutput, LPCSTR lpTitle, HDC16 hDC)
559 {
560     HPJOB16 hHandle = (HPJOB16)SP_ERROR;
561     PPRINTJOB pPrintJob;
562
563     TRACE("'%s' '%s' %04x\n", lpOutput, lpTitle, hDC);
564
565     pPrintJob = gPrintJobsTable[0];
566     if (pPrintJob == NULL)
567     {
568         int fd;
569
570         /* Try and create a spool file */
571         fd = CreateSpoolFile(lpOutput);
572         if (fd >= 0)
573         {
574             pPrintJob = HeapAlloc(GetProcessHeap(), 0, sizeof(PRINTJOB));
575             if(pPrintJob == NULL) {
576                 WARN("Memory exausted!\n");
577                 return hHandle;
578             }
579
580             hHandle = 1;
581
582             pPrintJob->pszOutput = HeapAlloc(GetProcessHeap(), 0, strlen(lpOutput)+1);
583             strcpy( pPrintJob->pszOutput, lpOutput );
584             if(lpTitle)
585             {
586                 pPrintJob->pszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(lpTitle)+1);
587                 strcpy( pPrintJob->pszTitle, lpTitle );
588             }
589             pPrintJob->hDC = hDC;
590             pPrintJob->fd = fd;
591             pPrintJob->nIndex = 0;
592             pPrintJob->hHandle = hHandle;
593             gPrintJobsTable[pPrintJob->nIndex] = pPrintJob;
594         }
595     }
596     TRACE("return %04x\n", hHandle);
597     return hHandle;
598 }
599
600 /**********************************************************************
601  *           CloseJob   (GDI.243)
602  *
603  */
604 INT16 WINAPI CloseJob16(HPJOB16 hJob)
605 {
606     int nRet = SP_ERROR;
607     PPRINTJOB pPrintJob = NULL;
608
609     TRACE("%04x\n", hJob);
610
611     pPrintJob = FindPrintJobFromHandle(hJob);
612     if (pPrintJob != NULL)
613     {
614         /* Close the spool file */
615         close(pPrintJob->fd);
616         FreePrintJob(hJob);
617         nRet  = 1;
618     }
619     return nRet;
620 }
621
622 /**********************************************************************
623  *           WriteSpool   (GDI.241)
624  *
625  */
626 INT16 WINAPI WriteSpool16(HPJOB16 hJob, LPSTR lpData, INT16 cch)
627 {
628     int nRet = SP_ERROR;
629     PPRINTJOB pPrintJob = NULL;
630
631     TRACE("%04x %p %04x\n", hJob, lpData, cch);
632
633     pPrintJob = FindPrintJobFromHandle(hJob);
634     if (pPrintJob != NULL && pPrintJob->fd >= 0 && cch)
635     {
636         if (write(pPrintJob->fd, lpData, cch) != cch)
637           nRet = SP_OUTOFDISK;
638         else
639           nRet = cch;
640 #if 0
641         /* FIXME: We just cannot call 16 bit functions from here, since we
642          * have acquired several locks (DC). And we do not really need to.
643          */
644         if (pPrintJob->hDC == 0) {
645             TRACE("hDC == 0 so no QueryAbort\n");
646         }
647         else if (!(QueryAbort16(pPrintJob->hDC, (nRet == SP_OUTOFDISK) ? nRet : 0 )))
648         {
649             CloseJob16(hJob); /* printing aborted */
650             nRet = SP_APPABORT;
651         }
652 #endif
653     }
654     return nRet;
655 }
656
657 typedef INT (WINAPI *MSGBOX_PROC)( HWND, LPCSTR, LPCSTR, UINT );
658
659 /**********************************************************************
660  *           WriteDialog   (GDI.242)
661  *
662  */
663 INT16 WINAPI WriteDialog16(HPJOB16 hJob, LPSTR lpMsg, INT16 cchMsg)
664 {
665     HMODULE mod;
666     MSGBOX_PROC pMessageBoxA;
667     INT16 ret = 0;
668
669     TRACE("%04x %04x '%s'\n", hJob,  cchMsg, lpMsg);
670
671     if ((mod = GetModuleHandleA("user32.dll")))
672     {
673         if ((pMessageBoxA = (MSGBOX_PROC)GetProcAddress( mod, "MessageBoxA" )))
674             ret = pMessageBoxA(0, lpMsg, "Printing Error", MB_OKCANCEL);
675     }
676     return ret;
677 }
678
679
680 /**********************************************************************
681  *           DeleteJob  (GDI.244)
682  *
683  */
684 INT16 WINAPI DeleteJob16(HPJOB16 hJob, INT16 nNotUsed)
685 {
686     int nRet;
687
688     TRACE("%04x\n", hJob);
689
690     nRet = FreePrintJob(hJob);
691     return nRet;
692 }
693
694 /*
695  * The following two function would allow a page to be sent to the printer
696  * when it has been processed.  For simplicity they havn't been implemented.
697  * This means a whole job has to be processed before it is sent to the printer.
698  */
699
700 /**********************************************************************
701  *           StartSpoolPage   (GDI.246)
702  *
703  */
704 INT16 WINAPI StartSpoolPage16(HPJOB16 hJob)
705 {
706     FIXME("StartSpoolPage GDI.246 unimplemented\n");
707     return 1;
708
709 }
710
711
712 /**********************************************************************
713  *           EndSpoolPage   (GDI.247)
714  *
715  */
716 INT16 WINAPI EndSpoolPage16(HPJOB16 hJob)
717 {
718     FIXME("EndSpoolPage GDI.247 unimplemented\n");
719     return 1;
720 }
721
722
723 /**********************************************************************
724  *           GetSpoolJob   (GDI.245)
725  *
726  */
727 DWORD WINAPI GetSpoolJob16(int nOption, LONG param)
728 {
729     DWORD retval = 0;
730     TRACE("In GetSpoolJob param 0x%x noption %d\n",param, nOption);
731     return retval;
732 }
733
734
735 /******************************************************************
736  *                  DrvGetPrinterDataInternal
737  *
738  * Helper for DrvGetPrinterData
739  */
740 static DWORD DrvGetPrinterDataInternal(LPSTR RegStr_Printer,
741 LPBYTE lpPrinterData, int cbData, int what)
742 {
743     DWORD res = -1;
744     HKEY hkey;
745     DWORD dwType, cbQueryData;
746
747     if (!(RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey))) {
748         if (what == INT_PD_DEFAULT_DEVMODE) { /* "Default DevMode" */
749             if (!(RegQueryValueExA(hkey, DefaultDevMode, 0, &dwType, 0, &cbQueryData))) {
750                 if (!lpPrinterData)
751                     res = cbQueryData;
752                 else if ((cbQueryData) && (cbQueryData <= cbData)) {
753                     cbQueryData = cbData;
754                     if (RegQueryValueExA(hkey, DefaultDevMode, 0,
755                                 &dwType, lpPrinterData, &cbQueryData))
756                         res = cbQueryData;
757                 }
758             }
759         } else { /* "Printer Driver" */
760             cbQueryData = 32;
761             RegQueryValueExA(hkey, "Printer Driver", 0,
762                         &dwType, lpPrinterData, &cbQueryData);
763             res = cbQueryData;
764         }
765     }
766     if (hkey) RegCloseKey(hkey);
767     return res;
768 }
769
770 /******************************************************************
771  *                DrvGetPrinterData     (GDI.282)
772  *
773  */
774 DWORD WINAPI DrvGetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
775                                LPDWORD lpType, LPBYTE lpPrinterData,
776                                int cbData, LPDWORD lpNeeded)
777 {
778     LPSTR RegStr_Printer;
779     HKEY hkey = 0, hkey2 = 0;
780     DWORD res = 0;
781     DWORD dwType, PrinterAttr, cbPrinterAttr, SetData, size;
782
783     if (HIWORD(lpPrinter))
784             TRACE("printer %s\n",lpPrinter);
785     else
786             TRACE("printer %p\n",lpPrinter);
787     if (HIWORD(lpProfile))
788             TRACE("profile %s\n",lpProfile);
789     else
790             TRACE("profile %p\n",lpProfile);
791     TRACE("lpType %p\n",lpType);
792
793     if ((!lpPrinter) || (!lpProfile) || (!lpNeeded))
794         return ERROR_INVALID_PARAMETER;
795
796     RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
797                                strlen(Printers) + strlen(lpPrinter) + 2);
798     strcpy(RegStr_Printer, Printers);
799     strcat(RegStr_Printer, lpPrinter);
800
801     if ((PtrToUlong(lpProfile) == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
802     (!strcmp(lpProfile, DefaultDevMode)))) {
803         size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
804                                          INT_PD_DEFAULT_DEVMODE);
805         if (size+1) {
806             *lpNeeded = size;
807             if ((lpPrinterData) && (*lpNeeded > cbData))
808                 res = ERROR_MORE_DATA;
809         }
810         else res = ERROR_INVALID_PRINTER_NAME;
811     }
812     else
813     if ((PtrToUlong(lpProfile) == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
814     (!strcmp(lpProfile, PrinterModel)))) {
815         *lpNeeded = 32;
816         if (!lpPrinterData) goto failed;
817         if (cbData < 32) {
818             res = ERROR_MORE_DATA;
819             goto failed;
820         }
821         size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
822                                          INT_PD_DEFAULT_MODEL);
823         if ((size+1) && (lpType))
824             *lpType = REG_SZ;
825         else
826             res = ERROR_INVALID_PRINTER_NAME;
827     }
828     else
829     {
830         if ((res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)))
831             goto failed;
832         cbPrinterAttr = 4;
833         if ((res = RegQueryValueExA(hkey, "Attributes", 0,
834                         &dwType, (LPBYTE)&PrinterAttr, &cbPrinterAttr)))
835             goto failed;
836         if ((res = RegOpenKeyA(hkey, PrinterDriverData, &hkey2)))
837             goto failed;
838         *lpNeeded = cbData;
839         res = RegQueryValueExA(hkey2, lpProfile, 0,
840                 lpType, lpPrinterData, lpNeeded);
841         if ((res != ERROR_CANTREAD) &&
842          ((PrinterAttr &
843         (PRINTER_ATTRIBUTE_ENABLE_BIDI|PRINTER_ATTRIBUTE_NETWORK))
844         == PRINTER_ATTRIBUTE_NETWORK))
845         {
846             if (!(res) && (*lpType == REG_DWORD) && (*(LPDWORD)lpPrinterData == -1))
847                 res = ERROR_INVALID_DATA;
848         }
849         else
850         {
851             SetData = -1;
852             RegSetValueExA(hkey2, lpProfile, 0, REG_DWORD, (LPBYTE)&SetData, 4); /* no result returned */
853         }
854     }
855
856 failed:
857     if (hkey2) RegCloseKey(hkey2);
858     if (hkey) RegCloseKey(hkey);
859     HeapFree(GetProcessHeap(), 0, RegStr_Printer);
860     return res;
861 }
862
863
864 /******************************************************************
865  *                 DrvSetPrinterData     (GDI.281)
866  *
867  */
868 DWORD WINAPI DrvSetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
869                                DWORD lpType, LPBYTE lpPrinterData,
870                                DWORD dwSize)
871 {
872     LPSTR RegStr_Printer;
873     HKEY hkey = 0;
874     DWORD res = 0;
875
876     if (HIWORD(lpPrinter))
877             TRACE("printer %s\n",lpPrinter);
878     else
879             TRACE("printer %p\n",lpPrinter);
880     if (HIWORD(lpProfile))
881             TRACE("profile %s\n",lpProfile);
882     else
883             TRACE("profile %p\n",lpProfile);
884     TRACE("lpType %08x\n",lpType);
885
886     if ((!lpPrinter) || (!lpProfile) ||
887     (PtrToUlong(lpProfile) == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
888     (!strcmp(lpProfile, PrinterModel))))
889         return ERROR_INVALID_PARAMETER;
890
891     RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
892                         strlen(Printers) + strlen(lpPrinter) + 2);
893     strcpy(RegStr_Printer, Printers);
894     strcat(RegStr_Printer, lpPrinter);
895
896     if ((PtrToUlong(lpProfile) == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
897     (!strcmp(lpProfile, DefaultDevMode)))) {
898         if ( RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)
899              != ERROR_SUCCESS ||
900              RegSetValueExA(hkey, DefaultDevMode, 0, REG_BINARY,
901                               lpPrinterData, dwSize) != ERROR_SUCCESS )
902                 res = ERROR_INVALID_PRINTER_NAME;
903     }
904     else
905     {
906         strcat(RegStr_Printer, "\\");
907
908         if( (res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)) ==
909             ERROR_SUCCESS ) {
910
911             if (!lpPrinterData)
912                 res = RegDeleteValueA(hkey, lpProfile);
913             else
914                 res = RegSetValueExA(hkey, lpProfile, 0, lpType,
915                                        lpPrinterData, dwSize);
916         }
917     }
918
919     if (hkey) RegCloseKey(hkey);
920     HeapFree(GetProcessHeap(), 0, RegStr_Printer);
921     return res;
922 }