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