4 * Copyright 1998 Patrik Stridvall
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(imagehlp);
34 /***********************************************************************
38 static PLOADED_IMAGE IMAGEHLP_pFirstLoadedImage=NULL;
39 static PLOADED_IMAGE IMAGEHLP_pLastLoadedImage=NULL;
41 static LOADED_IMAGE IMAGEHLP_EmptyLoadedImage = {
42 NULL, /* ModuleName */
44 NULL, /* MappedAddress */
45 NULL, /* FileHeader */
46 NULL, /* LastRvaSection */
47 0, /* NumberOfSections */
49 1, /* Characteristics */
50 FALSE, /* fSystemImage */
51 FALSE, /* fDOSImage */
52 { &IMAGEHLP_EmptyLoadedImage.Links, &IMAGEHLP_EmptyLoadedImage.Links }, /* Links */
53 148, /* SizeOfImage; */
56 extern HANDLE IMAGEHLP_hHeap;
58 /***********************************************************************
59 * GetImageConfigInformation (IMAGEHLP.@)
61 BOOL WINAPI GetImageConfigInformation(
62 PLOADED_IMAGE LoadedImage,
63 PIMAGE_LOAD_CONFIG_DIRECTORY ImageConfigInformation)
65 FIXME("(%p, %p): stub\n",
66 LoadedImage, ImageConfigInformation
68 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
72 /***********************************************************************
73 * GetImageUnusedHeaderBytes (IMAGEHLP.@)
75 DWORD WINAPI GetImageUnusedHeaderBytes(
76 PLOADED_IMAGE LoadedImage,
77 LPDWORD SizeUnusedHeaderBytes)
79 FIXME("(%p, %p): stub\n",
80 LoadedImage, SizeUnusedHeaderBytes
82 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
86 /***********************************************************************
87 * ImageLoad (IMAGEHLP.@)
89 PLOADED_IMAGE WINAPI ImageLoad(LPSTR DllName, LPSTR DllPath)
91 PLOADED_IMAGE pLoadedImage;
93 FIXME("(%s, %s): stub\n", DllName, DllPath);
95 pLoadedImage = HeapAlloc(IMAGEHLP_hHeap, 0, sizeof(LOADED_IMAGE));
97 pLoadedImage->FileHeader = HeapAlloc(IMAGEHLP_hHeap, 0, sizeof(IMAGE_NT_HEADERS));
102 /***********************************************************************
103 * ImageUnload (IMAGEHLP.@)
105 BOOL WINAPI ImageUnload(PLOADED_IMAGE pLoadedImage)
107 LIST_ENTRY *pCurrent, *pFind;
109 TRACE("(%p)\n", pLoadedImage);
111 if(!IMAGEHLP_pFirstLoadedImage || !pLoadedImage)
113 /* No image loaded or null pointer */
114 SetLastError(ERROR_INVALID_PARAMETER);
118 pFind=&pLoadedImage->Links;
119 pCurrent=&IMAGEHLP_pFirstLoadedImage->Links;
120 while((pCurrent != pFind) &&
122 pCurrent = pCurrent->Flink;
126 SetLastError(ERROR_INVALID_PARAMETER);
131 pCurrent->Blink->Flink = pCurrent->Flink;
133 IMAGEHLP_pFirstLoadedImage = pCurrent->Flink?CONTAINING_RECORD(
134 pCurrent->Flink, LOADED_IMAGE, Links):NULL;
137 pCurrent->Flink->Blink = pCurrent->Blink;
139 IMAGEHLP_pLastLoadedImage = pCurrent->Blink?CONTAINING_RECORD(
140 pCurrent->Blink, LOADED_IMAGE, Links):NULL;
145 /***********************************************************************
146 * MapAndLoad (IMAGEHLP.@)
148 BOOL WINAPI MapAndLoad(LPSTR pszImageName, LPSTR pszDllPath, PLOADED_IMAGE pLoadedImage,
149 BOOL bDotDll, BOOL bReadOnly)
151 CHAR szFileName[MAX_PATH];
152 HANDLE hFile = INVALID_HANDLE_VALUE;
153 HANDLE hFileMapping = NULL;
154 PVOID mapping = NULL;
155 PIMAGE_NT_HEADERS pNtHeader = NULL;
157 TRACE("(%s, %s, %p, %d, %d)\n",
158 pszImageName, pszDllPath, pLoadedImage, bDotDll, bReadOnly);
160 if (!SearchPathA(pszDllPath, pszImageName, bDotDll ? ".DLL" : ".EXE",
161 sizeof(szFileName), szFileName, NULL))
163 SetLastError(ERROR_FILE_NOT_FOUND);
167 hFile = CreateFileA(szFileName,
168 GENERIC_READ | (bReadOnly ? 0 : GENERIC_WRITE),
170 NULL, OPEN_EXISTING, 0, NULL);
171 if (hFile == INVALID_HANDLE_VALUE)
173 WARN("CreateFile: Error = %d\n", GetLastError());
177 hFileMapping = CreateFileMappingA(hFile, NULL,
178 (bReadOnly ? PAGE_READONLY : PAGE_READWRITE) | SEC_COMMIT,
182 WARN("CreateFileMapping: Error = %d\n", GetLastError());
186 mapping = MapViewOfFile(hFileMapping, bReadOnly ? FILE_MAP_READ : FILE_MAP_WRITE, 0, 0, 0);
187 CloseHandle(hFileMapping);
190 WARN("MapViewOfFile: Error = %d\n", GetLastError());
194 if (!(pNtHeader = RtlImageNtHeader(mapping)))
196 WARN("Not an NT header\n");
197 UnmapViewOfFile(mapping);
201 pLoadedImage->ModuleName = HeapAlloc(GetProcessHeap(), 0,
202 strlen(szFileName) + 1);
203 if (pLoadedImage->ModuleName) strcpy(pLoadedImage->ModuleName, szFileName);
204 pLoadedImage->hFile = hFile;
205 pLoadedImage->MappedAddress = mapping;
206 pLoadedImage->FileHeader = pNtHeader;
207 pLoadedImage->Sections = (PIMAGE_SECTION_HEADER)
208 ((LPBYTE) &pNtHeader->OptionalHeader +
209 pNtHeader->FileHeader.SizeOfOptionalHeader);
210 pLoadedImage->NumberOfSections = pNtHeader->FileHeader.NumberOfSections;
211 pLoadedImage->SizeOfImage = GetFileSize(hFile, NULL);
212 pLoadedImage->Characteristics = pNtHeader->FileHeader.Characteristics;
213 pLoadedImage->LastRvaSection = pLoadedImage->Sections;
215 pLoadedImage->fSystemImage = FALSE; /* FIXME */
216 pLoadedImage->fDOSImage = FALSE; /* FIXME */
218 pLoadedImage->Links.Flink = &pLoadedImage->Links;
219 pLoadedImage->Links.Blink = &pLoadedImage->Links;
224 if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
228 /***********************************************************************
229 * SetImageConfigInformation (IMAGEHLP.@)
231 BOOL WINAPI SetImageConfigInformation(
232 PLOADED_IMAGE LoadedImage,
233 PIMAGE_LOAD_CONFIG_DIRECTORY ImageConfigInformation)
235 FIXME("(%p, %p): stub\n",
236 LoadedImage, ImageConfigInformation
238 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
242 /***********************************************************************
243 * UnMapAndLoad (IMAGEHLP.@)
245 BOOL WINAPI UnMapAndLoad(PLOADED_IMAGE pLoadedImage)
247 HeapFree(GetProcessHeap(), 0, pLoadedImage->ModuleName);
248 /* FIXME: MSDN states that a new checksum is computed and stored into the file */
249 if (pLoadedImage->MappedAddress) UnmapViewOfFile(pLoadedImage->MappedAddress);
250 if (pLoadedImage->hFile != INVALID_HANDLE_VALUE) CloseHandle(pLoadedImage->hFile);