Incorporate rosapps. 0.3.15 was branched somewhat incorrectly so rosapps is not synce...
[reactos.git] / modules / rosapps / applications / sysutils / chklib / chklib.c
1 /*
2 * chklib.c
3 *
4 * Copyright (C) 1998, 1999 Emanuele Aliberti.
5 *
6 * --------------------------------------------------------------------
7 *
8 * This software is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
12 *
13 * This software 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 GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this software; see the file COPYING. If
20 * not, write to the Free Software Foundation, Inc., 675 Mass Ave,
21 * Cambridge, MA 02139, USA.
22 *
23 * --------------------------------------------------------------------
24 * Check a PE DLL for loading and get an exported symbol's address
25 * (relocated).
26 *
27 */
28 //#define UNICODE
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <assert.h>
32
33 #include <stdarg.h>
34 #include <windef.h>
35 #include <winbase.h>
36
37 #include "win32err.h"
38
39 #ifdef DISPLAY_VERSION
40 static
41 void
42 DisplayVersion(
43 HANDLE dll,
44 PCHAR ModuleName
45 )
46 {
47 DWORD Zero;
48 DWORD Size;
49 PVOID vi = NULL;
50
51 assert(ModuleName);
52 Size = GetFileVersionInfoSize(
53 ModuleName,
54 & Zero
55 );
56 if (Size == 0)
57 {
58 PrintWin32Error(
59 L"GetFileVersionInfoSize",
60 GetLastError()
61 );
62 return;
63 }
64 vi = (PVOID) LocalAlloc(LMEM_ZEROINIT,Size);
65 if (!vi) return;
66 assert(dll != INVALID_HANDLE_VALUE);
67 if (0 == GetFileVersionInfo(
68 ModuleName,
69 (DWORD) dll,
70 Size,
71 vi
72 )
73 ) {
74 PrintWin32Error(
75 L"GetFileVersionInfo",
76 GetLastError()
77 );
78 return;
79 }
80 /*
81 VerQueryValue(
82 vi,
83 L"\\StringFileInfo\\040904E4\\FileDescription",
84 & lpBuffer,
85 & dwBytes
86 );
87 */
88 LocalFree(vi);
89 }
90 #endif /* def DISPLAY_VERSION */
91
92
93 static
94 void
95 DisplayEntryPoint(
96 const HANDLE dll,
97 LPCSTR SymbolName
98 )
99 {
100 FARPROC EntryPoint;
101
102 printf(
103 "[%s]\n",
104 SymbolName
105 );
106 EntryPoint = GetProcAddress(
107 dll,
108 SymbolName
109 );
110 if (!EntryPoint)
111 {
112 PrintWin32Error(
113 L"GetProcAddress",
114 GetLastError()
115 );
116 return;
117 }
118 printf(
119 "0x%p %s\n",
120 EntryPoint,
121 SymbolName
122 );
123 }
124
125
126 /* --- MAIN --- */
127
128
129 int
130 main(
131 int argc,
132 char * argv []
133 )
134 {
135 HINSTANCE dll;
136 TCHAR ModuleName [_MAX_PATH];
137
138 if (argc < 2)
139 {
140 fprintf(
141 stderr,
142 "\
143 ReactOS System Tools\n\
144 Check a Dynamic Link Library (DLL) for loading\n\
145 Copyright (c) 1998, 1999 Emanuele Aliberti\n\n\
146 usage: %s module [symbol [, ...]]\n",
147 argv[0]
148 );
149 exit(EXIT_FAILURE);
150 }
151 dll = LoadLibraryA(argv[1]);
152 if (!dll)
153 {
154 UINT LastError;
155
156 LastError = GetLastError();
157 PrintWin32Error(L"LoadLibrary",LastError);
158 fprintf(
159 stderr,
160 "%s: loading %s failed (%d).\n",
161 argv[0],
162 argv[1],
163 LastError
164 );
165 exit(EXIT_FAILURE);
166 }
167 GetModuleFileName(
168 (HANDLE) dll,
169 ModuleName,
170 sizeof ModuleName
171 );
172 printf(
173 "%s loaded.\n",
174 ModuleName
175 );
176 #ifdef DISPLAY_VERSION
177 DisplayVersion(dll,ModuleName);
178 #endif
179 if (argc > 2)
180 {
181 int CurrentSymbol;
182
183 for ( CurrentSymbol = 2;
184 (CurrentSymbol < argc);
185 ++CurrentSymbol
186 )
187 {
188 DisplayEntryPoint( dll, argv[CurrentSymbol] );
189 }
190 }
191 FreeLibrary(dll);
192 printf(
193 "%s unloaded.\n",
194 ModuleName
195 );
196 return EXIT_SUCCESS;
197 }
198
199 /* EOF */