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