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