Release 1.3.7.
[wine/gsoc-2012-control.git] / dlls / imagehlp / access.c
blob009d6f724697007435607fe458d864c7d3ea2906
1 /*
2 * IMAGEHLP library
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
21 #include <stdarg.h>
22 #include <string.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winnt.h"
26 #include "winternl.h"
27 #include "winerror.h"
28 #include "wine/debug.h"
29 #include "imagehlp.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(imagehlp);
33 /***********************************************************************
34 * Data
37 static PLOADED_IMAGE IMAGEHLP_pFirstLoadedImage=NULL;
38 static PLOADED_IMAGE IMAGEHLP_pLastLoadedImage=NULL;
40 static LOADED_IMAGE IMAGEHLP_EmptyLoadedImage = {
41 NULL, /* ModuleName */
42 0, /* hFile */
43 NULL, /* MappedAddress */
44 NULL, /* FileHeader */
45 NULL, /* LastRvaSection */
46 0, /* NumberOfSections */
47 NULL, /* Sections */
48 1, /* Characteristics */
49 FALSE, /* fSystemImage */
50 FALSE, /* fDOSImage */
51 FALSE, /* fReadOnly */
52 0, /* Version */
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);
70 return FALSE;
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);
84 return 0;
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));
97 if (pLoadedImage)
98 pLoadedImage->FileHeader = HeapAlloc(IMAGEHLP_hHeap, 0, sizeof(IMAGE_NT_HEADERS));
100 return pLoadedImage;
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);
116 return FALSE;
119 pFind=&pLoadedImage->Links;
120 pCurrent=&IMAGEHLP_pFirstLoadedImage->Links;
121 while((pCurrent != pFind) &&
122 (pCurrent != NULL))
123 pCurrent = pCurrent->Flink;
124 if(!pCurrent)
126 /* Not found */
127 SetLastError(ERROR_INVALID_PARAMETER);
128 return FALSE;
131 if(pCurrent->Blink)
132 pCurrent->Blink->Flink = pCurrent->Flink;
133 else
134 IMAGEHLP_pFirstLoadedImage = pCurrent->Flink?CONTAINING_RECORD(
135 pCurrent->Flink, LOADED_IMAGE, Links):NULL;
137 if(pCurrent->Flink)
138 pCurrent->Flink->Blink = pCurrent->Blink;
139 else
140 IMAGEHLP_pLastLoadedImage = pCurrent->Blink?CONTAINING_RECORD(
141 pCurrent->Blink, LOADED_IMAGE, Links):NULL;
143 return FALSE;
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);
165 goto Error;
168 hFile = CreateFileA(szFileName,
169 GENERIC_READ | (bReadOnly ? 0 : GENERIC_WRITE),
170 FILE_SHARE_READ,
171 NULL, OPEN_EXISTING, 0, NULL);
172 if (hFile == INVALID_HANDLE_VALUE)
174 WARN("CreateFile: Error = %d\n", GetLastError());
175 goto Error;
178 hFileMapping = CreateFileMappingA(hFile, NULL,
179 (bReadOnly ? PAGE_READONLY : PAGE_READWRITE) | SEC_COMMIT,
180 0, 0, NULL);
181 if (!hFileMapping)
183 WARN("CreateFileMapping: Error = %d\n", GetLastError());
184 goto Error;
187 mapping = MapViewOfFile(hFileMapping, bReadOnly ? FILE_MAP_READ : FILE_MAP_WRITE, 0, 0, 0);
188 CloseHandle(hFileMapping);
189 if (!mapping)
191 WARN("MapViewOfFile: Error = %d\n", GetLastError());
192 goto Error;
195 if (!(pNtHeader = RtlImageNtHeader(mapping)))
197 WARN("Not an NT header\n");
198 UnmapViewOfFile(mapping);
199 goto Error;
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;
222 return TRUE;
224 Error:
225 if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
226 return FALSE;
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);
240 return FALSE;
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);
252 return TRUE;