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
28 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(imagehlp);
33 /***********************************************************************
37 static PLOADED_IMAGE IMAGEHLP_pFirstLoadedImage=NULL;
38 static PLOADED_IMAGE IMAGEHLP_pLastLoadedImage=NULL;
40 static LOADED_IMAGE IMAGEHLP_EmptyLoadedImage = {
41 NULL, /* ModuleName */
43 NULL, /* MappedAddress */
44 NULL, /* FileHeader */
45 NULL, /* LastRvaSection */
46 0, /* NumberOfSections */
48 1, /* Characteristics */
49 FALSE, /* fSystemImage */
50 FALSE, /* fDOSImage */
51 FALSE, /* fReadOnly */
53 { &IMAGEHLP_EmptyLoadedImage.Links, &IMAGEHLP_EmptyLoadedImage.Links }, /* Links */
54 148, /* SizeOfImage; */
57 extern HANDLE IMAGEHLP_hHeap;
59 /***********************************************************************
60 * GetImageConfigInformation (IMAGEHLP.@)
62 BOOL WINAPI GetImageConfigInformation(
63 PLOADED_IMAGE LoadedImage,
64 PIMAGE_LOAD_CONFIG_DIRECTORY ImageConfigInformation)
66 FIXME("(%p, %p): stub\n",
67 LoadedImage, ImageConfigInformation
69 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
73 /***********************************************************************
74 * GetImageUnusedHeaderBytes (IMAGEHLP.@)
76 DWORD WINAPI GetImageUnusedHeaderBytes(
77 PLOADED_IMAGE LoadedImage,
78 LPDWORD SizeUnusedHeaderBytes)
80 FIXME("(%p, %p): stub\n",
81 LoadedImage, SizeUnusedHeaderBytes
83 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
87 /***********************************************************************
88 * ImageLoad (IMAGEHLP.@)
90 PLOADED_IMAGE WINAPI ImageLoad(PCSTR DllName, PCSTR DllPath)
92 PLOADED_IMAGE pLoadedImage;
94 FIXME("(%s, %s): stub\n", DllName, DllPath);
96 pLoadedImage = HeapAlloc(IMAGEHLP_hHeap, 0, sizeof(LOADED_IMAGE));
98 pLoadedImage->FileHeader = HeapAlloc(IMAGEHLP_hHeap, 0, sizeof(IMAGE_NT_HEADERS));
103 /***********************************************************************
104 * ImageUnload (IMAGEHLP.@)
106 BOOL WINAPI ImageUnload(PLOADED_IMAGE pLoadedImage)
108 LIST_ENTRY *pCurrent, *pFind;
110 TRACE("(%p)\n", pLoadedImage);
112 if(!IMAGEHLP_pFirstLoadedImage || !pLoadedImage)
114 /* No image loaded or null pointer */
115 SetLastError(ERROR_INVALID_PARAMETER);
119 pFind=&pLoadedImage->Links;
120 pCurrent=&IMAGEHLP_pFirstLoadedImage->Links;
121 while((pCurrent != pFind) &&
123 pCurrent = pCurrent->Flink;
127 SetLastError(ERROR_INVALID_PARAMETER);
132 pCurrent->Blink->Flink = pCurrent->Flink;
134 IMAGEHLP_pFirstLoadedImage = pCurrent->Flink?CONTAINING_RECORD(
135 pCurrent->Flink, LOADED_IMAGE, Links):NULL;
138 pCurrent->Flink->Blink = pCurrent->Blink;
140 IMAGEHLP_pLastLoadedImage = pCurrent->Blink?CONTAINING_RECORD(
141 pCurrent->Blink, LOADED_IMAGE, Links):NULL;
146 /***********************************************************************
147 * MapAndLoad (IMAGEHLP.@)
149 BOOL WINAPI MapAndLoad(PCSTR pszImageName, PCSTR pszDllPath, PLOADED_IMAGE pLoadedImage,
150 BOOL bDotDll, BOOL bReadOnly)
152 CHAR szFileName[MAX_PATH];
153 HANDLE hFile = INVALID_HANDLE_VALUE;
154 HANDLE hFileMapping = NULL;
155 PVOID mapping = NULL;
156 PIMAGE_NT_HEADERS pNtHeader = NULL;
158 TRACE("(%s, %s, %p, %d, %d)\n",
159 pszImageName, pszDllPath, pLoadedImage, bDotDll, bReadOnly);
161 if (!SearchPathA(pszDllPath, pszImageName, bDotDll ? ".DLL" : ".EXE",
162 sizeof(szFileName), szFileName, NULL))
164 SetLastError(ERROR_FILE_NOT_FOUND);
168 hFile = CreateFileA(szFileName,
169 GENERIC_READ | (bReadOnly ? 0 : GENERIC_WRITE),
171 NULL, OPEN_EXISTING, 0, NULL);
172 if (hFile == INVALID_HANDLE_VALUE)
174 WARN("CreateFile: Error = %d\n", GetLastError());
178 hFileMapping = CreateFileMappingA(hFile, NULL,
179 (bReadOnly ? PAGE_READONLY : PAGE_READWRITE) | SEC_COMMIT,
183 WARN("CreateFileMapping: Error = %d\n", GetLastError());
187 mapping = MapViewOfFile(hFileMapping, bReadOnly ? FILE_MAP_READ : FILE_MAP_WRITE, 0, 0, 0);
188 CloseHandle(hFileMapping);
191 WARN("MapViewOfFile: Error = %d\n", GetLastError());
195 if (!(pNtHeader = RtlImageNtHeader(mapping)))
197 WARN("Not an NT header\n");
198 UnmapViewOfFile(mapping);
202 pLoadedImage->ModuleName = HeapAlloc(GetProcessHeap(), 0,
203 strlen(szFileName) + 1);
204 if (pLoadedImage->ModuleName) strcpy(pLoadedImage->ModuleName, szFileName);
205 pLoadedImage->hFile = hFile;
206 pLoadedImage->MappedAddress = mapping;
207 pLoadedImage->FileHeader = pNtHeader;
208 pLoadedImage->Sections = (PIMAGE_SECTION_HEADER)
209 ((LPBYTE) &pNtHeader->OptionalHeader +
210 pNtHeader->FileHeader.SizeOfOptionalHeader);
211 pLoadedImage->NumberOfSections = pNtHeader->FileHeader.NumberOfSections;
212 pLoadedImage->SizeOfImage = GetFileSize(hFile, NULL);
213 pLoadedImage->Characteristics = pNtHeader->FileHeader.Characteristics;
214 pLoadedImage->LastRvaSection = pLoadedImage->Sections;
216 pLoadedImage->fSystemImage = FALSE; /* FIXME */
217 pLoadedImage->fDOSImage = FALSE; /* FIXME */
219 pLoadedImage->Links.Flink = &pLoadedImage->Links;
220 pLoadedImage->Links.Blink = &pLoadedImage->Links;
225 if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
229 /***********************************************************************
230 * SetImageConfigInformation (IMAGEHLP.@)
232 BOOL WINAPI SetImageConfigInformation(
233 PLOADED_IMAGE LoadedImage,
234 PIMAGE_LOAD_CONFIG_DIRECTORY ImageConfigInformation)
236 FIXME("(%p, %p): stub\n",
237 LoadedImage, ImageConfigInformation
239 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
243 /***********************************************************************
244 * UnMapAndLoad (IMAGEHLP.@)
246 BOOL WINAPI UnMapAndLoad(PLOADED_IMAGE pLoadedImage)
248 HeapFree(GetProcessHeap(), 0, pLoadedImage->ModuleName);
249 /* FIXME: MSDN states that a new checksum is computed and stored into the file */
250 if (pLoadedImage->MappedAddress) UnmapViewOfFile(pLoadedImage->MappedAddress);
251 if (pLoadedImage->hFile != INVALID_HANDLE_VALUE) CloseHandle(pLoadedImage->hFile);