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