netstat: Initial implementation.
[wine] / dlls / 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 typedef struct {
40   LPMONITOR_INFO_2W mi2;    /* Buffer for installed Monitors */
41   DWORD installed;          /* Number of installed Monitors */
42 } monitorinfo_t;
43
44 /*****************************************************
45  *      DllMain
46  */
47 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
48 {
49     TRACE("(%p, %d, %p)\n",hinstDLL, fdwReason, lpvReserved);
50
51     switch(fdwReason)
52     {
53         case DLL_WINE_PREATTACH:
54             return FALSE;           /* prefer native version */
55
56         case DLL_PROCESS_ATTACH:
57             DisableThreadLibraryCalls( hinstDLL );
58             break;
59     }
60     return TRUE;
61 }
62
63 /*****************************************************
64  *  PSetupCreateMonitorInfo  [NTPRINT.@]
65  *
66  *
67  */
68
69 HANDLE WINAPI PSetupCreateMonitorInfo(LPVOID unknown1, LPVOID  unknown2,LPVOID unknown3)
70 {
71     monitorinfo_t * mi=NULL;
72     DWORD needed;
73     DWORD res;
74
75     TRACE("(%p, %p, %p)\n", unknown1, unknown2, unknown3);
76
77     if ((unknown2 != NULL) || (unknown3 != NULL)) {
78         FIXME("got unknown parameter: (%p, %p, %p)\n", unknown1, unknown2, unknown3);
79         return NULL;
80     }
81
82     mi = HeapAlloc(GetProcessHeap(), 0, sizeof(monitorinfo_t));
83     if (!mi) {
84         /* FIXME: SetLastError() needed? */
85         return NULL;
86     }
87
88     /* Get the needed size for all Monitors */
89     res = EnumMonitorsW(NULL, 2, NULL, 0, &needed, &mi->installed);
90     if (!res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER)) {
91         mi->mi2 = HeapAlloc(GetProcessHeap(), 0, needed);
92         res = EnumMonitorsW(NULL, 2, (LPBYTE) mi->mi2, needed, &needed, &mi->installed);
93     }
94
95     if (!res) {
96         HeapFree(GetProcessHeap(), 0, mi);
97         /* FIXME: SetLastError() needed? */
98         return NULL;
99     }
100
101     TRACE("=> %p (%u monitors installed)\n", mi, mi->installed);
102     return mi;
103 }
104
105 /*****************************************************
106  *  PSetupDestroyMonitorInfo  [NTPRINT.@]
107  *
108  */
109
110 VOID WINAPI PSetupDestroyMonitorInfo(HANDLE monitorinfo)
111 {
112     monitorinfo_t * mi = monitorinfo;
113
114     TRACE("(%p)\n", mi);
115     if (mi) {
116         if (mi->installed) HeapFree(GetProcessHeap(), 0, mi->mi2);
117         HeapFree(GetProcessHeap(), 0, mi);
118     }
119 }
120
121 /*****************************************************
122  *  PSetupEnumMonitor  [NTPRINT.@]
123  *
124  * Copy the selected Monitorname to a buffer
125  *
126  * PARAMS
127  *  monitorinfo [I]  HANDLE from PSetupCreateMonitorInfo
128  *  index       [I]  Nr. of the Monitorname to copy
129  *  buffer      [I]  Target, that receive the Monitorname
130  *  psize       [IO] PTR to a DWORD that hold the size of the buffer and receive
131  *                   the needed size, when the buffer is too small
132  *
133  * RETURNS
134  *  Success:  TRUE
135  *  Failure:  FALSE
136  *
137  * NOTES
138  *   size is in Bytes on w2k and WCHAR on XP
139  *
140  */
141
142 BOOL WINAPI PSetupEnumMonitor(HANDLE monitorinfo, DWORD index, LPWSTR buffer, LPDWORD psize)
143 {
144     monitorinfo_t * mi = monitorinfo;
145     LPWSTR  nameW;
146     DWORD   len;
147
148     TRACE("(%p, %u, %p, %p) => %d\n", mi, index, buffer, psize, psize ? *psize : 0);
149
150     if (index < mi->installed) {
151         nameW = mi->mi2[index].pName;
152         len = lstrlenW(nameW) + 1;
153         if (len <= *psize) {
154             memcpy(buffer, nameW, len * sizeof(WCHAR));
155             TRACE("#%u: %s\n", index, debugstr_w(buffer));
156             return TRUE;
157         }
158         *psize = len;
159         SetLastError(ERROR_INSUFFICIENT_BUFFER);
160         return FALSE;
161     }
162     SetLastError(ERROR_NO_MORE_ITEMS);
163     return FALSE;
164 }