Split out some 16-bit GDI code.
[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  *
524  */
525 HPJOB16 WINAPI OpenJob16(LPCSTR lpOutput, LPCSTR lpTitle, HDC16 hDC)
526 {
527     HPJOB16 hHandle = (HPJOB16)SP_ERROR;
528     PPRINTJOB pPrintJob;
529
530     TRACE("'%s' '%s' %04x\n", lpOutput, lpTitle, hDC);
531
532     pPrintJob = gPrintJobsTable[0];
533     if (pPrintJob == NULL)
534     {
535         int fd;
536
537         /* Try and create a spool file */
538         fd = CreateSpoolFile(lpOutput);
539         if (fd >= 0)
540         {
541             pPrintJob = HeapAlloc(GetProcessHeap(), 0, sizeof(PRINTJOB));
542             if(pPrintJob == NULL) {
543                 WARN("Memory exausted!\n");
544                 return hHandle;
545             }
546
547             hHandle = 1;
548
549             pPrintJob->pszOutput = HeapAlloc(GetProcessHeap(), 0, strlen(lpOutput)+1);
550             strcpy( pPrintJob->pszOutput, lpOutput );
551             if(lpTitle)
552             {
553                 pPrintJob->pszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(lpTitle)+1);
554                 strcpy( pPrintJob->pszTitle, lpTitle );
555             }
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 0
608         /* FIXME: We just cannot call 16 bit functions from here, since we
609          * have acquired several locks (DC). And we do not really need to.
610          */
611         if (pPrintJob->hDC == 0) {
612             TRACE("hDC == 0 so no QueryAbort\n");
613         }
614         else if (!(QueryAbort16(pPrintJob->hDC, (nRet == SP_OUTOFDISK) ? nRet : 0 )))
615         {
616             CloseJob16(hJob); /* printing aborted */
617             nRet = SP_APPABORT;
618         }
619 #endif
620     }
621     return nRet;
622 }
623
624 typedef INT (WINAPI *MSGBOX_PROC)( HWND, LPCSTR, LPCSTR, UINT );
625
626 /**********************************************************************
627  *           WriteDialog   (GDI.242)
628  *
629  */
630 INT16 WINAPI WriteDialog16(HPJOB16 hJob, LPSTR lpMsg, INT16 cchMsg)
631 {
632     HMODULE mod;
633     MSGBOX_PROC pMessageBoxA;
634     INT16 ret = 0;
635
636     TRACE("%04x %04x '%s'\n", hJob,  cchMsg, lpMsg);
637
638     if ((mod = GetModuleHandleA("user32.dll")))
639     {
640         if ((pMessageBoxA = (MSGBOX_PROC)GetProcAddress( mod, "MessageBoxA" )))
641             ret = pMessageBoxA(0, lpMsg, "Printing Error", MB_OKCANCEL);
642     }
643     return ret;
644 }
645
646
647 /**********************************************************************
648  *           DeleteJob  (GDI.244)
649  *
650  */
651 INT16 WINAPI DeleteJob16(HPJOB16 hJob, INT16 nNotUsed)
652 {
653     int nRet;
654
655     TRACE("%04x\n", hJob);
656
657     nRet = FreePrintJob(hJob);
658     return nRet;
659 }
660
661 /*
662  * The following two function would allow a page to be sent to the printer
663  * when it has been processed.  For simplicity they havn't been implemented.
664  * This means a whole job has to be processed before it is sent to the printer.
665  */
666
667 /**********************************************************************
668  *           StartSpoolPage   (GDI.246)
669  *
670  */
671 INT16 WINAPI StartSpoolPage16(HPJOB16 hJob)
672 {
673     FIXME("StartSpoolPage GDI.246 unimplemented\n");
674     return 1;
675
676 }
677
678
679 /**********************************************************************
680  *           EndSpoolPage   (GDI.247)
681  *
682  */
683 INT16 WINAPI EndSpoolPage16(HPJOB16 hJob)
684 {
685     FIXME("EndSpoolPage GDI.247 unimplemented\n");
686     return 1;
687 }
688
689
690 /**********************************************************************
691  *           GetSpoolJob   (GDI.245)
692  *
693  */
694 DWORD WINAPI GetSpoolJob16(int nOption, LONG param)
695 {
696     DWORD retval = 0;
697     TRACE("In GetSpoolJob param 0x%lx noption %d\n",param, nOption);
698     return retval;
699 }
700
701
702 /******************************************************************
703  *                  DrvGetPrinterDataInternal
704  *
705  * Helper for DrvGetPrinterData
706  */
707 static DWORD DrvGetPrinterDataInternal(LPSTR RegStr_Printer,
708 LPBYTE lpPrinterData, int cbData, int what)
709 {
710     DWORD res = -1;
711     HKEY hkey;
712     DWORD dwType, cbQueryData;
713
714     if (!(RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey))) {
715         if (what == INT_PD_DEFAULT_DEVMODE) { /* "Default DevMode" */
716             if (!(RegQueryValueExA(hkey, DefaultDevMode, 0, &dwType, 0, &cbQueryData))) {
717                 if (!lpPrinterData)
718                     res = cbQueryData;
719                 else if ((cbQueryData) && (cbQueryData <= cbData)) {
720                     cbQueryData = cbData;
721                     if (RegQueryValueExA(hkey, DefaultDevMode, 0,
722                                 &dwType, lpPrinterData, &cbQueryData))
723                         res = cbQueryData;
724                 }
725             }
726         } else { /* "Printer Driver" */
727             cbQueryData = 32;
728             RegQueryValueExA(hkey, "Printer Driver", 0,
729                         &dwType, lpPrinterData, &cbQueryData);
730             res = cbQueryData;
731         }
732     }
733     if (hkey) RegCloseKey(hkey);
734     return res;
735 }
736
737 /******************************************************************
738  *                DrvGetPrinterData     (GDI.282)
739  *
740  */
741 DWORD WINAPI DrvGetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
742                                LPDWORD lpType, LPBYTE lpPrinterData,
743                                int cbData, LPDWORD lpNeeded)
744 {
745     LPSTR RegStr_Printer;
746     HKEY hkey = 0, hkey2 = 0;
747     DWORD res = 0;
748     DWORD dwType, PrinterAttr, cbPrinterAttr, SetData, size;
749
750     if (HIWORD(lpPrinter))
751             TRACE("printer %s\n",lpPrinter);
752     else
753             TRACE("printer %p\n",lpPrinter);
754     if (HIWORD(lpProfile))
755             TRACE("profile %s\n",lpProfile);
756     else
757             TRACE("profile %p\n",lpProfile);
758     TRACE("lpType %p\n",lpType);
759
760     if ((!lpPrinter) || (!lpProfile) || (!lpNeeded))
761         return ERROR_INVALID_PARAMETER;
762
763     RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
764                                strlen(Printers) + strlen(lpPrinter) + 2);
765     strcpy(RegStr_Printer, Printers);
766     strcat(RegStr_Printer, lpPrinter);
767
768     if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
769     (!strcmp(lpProfile, DefaultDevMode)))) {
770         size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
771                                          INT_PD_DEFAULT_DEVMODE);
772         if (size+1) {
773             *lpNeeded = size;
774             if ((lpPrinterData) && (*lpNeeded > cbData))
775                 res = ERROR_MORE_DATA;
776         }
777         else res = ERROR_INVALID_PRINTER_NAME;
778     }
779     else
780     if (((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
781     (!strcmp(lpProfile, PrinterModel)))) {
782         *lpNeeded = 32;
783         if (!lpPrinterData) goto failed;
784         if (cbData < 32) {
785             res = ERROR_MORE_DATA;
786             goto failed;
787         }
788         size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
789                                          INT_PD_DEFAULT_MODEL);
790         if ((size+1) && (lpType))
791             *lpType = REG_SZ;
792         else
793             res = ERROR_INVALID_PRINTER_NAME;
794     }
795     else
796     {
797         if ((res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)))
798             goto failed;
799         cbPrinterAttr = 4;
800         if ((res = RegQueryValueExA(hkey, "Attributes", 0,
801                         &dwType, (LPBYTE)&PrinterAttr, &cbPrinterAttr)))
802             goto failed;
803         if ((res = RegOpenKeyA(hkey, PrinterDriverData, &hkey2)))
804             goto failed;
805         *lpNeeded = cbData;
806         res = RegQueryValueExA(hkey2, lpProfile, 0,
807                 lpType, lpPrinterData, lpNeeded);
808         if ((res != ERROR_CANTREAD) &&
809          ((PrinterAttr &
810         (PRINTER_ATTRIBUTE_ENABLE_BIDI|PRINTER_ATTRIBUTE_NETWORK))
811         == PRINTER_ATTRIBUTE_NETWORK))
812         {
813             if (!(res) && (*lpType == REG_DWORD) && (*(LPDWORD)lpPrinterData == -1))
814                 res = ERROR_INVALID_DATA;
815         }
816         else
817         {
818             SetData = -1;
819             RegSetValueExA(hkey2, lpProfile, 0, REG_DWORD, (LPBYTE)&SetData, 4); /* no result returned */
820         }
821     }
822
823 failed:
824     if (hkey2) RegCloseKey(hkey2);
825     if (hkey) RegCloseKey(hkey);
826     HeapFree(GetProcessHeap(), 0, RegStr_Printer);
827     return res;
828 }
829
830
831 /******************************************************************
832  *                 DrvSetPrinterData     (GDI.281)
833  *
834  */
835 DWORD WINAPI DrvSetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
836                                DWORD lpType, LPBYTE lpPrinterData,
837                                DWORD dwSize)
838 {
839     LPSTR RegStr_Printer;
840     HKEY hkey = 0;
841     DWORD res = 0;
842
843     if (HIWORD(lpPrinter))
844             TRACE("printer %s\n",lpPrinter);
845     else
846             TRACE("printer %p\n",lpPrinter);
847     if (HIWORD(lpProfile))
848             TRACE("profile %s\n",lpProfile);
849     else
850             TRACE("profile %p\n",lpProfile);
851     TRACE("lpType %08lx\n",lpType);
852
853     if ((!lpPrinter) || (!lpProfile) ||
854     ((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
855     (!strcmp(lpProfile, PrinterModel))))
856         return ERROR_INVALID_PARAMETER;
857
858     RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
859                         strlen(Printers) + strlen(lpPrinter) + 2);
860     strcpy(RegStr_Printer, Printers);
861     strcat(RegStr_Printer, lpPrinter);
862
863     if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
864     (!strcmp(lpProfile, DefaultDevMode)))) {
865         if ( RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)
866              != ERROR_SUCCESS ||
867              RegSetValueExA(hkey, DefaultDevMode, 0, REG_BINARY,
868                               lpPrinterData, dwSize) != ERROR_SUCCESS )
869                 res = ERROR_INVALID_PRINTER_NAME;
870     }
871     else
872     {
873         strcat(RegStr_Printer, "\\");
874
875         if( (res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)) ==
876             ERROR_SUCCESS ) {
877
878             if (!lpPrinterData)
879                 res = RegDeleteValueA(hkey, lpProfile);
880             else
881                 res = RegSetValueExA(hkey, lpProfile, 0, lpType,
882                                        lpPrinterData, dwSize);
883         }
884     }
885
886     if (hkey) RegCloseKey(hkey);
887     HeapFree(GetProcessHeap(), 0, RegStr_Printer);
888     return res;
889 }