Fix PRINTER_DEFAULTSA and PRINTER_DEFAULTSW structures
[reactos.git] / reactos / dll / win32 / ntprint / ntprint.c
1 /*
2 * Implementation of the Spooler Setup API (Printing)
3 *
4 * Copyright 2007 Detlef Riekenberg
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #define WIN32_NO_STATUS
22
23 #include <stdarg.h>
24
25 #define COBJMACROS
26 #define NONAMELESSUNION
27
28 #include <windef.h>
29 #include <winbase.h>
30 //#include "winerror.h"
31 #include <wingdi.h>
32 //#include "winnls.h"
33 //#include "winver.h"
34 #include <winspool.h>
35
36 //#include "wine/unicode.h"
37 #include <wine/debug.h>
38
39 WINE_DEFAULT_DEBUG_CHANNEL(ntprint);
40
41 typedef struct {
42 LPMONITOR_INFO_2W mi2; /* Buffer for installed Monitors */
43 DWORD installed; /* Number of installed Monitors */
44 } monitorinfo_t;
45
46 /*****************************************************
47 * DllMain
48 */
49 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
50 {
51 TRACE("(%p, %d, %p)\n",hinstDLL, fdwReason, lpvReserved);
52
53 switch(fdwReason)
54 {
55 case DLL_WINE_PREATTACH:
56 return FALSE; /* prefer native version */
57
58 case DLL_PROCESS_ATTACH:
59 DisableThreadLibraryCalls( hinstDLL );
60 break;
61 }
62 return TRUE;
63 }
64
65 /*****************************************************
66 * PSetupCreateMonitorInfo [NTPRINT.@]
67 *
68 *
69 */
70
71 HANDLE WINAPI PSetupCreateMonitorInfo(LPVOID unknown1, LPVOID unknown2,LPVOID unknown3)
72 {
73 monitorinfo_t * mi=NULL;
74 DWORD needed;
75 DWORD res;
76
77 TRACE("(%p, %p, %p)\n", unknown1, unknown2, unknown3);
78
79 if ((unknown2 != NULL) || (unknown3 != NULL)) {
80 FIXME("got unknown parameter: (%p, %p, %p)\n", unknown1, unknown2, unknown3);
81 return NULL;
82 }
83
84 mi = HeapAlloc(GetProcessHeap(), 0, sizeof(monitorinfo_t));
85 if (!mi) {
86 /* FIXME: SetLastError() needed? */
87 return NULL;
88 }
89
90 /* Get the needed size for all Monitors */
91 res = EnumMonitorsW(NULL, 2, NULL, 0, &needed, &mi->installed);
92 if (!res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER)) {
93 mi->mi2 = HeapAlloc(GetProcessHeap(), 0, needed);
94 res = EnumMonitorsW(NULL, 2, (LPBYTE) mi->mi2, needed, &needed, &mi->installed);
95 }
96
97 if (!res) {
98 HeapFree(GetProcessHeap(), 0, mi);
99 /* FIXME: SetLastError() needed? */
100 return NULL;
101 }
102
103 TRACE("=> %p (%u monitors installed)\n", mi, mi->installed);
104 return mi;
105 }
106
107 /*****************************************************
108 * PSetupDestroyMonitorInfo [NTPRINT.@]
109 *
110 */
111
112 VOID WINAPI PSetupDestroyMonitorInfo(HANDLE monitorinfo)
113 {
114 monitorinfo_t * mi = monitorinfo;
115
116 TRACE("(%p)\n", mi);
117 if (mi) {
118 if (mi->installed) HeapFree(GetProcessHeap(), 0, mi->mi2);
119 HeapFree(GetProcessHeap(), 0, mi);
120 }
121 }
122
123 /*****************************************************
124 * PSetupEnumMonitor [NTPRINT.@]
125 *
126 * Copy the selected Monitorname to a buffer
127 *
128 * PARAMS
129 * monitorinfo [I] HANDLE from PSetupCreateMonitorInfo
130 * index [I] Nr. of the Monitorname to copy
131 * buffer [I] Target, that receive the Monitorname
132 * psize [IO] PTR to a DWORD that hold the size of the buffer and receive
133 * the needed size, when the buffer is too small
134 *
135 * RETURNS
136 * Success: TRUE
137 * Failure: FALSE
138 *
139 * NOTES
140 * size is in Bytes on w2k and WCHAR on XP
141 *
142 */
143
144 BOOL WINAPI PSetupEnumMonitor(HANDLE monitorinfo, DWORD index, LPWSTR buffer, LPDWORD psize)
145 {
146 monitorinfo_t * mi = monitorinfo;
147 LPWSTR nameW;
148 DWORD len;
149
150 TRACE("(%p, %u, %p, %p) => %d\n", mi, index, buffer, psize, psize ? *psize : 0);
151
152 if (index < mi->installed) {
153 nameW = mi->mi2[index].pName;
154 len = lstrlenW(nameW) + 1;
155 if (len <= *psize) {
156 memcpy(buffer, nameW, len * sizeof(WCHAR));
157 TRACE("#%u: %s\n", index, debugstr_w(buffer));
158 return TRUE;
159 }
160 *psize = len;
161 SetLastError(ERROR_INSUFFICIENT_BUFFER);
162 return FALSE;
163 }
164 SetLastError(ERROR_NO_MORE_ITEMS);
165 return FALSE;
166 }