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