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

Last change on this file since 2786 was 2786, checked in by sandervl, 26 years ago

search in path for dlls if not found

File size: 7.6 KB
Line 
1/* $Id: winimagebase.cpp,v 1.7 2000-02-15 00:14:27 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 <win\virtual.h>
39
40//******************************************************************************
41//******************************************************************************
42Win32ImageBase::Win32ImageBase(HINSTANCE hInstance) :
43 errorState(NO_ERROR), entryPoint(0), fullpath(NULL),
44 tlsAddress(0), tlsIndexAddr(0), tlsInitSize(0), tlsTotalSize(0),
45 tlsCallBackAddr(0), tlsIndex(-1), winres(NULL), pResDir(NULL),
46 ulRVAResourceSection(0)
47{
48#ifdef DEBUG
49 magic = MAGIC_WINIMAGE;
50#endif
51
52 if(hInstance != -1) {
53 this->hinstance = hInstance;
54
55 char *name = OSLibGetDllName(hinstance);
56 strcpy(szFileName, name);
57 strupr(szFileName);
58
59 //rename dll (os/2 -> win32) if necessary (i.e. OLE32OS2 -> OLE32)
60 Win32DllBase::renameDll(szFileName, FALSE);
61
62 name = strrchr(szFileName, '\\')+1;
63 strcpy(szModule, name);
64
65 char *dot = strrchr(szModule, '.');
66 if(dot)
67 *dot = 0;
68 }
69 else {
70 szModule[0] = 0;
71 this->hinstance = -1;
72 }
73}
74//******************************************************************************
75//******************************************************************************
76Win32ImageBase::~Win32ImageBase()
77{
78 Win32Resource *res;
79
80 while(winres)
81 {
82 res = winres->next;
83 delete(winres);
84 winres = res;
85 }
86 if(fullpath)
87 free(fullpath);
88}
89//******************************************************************************
90//******************************************************************************
91void Win32ImageBase::setFullPath(char *name)
92{
93 dassert(name, ("setFullPath, name == NULL"));
94 fullpath = (char *)malloc(strlen(name)+1);
95 dassert(fullpath, ("setFullPath, fullpath == NULL"));
96 strcpy(fullpath, name);
97}
98//******************************************************************************
99//Returns required OS version for this image
100//******************************************************************************
101ULONG Win32ImageBase::getVersion()
102{
103 dprintf(("Win32ImageBase::getVersion: NOT IMPLEMENTED!"));
104 return 0x40000; //NT 4
105}
106//******************************************************************************
107//******************************************************************************
108BOOL Win32ImageBase::isPEImage(char *szFileName)
109{
110 char modname[CCHMAXPATH];
111 char filename[CCHMAXPATH];
112 char *syspath;
113 HFILE dllfile;
114 IMAGE_FILE_HEADER fh;
115 HFILE win32handle;
116 ULONG ulAction = 0; /* Action taken by DosOpen */
117 ULONG ulLocal = 0; /* File pointer position after DosSetFilePtr */
118 APIRET rc = NO_ERROR; /* Return code */
119 LPVOID win32file = NULL;
120 ULONG ulRead;
121 int nSections, i;
122
123 strcpy(filename, szFileName);
124 strupr(filename);
125 if(!strchr(filename, (int)'.')) {
126 strcat(filename,".DLL");
127 }
128 dllfile = OSLibDosOpen(filename, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
129 if(dllfile == NULL) {//search in libpath for dll
130 strcpy(modname, kernel32Path);
131 strcat(modname, filename);
132 dllfile = OSLibDosOpen(modname, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
133 if(dllfile == NULL) {
134 OSLibDosSearchPath(OSLIB_SEARCHENV, "PATH", filename, filename, sizeof(filename));
135 }
136 else {
137 strcpy(filename, modname);
138 OSLibDosClose(dllfile);
139 }
140 }
141 else OSLibDosClose(dllfile);
142
143 rc = DosOpen(filename, /* File path name */
144 &win32handle, /* File handle */
145 &ulAction, /* Action taken */
146 0L, /* File primary allocation */
147 0L, /* File attribute */
148 OPEN_ACTION_FAIL_IF_NEW |
149 OPEN_ACTION_OPEN_IF_EXISTS, /* Open function type */
150 OPEN_FLAGS_NOINHERIT |
151 OPEN_SHARE_DENYNONE |
152 OPEN_ACCESS_READONLY, /* Open mode of the file */
153 0L); /* No extended attribute */
154
155 if (rc != NO_ERROR)
156 {
157 dprintf(("KERNEL32:Win32ImageBase::isPEImage(%s) failed with %u\n",
158 szFileName, rc));
159 return(FALSE);
160 }
161
162 /* Move the file pointer back to the beginning of the file */
163 DosSetFilePtr(win32handle, 0L, FILE_BEGIN, &ulLocal);
164
165 IMAGE_DOS_HEADER *pdoshdr = (IMAGE_DOS_HEADER *)malloc(sizeof(IMAGE_DOS_HEADER));
166 if(pdoshdr == NULL) {
167 DosClose(win32handle); /* Close the file */
168 return(FALSE);
169 }
170 rc = DosRead(win32handle, pdoshdr, sizeof(IMAGE_DOS_HEADER), &ulRead);
171 if(rc != NO_ERROR) {
172 DosClose(win32handle); /* Close the file */
173 return(FALSE);
174 }
175 ULONG hdrsize = pdoshdr->e_lfanew + SIZE_OF_NT_SIGNATURE + sizeof(IMAGE_FILE_HEADER);
176 free(pdoshdr);
177
178 /* Move the file pointer back to the beginning of the file */
179 DosSetFilePtr(win32handle, 0L, FILE_BEGIN, &ulLocal);
180
181 win32file = malloc(hdrsize);
182 if(win32file == NULL) {
183 DosClose(win32handle); /* Close the file */
184 return(FALSE);
185 }
186 rc = DosRead(win32handle, win32file, hdrsize, &ulRead);
187 if(rc != NO_ERROR) {
188 goto failure;
189 }
190
191 if(GetPEFileHeader (win32file, &fh) == FALSE) {
192 goto failure;
193 }
194
195 if(!(fh.Characteristics & IMAGE_FILE_EXECUTABLE_IMAGE)) {//not valid
196 goto failure;
197 }
198 if(fh.Machine != IMAGE_FILE_MACHINE_I386) {
199 goto failure;
200 }
201 //IMAGE_FILE_SYSTEM == only drivers (device/file system/video etc)?
202 if(fh.Characteristics & IMAGE_FILE_SYSTEM) {
203 goto failure;
204 }
205 DosClose(win32handle);
206 return(TRUE);
207
208failure:
209 free(win32file);
210 DosClose(win32handle);
211 return(FALSE);
212}
213//******************************************************************************
214//******************************************************************************
215/**
216 * Static helper which finds the Win32ImageBase object corresponding to hModule.
217 * @returns Pointer to Win32ImageBase object corresponding to hModule.
218 * @param hModule Odin32 modulehandler. 0 and -1 is aliases for the executable module
219 * @status completely implemented and tested.
220 * @author knut st. osmundsen
221 */
222Win32ImageBase * Win32ImageBase::findModule(HMODULE hModule)
223{
224 Win32ImageBase *pRet;
225
226 if (hModule == -1 || hModule == 0 || /* note: WinNt 4, SP4 don't accept -1 as the EXE handle.*/
227 (WinExe != NULL && hModule == WinExe->getInstanceHandle())
228 )
229 pRet = WinExe;
230 else
231 pRet = Win32DllBase::findModule(hModule);
232
233 if (pRet == NULL)
234 {
235 if (WinExe == NULL)
236 dprintf(("Win32ImageBase::findModule: Module not found. WinExe is NULL, hModule=%#x\n", hModule));
237 else
238 dprintf(("Win32ImageBase::findModule: Module not found, hModule=%#x\n", hModule));
239 }
240
241 return pRet;
242}
243
244
Note: See TracBrowser for help on using the repository browser.