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