ReactOS  0.4.13-dev-482-ge57f103
loadlib.c
Go to the documentation of this file.
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"
35 #else
36 #define TARGET "MBCS"
38 #endif
43 
46 
47 
48 void dprintf(char* fmt, ...)
49 {
50  va_list args;
51  char buffer[255];
52 
53  va_start(args, fmt);
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 
68 {
69  DWORD dwError = GetLastError();
70  if (dwError != ERROR_SUCCESS) {
71  PSTR msg = NULL;
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 {
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 {
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();
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';
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__*/
#define ERROR_INVALID_PARAMETER
Definition: compat.h:91
static int argc
Definition: ServiceArgs.c:12
#define TRUE
Definition: types.h:120
BOOL WINAPI AllocConsole(VOID)
Definition: console.c:48
BOOL debug_flagged
Definition: loadlib.c:40
#define ERROR_SUCCESS
Definition: deptool.c:10
int WINAPI lstrcmpiA(LPCSTR lpString1, LPCSTR lpString2)
Definition: lstring.c:42
#define __cdecl
Definition: accygwin.h:79
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
size_t __cdecl mbstowcs(_Out_writes_opt_z_(_MaxCount) wchar_t *_Dest, _In_z_ const char *_Source, _In_ size_t _MaxCount)
#define LANG_NEUTRAL
Definition: nls.h:22
DWORD WINAPI FormatMessageA(DWORD dwFlags, LPCVOID lpSource, DWORD dwMessageId, DWORD dwLanguageId, LPSTR lpBuffer, DWORD nSize, __ms_va_list *args)
Definition: format_msg.c:483
#define SUBLANG_DEFAULT
Definition: nls.h:168
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
HANDLE WINAPI GetStdHandle(IN DWORD nStdHandle)
Definition: console.c:152
GLuint buffer
Definition: glext.h:5915
BOOL recursive_flagged
Definition: loadlib.c:42
_Check_return_ _CRTIMP _CONST_RETURN char *__cdecl strrchr(_In_z_ const char *_Str, _In_ int _Ch)
#define argv
Definition: mplay32.c:18
int WINAPI wvsprintfA(_Out_ LPSTR, _In_ _Printf_format_string_ LPCSTR, _In_ va_list arglist)
Definition: match.c:390
HANDLE InputHandle
Definition: loadlib.c:45
#define mainCRTStartup
Definition: wcrtexe.c:10
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define va_end(ap)
Definition: acmsvcex.h:90
BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleA(IN HANDLE hConsoleOutput, IN CONST VOID *lpBuffer, IN DWORD nNumberOfCharsToWrite, OUT LPDWORD lpNumberOfCharsWritten, LPVOID lpReserved)
Definition: readwrite.c:1470
unsigned int BOOL
Definition: ntddk_ex.h:94
HINSTANCE WINAPI DECLSPEC_HOTPATCH LoadLibraryA(LPCSTR lpLibFileName)
Definition: loader.c:111
#define FORMAT_MESSAGE_ALLOCATE_BUFFER
Definition: winbase.h:400
#define LoadLibraryW(x)
Definition: compat.h:404
smooth NULL
Definition: ftsmooth.c:416
DWORD ReportLastError(void)
Definition: loadlib.c:67
char * va_list
Definition: acmsvcex.h:78
#define APP_VERSION
Definition: loadlib.c:29
#define TARGET
Definition: loadlib.c:36
#define STD_INPUT_HANDLE
Definition: winbase.h:264
#define FORMAT_MESSAGE_FROM_SYSTEM
Definition: winbase.h:404
#define MAX_LIBS
Definition: loadlib.c:30
#define FreeLibrary(x)
Definition: compat.h:405
int usage(const char *appName)
Definition: loadlib.c:99
long getinput(char *buf, int buflen)
Definition: loadlib.c:59
unsigned long DWORD
Definition: ntddk_ex.h:95
BOOL WINAPI DECLSPEC_HOTPATCH ReadConsoleA(IN HANDLE hConsoleInput, OUT LPVOID lpBuffer, IN DWORD nNumberOfCharsToRead, OUT LPDWORD lpNumberOfCharsRead, IN PCONSOLE_READCONSOLE_CONTROL pInputControl OPTIONAL)
Definition: readwrite.c:1197
static const WCHAR L[]
Definition: oid.c:1250
GLenum GLsizei len
Definition: glext.h:6722
static char argv0[MAX_PATH]
Definition: shlexec.c:56
#define STD_OUTPUT_HANDLE
Definition: winbase.h:265
BOOL loop_flagged
Definition: loadlib.c:41
void dprintf(char *fmt,...)
Definition: loadlib.c:48
HANDLE OutputHandle
Definition: loadlib.c:44
int WINAPI lstrlenA(LPCSTR lpString)
Definition: lstring.c:145
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
signed char * PSTR
Definition: retypes.h:7
BOOL verbose_flagged
Definition: loadlib.c:39
#define va_start(ap, A)
Definition: acmsvcex.h:91
int __cdecl main(int argc, char *argv[])
Definition: loadlib.c:152
#define msg(x)
Definition: auth_time.c:54
Definition: name.c:36
#define MAKELANGID(p, s)
Definition: nls.h:15
const char * appName(const char *argv0)
Definition: loadlib.c:89
DWORD LoadLibraryList(char **libnames, int counter, BOOL bUseAnsi)
Definition: loadlib.c:113
GLuint64EXT * result
Definition: glext.h:11304
#define memset(x, y, z)
Definition: compat.h:39
#define args
Definition: format.c:66
BOOL bUseAnsi
Definition: loadlib.c:37
Definition: dsound.c:943
HMODULE hModule
Definition: animate.c:44
GLuint const GLchar * name
Definition: glext.h:6031