[MMDEVAPI_WINETEST] Sync with Wine Staging 4.0. CORE-15682
[reactos.git] / modules / rostests / win32 / loadlib / loadlib.c
1 /*
2 * ReactOS test program -
3 *
4 * loadlib.c
5 *
6 * Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include <windows.h>
24 #include "loadlib.h"
25 #include <string.h>
26 #include <stdlib.h>
27 #include <wchar.h>
28
29 #define APP_VERSION 1
30 #define MAX_LIBS 25
31
32 #ifdef UNICODE
33 #define TARGET "UNICODE"
34 BOOL bUseAnsi = FALSE;
35 #else
36 #define TARGET "MBCS"
37 BOOL bUseAnsi = TRUE;
38 #endif
39 BOOL verbose_flagged = FALSE;
40 BOOL debug_flagged = FALSE;
41 BOOL loop_flagged = FALSE;
42 BOOL recursive_flagged = FALSE;
43
44 HANDLE OutputHandle;
45 HANDLE InputHandle;
46
47
48 void dprintf(char* fmt, ...)
49 {
50 va_list args;
51 char buffer[255];
52
53 va_start(args, fmt);
54 wvsprintfA(buffer, fmt, args);
55 WriteConsoleA(OutputHandle, buffer, lstrlenA(buffer), NULL, NULL);
56 va_end(args);
57 }
58
59 long getinput(char* buf, int buflen)
60 {
61 DWORD result;
62
63 ReadConsoleA(InputHandle, buf, buflen, &result, NULL);
64 return (long)result;
65 }
66
67 DWORD ReportLastError(void)
68 {
69 DWORD dwError = GetLastError();
70 if (dwError != ERROR_SUCCESS) {
71 PSTR msg = NULL;
72 if (FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
73 0, dwError, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), (PSTR)&msg, 0, NULL)) {
74 if (msg != NULL) {
75 dprintf("ReportLastError() %d - %s\n", dwError, msg);
76 } else {
77 dprintf("ERROR: ReportLastError() %d - returned TRUE but with no msg string!\n", dwError);
78 }
79 } else {
80 dprintf("ReportLastError() %d - unknown error\n", dwError);
81 }
82 if (msg != NULL) {
83 LocalFree(msg);
84 }
85 }
86 return dwError;
87 }
88
89 const char* appName(const char* argv0)
90 {
91 const char* name;
92
93 name = (const char*)strrchr(argv0, '\\');
94 if (name != NULL)
95 return name + 1;
96 return argv0;
97 }
98
99 int usage(const char* appName)
100 {
101 dprintf("USAGE: %s libname [libname ...] [unicode]|[ansi] [loop][recurse]\n", appName);
102 dprintf("\tWhere libname(s) is one or more libraries to load.\n");
103 dprintf("\t[unicode] - perform tests using UNICODE api calls\n");
104 dprintf("\t[ansi] - perform tests using ANSI api calls\n");
105 dprintf("\t default is %s\n", TARGET);
106 dprintf("\t[loop] - run test process in continuous loop\n");
107 dprintf("\t[recurse] - load libraries recursively rather than sequentually\n");
108 dprintf("\t[debug] - enable debug mode (unused)\n");
109 dprintf("\t[verbose] - enable verbose output (unused)\n");
110 return 0;
111 }
112
113 DWORD LoadLibraryList(char** libnames, int counter, BOOL bUseAnsi)
114 {
115 HMODULE hModule;
116
117 dprintf("Attempting to LoadLibrary");
118 if (bUseAnsi) {
119 dprintf("A(%s) - ", *libnames);
120 hModule = LoadLibraryA(*libnames);
121 } else {
122 int len;
123 wchar_t libnameW[500];
124 len = mbstowcs(libnameW, *libnames, strlen(*libnames));
125 if (len) {
126 libnameW[len] = L'\0';
127 dprintf("W(%S) - ", libnameW);
128 hModule = LoadLibraryW(libnameW);
129 } else {
130 return ERROR_INVALID_PARAMETER;
131 }
132 }
133 if (hModule == NULL) {
134 dprintf("\nERROR: failed to obtain handle to module %s - %x\n", *libnames, hModule);
135 return ReportLastError();
136 }
137 dprintf("%x\n", hModule);
138
139 if (counter--) {
140 LoadLibraryList(++libnames, counter, bUseAnsi);
141 }
142
143 if (!FreeLibrary(hModule)) {
144 dprintf("ERROR: failed to free module %s - %x\n", *libnames, hModule);
145 return ReportLastError();
146 } else {
147 dprintf("FreeLibrary(%x) - successfull.\n", hModule);
148 }
149 return 0L;
150 }
151
152 int __cdecl main(int argc, char* argv[])
153 {
154 char* libs[MAX_LIBS];
155 int lib_count = 0;
156 int result = 0;
157 int i = 0;
158
159 AllocConsole();
160 InputHandle = GetStdHandle(STD_INPUT_HANDLE);
161 OutputHandle = GetStdHandle(STD_OUTPUT_HANDLE);
162
163 dprintf("%s application - build %03d (default: %s)\n", appName(argv[0]), APP_VERSION, TARGET);
164 if (argc < 2) {
165 /*return */usage(appName(argv[0]));
166 }
167 memset(libs, 0, sizeof(libs));
168 for (i = 1; i < argc; i++) {
169 if (lstrcmpiA(argv[i], "ansi") == 0) {
170 bUseAnsi = TRUE;
171 } else if (lstrcmpiA(argv[i], "unicode") == 0) {
172 bUseAnsi = FALSE;
173 } else if (lstrcmpiA(argv[i], "loop") == 0) {
174 loop_flagged = 1;
175 } else if (lstrcmpiA(argv[i], "recurse") == 0) {
176 recursive_flagged = 1;
177 } else if (lstrcmpiA(argv[i], "verbose") == 0) {
178 verbose_flagged = 1;
179 } else if (lstrcmpiA(argv[i], "debug") == 0) {
180 debug_flagged = 1;
181 } else {
182 if (lib_count < MAX_LIBS) {
183 libs[lib_count] = argv[i];
184 ++lib_count;
185 }
186 }
187 }
188 if (lib_count) {
189 do {
190 if (recursive_flagged) {
191 result = LoadLibraryList(libs, lib_count - 1, bUseAnsi);
192 } else {
193 for (i = 0; i < lib_count; i++) {
194 result = LoadLibraryList(&libs[i], 0, bUseAnsi);
195 //if (result != 0) break;
196 }
197 }
198 } while (loop_flagged);
199 } else {
200 int len;
201 char buffer[500];
202 do {
203 dprintf("\nEnter library name to attempt loading: ");
204 len = getinput(buffer, sizeof(buffer) - 1);
205 if (len > 2) {
206 char* buf = buffer;
207 buffer[len-2] = '\0';
208 result = LoadLibraryList(&buf, 0, bUseAnsi);
209 } else break;
210 } while (!result && len);
211 }
212 dprintf("finished\n");
213 return result;
214 }
215
216
217 #ifdef _NOCRT
218 char* args[] = { "loadlib.exe", "advapi32.dll", "user32.dll", "recurse"};
219 int __cdecl mainCRTStartup(void)
220 {
221 return main(3, args);
222 }
223 #endif /*__GNUC__*/