source: trunk/src/kernel32/winimagebase.cpp@ 3404

Last change on this file since 3404 was 3404, checked in by sandervl, 25 years ago

put back dll search method + loadlibraryex fix

File size: 9.8 KB
Line 
1/* $Id: winimagebase.cpp,v 1.18 2000-04-16 10:42:12 sandervl Exp $ */
2
3/*
4 * Win32 PE Image base class
5 *
6 * Copyright 1998-1999 Sander van Leeuwen (sandervl@xs4all.nl)
7 * Copyright 1998 Knut St. Osmundsen
8 *
9 * Project Odin Software License can be found in LICENSE.TXT
10 *
11 */
12
13#define INCL_DOSFILEMGR /* File Manager values */
14#define INCL_DOSMODULEMGR
15#define INCL_DOSERRORS /* DOS Error values */
16#define INCL_DOSPROCESS /* DOS Process values */
17#define INCL_DOSMISC /* DOS Miscellanous values */
18#define INCL_WIN
19#define INCL_BASE
20#include <os2wrap.h> //Odin32 OS/2 api wrappers
21
22#include <stdio.h>
23#include <string.h>
24#include <stdlib.h>
25
26#include <assert.h>
27#include <misc.h>
28#include <win32type.h>
29#include <winimagebase.h>
30#include <windllbase.h>
31#include <winexebase.h>
32#include <pefile.h>
33#include <unicode.h>
34#include <winres.h>
35#include "oslibmisc.h"
36#include "oslibdos.h"
37#include "initterm.h"
38#include "directory.h"
39#include <win\virtual.h>
40
41#define DBG_LOCALLOG DBG_winimagebase
42#include "dbglocal.h"
43
44//******************************************************************************
45//******************************************************************************
46Win32ImageBase::Win32ImageBase(HINSTANCE hInstance) :
47 errorState(NO_ERROR), entryPoint(0), fullpath(NULL),
48 tlsAddress(0), tlsIndexAddr(0), tlsInitSize(0), tlsTotalSize(0),
49 tlsCallBackAddr(0), tlsIndex(-1), winres(NULL), pResDir(NULL),
50 ulRVAResourceSection(0)
51{
52 magic = MAGIC_WINIMAGE;
53
54 if(hInstance != -1) {
55 this->hinstance = hInstance;
56
57 char *name = OSLibGetDllName(hinstance);
58 strcpy(szFileName, name);
59 strupr(szFileName);
60
61 //rename dll (os/2 -> win32) if necessary (i.e. OLE32OS2 -> OLE32)
62 Win32DllBase::renameDll(szFileName, FALSE);
63
64 name = strrchr(szFileName, '\\')+1;
65 strcpy(szModule, name);
66
67 char *dot = strrchr(szModule, '.');
68 if(dot)
69 *dot = 0;
70 }
71 else {
72 szModule[0] = 0;
73 this->hinstance = -1;
74 }
75}
76//******************************************************************************
77//******************************************************************************
78Win32ImageBase::~Win32ImageBase()
79{
80 Win32Resource *res;
81
82 while(winres)
83 {
84 res = winres->next;
85 delete(winres);
86 winres = res;
87 }
88 if(fullpath)
89 free(fullpath);
90}
91//******************************************************************************
92//******************************************************************************
93void Win32ImageBase::setFullPath(char *name)
94{
95 dassert(name, ("setFullPath, name == NULL"));
96 fullpath = (char *)malloc(strlen(name)+1);
97 dassert(fullpath, ("setFullPath, fullpath == NULL"));
98 strcpy(fullpath, name);
99}
100//******************************************************************************
101//Add image to dependency list of this image
102//******************************************************************************
103void Win32ImageBase::addDependency(Win32DllBase *image)
104{
105 loadedDlls.Push((ULONG)image);
106}
107//******************************************************************************
108//******************************************************************************
109BOOL Win32ImageBase::dependsOn(Win32DllBase *image)
110{
111 QueueItem *item;
112 BOOL ret = FALSE;
113
114 dlllistmutex.enter();
115 item = loadedDlls.Head();
116 while(item) {
117 if(loadedDlls.getItem(item) == (ULONG)image) {
118 ret = TRUE;
119 break;
120 }
121 item = loadedDlls.getNext(item);
122 }
123 dlllistmutex.leave();
124 return ret;
125}
126//******************************************************************************
127//Returns required OS version for this image
128//******************************************************************************
129ULONG Win32ImageBase::getVersion()
130{
131 dprintf(("Win32ImageBase::getVersion: NOT IMPLEMENTED!"));
132 return 0x40000; //NT 4
133}
134//******************************************************************************
135//******************************************************************************
136BOOL Win32ImageBase::findDll(const char *szFileName, char *szFullName,
137 int cchFullFileName, const char *pszAltPath)
138{
139 char modname[CCHMAXPATH];
140 HFILE dllfile = NULL;
141 char *imagepath;
142
143 strcpy(szFullName, szFileName);
144 strupr(szFullName);
145 if(!strchr(szFullName, (int)'.')) {
146 strcat(szFullName,".DLL");
147 }
148
149 //search order:
150 //1) exe dir
151 //2) current dir
152 //3) windows system dir (kernel32 path)
153 //4) windows dir
154 //5) path
155 if(WinExe) {
156 strcpy(modname, WinExe->getFullPath());
157 //remove file name from full path
158 imagepath = modname + strlen(modname) - 1;
159 while(*imagepath != '\\') imagepath--;
160 imagepath[1] = 0;
161 strcat(modname, szFullName);
162 dllfile = OSLibDosOpen(modname, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
163 }
164 if(dllfile == NULL) {
165 strcpy(modname, szFullName);
166 dllfile = OSLibDosOpen(szFullName, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
167 if(dllfile == NULL) {
168 strcpy(modname, InternalGetSystemDirectoryA());
169 strcat(modname, "\\");
170 strcat(modname, szFullName);
171 dllfile = OSLibDosOpen(modname, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
172 if(dllfile == NULL) {
173 strcpy(modname, InternalGetWindowsDirectoryA());
174 strcat(modname, "\\");
175 strcat(modname, szFullName);
176 dllfile = OSLibDosOpen(modname, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
177 if(dllfile == NULL) {
178 if(OSLibDosSearchPath(OSLIB_SEARCHENV, "PATH", szFullName, modname, sizeof(modname)) == 0) {
179 return FALSE;
180 }
181 }
182 }
183 }
184 }
185 strcpy(szFullName, modname);
186 if(dllfile) OSLibDosClose(dllfile);
187 return TRUE;
188}
189//******************************************************************************
190//******************************************************************************
191BOOL Win32ImageBase::isPEImage(char *szFileName)
192{
193 char filename[CCHMAXPATH];
194 char *syspath;
195 HFILE dllfile;
196 IMAGE_FILE_HEADER fh;
197 HFILE win32handle;
198 ULONG ulAction = 0; /* Action taken by DosOpen */
199 ULONG ulLocal = 0; /* File pointer position after DosSetFilePtr */
200 APIRET rc = NO_ERROR; /* Return code */
201 LPVOID win32file = NULL;
202 ULONG ulRead;
203 int nSections, i;
204
205 if (!findDll(szFileName, filename, sizeof(filename)))
206 {
207 dprintf(("KERNEL32:Win32ImageBase::isPEImage(%s) findDll failed to find the file.\n",
208 szFileName, rc));
209 return FALSE;
210 }
211 rc = DosOpen(filename, /* File path name */
212 &win32handle, /* File handle */
213 &ulAction, /* Action taken */
214 0L, /* File primary allocation */
215 0L, /* File attribute */
216 OPEN_ACTION_FAIL_IF_NEW |
217 OPEN_ACTION_OPEN_IF_EXISTS, /* Open function type */
218 OPEN_FLAGS_NOINHERIT |
219 OPEN_SHARE_DENYNONE |
220 OPEN_ACCESS_READONLY, /* Open mode of the file */
221 0L); /* No extended attribute */
222
223 if (rc != NO_ERROR)
224 {
225 dprintf(("KERNEL32:Win32ImageBase::isPEImage(%s) failed with %u\n",
226 szFileName, rc));
227 return(FALSE);
228 }
229
230 /* Move the file pointer back to the beginning of the file */
231 DosSetFilePtr(win32handle, 0L, FILE_BEGIN, &ulLocal);
232
233 IMAGE_DOS_HEADER *pdoshdr = (IMAGE_DOS_HEADER *)malloc(sizeof(IMAGE_DOS_HEADER));
234 if(pdoshdr == NULL) {
235 DosClose(win32handle); /* Close the file */
236 return(FALSE);
237 }
238 rc = DosRead(win32handle, pdoshdr, sizeof(IMAGE_DOS_HEADER), &ulRead);
239 if(rc != NO_ERROR) {
240 DosClose(win32handle); /* Close the file */
241 return(FALSE);
242 }
243 ULONG hdrsize = pdoshdr->e_lfanew + SIZE_OF_NT_SIGNATURE + sizeof(IMAGE_FILE_HEADER);
244 free(pdoshdr);
245
246 /* Move the file pointer back to the beginning of the file */
247 DosSetFilePtr(win32handle, 0L, FILE_BEGIN, &ulLocal);
248
249 win32file = malloc(hdrsize);
250 if(win32file == NULL) {
251 DosClose(win32handle); /* Close the file */
252 return(FALSE);
253 }
254 rc = DosRead(win32handle, win32file, hdrsize, &ulRead);
255 if(rc != NO_ERROR) {
256 goto failure;
257 }
258
259 if(GetPEFileHeader (win32file, &fh) == FALSE) {
260 goto failure;
261 }
262
263 if(!(fh.Characteristics & IMAGE_FILE_EXECUTABLE_IMAGE)) {//not valid
264 goto failure;
265 }
266 if(fh.Machine != IMAGE_FILE_MACHINE_I386) {
267 goto failure;
268 }
269 //IMAGE_FILE_SYSTEM == only drivers (device/file system/video etc)?
270 if(fh.Characteristics & IMAGE_FILE_SYSTEM) {
271 goto failure;
272 }
273 DosClose(win32handle);
274 return(TRUE);
275
276failure:
277 free(win32file);
278 DosClose(win32handle);
279 return(FALSE);
280}
281//******************************************************************************
282//******************************************************************************
283/**
284 * Static helper which finds the Win32ImageBase object corresponding to hModule.
285 * @returns Pointer to Win32ImageBase object corresponding to hModule.
286 * @param hModule Odin32 modulehandler. 0 and -1 is aliases for the executable module
287 * @status completely implemented and tested.
288 * @author knut st. osmundsen
289 */
290Win32ImageBase * Win32ImageBase::findModule(HMODULE hModule)
291{
292 Win32ImageBase *pRet;
293
294 if (hModule == -1 || hModule == 0 || /* note: WinNt 4, SP4 don't accept -1 as the EXE handle.*/
295 (WinExe != NULL && hModule == WinExe->getInstanceHandle())
296 )
297 pRet = WinExe;
298 else
299 pRet = Win32DllBase::findModule(hModule);
300
301 if (pRet == NULL)
302 {
303 if (WinExe == NULL)
304 dprintf(("Win32ImageBase::findModule: Module not found. WinExe is NULL, hModule=%#x\n", hModule));
305 else
306 dprintf(("Win32ImageBase::findModule: Module not found, hModule=%#x\n", hModule));
307 }
308
309 return pRet;
310}
311
312
Note: See TracBrowser for help on using the repository browser.