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

Last change on this file since 1872 was 1872, checked in by bird, 26 years ago

Implemented EnumResourceNamesA/W.

File size: 7.1 KB
Line 
1/* $Id: winimagebase.cpp,v 1.5 1999-11-29 00:04:06 bird 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//******************************************************************************
100BOOL Win32ImageBase::isPEImage(char *szFileName)
101{
102 char modname[CCHMAXPATH];
103 char filename[CCHMAXPATH];
104 char *syspath;
105 HFILE dllfile;
106 IMAGE_FILE_HEADER fh;
107 HFILE win32handle;
108 ULONG ulAction = 0; /* Action taken by DosOpen */
109 ULONG ulLocal = 0; /* File pointer position after DosSetFilePtr */
110 APIRET rc = NO_ERROR; /* Return code */
111 LPVOID win32file = NULL;
112 ULONG ulRead;
113 int nSections, i;
114
115 strcpy(filename, szFileName);
116 strupr(filename);
117 if(!strchr(filename, (int)'.')) {
118 strcat(filename,".DLL");
119 }
120 dllfile = OSLibDosOpen(filename, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
121 if(dllfile == NULL) {//search in libpath for dll
122 strcpy(modname, kernel32Path);
123 strcat(modname, filename);
124 strcpy(filename, modname);
125 }
126 else OSLibDosClose(dllfile);
127
128 rc = DosOpen(filename, /* File path name */
129 &win32handle, /* File handle */
130 &ulAction, /* Action taken */
131 0L, /* File primary allocation */
132 0L, /* File attribute */
133 OPEN_ACTION_FAIL_IF_NEW |
134 OPEN_ACTION_OPEN_IF_EXISTS, /* Open function type */
135 OPEN_FLAGS_NOINHERIT |
136 OPEN_SHARE_DENYNONE |
137 OPEN_ACCESS_READONLY, /* Open mode of the file */
138 0L); /* No extended attribute */
139
140 if (rc != NO_ERROR)
141 {
142 dprintf(("KERNEL32:Win32ImageBase::isPEImage(%s) failed with %u\n",
143 szFileName, rc));
144 return(FALSE);
145 }
146
147 /* Move the file pointer back to the beginning of the file */
148 DosSetFilePtr(win32handle, 0L, FILE_BEGIN, &ulLocal);
149
150 IMAGE_DOS_HEADER *pdoshdr = (IMAGE_DOS_HEADER *)malloc(sizeof(IMAGE_DOS_HEADER));
151 if(pdoshdr == NULL) {
152 DosClose(win32handle); /* Close the file */
153 return(FALSE);
154 }
155 rc = DosRead(win32handle, pdoshdr, sizeof(IMAGE_DOS_HEADER), &ulRead);
156 if(rc != NO_ERROR) {
157 DosClose(win32handle); /* Close the file */
158 return(FALSE);
159 }
160 ULONG hdrsize = pdoshdr->e_lfanew + SIZE_OF_NT_SIGNATURE + sizeof(IMAGE_FILE_HEADER);
161 free(pdoshdr);
162
163 /* Move the file pointer back to the beginning of the file */
164 DosSetFilePtr(win32handle, 0L, FILE_BEGIN, &ulLocal);
165
166 win32file = malloc(hdrsize);
167 if(win32file == NULL) {
168 DosClose(win32handle); /* Close the file */
169 return(FALSE);
170 }
171 rc = DosRead(win32handle, win32file, hdrsize, &ulRead);
172 if(rc != NO_ERROR) {
173 goto failure;
174 }
175
176 if(GetPEFileHeader (win32file, &fh) == FALSE) {
177 goto failure;
178 }
179
180 if(!(fh.Characteristics & IMAGE_FILE_EXECUTABLE_IMAGE)) {//not valid
181 goto failure;
182 }
183 if(fh.Machine != IMAGE_FILE_MACHINE_I386) {
184 goto failure;
185 }
186 //IMAGE_FILE_SYSTEM == only drivers (device/file system/video etc)?
187 if(fh.Characteristics & IMAGE_FILE_SYSTEM) {
188 goto failure;
189 }
190 DosClose(win32handle);
191 return(TRUE);
192
193failure:
194 free(win32file);
195 DosClose(win32handle);
196 return(FALSE);
197}
198//******************************************************************************
199//******************************************************************************
200/**
201 * Static helper which finds the Win32ImageBase object corresponding to hModule.
202 * @returns Pointer to Win32ImageBase object corresponding to hModule.
203 * @param hModule Odin32 modulehandler. 0 and -1 is aliases for the executable module
204 * @status completely implemented and tested.
205 * @author knut st. osmundsen
206 */
207Win32ImageBase * Win32ImageBase::findModule(HMODULE hModule)
208{
209 Win32ImageBase *pRet;
210
211 if (hModule == -1 || hModule == 0 || /* note: WinNt 4, SP4 don't accept -1 as the EXE handle.*/
212 (WinExe != NULL && hModule == WinExe->getInstanceHandle())
213 )
214 pRet = WinExe;
215 else
216 pRet = Win32DllBase::findModule(hModule);
217
218 if (pRet == NULL)
219 {
220 if (WinExe == NULL)
221 dprintf(("Win32ImageBase::findModule: Module not found. WinExe is NULL, hModule=%#x\n", hModule));
222 else
223 dprintf(("Win32ImageBase::findModule: Module not found, hModule=%#x\n", hModule));
224 }
225
226 return pRet;
227}
228
229
Note: See TracBrowser for help on using the repository browser.