fdopen: don't rewind the file after creating the FILE* handle. Added
[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 /* ### start build ### */
218 extern WORD CALLBACK PRTDRV_CallTo16_word_ww(FARPROC16,WORD,WORD);
219 /* ### stop build ### */
220
221 /**********************************************************************
222  *           call_abort_proc16
223  */
224 static BOOL CALLBACK call_abort_proc16( HDC hdc, INT code )
225 {
226     ABORTPROC16 proc16;
227     DC *dc = DC_GetDCPtr( hdc );
228
229     if (!dc) return FALSE;
230     proc16 = dc->pAbortProc16;
231     GDI_ReleaseObj( hdc );
232     if (proc16) return PRTDRV_CallTo16_word_ww( (FARPROC16)proc16, HDC_16(hdc), code );
233     return TRUE;
234 }
235
236
237 /**********************************************************************
238  *           SetAbortProc   (GDI.381)
239  */
240 INT16 WINAPI SetAbortProc16(HDC16 hdc16, ABORTPROC16 abrtprc)
241 {
242     HDC hdc = HDC_32( hdc16 );
243     DC *dc = DC_GetDCPtr( hdc );
244
245     if (!dc) return FALSE;
246     dc->pAbortProc16 = abrtprc;
247     GDI_ReleaseObj( hdc );
248     return SetAbortProc( hdc, call_abort_proc16 );
249 }
250
251 /**********************************************************************
252  *           SetAbortProc   (GDI32.@)
253  *
254  */
255 INT WINAPI SetAbortProc(HDC hdc, ABORTPROC abrtprc)
256 {
257     DC *dc = DC_GetDCPtr( hdc );
258
259     if (!dc) return FALSE;
260     dc->pAbortProc = abrtprc;
261     GDI_ReleaseObj( hdc );
262     return TRUE;
263 }
264
265
266 /****************** misc. printer related functions */
267
268 /*
269  * The following function should implement a queing system
270  */
271 struct hpq
272 {
273     struct hpq  *next;
274     int          tag;
275     int          key;
276 };
277
278 static struct hpq *hpqueue;
279
280 /**********************************************************************
281  *           CreatePQ   (GDI.230)
282  *
283  */
284 HPQ16 WINAPI CreatePQ16(INT16 size)
285 {
286 #if 0
287     HGLOBAL16 hpq = 0;
288     WORD tmp_size;
289     LPWORD pPQ;
290
291     tmp_size = size << 2;
292     if (!(hpq = GlobalAlloc16(GMEM_SHARE|GMEM_MOVEABLE, tmp_size + 8)))
293        return 0xffff;
294     pPQ = GlobalLock16(hpq);
295     *pPQ++ = 0;
296     *pPQ++ = tmp_size;
297     *pPQ++ = 0;
298     *pPQ++ = 0;
299     GlobalUnlock16(hpq);
300
301     return (HPQ16)hpq;
302 #else
303     FIXME("(%d): stub\n",size);
304     return 1;
305 #endif
306 }
307
308 /**********************************************************************
309  *           DeletePQ   (GDI.235)
310  *
311  */
312 INT16 WINAPI DeletePQ16(HPQ16 hPQ)
313 {
314     return GlobalFree16((HGLOBAL16)hPQ);
315 }
316
317 /**********************************************************************
318  *           ExtractPQ   (GDI.232)
319  *
320  */
321 INT16 WINAPI ExtractPQ16(HPQ16 hPQ)
322 {
323     struct hpq *queue, *prev, *current, *currentPrev;
324     int key = 0, tag = -1;
325     currentPrev = prev = NULL;
326     queue = current = hpqueue;
327     if (current)
328         key = current->key;
329
330     while (current)
331     {
332         currentPrev = current;
333         current = current->next;
334         if (current)
335         {
336             if (current->key < key)
337             {
338                 queue = current;
339                 prev = currentPrev;
340             }
341         }
342     }
343     if (queue)
344     {
345         tag = queue->tag;
346
347         if (prev)
348             prev->next = queue->next;
349         else
350             hpqueue = queue->next;
351         HeapFree(GetProcessHeap(), 0, queue);
352     }
353
354     TRACE("%x got tag %d key %d\n", hPQ, tag, key);
355
356     return tag;
357 }
358
359 /**********************************************************************
360  *           InsertPQ   (GDI.233)
361  *
362  */
363 INT16 WINAPI InsertPQ16(HPQ16 hPQ, INT16 tag, INT16 key)
364 {
365     struct hpq *queueItem = HeapAlloc(GetProcessHeap(), 0, sizeof(struct hpq));
366     if(queueItem == NULL) {
367         ERR("Memory exausted!\n");
368         return FALSE;
369     }
370     queueItem->next = hpqueue;
371     hpqueue = queueItem;
372     queueItem->key = key;
373     queueItem->tag = tag;
374
375     FIXME("(%x %d %d): stub???\n", hPQ, tag, key);
376     return TRUE;
377 }
378
379 /**********************************************************************
380  *           MinPQ   (GDI.231)
381  *
382  */
383 INT16 WINAPI MinPQ16(HPQ16 hPQ)
384 {
385     FIXME("(%x): stub\n", hPQ);
386     return 0;
387 }
388
389 /**********************************************************************
390  *           SizePQ   (GDI.234)
391  *
392  */
393 INT16 WINAPI SizePQ16(HPQ16 hPQ, INT16 sizechange)
394 {
395     FIXME("(%x %d): stub\n", hPQ, sizechange);
396     return -1;
397 }
398
399
400
401 /*
402  * The following functions implement part of the spooling process to
403  * print manager.  I would like to see wine have a version of print managers
404  * that used LPR/LPD.  For simplicity print jobs will be sent to a file for
405  * now.
406  */
407 typedef struct PRINTJOB
408 {
409     char        *pszOutput;
410     char        *pszTitle;
411     HDC16       hDC;
412     HANDLE16    hHandle;
413     int         nIndex;
414     int         fd;
415 } PRINTJOB, *PPRINTJOB;
416
417 #define MAX_PRINT_JOBS 1
418 #define SP_OK 1
419
420 PPRINTJOB gPrintJobsTable[MAX_PRINT_JOBS];
421
422
423 static PPRINTJOB FindPrintJobFromHandle(HANDLE16 hHandle)
424 {
425     return gPrintJobsTable[0];
426 }
427
428 static int CreateSpoolFile(LPCSTR pszOutput)
429 {
430     int fd=-1;
431     char psCmd[1024];
432     char *psCmdP = psCmd;
433
434     /* TTD convert the 'output device' into a spool file name */
435
436     if (pszOutput == NULL || *pszOutput == '\0')
437       return -1;
438
439     if (!strncmp("LPR:",pszOutput,4))
440       sprintf(psCmd,"|lpr -P%s",pszOutput+4);
441     else
442     {
443         HKEY hkey;
444         /* default value */
445         psCmd[0] = 0;
446         if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\spooler", &hkey))
447         {
448             DWORD type, count = sizeof(psCmd);
449             RegQueryValueExA(hkey, pszOutput, 0, &type, psCmd, &count);
450             RegCloseKey(hkey);
451         }
452     }
453     TRACE("Got printerSpoolCommand '%s' for output device '%s'\n",
454           psCmd, pszOutput);
455     if (!*psCmd)
456         psCmdP = (char *)pszOutput;
457     else
458     {
459         while (*psCmdP && isspace(*psCmdP))
460         {
461             psCmdP++;
462         };
463         if (!*psCmdP)
464             return -1;
465     }
466     if (*psCmdP == '|')
467     {
468         int fds[2];
469         if (pipe(fds))
470             return -1;
471         if (fork() == 0)
472         {
473             psCmdP++;
474
475             TRACE("In child need to exec %s\n",psCmdP);
476             close(0);
477             dup2(fds[0],0);
478             close (fds[1]);
479             system(psCmdP);
480             exit(0);
481
482         }
483         close (fds[0]);
484         fd = fds[1];
485         TRACE("Need to execute a cmnd and pipe the output to it\n");
486     }
487     else
488     {
489         char buffer[MAX_PATH];
490
491         TRACE("Just assume it's a file\n");
492
493         /**
494          * The file name can be dos based, we have to find its
495          * Unix correspondant file name
496          */
497         wine_get_unix_file_name(psCmdP, buffer, sizeof(buffer));
498
499         if ((fd = open(buffer, O_CREAT | O_TRUNC | O_WRONLY , 0600)) < 0)
500         {
501             ERR("Failed to create spool file '%s' ('%s'). (error %s)\n",
502                 buffer, psCmdP, strerror(errno));
503         }
504     }
505     return fd;
506 }
507
508 static int FreePrintJob(HANDLE16 hJob)
509 {
510     int nRet = SP_ERROR;
511     PPRINTJOB pPrintJob;
512
513     pPrintJob = FindPrintJobFromHandle(hJob);
514     if (pPrintJob != NULL)
515     {
516         gPrintJobsTable[pPrintJob->nIndex] = NULL;
517         HeapFree(GetProcessHeap(), 0, pPrintJob->pszOutput);
518         HeapFree(GetProcessHeap(), 0, pPrintJob->pszTitle);
519         if (pPrintJob->fd >= 0) close(pPrintJob->fd);
520         HeapFree(GetProcessHeap(), 0, pPrintJob);
521         nRet = SP_OK;
522     }
523     return nRet;
524 }
525
526 /**********************************************************************
527  *           OpenJob   (GDI.240)
528  *
529  */
530 HPJOB16 WINAPI OpenJob16(LPCSTR lpOutput, LPCSTR lpTitle, HDC16 hDC)
531 {
532     HPJOB16 hHandle = (HPJOB16)SP_ERROR;
533     PPRINTJOB pPrintJob;
534
535     TRACE("'%s' '%s' %04x\n", lpOutput, lpTitle, hDC);
536
537     pPrintJob = gPrintJobsTable[0];
538     if (pPrintJob == NULL)
539     {
540         int fd;
541
542         /* Try and create a spool file */
543         fd = CreateSpoolFile(lpOutput);
544         if (fd >= 0)
545         {
546             pPrintJob = HeapAlloc(GetProcessHeap(), 0, sizeof(PRINTJOB));
547             if(pPrintJob == NULL) {
548                 WARN("Memory exausted!\n");
549                 return hHandle;
550             }
551
552             hHandle = 1;
553
554             pPrintJob->pszOutput = HeapAlloc(GetProcessHeap(), 0, strlen(lpOutput)+1);
555             strcpy( pPrintJob->pszOutput, lpOutput );
556             if(lpTitle)
557             {
558                 pPrintJob->pszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(lpTitle)+1);
559                 strcpy( pPrintJob->pszTitle, lpTitle );
560             }
561             pPrintJob->hDC = hDC;
562             pPrintJob->fd = fd;
563             pPrintJob->nIndex = 0;
564             pPrintJob->hHandle = hHandle;
565             gPrintJobsTable[pPrintJob->nIndex] = pPrintJob;
566         }
567     }
568     TRACE("return %04x\n", hHandle);
569     return hHandle;
570 }
571
572 /**********************************************************************
573  *           CloseJob   (GDI.243)
574  *
575  */
576 INT16 WINAPI CloseJob16(HPJOB16 hJob)
577 {
578     int nRet = SP_ERROR;
579     PPRINTJOB pPrintJob = NULL;
580
581     TRACE("%04x\n", hJob);
582
583     pPrintJob = FindPrintJobFromHandle(hJob);
584     if (pPrintJob != NULL)
585     {
586         /* Close the spool file */
587         close(pPrintJob->fd);
588         FreePrintJob(hJob);
589         nRet  = 1;
590     }
591     return nRet;
592 }
593
594 /**********************************************************************
595  *           WriteSpool   (GDI.241)
596  *
597  */
598 INT16 WINAPI WriteSpool16(HPJOB16 hJob, LPSTR lpData, INT16 cch)
599 {
600     int nRet = SP_ERROR;
601     PPRINTJOB pPrintJob = NULL;
602
603     TRACE("%04x %08lx %04x\n", hJob, (DWORD)lpData, cch);
604
605     pPrintJob = FindPrintJobFromHandle(hJob);
606     if (pPrintJob != NULL && pPrintJob->fd >= 0 && cch)
607     {
608         if (write(pPrintJob->fd, lpData, cch) != cch)
609           nRet = SP_OUTOFDISK;
610         else
611           nRet = cch;
612 #if 0
613         /* FIXME: We just cannot call 16 bit functions from here, since we
614          * have acquired several locks (DC). And we do not really need to.
615          */
616         if (pPrintJob->hDC == 0) {
617             TRACE("hDC == 0 so no QueryAbort\n");
618         }
619         else if (!(QueryAbort16(pPrintJob->hDC, (nRet == SP_OUTOFDISK) ? nRet : 0 )))
620         {
621             CloseJob16(hJob); /* printing aborted */
622             nRet = SP_APPABORT;
623         }
624 #endif
625     }
626     return nRet;
627 }
628
629 typedef INT (WINAPI *MSGBOX_PROC)( HWND, LPCSTR, LPCSTR, UINT );
630
631 /**********************************************************************
632  *           WriteDialog   (GDI.242)
633  *
634  */
635 INT16 WINAPI WriteDialog16(HPJOB16 hJob, LPSTR lpMsg, INT16 cchMsg)
636 {
637     HMODULE mod;
638     MSGBOX_PROC pMessageBoxA;
639     INT16 ret = 0;
640
641     TRACE("%04x %04x '%s'\n", hJob,  cchMsg, lpMsg);
642
643     if ((mod = GetModuleHandleA("user32.dll")))
644     {
645         if ((pMessageBoxA = (MSGBOX_PROC)GetProcAddress( mod, "MessageBoxA" )))
646             ret = pMessageBoxA(0, lpMsg, "Printing Error", MB_OKCANCEL);
647     }
648     return ret;
649 }
650
651
652 /**********************************************************************
653  *           DeleteJob  (GDI.244)
654  *
655  */
656 INT16 WINAPI DeleteJob16(HPJOB16 hJob, INT16 nNotUsed)
657 {
658     int nRet;
659
660     TRACE("%04x\n", hJob);
661
662     nRet = FreePrintJob(hJob);
663     return nRet;
664 }
665
666 /*
667  * The following two function would allow a page to be sent to the printer
668  * when it has been processed.  For simplicity they havn't been implemented.
669  * This means a whole job has to be processed before it is sent to the printer.
670  */
671
672 /**********************************************************************
673  *           StartSpoolPage   (GDI.246)
674  *
675  */
676 INT16 WINAPI StartSpoolPage16(HPJOB16 hJob)
677 {
678     FIXME("StartSpoolPage GDI.246 unimplemented\n");
679     return 1;
680
681 }
682
683
684 /**********************************************************************
685  *           EndSpoolPage   (GDI.247)
686  *
687  */
688 INT16 WINAPI EndSpoolPage16(HPJOB16 hJob)
689 {
690     FIXME("EndSpoolPage GDI.247 unimplemented\n");
691     return 1;
692 }
693
694
695 /**********************************************************************
696  *           GetSpoolJob   (GDI.245)
697  *
698  */
699 DWORD WINAPI GetSpoolJob16(int nOption, LONG param)
700 {
701     DWORD retval = 0;
702     TRACE("In GetSpoolJob param 0x%lx noption %d\n",param, nOption);
703     return retval;
704 }
705
706
707 /******************************************************************
708  *                  DrvGetPrinterDataInternal
709  *
710  * Helper for DrvGetPrinterData
711  */
712 static DWORD DrvGetPrinterDataInternal(LPSTR RegStr_Printer,
713 LPBYTE lpPrinterData, int cbData, int what)
714 {
715     DWORD res = -1;
716     HKEY hkey;
717     DWORD dwType, cbQueryData;
718
719     if (!(RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey))) {
720         if (what == INT_PD_DEFAULT_DEVMODE) { /* "Default DevMode" */
721             if (!(RegQueryValueExA(hkey, DefaultDevMode, 0, &dwType, 0, &cbQueryData))) {
722                 if (!lpPrinterData)
723                     res = cbQueryData;
724                 else if ((cbQueryData) && (cbQueryData <= cbData)) {
725                     cbQueryData = cbData;
726                     if (RegQueryValueExA(hkey, DefaultDevMode, 0,
727                                 &dwType, lpPrinterData, &cbQueryData))
728                         res = cbQueryData;
729                 }
730             }
731         } else { /* "Printer Driver" */
732             cbQueryData = 32;
733             RegQueryValueExA(hkey, "Printer Driver", 0,
734                         &dwType, lpPrinterData, &cbQueryData);
735             res = cbQueryData;
736         }
737     }
738     if (hkey) RegCloseKey(hkey);
739     return res;
740 }
741
742 /******************************************************************
743  *                DrvGetPrinterData     (GDI.282)
744  *
745  */
746 DWORD WINAPI DrvGetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
747                                LPDWORD lpType, LPBYTE lpPrinterData,
748                                int cbData, LPDWORD lpNeeded)
749 {
750     LPSTR RegStr_Printer;
751     HKEY hkey = 0, hkey2 = 0;
752     DWORD res = 0;
753     DWORD dwType, PrinterAttr, cbPrinterAttr, SetData, size;
754
755     if (HIWORD(lpPrinter))
756             TRACE("printer %s\n",lpPrinter);
757     else
758             TRACE("printer %p\n",lpPrinter);
759     if (HIWORD(lpProfile))
760             TRACE("profile %s\n",lpProfile);
761     else
762             TRACE("profile %p\n",lpProfile);
763     TRACE("lpType %p\n",lpType);
764
765     if ((!lpPrinter) || (!lpProfile) || (!lpNeeded))
766         return ERROR_INVALID_PARAMETER;
767
768     RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
769                                strlen(Printers) + strlen(lpPrinter) + 2);
770     strcpy(RegStr_Printer, Printers);
771     strcat(RegStr_Printer, lpPrinter);
772
773     if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
774     (!strcmp(lpProfile, DefaultDevMode)))) {
775         size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
776                                          INT_PD_DEFAULT_DEVMODE);
777         if (size+1) {
778             *lpNeeded = size;
779             if ((lpPrinterData) && (*lpNeeded > cbData))
780                 res = ERROR_MORE_DATA;
781         }
782         else res = ERROR_INVALID_PRINTER_NAME;
783     }
784     else
785     if (((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
786     (!strcmp(lpProfile, PrinterModel)))) {
787         *lpNeeded = 32;
788         if (!lpPrinterData) goto failed;
789         if (cbData < 32) {
790             res = ERROR_MORE_DATA;
791             goto failed;
792         }
793         size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
794                                          INT_PD_DEFAULT_MODEL);
795         if ((size+1) && (lpType))
796             *lpType = REG_SZ;
797         else
798             res = ERROR_INVALID_PRINTER_NAME;
799     }
800     else
801     {
802         if ((res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)))
803             goto failed;
804         cbPrinterAttr = 4;
805         if ((res = RegQueryValueExA(hkey, "Attributes", 0,
806                         &dwType, (LPBYTE)&PrinterAttr, &cbPrinterAttr)))
807             goto failed;
808         if ((res = RegOpenKeyA(hkey, PrinterDriverData, &hkey2)))
809             goto failed;
810         *lpNeeded = cbData;
811         res = RegQueryValueExA(hkey2, lpProfile, 0,
812                 lpType, lpPrinterData, lpNeeded);
813         if ((res != ERROR_CANTREAD) &&
814          ((PrinterAttr &
815         (PRINTER_ATTRIBUTE_ENABLE_BIDI|PRINTER_ATTRIBUTE_NETWORK))
816         == PRINTER_ATTRIBUTE_NETWORK))
817         {
818             if (!(res) && (*lpType == REG_DWORD) && (*(LPDWORD)lpPrinterData == -1))
819                 res = ERROR_INVALID_DATA;
820         }
821         else
822         {
823             SetData = -1;
824             RegSetValueExA(hkey2, lpProfile, 0, REG_DWORD, (LPBYTE)&SetData, 4); /* no result returned */
825         }
826     }
827
828 failed:
829     if (hkey2) RegCloseKey(hkey2);
830     if (hkey) RegCloseKey(hkey);
831     HeapFree(GetProcessHeap(), 0, RegStr_Printer);
832     return res;
833 }
834
835
836 /******************************************************************
837  *                 DrvSetPrinterData     (GDI.281)
838  *
839  */
840 DWORD WINAPI DrvSetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
841                                DWORD lpType, LPBYTE lpPrinterData,
842                                DWORD dwSize)
843 {
844     LPSTR RegStr_Printer;
845     HKEY hkey = 0;
846     DWORD res = 0;
847
848     if (HIWORD(lpPrinter))
849             TRACE("printer %s\n",lpPrinter);
850     else
851             TRACE("printer %p\n",lpPrinter);
852     if (HIWORD(lpProfile))
853             TRACE("profile %s\n",lpProfile);
854     else
855             TRACE("profile %p\n",lpProfile);
856     TRACE("lpType %08lx\n",lpType);
857
858     if ((!lpPrinter) || (!lpProfile) ||
859     ((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
860     (!strcmp(lpProfile, PrinterModel))))
861         return ERROR_INVALID_PARAMETER;
862
863     RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
864                         strlen(Printers) + strlen(lpPrinter) + 2);
865     strcpy(RegStr_Printer, Printers);
866     strcat(RegStr_Printer, lpPrinter);
867
868     if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
869     (!strcmp(lpProfile, DefaultDevMode)))) {
870         if ( RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)
871              != ERROR_SUCCESS ||
872              RegSetValueExA(hkey, DefaultDevMode, 0, REG_BINARY,
873                               lpPrinterData, dwSize) != ERROR_SUCCESS )
874                 res = ERROR_INVALID_PRINTER_NAME;
875     }
876     else
877     {
878         strcat(RegStr_Printer, "\\");
879
880         if( (res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)) ==
881             ERROR_SUCCESS ) {
882
883             if (!lpPrinterData)
884                 res = RegDeleteValueA(hkey, lpProfile);
885             else
886                 res = RegSetValueExA(hkey, lpProfile, 0, lpType,
887                                        lpPrinterData, dwSize);
888         }
889     }
890
891     if (hkey) RegCloseKey(hkey);
892     HeapFree(GetProcessHeap(), 0, RegStr_Printer);
893     return res;
894 }