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