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