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 { &IMAGEHLP_EmptyLoadedImage
.Links
, &IMAGEHLP_EmptyLoadedImage
.Links
}, /* Links */
52 148, /* SizeOfImage; */
55 extern HANDLE IMAGEHLP_hHeap
;
57 /***********************************************************************
58 * GetImageConfigInformation (IMAGEHLP.@)
60 BOOL WINAPI
GetImageConfigInformation(
61 PLOADED_IMAGE LoadedImage
,
62 PIMAGE_LOAD_CONFIG_DIRECTORY ImageConfigInformation
)
64 FIXME("(%p, %p): stub\n",
65 LoadedImage
, ImageConfigInformation
67 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
71 /***********************************************************************
72 * GetImageUnusedHeaderBytes (IMAGEHLP.@)
74 DWORD WINAPI
GetImageUnusedHeaderBytes(
75 PLOADED_IMAGE LoadedImage
,
76 LPDWORD SizeUnusedHeaderBytes
)
78 FIXME("(%p, %p): stub\n",
79 LoadedImage
, SizeUnusedHeaderBytes
81 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
85 /***********************************************************************
86 * ImageLoad (IMAGEHLP.@)
88 PLOADED_IMAGE WINAPI
ImageLoad(LPSTR DllName
, LPSTR DllPath
)
90 PLOADED_IMAGE pLoadedImage
;
92 FIXME("(%s, %s): stub\n", DllName
, DllPath
);
94 pLoadedImage
= HeapAlloc(IMAGEHLP_hHeap
, 0, sizeof(LOADED_IMAGE
));
96 pLoadedImage
->FileHeader
= HeapAlloc(IMAGEHLP_hHeap
, 0, sizeof(IMAGE_NT_HEADERS
));
101 /***********************************************************************
102 * ImageUnload (IMAGEHLP.@)
104 BOOL WINAPI
ImageUnload(PLOADED_IMAGE pLoadedImage
)
106 LIST_ENTRY
*pCurrent
, *pFind
;
108 TRACE("(%p)\n", pLoadedImage
);
110 if(!IMAGEHLP_pFirstLoadedImage
|| !pLoadedImage
)
112 /* No image loaded or null pointer */
113 SetLastError(ERROR_INVALID_PARAMETER
);
117 pFind
=&pLoadedImage
->Links
;
118 pCurrent
=&IMAGEHLP_pFirstLoadedImage
->Links
;
119 while((pCurrent
!= pFind
) &&
121 pCurrent
= pCurrent
->Flink
;
125 SetLastError(ERROR_INVALID_PARAMETER
);
130 pCurrent
->Blink
->Flink
= pCurrent
->Flink
;
132 IMAGEHLP_pFirstLoadedImage
= pCurrent
->Flink
?CONTAINING_RECORD(
133 pCurrent
->Flink
, LOADED_IMAGE
, Links
):NULL
;
136 pCurrent
->Flink
->Blink
= pCurrent
->Blink
;
138 IMAGEHLP_pLastLoadedImage
= pCurrent
->Blink
?CONTAINING_RECORD(
139 pCurrent
->Blink
, LOADED_IMAGE
, Links
):NULL
;
144 /***********************************************************************
145 * MapAndLoad (IMAGEHLP.@)
147 BOOL WINAPI
MapAndLoad(LPSTR pszImageName
, LPSTR pszDllPath
, PLOADED_IMAGE pLoadedImage
,
148 BOOL bDotDll
, BOOL bReadOnly
)
150 CHAR szFileName
[MAX_PATH
];
151 HANDLE hFile
= INVALID_HANDLE_VALUE
;
152 HANDLE hFileMapping
= NULL
;
153 PVOID mapping
= NULL
;
154 PIMAGE_NT_HEADERS pNtHeader
= NULL
;
156 TRACE("(%s, %s, %p, %d, %d)\n",
157 pszImageName
, pszDllPath
, pLoadedImage
, bDotDll
, bReadOnly
);
159 if (!SearchPathA(pszDllPath
, pszImageName
, bDotDll
? ".DLL" : ".EXE",
160 sizeof(szFileName
), szFileName
, NULL
))
162 SetLastError(ERROR_FILE_NOT_FOUND
);
166 hFile
= CreateFileA(szFileName
,
167 GENERIC_READ
| (bReadOnly
? 0 : GENERIC_WRITE
),
169 NULL
, OPEN_EXISTING
, 0, NULL
);
170 if (hFile
== INVALID_HANDLE_VALUE
)
172 WARN("CreateFile: Error = %d\n", GetLastError());
176 hFileMapping
= CreateFileMappingA(hFile
, NULL
,
177 (bReadOnly
? PAGE_READONLY
: PAGE_READWRITE
) | SEC_COMMIT
,
181 WARN("CreateFileMapping: Error = %d\n", GetLastError());
185 mapping
= MapViewOfFile(hFileMapping
, bReadOnly
? FILE_MAP_READ
: FILE_MAP_WRITE
, 0, 0, 0);
186 CloseHandle(hFileMapping
);
189 WARN("MapViewOfFile: Error = %d\n", GetLastError());
193 if (!(pNtHeader
= RtlImageNtHeader(mapping
)))
195 WARN("Not an NT header\n");
196 UnmapViewOfFile(mapping
);
200 pLoadedImage
->ModuleName
= HeapAlloc(GetProcessHeap(), 0,
201 strlen(szFileName
) + 1);
202 if (pLoadedImage
->ModuleName
) strcpy(pLoadedImage
->ModuleName
, szFileName
);
203 pLoadedImage
->hFile
= hFile
;
204 pLoadedImage
->MappedAddress
= mapping
;
205 pLoadedImage
->FileHeader
= pNtHeader
;
206 pLoadedImage
->Sections
= (PIMAGE_SECTION_HEADER
)
207 ((LPBYTE
) &pNtHeader
->OptionalHeader
+
208 pNtHeader
->FileHeader
.SizeOfOptionalHeader
);
209 pLoadedImage
->NumberOfSections
= pNtHeader
->FileHeader
.NumberOfSections
;
210 pLoadedImage
->SizeOfImage
= GetFileSize(hFile
, NULL
);
211 pLoadedImage
->Characteristics
= pNtHeader
->FileHeader
.Characteristics
;
212 pLoadedImage
->LastRvaSection
= pLoadedImage
->Sections
;
214 pLoadedImage
->fSystemImage
= FALSE
; /* FIXME */
215 pLoadedImage
->fDOSImage
= FALSE
; /* FIXME */
217 pLoadedImage
->Links
.Flink
= &pLoadedImage
->Links
;
218 pLoadedImage
->Links
.Blink
= &pLoadedImage
->Links
;
223 if (hFile
!= INVALID_HANDLE_VALUE
) CloseHandle(hFile
);
227 /***********************************************************************
228 * SetImageConfigInformation (IMAGEHLP.@)
230 BOOL WINAPI
SetImageConfigInformation(
231 PLOADED_IMAGE LoadedImage
,
232 PIMAGE_LOAD_CONFIG_DIRECTORY ImageConfigInformation
)
234 FIXME("(%p, %p): stub\n",
235 LoadedImage
, ImageConfigInformation
237 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
241 /***********************************************************************
242 * UnMapAndLoad (IMAGEHLP.@)
244 BOOL WINAPI
UnMapAndLoad(PLOADED_IMAGE pLoadedImage
)
246 HeapFree(GetProcessHeap(), 0, pLoadedImage
->ModuleName
);
247 /* FIXME: MSDN states that a new checksum is computed and stored into the file */
248 if (pLoadedImage
->MappedAddress
) UnmapViewOfFile(pLoadedImage
->MappedAddress
);
249 if (pLoadedImage
->hFile
!= INVALID_HANDLE_VALUE
) CloseHandle(pLoadedImage
->hFile
);