push fa42aed23791b941c2d9b5c5907404552eb71b52
[wine/hacks.git] / dlls / gdi32 / printdrv.c
blob8524388100ddb0c50d6b7ec551a4058f580d6da8
1 /*
2 * Implementation of some printer driver bits
4 * Copyright 1996 John Harvey
5 * Copyright 1998 Huw Davies
6 * Copyright 1998 Andreas Mohr
7 * Copyright 1999 Klaas van Gend
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.
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.
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
24 #include "config.h"
25 #include "wine/port.h"
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"
53 WINE_DEFAULT_DEBUG_CHANNEL(print);
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\\";
60 /******************************************************************
61 * GdiGetSpoolMessage [GDI32.@]
64 DWORD WINAPI GdiGetSpoolMessage(LPVOID ptr1, DWORD data2, LPVOID ptr3, DWORD data4)
66 TRACE("(%p 0x%x %p 0x%x) stub\n", ptr1, data2, ptr3, data4);
67 /* avoid 100% cpu usage with spoolsv.exe from w2k
68 (spoolsv.exe from xp does Sleep 1000/1500/2000 in a loop) */
69 Sleep(500);
70 return 0;
73 /******************************************************************
74 * GdiInitSpool [GDI32.@]
77 DWORD WINAPI GdiInitSpool(void)
79 FIXME("stub\n");
80 return TRUE;
83 /******************************************************************
84 * StartDocA [GDI32.@]
86 * StartDoc calls the STARTDOC Escape with the input data pointing to DocName
87 * and the output data (which is used as a second input parameter).pointing at
88 * the whole docinfo structure. This seems to be an undocumented feature of
89 * the STARTDOC Escape.
91 * Note: we now do it the other way, with the STARTDOC Escape calling StartDoc.
93 INT WINAPI StartDocW(HDC hdc, const DOCINFOW* doc)
95 INT ret = 0;
96 DC *dc = get_dc_ptr( hdc );
98 TRACE("DocName = %s Output = %s Datatype = %s\n",
99 debugstr_w(doc->lpszDocName), debugstr_w(doc->lpszOutput),
100 debugstr_w(doc->lpszDatatype));
102 if(!dc) return SP_ERROR;
104 if (dc->funcs->pStartDoc) ret = dc->funcs->pStartDoc( dc->physDev, doc );
105 release_dc_ptr( dc );
106 return ret;
109 /*************************************************************************
110 * StartDocA [GDI32.@]
113 INT WINAPI StartDocA(HDC hdc, const DOCINFOA* doc)
115 LPWSTR szDocName = NULL, szOutput = NULL, szDatatype = NULL;
116 DOCINFOW docW;
117 INT ret, len;
119 docW.cbSize = doc->cbSize;
120 if (doc->lpszDocName)
122 len = MultiByteToWideChar(CP_ACP,0,doc->lpszDocName,-1,NULL,0);
123 szDocName = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
124 MultiByteToWideChar(CP_ACP,0,doc->lpszDocName,-1,szDocName,len);
126 if (doc->lpszOutput)
128 len = MultiByteToWideChar(CP_ACP,0,doc->lpszOutput,-1,NULL,0);
129 szOutput = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
130 MultiByteToWideChar(CP_ACP,0,doc->lpszOutput,-1,szOutput,len);
132 if (doc->lpszDatatype)
134 len = MultiByteToWideChar(CP_ACP,0,doc->lpszDatatype,-1,NULL,0);
135 szDatatype = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
136 MultiByteToWideChar(CP_ACP,0,doc->lpszDatatype,-1,szDatatype,len);
139 docW.lpszDocName = szDocName;
140 docW.lpszOutput = szOutput;
141 docW.lpszDatatype = szDatatype;
142 docW.fwType = doc->fwType;
144 ret = StartDocW(hdc, &docW);
146 HeapFree( GetProcessHeap(), 0, szDocName );
147 HeapFree( GetProcessHeap(), 0, szOutput );
148 HeapFree( GetProcessHeap(), 0, szDatatype );
150 return ret;
154 /******************************************************************
155 * EndDoc [GDI32.@]
158 INT WINAPI EndDoc(HDC hdc)
160 INT ret = 0;
161 DC *dc = get_dc_ptr( hdc );
162 if(!dc) return SP_ERROR;
164 if (dc->funcs->pEndDoc) ret = dc->funcs->pEndDoc( dc->physDev );
165 release_dc_ptr( dc );
166 return ret;
170 /******************************************************************
171 * StartPage [GDI32.@]
174 INT WINAPI StartPage(HDC hdc)
176 INT ret = 1;
177 DC *dc = get_dc_ptr( hdc );
178 if(!dc) return SP_ERROR;
180 if(dc->funcs->pStartPage)
181 ret = dc->funcs->pStartPage( dc->physDev );
182 else
183 FIXME("stub\n");
184 release_dc_ptr( dc );
185 return ret;
189 /******************************************************************
190 * EndPage [GDI32.@]
193 INT WINAPI EndPage(HDC hdc)
195 INT ret = 0;
196 DC *dc = get_dc_ptr( hdc );
197 if(!dc) return SP_ERROR;
199 if (dc->funcs->pEndPage) ret = dc->funcs->pEndPage( dc->physDev );
200 if (dc->pAbortProc && !dc->pAbortProc( hdc, 0 ))
202 EndDoc( hdc );
203 ret = 0;
205 release_dc_ptr( dc );
206 return ret;
210 /******************************************************************************
211 * AbortDoc [GDI32.@]
213 INT WINAPI AbortDoc(HDC hdc)
215 INT ret = 0;
216 DC *dc = get_dc_ptr( hdc );
217 if(!dc) return SP_ERROR;
219 if (dc->funcs->pAbortDoc) ret = dc->funcs->pAbortDoc( dc->physDev );
220 release_dc_ptr( dc );
221 return ret;
224 /**********************************************************************
225 * QueryAbort (GDI.155)
227 * Calls the app's AbortProc function if avail.
229 * RETURNS
230 * TRUE if no AbortProc avail or AbortProc wants to continue printing.
231 * FALSE if AbortProc wants to abort printing.
233 BOOL16 WINAPI QueryAbort16(HDC16 hdc16, INT16 reserved)
235 BOOL ret = TRUE;
236 HDC hdc = HDC_32( hdc16 );
237 DC *dc = get_dc_ptr( hdc );
239 if(!dc) {
240 ERR("Invalid hdc %p\n", hdc);
241 return FALSE;
243 if (dc->pAbortProc) ret = dc->pAbortProc(hdc, 0);
244 release_dc_ptr( dc );
245 return ret;
249 /**********************************************************************
250 * call_abort_proc16
252 static BOOL CALLBACK call_abort_proc16( HDC hdc, INT code )
254 ABORTPROC16 proc16;
255 DC *dc = get_dc_ptr( hdc );
257 if (!dc) return FALSE;
258 proc16 = dc->pAbortProc16;
259 release_dc_ptr( dc );
260 if (proc16)
262 WORD args[2];
263 DWORD ret;
265 args[1] = HDC_16(hdc);
266 args[0] = code;
267 WOWCallback16Ex( (DWORD)proc16, WCB16_PASCAL, sizeof(args), args, &ret );
268 return LOWORD(ret);
270 return TRUE;
274 /**********************************************************************
275 * SetAbortProc (GDI.381)
277 INT16 WINAPI SetAbortProc16(HDC16 hdc16, ABORTPROC16 abrtprc)
279 HDC hdc = HDC_32( hdc16 );
280 DC *dc = get_dc_ptr( hdc );
282 if (!dc) return FALSE;
283 dc->pAbortProc16 = abrtprc;
284 dc->pAbortProc = call_abort_proc16;
285 release_dc_ptr( dc );
286 return TRUE;
289 /**********************************************************************
290 * SetAbortProc (GDI32.@)
293 INT WINAPI SetAbortProc(HDC hdc, ABORTPROC abrtprc)
295 DC *dc = get_dc_ptr( hdc );
297 if (!dc) return FALSE;
298 dc->pAbortProc = abrtprc;
299 release_dc_ptr( dc );
300 return TRUE;
304 /****************** misc. printer related functions */
307 * The following function should implement a queing system
309 struct hpq
311 struct hpq *next;
312 int tag;
313 int key;
316 static struct hpq *hpqueue;
318 /**********************************************************************
319 * CreatePQ (GDI.230)
322 HPQ16 WINAPI CreatePQ16(INT16 size)
324 #if 0
325 HGLOBAL16 hpq = 0;
326 WORD tmp_size;
327 LPWORD pPQ;
329 tmp_size = size << 2;
330 if (!(hpq = GlobalAlloc16(GMEM_SHARE|GMEM_MOVEABLE, tmp_size + 8)))
331 return 0xffff;
332 pPQ = GlobalLock16(hpq);
333 *pPQ++ = 0;
334 *pPQ++ = tmp_size;
335 *pPQ++ = 0;
336 *pPQ++ = 0;
337 GlobalUnlock16(hpq);
339 return (HPQ16)hpq;
340 #else
341 FIXME("(%d): stub\n",size);
342 return 1;
343 #endif
346 /**********************************************************************
347 * DeletePQ (GDI.235)
350 INT16 WINAPI DeletePQ16(HPQ16 hPQ)
352 return GlobalFree16(hPQ);
355 /**********************************************************************
356 * ExtractPQ (GDI.232)
359 INT16 WINAPI ExtractPQ16(HPQ16 hPQ)
361 struct hpq *queue, *prev, *current, *currentPrev;
362 int key = 0, tag = -1;
363 currentPrev = prev = NULL;
364 queue = current = hpqueue;
365 if (current)
366 key = current->key;
368 while (current)
370 currentPrev = current;
371 current = current->next;
372 if (current)
374 if (current->key < key)
376 queue = current;
377 prev = currentPrev;
381 if (queue)
383 tag = queue->tag;
385 if (prev)
386 prev->next = queue->next;
387 else
388 hpqueue = queue->next;
389 HeapFree(GetProcessHeap(), 0, queue);
392 TRACE("%x got tag %d key %d\n", hPQ, tag, key);
394 return tag;
397 /**********************************************************************
398 * InsertPQ (GDI.233)
401 INT16 WINAPI InsertPQ16(HPQ16 hPQ, INT16 tag, INT16 key)
403 struct hpq *queueItem = HeapAlloc(GetProcessHeap(), 0, sizeof(struct hpq));
404 if(queueItem == NULL) {
405 ERR("Memory exausted!\n");
406 return FALSE;
408 queueItem->next = hpqueue;
409 hpqueue = queueItem;
410 queueItem->key = key;
411 queueItem->tag = tag;
413 FIXME("(%x %d %d): stub???\n", hPQ, tag, key);
414 return TRUE;
417 /**********************************************************************
418 * MinPQ (GDI.231)
421 INT16 WINAPI MinPQ16(HPQ16 hPQ)
423 FIXME("(%x): stub\n", hPQ);
424 return 0;
427 /**********************************************************************
428 * SizePQ (GDI.234)
431 INT16 WINAPI SizePQ16(HPQ16 hPQ, INT16 sizechange)
433 FIXME("(%x %d): stub\n", hPQ, sizechange);
434 return -1;
440 * The following functions implement part of the spooling process to
441 * print manager. I would like to see wine have a version of print managers
442 * that used LPR/LPD. For simplicity print jobs will be sent to a file for
443 * now.
445 typedef struct PRINTJOB
447 char *pszOutput;
448 char *pszTitle;
449 HDC16 hDC;
450 HANDLE16 hHandle;
451 int nIndex;
452 int fd;
453 } PRINTJOB, *PPRINTJOB;
455 #define MAX_PRINT_JOBS 1
456 #define SP_OK 1
458 static PPRINTJOB gPrintJobsTable[MAX_PRINT_JOBS];
461 static PPRINTJOB FindPrintJobFromHandle(HANDLE16 hHandle)
463 return gPrintJobsTable[0];
466 static int CreateSpoolFile(LPCSTR pszOutput)
468 int fd=-1;
469 char psCmd[1024];
470 const char *psCmdP = psCmd;
471 HKEY hkey;
473 /* TTD convert the 'output device' into a spool file name */
475 if (pszOutput == NULL || *pszOutput == '\0')
476 return -1;
478 psCmd[0] = 0;
479 /* @@ Wine registry key: HKCU\Software\Wine\Printing\Spooler */
480 if(!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Printing\\Spooler", &hkey))
482 DWORD type, count = sizeof(psCmd);
483 RegQueryValueExA(hkey, pszOutput, 0, &type, (LPBYTE)psCmd, &count);
484 RegCloseKey(hkey);
486 if (!psCmd[0] && !strncmp("LPR:",pszOutput,4))
487 sprintf(psCmd,"|lpr -P'%s'",pszOutput+4);
489 TRACE("Got printerSpoolCommand '%s' for output device '%s'\n",
490 psCmd, pszOutput);
491 if (!*psCmd)
492 psCmdP = pszOutput;
493 else
495 while (*psCmdP && isspace(*psCmdP))
497 psCmdP++;
499 if (!*psCmdP)
500 return -1;
502 TRACE("command: '%s'\n", psCmdP);
503 #ifdef HAVE_FORK
504 if (*psCmdP == '|')
506 int fds[2];
507 if (pipe(fds)) {
508 ERR("pipe() failed!\n");
509 return -1;
511 if (fork() == 0)
513 psCmdP++;
515 TRACE("In child need to exec %s\n",psCmdP);
516 close(0);
517 dup2(fds[0],0);
518 close (fds[1]);
520 /* reset signals that we previously set to SIG_IGN */
521 signal( SIGPIPE, SIG_DFL );
522 signal( SIGCHLD, SIG_DFL );
524 execl("/bin/sh", "/bin/sh", "-c", psCmdP, (char*)0);
525 _exit(1);
528 close (fds[0]);
529 fd = fds[1];
530 TRACE("Need to execute a cmnd and pipe the output to it\n");
532 else
533 #endif
535 char *buffer;
536 WCHAR psCmdPW[MAX_PATH];
538 TRACE("Just assume it's a file\n");
541 * The file name can be dos based, we have to find its
542 * Unix correspondant file name
544 MultiByteToWideChar(CP_ACP, 0, psCmdP, -1, psCmdPW, MAX_PATH);
545 if ((buffer = wine_get_unix_file_name(psCmdPW)))
547 if ((fd = open(buffer, O_CREAT | O_TRUNC | O_WRONLY, 0666)) < 0)
549 ERR("Failed to create spool file '%s' ('%s'). (error %s)\n",
550 buffer, psCmdP, strerror(errno));
552 HeapFree(GetProcessHeap(), 0, buffer);
555 return fd;
558 static int FreePrintJob(HANDLE16 hJob)
560 int nRet = SP_ERROR;
561 PPRINTJOB pPrintJob;
563 pPrintJob = FindPrintJobFromHandle(hJob);
564 if (pPrintJob != NULL)
566 gPrintJobsTable[pPrintJob->nIndex] = NULL;
567 HeapFree(GetProcessHeap(), 0, pPrintJob->pszOutput);
568 HeapFree(GetProcessHeap(), 0, pPrintJob->pszTitle);
569 if (pPrintJob->fd >= 0) close(pPrintJob->fd);
570 HeapFree(GetProcessHeap(), 0, pPrintJob);
571 nRet = SP_OK;
573 return nRet;
576 /**********************************************************************
577 * OpenJob (GDI.240)
580 HPJOB16 WINAPI OpenJob16(LPCSTR lpOutput, LPCSTR lpTitle, HDC16 hDC)
582 HPJOB16 hHandle = (HPJOB16)SP_ERROR;
583 PPRINTJOB pPrintJob;
585 TRACE("'%s' '%s' %04x\n", lpOutput, lpTitle, hDC);
587 pPrintJob = gPrintJobsTable[0];
588 if (pPrintJob == NULL)
590 int fd;
592 /* Try and create a spool file */
593 fd = CreateSpoolFile(lpOutput);
594 if (fd >= 0)
596 pPrintJob = HeapAlloc(GetProcessHeap(), 0, sizeof(PRINTJOB));
597 if(pPrintJob == NULL) {
598 WARN("Memory exausted!\n");
599 return hHandle;
602 hHandle = 1;
604 pPrintJob->pszOutput = HeapAlloc(GetProcessHeap(), 0, strlen(lpOutput)+1);
605 strcpy( pPrintJob->pszOutput, lpOutput );
606 if(lpTitle)
608 pPrintJob->pszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(lpTitle)+1);
609 strcpy( pPrintJob->pszTitle, lpTitle );
611 pPrintJob->hDC = hDC;
612 pPrintJob->fd = fd;
613 pPrintJob->nIndex = 0;
614 pPrintJob->hHandle = hHandle;
615 gPrintJobsTable[pPrintJob->nIndex] = pPrintJob;
618 TRACE("return %04x\n", hHandle);
619 return hHandle;
622 /**********************************************************************
623 * CloseJob (GDI.243)
626 INT16 WINAPI CloseJob16(HPJOB16 hJob)
628 int nRet = SP_ERROR;
629 PPRINTJOB pPrintJob = NULL;
631 TRACE("%04x\n", hJob);
633 pPrintJob = FindPrintJobFromHandle(hJob);
634 if (pPrintJob != NULL)
636 /* Close the spool file */
637 close(pPrintJob->fd);
638 FreePrintJob(hJob);
639 nRet = 1;
641 return nRet;
644 /**********************************************************************
645 * WriteSpool (GDI.241)
648 INT16 WINAPI WriteSpool16(HPJOB16 hJob, LPSTR lpData, INT16 cch)
650 int nRet = SP_ERROR;
651 PPRINTJOB pPrintJob = NULL;
653 TRACE("%04x %p %04x\n", hJob, lpData, cch);
655 pPrintJob = FindPrintJobFromHandle(hJob);
656 if (pPrintJob != NULL && pPrintJob->fd >= 0 && cch)
658 if (write(pPrintJob->fd, lpData, cch) != cch)
659 nRet = SP_OUTOFDISK;
660 else
661 nRet = cch;
662 #if 0
663 /* FIXME: We just cannot call 16 bit functions from here, since we
664 * have acquired several locks (DC). And we do not really need to.
666 if (pPrintJob->hDC == 0) {
667 TRACE("hDC == 0 so no QueryAbort\n");
669 else if (!(QueryAbort16(pPrintJob->hDC, (nRet == SP_OUTOFDISK) ? nRet : 0 )))
671 CloseJob16(hJob); /* printing aborted */
672 nRet = SP_APPABORT;
674 #endif
676 return nRet;
679 typedef INT (WINAPI *MSGBOX_PROC)( HWND, LPCSTR, LPCSTR, UINT );
681 /**********************************************************************
682 * WriteDialog (GDI.242)
685 INT16 WINAPI WriteDialog16(HPJOB16 hJob, LPSTR lpMsg, INT16 cchMsg)
687 HMODULE mod;
688 MSGBOX_PROC pMessageBoxA;
689 INT16 ret = 0;
691 TRACE("%04x %04x '%s'\n", hJob, cchMsg, lpMsg);
693 if ((mod = GetModuleHandleA("user32.dll")))
695 if ((pMessageBoxA = (MSGBOX_PROC)GetProcAddress( mod, "MessageBoxA" )))
696 ret = pMessageBoxA(0, lpMsg, "Printing Error", MB_OKCANCEL);
698 return ret;
702 /**********************************************************************
703 * DeleteJob (GDI.244)
706 INT16 WINAPI DeleteJob16(HPJOB16 hJob, INT16 nNotUsed)
708 int nRet;
710 TRACE("%04x\n", hJob);
712 nRet = FreePrintJob(hJob);
713 return nRet;
717 * The following two function would allow a page to be sent to the printer
718 * when it has been processed. For simplicity they haven't been implemented.
719 * This means a whole job has to be processed before it is sent to the printer.
722 /**********************************************************************
723 * StartSpoolPage (GDI.246)
726 INT16 WINAPI StartSpoolPage16(HPJOB16 hJob)
728 FIXME("StartSpoolPage GDI.246 unimplemented\n");
729 return 1;
734 /**********************************************************************
735 * EndSpoolPage (GDI.247)
738 INT16 WINAPI EndSpoolPage16(HPJOB16 hJob)
740 FIXME("EndSpoolPage GDI.247 unimplemented\n");
741 return 1;
745 /**********************************************************************
746 * GetSpoolJob (GDI.245)
749 DWORD WINAPI GetSpoolJob16(int nOption, LONG param)
751 DWORD retval = 0;
752 TRACE("In GetSpoolJob param 0x%x noption %d\n",param, nOption);
753 return retval;
757 /******************************************************************
758 * DrvGetPrinterDataInternal
760 * Helper for DrvGetPrinterData
762 static DWORD DrvGetPrinterDataInternal(LPSTR RegStr_Printer,
763 LPBYTE lpPrinterData, int cbData, int what)
765 DWORD res = -1;
766 HKEY hkey;
767 DWORD dwType, cbQueryData;
769 if (!(RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey))) {
770 if (what == INT_PD_DEFAULT_DEVMODE) { /* "Default DevMode" */
771 if (!(RegQueryValueExA(hkey, DefaultDevMode, 0, &dwType, 0, &cbQueryData))) {
772 if (!lpPrinterData)
773 res = cbQueryData;
774 else if ((cbQueryData) && (cbQueryData <= cbData)) {
775 cbQueryData = cbData;
776 if (RegQueryValueExA(hkey, DefaultDevMode, 0,
777 &dwType, lpPrinterData, &cbQueryData))
778 res = cbQueryData;
781 } else { /* "Printer Driver" */
782 cbQueryData = 32;
783 RegQueryValueExA(hkey, "Printer Driver", 0,
784 &dwType, lpPrinterData, &cbQueryData);
785 res = cbQueryData;
788 if (hkey) RegCloseKey(hkey);
789 return res;
792 /******************************************************************
793 * DrvGetPrinterData (GDI.282)
796 DWORD WINAPI DrvGetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
797 LPDWORD lpType, LPBYTE lpPrinterData,
798 int cbData, LPDWORD lpNeeded)
800 LPSTR RegStr_Printer;
801 HKEY hkey = 0, hkey2 = 0;
802 DWORD res = 0;
803 DWORD dwType, PrinterAttr, cbPrinterAttr, SetData, size;
805 if (HIWORD(lpPrinter))
806 TRACE("printer %s\n",lpPrinter);
807 else
808 TRACE("printer %p\n",lpPrinter);
809 if (HIWORD(lpProfile))
810 TRACE("profile %s\n",lpProfile);
811 else
812 TRACE("profile %p\n",lpProfile);
813 TRACE("lpType %p\n",lpType);
815 if ((!lpPrinter) || (!lpProfile) || (!lpNeeded))
816 return ERROR_INVALID_PARAMETER;
818 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
819 strlen(Printers) + strlen(lpPrinter) + 2);
820 strcpy(RegStr_Printer, Printers);
821 strcat(RegStr_Printer, lpPrinter);
823 if ((PtrToUlong(lpProfile) == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
824 (!strcmp(lpProfile, DefaultDevMode)))) {
825 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
826 INT_PD_DEFAULT_DEVMODE);
827 if (size+1) {
828 *lpNeeded = size;
829 if ((lpPrinterData) && (*lpNeeded > cbData))
830 res = ERROR_MORE_DATA;
832 else res = ERROR_INVALID_PRINTER_NAME;
834 else
835 if ((PtrToUlong(lpProfile) == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
836 (!strcmp(lpProfile, PrinterModel)))) {
837 *lpNeeded = 32;
838 if (!lpPrinterData) goto failed;
839 if (cbData < 32) {
840 res = ERROR_MORE_DATA;
841 goto failed;
843 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
844 INT_PD_DEFAULT_MODEL);
845 if ((size+1) && (lpType))
846 *lpType = REG_SZ;
847 else
848 res = ERROR_INVALID_PRINTER_NAME;
850 else
852 if ((res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)))
853 goto failed;
854 cbPrinterAttr = 4;
855 if ((res = RegQueryValueExA(hkey, "Attributes", 0,
856 &dwType, (LPBYTE)&PrinterAttr, &cbPrinterAttr)))
857 goto failed;
858 if ((res = RegOpenKeyA(hkey, PrinterDriverData, &hkey2)))
859 goto failed;
860 *lpNeeded = cbData;
861 res = RegQueryValueExA(hkey2, lpProfile, 0,
862 lpType, lpPrinterData, lpNeeded);
863 if ((res != ERROR_CANTREAD) &&
864 ((PrinterAttr &
865 (PRINTER_ATTRIBUTE_ENABLE_BIDI|PRINTER_ATTRIBUTE_NETWORK))
866 == PRINTER_ATTRIBUTE_NETWORK))
868 if (!(res) && (*lpType == REG_DWORD) && (*(LPDWORD)lpPrinterData == -1))
869 res = ERROR_INVALID_DATA;
871 else
873 SetData = -1;
874 RegSetValueExA(hkey2, lpProfile, 0, REG_DWORD, (LPBYTE)&SetData, 4); /* no result returned */
878 failed:
879 if (hkey2) RegCloseKey(hkey2);
880 if (hkey) RegCloseKey(hkey);
881 HeapFree(GetProcessHeap(), 0, RegStr_Printer);
882 return res;
886 /******************************************************************
887 * DrvSetPrinterData (GDI.281)
890 DWORD WINAPI DrvSetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
891 DWORD lpType, LPBYTE lpPrinterData,
892 DWORD dwSize)
894 LPSTR RegStr_Printer;
895 HKEY hkey = 0;
896 DWORD res = 0;
898 if (HIWORD(lpPrinter))
899 TRACE("printer %s\n",lpPrinter);
900 else
901 TRACE("printer %p\n",lpPrinter);
902 if (HIWORD(lpProfile))
903 TRACE("profile %s\n",lpProfile);
904 else
905 TRACE("profile %p\n",lpProfile);
906 TRACE("lpType %08x\n",lpType);
908 if ((!lpPrinter) || (!lpProfile) ||
909 (PtrToUlong(lpProfile) == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
910 (!strcmp(lpProfile, PrinterModel))))
911 return ERROR_INVALID_PARAMETER;
913 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
914 strlen(Printers) + strlen(lpPrinter) + 2);
915 strcpy(RegStr_Printer, Printers);
916 strcat(RegStr_Printer, lpPrinter);
918 if ((PtrToUlong(lpProfile) == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
919 (!strcmp(lpProfile, DefaultDevMode)))) {
920 if ( RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)
921 != ERROR_SUCCESS ||
922 RegSetValueExA(hkey, DefaultDevMode, 0, REG_BINARY,
923 lpPrinterData, dwSize) != ERROR_SUCCESS )
924 res = ERROR_INVALID_PRINTER_NAME;
926 else
928 strcat(RegStr_Printer, "\\");
930 if( (res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)) ==
931 ERROR_SUCCESS ) {
933 if (!lpPrinterData)
934 res = RegDeleteValueA(hkey, lpProfile);
935 else
936 res = RegSetValueExA(hkey, lpProfile, 0, lpType,
937 lpPrinterData, dwSize);
941 if (hkey) RegCloseKey(hkey);
942 HeapFree(GetProcessHeap(), 0, RegStr_Printer);
943 return res;