Windows: AFS Redirector Network Provider
[openafs.git] / src / WINNT / afsrdr / npdll / tests / enumresources.c
1 /*
2  * This test code was obtained from
3  * http://msdn.microsoft.com/en-us/library/aa385341(VS.85).aspx
4  *
5  * No license specified.
6  */
7
8 #pragma comment(lib, "mpr.lib")
9
10 #ifndef UNICODE
11 #define UNICODE
12 #endif
13
14 #include <windows.h>
15 #include <stdio.h>
16 #include <winnetwk.h>
17
18 BOOL WINAPI EnumerateFunc(DWORD dwScope, LPNETRESOURCE lpnr);
19 void DisplayStruct(int i, LPNETRESOURCE lpnrLocal);
20 BOOL WINAPI NetErrorHandler(HWND hwnd, DWORD dwErrorCode, LPSTR lpszFunction);
21
22 int main()
23 {
24
25     LPNETRESOURCE lpnr = NULL;
26
27     printf("Connected Resources\n");
28     printf("-------------------\n");
29     if (EnumerateFunc(RESOURCE_CONNECTED, lpnr) == FALSE) {
30         printf("Call to EnumerateFunc(CONNECTED) failed\n");
31         return 1;
32     }
33
34     printf("\n");
35     printf("Context Resources\n");
36     printf("-----------------\n");
37     if (EnumerateFunc(RESOURCE_CONTEXT, lpnr) == FALSE) {
38         printf("Call to EnumerateFunc(CONTEXT) failed\n");
39         return 1;
40     }
41
42     printf("\n");
43     printf("Global Resources\n");
44     printf("----------------\n");
45     if (EnumerateFunc(RESOURCE_GLOBALNET, lpnr) == FALSE) {
46         printf("Call to EnumerateFunc(GLOBALNET) failed\n");
47         return 1;
48     }
49
50     printf("\n");
51     printf("Remembered Resources\n");
52     printf("--------------------\n");
53     if (EnumerateFunc(RESOURCE_REMEMBERED, lpnr) == FALSE) {
54         printf("Call to EnumerateFunc(REMEMBERED) failed\n");
55         return 1;
56     }
57
58     return 0;
59 }
60
61 BOOL WINAPI EnumerateFunc(DWORD dwScope, LPNETRESOURCE lpnr)
62 {
63     DWORD dwResult, dwResultEnum;
64     HANDLE hEnum;
65     DWORD cbBuffer = 16384;     // 16K is a good size
66     DWORD cEntries = -1;        // enumerate all possible entries
67     LPNETRESOURCE lpnrLocal;    // pointer to enumerated structures
68     DWORD i;
69     //
70     // Call the WNetOpenEnum function to begin the enumeration.
71     //
72     dwResult = WNetOpenEnum(dwScope, // all network resources
73                             RESOURCETYPE_DISK,   // all resources
74                             RESOURCEUSAGE_ALL,  // enumerate all resources
75                             lpnr,       // NULL first time the function is called
76                             &hEnum);    // handle to the resource
77
78     if (dwResult != NO_ERROR) {
79         printf("WnetOpenEnum failed with error %d\n", dwResult);
80         return FALSE;
81     }
82     //
83     // Call the GlobalAlloc function to allocate resources.
84     //
85     lpnrLocal = (LPNETRESOURCE) GlobalAlloc(GPTR, cbBuffer);
86     if (lpnrLocal == NULL) {
87         printf("WnetOpenEnum failed with error %d\n", dwResult);
88         return FALSE;
89     }
90
91     do {
92         //
93         // Initialize the buffer.
94         //
95         ZeroMemory(lpnrLocal, cbBuffer);
96         //
97         // Call the WNetEnumResource function to continue
98         //  the enumeration.
99         //
100         cEntries = -1;
101         dwResultEnum = WNetEnumResource(hEnum,  // resource handle
102                                         &cEntries,      // defined locally as -1
103                                         lpnrLocal,      // LPNETRESOURCE
104                                         &cbBuffer);     // buffer size
105         //
106         // If the call succeeds, loop through the structures.
107         //
108         if (dwResultEnum == NO_ERROR) {
109             for (i = 0; i < cEntries; i++) {
110                 // Call an application-defined function to
111                 //  display the contents of the NETRESOURCE structures.
112                 //
113                 DisplayStruct(i, &lpnrLocal[i]);
114
115                 // If the NETRESOURCE structure represents a container resource,
116                 //  call the EnumerateFunc function recursively.
117
118                 if (dwScope == RESOURCE_GLOBALNET &&
119                      RESOURCEUSAGE_CONTAINER == (lpnrLocal[i].dwUsage
120                                                 & RESOURCEUSAGE_CONTAINER))
121                     if (!EnumerateFunc(dwScope, &lpnrLocal[i]))
122                         printf("EnumerateFunc returned FALSE\n");
123             }
124         }
125         // Process errors.
126         //
127         else if (dwResultEnum != ERROR_NO_MORE_ITEMS) {
128             printf("WNetEnumResource failed with error %d\n", dwResultEnum);
129             break;
130         }
131     }
132     //
133     // End do.
134     //
135     while (dwResultEnum != ERROR_NO_MORE_ITEMS);
136     //
137     // Call the GlobalFree function to free the memory.
138     //
139     GlobalFree((HGLOBAL) lpnrLocal);
140     //
141     // Call WNetCloseEnum to end the enumeration.
142     //
143     dwResult = WNetCloseEnum(hEnum);
144
145     if (dwResult != NO_ERROR) {
146         //
147         // Process errors.
148         //
149         printf("WNetCloseEnum failed with error %d\n\n", dwResult);
150 //    NetErrorHandler(hwnd, dwResult, (LPSTR)"WNetCloseEnum");
151         return FALSE;
152     }
153
154     return TRUE;
155 }
156
157 void DisplayStruct(int i, LPNETRESOURCE lpnrLocal)
158 {
159     printf("NETRESOURCE[%d] Scope: ", i);
160     switch (lpnrLocal->dwScope) {
161     case (RESOURCE_CONNECTED):
162         printf("connected\n");
163         break;
164     case (RESOURCE_GLOBALNET):
165         printf("all resources\n");
166         break;
167     case (RESOURCE_REMEMBERED):
168         printf("remembered\n");
169         break;
170     case RESOURCE_RECENT:
171         printf("recent\n");
172         break;
173     case RESOURCE_CONTEXT:
174         printf("context\n");
175         break;
176     default:
177         printf("unknown scope %d\n", lpnrLocal->dwScope);
178         break;
179     }
180
181     printf("NETRESOURCE[%d] Type: ", i);
182     switch (lpnrLocal->dwType) {
183     case (RESOURCETYPE_ANY):
184         printf("any\n");
185         break;
186     case (RESOURCETYPE_DISK):
187         printf("disk\n");
188         break;
189     case (RESOURCETYPE_PRINT):
190         printf("print\n");
191         break;
192     case RESOURCETYPE_RESERVED:
193         printf("reserved\n");
194         break;
195     default:
196         printf("unknown type %d\n", lpnrLocal->dwType);
197         break;
198     }
199
200     printf("NETRESOURCE[%d] DisplayType: ", i);
201     switch (lpnrLocal->dwDisplayType) {
202     case (RESOURCEDISPLAYTYPE_GENERIC):
203         printf("generic\n");
204         break;
205     case (RESOURCEDISPLAYTYPE_DOMAIN):
206         printf("domain\n");
207         break;
208     case (RESOURCEDISPLAYTYPE_SERVER):
209         printf("server\n");
210         break;
211     case (RESOURCEDISPLAYTYPE_SHARE):
212         printf("share\n");
213         break;
214     case (RESOURCEDISPLAYTYPE_FILE):
215         printf("file\n");
216         break;
217     case (RESOURCEDISPLAYTYPE_GROUP):
218         printf("group\n");
219         break;
220     case (RESOURCEDISPLAYTYPE_NETWORK):
221         printf("network\n");
222         break;
223     case RESOURCEDISPLAYTYPE_ROOT:
224         printf("root\n");
225         break;
226     case RESOURCEDISPLAYTYPE_SHAREADMIN:
227         printf("share-admin\n");
228         break;
229     case RESOURCEDISPLAYTYPE_DIRECTORY:
230         printf("directory\n");
231         break;
232     case RESOURCEDISPLAYTYPE_TREE:
233         printf("tree\n");
234         break;
235     case RESOURCEDISPLAYTYPE_NDSCONTAINER:
236         printf("nds-container\n");
237         break;
238     default:
239         printf("unknown display type %d\n", lpnrLocal->dwDisplayType);
240         break;
241     }
242
243     printf("NETRESOURCE[%d] Usage: 0x%x = ", i, lpnrLocal->dwUsage);
244     if (lpnrLocal->dwUsage & RESOURCEUSAGE_CONNECTABLE)
245         printf("connectable ");
246     if (lpnrLocal->dwUsage & RESOURCEUSAGE_CONTAINER)
247         printf("container ");
248     if (lpnrLocal->dwUsage & RESOURCEUSAGE_NOLOCALDEVICE)
249         printf("no-local ");
250     if (lpnrLocal->dwUsage & RESOURCEUSAGE_SIBLING)
251         printf("sibling ");
252     if (lpnrLocal->dwUsage & RESOURCEUSAGE_ATTACHED)
253         printf("attached ");
254     if (lpnrLocal->dwUsage & RESOURCEUSAGE_RESERVED)
255         printf("reserved ");
256     printf("\n");
257
258     printf("NETRESOURCE[%d] Localname: %S\n", i, lpnrLocal->lpLocalName);
259     printf("NETRESOURCE[%d] Remotename: %S\n", i, lpnrLocal->lpRemoteName);
260     printf("NETRESOURCE[%d] Comment: %S\n", i, lpnrLocal->lpComment);
261     printf("NETRESOURCE[%d] Provider: %S\n", i, lpnrLocal->lpProvider);
262     printf("\n");
263 }
264
265
266 /*
267 BOOL WINAPI NetErrorHandler(HWND hwnd,
268                             DWORD dwErrorCode,
269                             LPSTR lpszFunction)
270 {
271     DWORD dwWNetResult, dwLastError;
272     CHAR szError[256];
273     CHAR szCaption[256];
274     CHAR szDescription[256];
275     CHAR szProvider[256];
276
277     // The following code performs standard error-handling.
278
279     if (dwErrorCode != ERROR_EXTENDED_ERROR)
280     {
281         sprintf_s((LPSTR) szError, sizeof(szError), "%s failed; \nResult is %ld",
282             lpszFunction, dwErrorCode);
283         sprintf_s((LPSTR) szCaption, sizeof(szCaption), "%s error", lpszFunction);
284         MessageBox(hwnd, (LPSTR) szError, (LPSTR) szCaption, MB_OK);
285         return TRUE;
286     }
287
288     // The following code performs error-handling when the
289     //  ERROR_EXTENDED_ERROR return value indicates that the
290     //  WNetGetLastError function can retrieve additional information.
291
292     else
293     {
294         dwWNetResult = WNetGetLastError(&dwLastError, // error code
295             (LPSTR) szDescription,  // buffer for error description
296             sizeof(szDescription),  // size of error buffer
297             (LPSTR) szProvider,     // buffer for provider name
298             sizeof(szProvider));    // size of name buffer
299
300         //
301         // Process errors.
302         //
303         if(dwWNetResult != NO_ERROR) {
304             sprintf_s((LPSTR) szError, sizeof(szError),
305                 "WNetGetLastError failed; error %ld", dwWNetResult);
306             MessageBox(hwnd, (LPSTR) szError,
307                 "WNetGetLastError", MB_OK);
308             return FALSE;
309         }
310
311         //
312         // Otherwise, print the additional error information.
313         //
314         sprintf_((LPSTR) szError, sizeof(szError),
315             "%s failed with code %ld;\n%s",
316             (LPSTR) szProvider, dwLastError, (LPSTR) szDescription);
317         sprintf_s((LPSTR) szCaption, sizeof(szCaption), "%s error", lpszFunction);
318         MessageBox(hwnd, (LPSTR) szError, (LPSTR) szCaption, MB_OK);
319         return TRUE;
320     }
321 }
322 */