* Sync up to trunk r55544.
[reactos.git] / 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 #include <stdarg.h>
22
23 #define COBJMACROS
24 #define NONAMELESSUNION
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winerror.h"
29 #include "wingdi.h"
30 #include "winnls.h"
31 #include "winver.h"
32 #include "winspool.h"
33
34 #include "wine/unicode.h"
35 #include "wine/debug.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(ntprint);
38
39 static HINSTANCE NTPRINT_hInstance = NULL;
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 NTPRINT_hInstance = hinstDLL;
60 DisableThreadLibraryCalls( hinstDLL );
61 break;
62 }
63 return TRUE;
64 }
65
66 /*****************************************************
67 * PSetupCreateMonitorInfo [NTPRINT.@]
68 *
69 *
70 */
71
72 HANDLE WINAPI PSetupCreateMonitorInfo(LPVOID unknown1, LPVOID unknown2,LPVOID unknown3)
73 {
74 monitorinfo_t * mi=NULL;
75 DWORD needed;
76 DWORD res;
77
78 TRACE("(%p, %p, %p)\n", unknown1, unknown2, unknown3);
79
80 if ((unknown2 != NULL) || (unknown3 != NULL)) {
81 FIXME("got unknown parameter: (%p, %p, %p)\n", unknown1, unknown2, unknown3);
82 return NULL;
83 }
84
85 mi = HeapAlloc(GetProcessHeap(), 0, sizeof(monitorinfo_t));
86 if (!mi) {
87 /* FIXME: SetLastError() needed? */
88 return NULL;
89 }
90
91 /* Get the needed size for all Monitors */
92 res = EnumMonitorsW(NULL, 2, NULL, 0, &needed, &mi->installed);
93 if (!res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER)) {
94 mi->mi2 = HeapAlloc(GetProcessHeap(), 0, needed);
95 res = EnumMonitorsW(NULL, 2, (LPBYTE) mi->mi2, needed, &needed, &mi->installed);
96 }
97
98 if (!res) {
99 HeapFree(GetProcessHeap(), 0, mi);
100 /* FIXME: SetLastError() needed? */
101 return NULL;
102 }
103
104 TRACE("=> %p (%u monitors installed)\n", mi, mi->installed);
105 return mi;
106 }
107
108 /*****************************************************
109 * PSetupDestroyMonitorInfo [NTPRINT.@]
110 *
111 */
112
113 VOID WINAPI PSetupDestroyMonitorInfo(HANDLE monitorinfo)
114 {
115 monitorinfo_t * mi = monitorinfo;
116
117 TRACE("(%p)\n", mi);
118 if (mi) {
119 if (mi->installed) HeapFree(GetProcessHeap(), 0, mi->mi2);
120 HeapFree(GetProcessHeap(), 0, mi);
121 }
122 }
123
124 /*****************************************************
125 * PSetupEnumMonitor [NTPRINT.@]
126 *
127 * Copy the selected Monitorname to a buffer
128 *
129 * PARAMS
130 * monitorinfo [I] HANDLE from PSetupCreateMonitorInfo
131 * index [I] Nr. of the Monitorname to copy
132 * buffer [I] Target, that receive the Monitorname
133 * psize [IO] PTR to a DWORD that hold the size of the buffer and receive
134 * the needed size, when the buffer is too small
135 *
136 * RETURNS
137 * Success: TRUE
138 * Failure: FALSE
139 *
140 * NOTES
141 * size is in Bytes on w2k and WCHAR on XP
142 *
143 */
144
145 BOOL WINAPI PSetupEnumMonitor(HANDLE monitorinfo, DWORD index, LPWSTR buffer, LPDWORD psize)
146 {
147 monitorinfo_t * mi = monitorinfo;
148 LPWSTR nameW;
149 DWORD len;
150
151 TRACE("(%p, %u, %p, %p) => %d\n", mi, index, buffer, psize, psize ? *psize : 0);
152
153 if (index < mi->installed) {
154 nameW = mi->mi2[index].pName;
155 len = lstrlenW(nameW) + 1;
156 if (len <= *psize) {
157 memcpy(buffer, nameW, len * sizeof(WCHAR));
158 TRACE("#%u: %s\n", index, debugstr_w(buffer));
159 return TRUE;
160 }
161 *psize = len;
162 SetLastError(ERROR_INSUFFICIENT_BUFFER);
163 return FALSE;
164 }
165 SetLastError(ERROR_NO_MORE_ITEMS);
166 return FALSE;
167 }