Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Modules |
| 3 | * |
| 4 | * Copyright 1995 Alexandre Julliard |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 5 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
Patrik Stridvall | d016f81 | 2002-08-17 00:43:16 +0000 | [diff] [blame] | 21 | #include "config.h" |
Patrik Stridvall | 9aab47e | 2002-08-28 23:42:34 +0000 | [diff] [blame] | 22 | #include "wine/port.h" |
Patrik Stridvall | d016f81 | 2002-08-17 00:43:16 +0000 | [diff] [blame] | 23 | |
Alexandre Julliard | a0b2b1d | 1997-11-16 17:38:29 +0000 | [diff] [blame] | 24 | #include <assert.h> |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 25 | #include <fcntl.h> |
| 26 | #include <stdlib.h> |
Jeremy White | d3e22d9 | 2000-02-10 19:03:02 +0000 | [diff] [blame] | 27 | #include <stdio.h> |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 28 | #include <string.h> |
| 29 | #include <sys/types.h> |
Patrik Stridvall | d016f81 | 2002-08-17 00:43:16 +0000 | [diff] [blame] | 30 | #ifdef HAVE_UNISTD_H |
| 31 | # include <unistd.h> |
| 32 | #endif |
Jeremy White | d3e22d9 | 2000-02-10 19:03:02 +0000 | [diff] [blame] | 33 | #include "wine/winbase16.h" |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 34 | #include "winerror.h" |
Patrik Stridvall | 9c1de6d | 2002-09-12 22:07:02 +0000 | [diff] [blame] | 35 | #include "winternl.h" |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 36 | #include "heap.h" |
Aric Stewart | e4d0932 | 2000-12-03 03:14:29 +0000 | [diff] [blame] | 37 | #include "file.h" |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 38 | #include "module.h" |
Patrik Stridvall | cf07e10 | 2002-10-23 20:20:59 +0000 | [diff] [blame] | 39 | |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 40 | #include "wine/debug.h" |
Patrik Stridvall | cf07e10 | 2002-10-23 20:20:59 +0000 | [diff] [blame] | 41 | #include "wine/unicode.h" |
Alexandre Julliard | 37e9503 | 2001-07-19 00:39:09 +0000 | [diff] [blame] | 42 | #include "wine/server.h" |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 43 | |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 44 | WINE_DEFAULT_DEBUG_CHANNEL(module); |
| 45 | WINE_DECLARE_DEBUG_CHANNEL(win32); |
| 46 | WINE_DECLARE_DEBUG_CHANNEL(loaddll); |
Patrik Stridvall | b4b9fae | 1999-04-19 14:56:29 +0000 | [diff] [blame] | 47 | |
Alexandre Julliard | 03e4ea1 | 2003-03-18 20:07:12 +0000 | [diff] [blame] | 48 | inline static HMODULE get_exe_module(void) |
| 49 | { |
Alexandre Julliard | 5980477 | 2003-04-05 05:13:33 +0000 | [diff] [blame] | 50 | HANDLE *pdb = (HANDLE *)NtCurrentTeb()->process; |
| 51 | return pdb[0x08 / sizeof(HANDLE)]; /* get dword at offset 0x08 in pdb */ |
Alexandre Julliard | 03e4ea1 | 2003-03-18 20:07:12 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Alexandre Julliard | 5edf4e1 | 2001-07-26 20:12:54 +0000 | [diff] [blame] | 54 | /*********************************************************************** |
| 55 | * wait_input_idle |
| 56 | * |
| 57 | * Wrapper to call WaitForInputIdle USER function |
| 58 | */ |
| 59 | typedef DWORD (WINAPI *WaitForInputIdle_ptr)( HANDLE hProcess, DWORD dwTimeOut ); |
| 60 | |
| 61 | static DWORD wait_input_idle( HANDLE process, DWORD timeout ) |
| 62 | { |
| 63 | HMODULE mod = GetModuleHandleA( "user32.dll" ); |
| 64 | if (mod) |
| 65 | { |
| 66 | WaitForInputIdle_ptr ptr = (WaitForInputIdle_ptr)GetProcAddress( mod, "WaitForInputIdle" ); |
| 67 | if (ptr) return ptr( process, timeout ); |
| 68 | } |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | |
Ulrich Weigand | e469a58 | 1999-03-27 16:45:57 +0000 | [diff] [blame] | 73 | /**************************************************************************** |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 74 | * DisableThreadLibraryCalls (KERNEL32.@) |
Ulrich Weigand | e469a58 | 1999-03-27 16:45:57 +0000 | [diff] [blame] | 75 | * |
| 76 | * Don't call DllEntryPoint for DLL_THREAD_{ATTACH,DETACH} if set. |
| 77 | */ |
| 78 | BOOL WINAPI DisableThreadLibraryCalls( HMODULE hModule ) |
| 79 | { |
Eric Pouech | 7d60964 | 2003-03-04 04:36:56 +0000 | [diff] [blame] | 80 | NTSTATUS nts = LdrDisableThreadCalloutsForDll( hModule ); |
| 81 | if (nts == STATUS_SUCCESS) return TRUE; |
Ulrich Weigand | e469a58 | 1999-03-27 16:45:57 +0000 | [diff] [blame] | 82 | |
Eric Pouech | 7d60964 | 2003-03-04 04:36:56 +0000 | [diff] [blame] | 83 | SetLastError( RtlNtStatusToDosError( nts ) ); |
| 84 | return FALSE; |
Ulrich Weigand | e469a58 | 1999-03-27 16:45:57 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Alexandre Julliard | a2f2e01 | 1995-06-06 16:40:35 +0000 | [diff] [blame] | 87 | |
| 88 | /*********************************************************************** |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 89 | * MODULE_CreateDummyModule |
| 90 | * |
| 91 | * Create a dummy NE module for Win32 or Winelib. |
| 92 | */ |
Alexandre Julliard | 8ff37b8 | 2001-06-06 20:24:12 +0000 | [diff] [blame] | 93 | HMODULE16 MODULE_CreateDummyModule( LPCSTR filename, HMODULE module32 ) |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 94 | { |
Alexandre Julliard | 8ff37b8 | 2001-06-06 20:24:12 +0000 | [diff] [blame] | 95 | HMODULE16 hModule; |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 96 | NE_MODULE *pModule; |
| 97 | SEGTABLEENTRY *pSegment; |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 98 | char *pStr,*s; |
Francois Gouget | baa9bf9 | 1999-12-27 05:24:06 +0000 | [diff] [blame] | 99 | unsigned int len; |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 100 | const char* basename; |
Ulrich Weigand | 237e8e9 | 1999-12-04 04:04:58 +0000 | [diff] [blame] | 101 | OFSTRUCT *ofs; |
| 102 | int of_size, size; |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 103 | |
Ulrich Weigand | 237e8e9 | 1999-12-04 04:04:58 +0000 | [diff] [blame] | 104 | /* Extract base filename */ |
| 105 | basename = strrchr(filename, '\\'); |
| 106 | if (!basename) basename = filename; |
| 107 | else basename++; |
| 108 | len = strlen(basename); |
| 109 | if ((s = strchr(basename, '.'))) len = s - basename; |
| 110 | |
| 111 | /* Allocate module */ |
| 112 | of_size = sizeof(OFSTRUCT) - sizeof(ofs->szPathName) |
| 113 | + strlen(filename) + 1; |
| 114 | size = sizeof(NE_MODULE) + |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 115 | /* loaded file info */ |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 116 | ((of_size + 3) & ~3) + |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 117 | /* segment table: DS,CS */ |
| 118 | 2 * sizeof(SEGTABLEENTRY) + |
| 119 | /* name table */ |
Ulrich Weigand | 237e8e9 | 1999-12-04 04:04:58 +0000 | [diff] [blame] | 120 | len + 2 + |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 121 | /* several empty tables */ |
| 122 | 8; |
| 123 | |
| 124 | hModule = GlobalAlloc16( GMEM_MOVEABLE | GMEM_ZEROINIT, size ); |
Alexandre Julliard | 8ff37b8 | 2001-06-06 20:24:12 +0000 | [diff] [blame] | 125 | if (!hModule) return (HMODULE16)11; /* invalid exe */ |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 126 | |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 127 | FarSetOwner16( hModule, hModule ); |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 128 | pModule = (NE_MODULE *)GlobalLock16( hModule ); |
| 129 | |
| 130 | /* Set all used entries */ |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 131 | pModule->magic = IMAGE_OS2_SIGNATURE; |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 132 | pModule->count = 1; |
| 133 | pModule->next = 0; |
| 134 | pModule->flags = 0; |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 135 | pModule->dgroup = 0; |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 136 | pModule->ss = 1; |
| 137 | pModule->cs = 2; |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 138 | pModule->heap_size = 0; |
| 139 | pModule->stack_size = 0; |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 140 | pModule->seg_count = 2; |
| 141 | pModule->modref_count = 0; |
| 142 | pModule->nrname_size = 0; |
| 143 | pModule->fileinfo = sizeof(NE_MODULE); |
| 144 | pModule->os_flags = NE_OSFLAGS_WINDOWS; |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 145 | pModule->self = hModule; |
Alexandre Julliard | b445952 | 2000-04-15 21:00:55 +0000 | [diff] [blame] | 146 | pModule->module32 = module32; |
| 147 | |
| 148 | /* Set version and flags */ |
| 149 | if (module32) |
| 150 | { |
Alexandre Julliard | a5dea21 | 2002-08-09 19:57:38 +0000 | [diff] [blame] | 151 | IMAGE_NT_HEADERS *nt = RtlImageNtHeader( module32 ); |
| 152 | pModule->expected_version = ((nt->OptionalHeader.MajorSubsystemVersion & 0xff) << 8 ) | |
| 153 | (nt->OptionalHeader.MinorSubsystemVersion & 0xff); |
Alexandre Julliard | b445952 | 2000-04-15 21:00:55 +0000 | [diff] [blame] | 154 | pModule->flags |= NE_FFLAGS_WIN32; |
Alexandre Julliard | a5dea21 | 2002-08-09 19:57:38 +0000 | [diff] [blame] | 155 | if (nt->FileHeader.Characteristics & IMAGE_FILE_DLL) |
Alexandre Julliard | b445952 | 2000-04-15 21:00:55 +0000 | [diff] [blame] | 156 | pModule->flags |= NE_FFLAGS_LIBMODULE | NE_FFLAGS_SINGLEDATA; |
| 157 | } |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 158 | |
| 159 | /* Set loaded file information */ |
Ulrich Weigand | 237e8e9 | 1999-12-04 04:04:58 +0000 | [diff] [blame] | 160 | ofs = (OFSTRUCT *)(pModule + 1); |
| 161 | memset( ofs, 0, of_size ); |
| 162 | ofs->cBytes = of_size < 256 ? of_size : 255; /* FIXME */ |
| 163 | strcpy( ofs->szPathName, filename ); |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 164 | |
Ulrich Weigand | acefd16 | 2000-12-29 05:09:15 +0000 | [diff] [blame] | 165 | pSegment = (SEGTABLEENTRY*)((char*)(pModule + 1) + ((of_size + 3) & ~3)); |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 166 | pModule->seg_table = (int)pSegment - (int)pModule; |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 167 | /* Data segment */ |
| 168 | pSegment->size = 0; |
| 169 | pSegment->flags = NE_SEGFLAGS_DATA; |
| 170 | pSegment->minsize = 0x1000; |
| 171 | pSegment++; |
| 172 | /* Code segment */ |
| 173 | pSegment->flags = 0; |
| 174 | pSegment++; |
| 175 | |
| 176 | /* Module name */ |
| 177 | pStr = (char *)pSegment; |
| 178 | pModule->name_table = (int)pStr - (int)pModule; |
Francois Gouget | baa9bf9 | 1999-12-27 05:24:06 +0000 | [diff] [blame] | 179 | assert(len<256); |
Alexandre Julliard | 17216f5 | 1997-10-12 16:30:17 +0000 | [diff] [blame] | 180 | *pStr = len; |
Francois Gouget | baa9bf9 | 1999-12-27 05:24:06 +0000 | [diff] [blame] | 181 | lstrcpynA( pStr+1, basename, len+1 ); |
Ulrich Weigand | 237e8e9 | 1999-12-04 04:04:58 +0000 | [diff] [blame] | 182 | pStr += len+2; |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 183 | |
| 184 | /* All tables zero terminated */ |
| 185 | pModule->res_table = pModule->import_table = pModule->entry_table = |
| 186 | (int)pStr - (int)pModule; |
| 187 | |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 188 | NE_RegisterModule( pModule ); |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 189 | return hModule; |
| 190 | } |
| 191 | |
| 192 | |
Andreas Mohr | b021fe2 | 2000-07-26 18:02:28 +0000 | [diff] [blame] | 193 | /* Check whether a file is an OS/2 or a very old Windows executable |
| 194 | * by testing on import of KERNEL. |
| 195 | * |
| 196 | * FIXME: is reading the module imports the only way of discerning |
| 197 | * old Windows binaries from OS/2 ones ? At least it seems so... |
| 198 | */ |
Alexandre Julliard | 3536316 | 2002-05-22 21:32:49 +0000 | [diff] [blame] | 199 | static enum binary_type MODULE_Decide_OS2_OldWin(HANDLE hfile, const IMAGE_DOS_HEADER *mz, |
| 200 | const IMAGE_OS2_HEADER *ne) |
Andreas Mohr | b021fe2 | 2000-07-26 18:02:28 +0000 | [diff] [blame] | 201 | { |
| 202 | DWORD currpos = SetFilePointer( hfile, 0, NULL, SEEK_CUR); |
Alexandre Julliard | 3536316 | 2002-05-22 21:32:49 +0000 | [diff] [blame] | 203 | enum binary_type ret = BINARY_OS216; |
Andreas Mohr | b021fe2 | 2000-07-26 18:02:28 +0000 | [diff] [blame] | 204 | LPWORD modtab = NULL; |
| 205 | LPSTR nametab = NULL; |
| 206 | DWORD len; |
| 207 | int i; |
| 208 | |
| 209 | /* read modref table */ |
| 210 | if ( (SetFilePointer( hfile, mz->e_lfanew + ne->ne_modtab, NULL, SEEK_SET ) == -1) |
| 211 | || (!(modtab = HeapAlloc( GetProcessHeap(), 0, ne->ne_cmod*sizeof(WORD)))) |
| 212 | || (!(ReadFile(hfile, modtab, ne->ne_cmod*sizeof(WORD), &len, NULL))) |
| 213 | || (len != ne->ne_cmod*sizeof(WORD)) ) |
| 214 | goto broken; |
| 215 | |
| 216 | /* read imported names table */ |
| 217 | if ( (SetFilePointer( hfile, mz->e_lfanew + ne->ne_imptab, NULL, SEEK_SET ) == -1) |
| 218 | || (!(nametab = HeapAlloc( GetProcessHeap(), 0, ne->ne_enttab - ne->ne_imptab))) |
| 219 | || (!(ReadFile(hfile, nametab, ne->ne_enttab - ne->ne_imptab, &len, NULL))) |
| 220 | || (len != ne->ne_enttab - ne->ne_imptab) ) |
| 221 | goto broken; |
| 222 | |
| 223 | for (i=0; i < ne->ne_cmod; i++) |
| 224 | { |
| 225 | LPSTR module = &nametab[modtab[i]]; |
| 226 | TRACE("modref: %.*s\n", module[0], &module[1]); |
| 227 | if (!(strncmp(&module[1], "KERNEL", module[0]))) |
| 228 | { /* very old Windows file */ |
| 229 | MESSAGE("This seems to be a very old (pre-3.0) Windows executable. Expect crashes, especially if this is a real-mode binary !\n"); |
Alexandre Julliard | 3536316 | 2002-05-22 21:32:49 +0000 | [diff] [blame] | 230 | ret = BINARY_WIN16; |
Andreas Mohr | b021fe2 | 2000-07-26 18:02:28 +0000 | [diff] [blame] | 231 | goto good; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | broken: |
| 236 | ERR("Hmm, an error occurred. Is this binary file broken ?\n"); |
| 237 | |
| 238 | good: |
| 239 | HeapFree( GetProcessHeap(), 0, modtab); |
| 240 | HeapFree( GetProcessHeap(), 0, nametab); |
| 241 | SetFilePointer( hfile, currpos, NULL, SEEK_SET); /* restore filepos */ |
Alexandre Julliard | 3536316 | 2002-05-22 21:32:49 +0000 | [diff] [blame] | 242 | return ret; |
Andreas Mohr | b021fe2 | 2000-07-26 18:02:28 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 245 | /*********************************************************************** |
| 246 | * MODULE_GetBinaryType |
Alexandre Julliard | 3536316 | 2002-05-22 21:32:49 +0000 | [diff] [blame] | 247 | */ |
| 248 | enum binary_type MODULE_GetBinaryType( HANDLE hfile ) |
| 249 | { |
| 250 | union |
| 251 | { |
| 252 | struct |
| 253 | { |
| 254 | unsigned char magic[4]; |
| 255 | unsigned char ignored[12]; |
| 256 | unsigned short type; |
| 257 | } elf; |
| 258 | IMAGE_DOS_HEADER mz; |
| 259 | } header; |
| 260 | |
| 261 | char magic[4]; |
| 262 | DWORD len; |
| 263 | |
| 264 | /* Seek to the start of the file and read the header information. */ |
| 265 | if (SetFilePointer( hfile, 0, NULL, SEEK_SET ) == -1) |
| 266 | return BINARY_UNKNOWN; |
| 267 | if (!ReadFile( hfile, &header, sizeof(header), &len, NULL ) || len != sizeof(header)) |
| 268 | return BINARY_UNKNOWN; |
| 269 | |
| 270 | if (!memcmp( header.elf.magic, "\177ELF", 4 )) |
| 271 | { |
| 272 | /* FIXME: we don't bother to check byte order, architecture, etc. */ |
| 273 | switch(header.elf.type) |
| 274 | { |
| 275 | case 2: return BINARY_UNIX_EXE; |
| 276 | case 3: return BINARY_UNIX_LIB; |
| 277 | } |
| 278 | return BINARY_UNKNOWN; |
| 279 | } |
| 280 | |
| 281 | /* Not ELF, try DOS */ |
| 282 | |
| 283 | if (header.mz.e_magic == IMAGE_DOS_SIGNATURE) |
| 284 | { |
| 285 | /* We do have a DOS image so we will now try to seek into |
| 286 | * the file by the amount indicated by the field |
| 287 | * "Offset to extended header" and read in the |
| 288 | * "magic" field information at that location. |
| 289 | * This will tell us if there is more header information |
| 290 | * to read or not. |
| 291 | */ |
| 292 | /* But before we do we will make sure that header |
| 293 | * structure encompasses the "Offset to extended header" |
| 294 | * field. |
| 295 | */ |
| 296 | if ((header.mz.e_cparhdr << 4) < sizeof(IMAGE_DOS_HEADER)) |
| 297 | return BINARY_DOS; |
| 298 | if (header.mz.e_crlc && (header.mz.e_lfarlc < sizeof(IMAGE_DOS_HEADER))) |
| 299 | return BINARY_DOS; |
| 300 | if (header.mz.e_lfanew < sizeof(IMAGE_DOS_HEADER)) |
| 301 | return BINARY_DOS; |
| 302 | if (SetFilePointer( hfile, header.mz.e_lfanew, NULL, SEEK_SET ) == -1) |
| 303 | return BINARY_DOS; |
| 304 | if (!ReadFile( hfile, magic, sizeof(magic), &len, NULL ) || len != sizeof(magic)) |
| 305 | return BINARY_DOS; |
| 306 | |
| 307 | /* Reading the magic field succeeded so |
| 308 | * we will try to determine what type it is. |
| 309 | */ |
| 310 | if (!memcmp( magic, "PE\0\0", 4 )) |
| 311 | { |
Dmitry Timoshkov | 1467bbd | 2002-08-27 00:34:41 +0000 | [diff] [blame] | 312 | IMAGE_FILE_HEADER FileHeader; |
Alexandre Julliard | 3536316 | 2002-05-22 21:32:49 +0000 | [diff] [blame] | 313 | |
Dmitry Timoshkov | 1467bbd | 2002-08-27 00:34:41 +0000 | [diff] [blame] | 314 | if (ReadFile( hfile, &FileHeader, sizeof(FileHeader), &len, NULL ) && len == sizeof(FileHeader)) |
Alexandre Julliard | 3536316 | 2002-05-22 21:32:49 +0000 | [diff] [blame] | 315 | { |
Dmitry Timoshkov | 1467bbd | 2002-08-27 00:34:41 +0000 | [diff] [blame] | 316 | if (FileHeader.Characteristics & IMAGE_FILE_DLL) return BINARY_PE_DLL; |
Alexandre Julliard | 3536316 | 2002-05-22 21:32:49 +0000 | [diff] [blame] | 317 | return BINARY_PE_EXE; |
| 318 | } |
Dmitry Timoshkov | 1467bbd | 2002-08-27 00:34:41 +0000 | [diff] [blame] | 319 | return BINARY_DOS; |
Alexandre Julliard | 3536316 | 2002-05-22 21:32:49 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | if (!memcmp( magic, "NE", 2 )) |
| 323 | { |
| 324 | /* This is a Windows executable (NE) header. This can |
| 325 | * mean either a 16-bit OS/2 or a 16-bit Windows or even a |
| 326 | * DOS program (running under a DOS extender). To decide |
| 327 | * which, we'll have to read the NE header. |
| 328 | */ |
| 329 | IMAGE_OS2_HEADER ne; |
| 330 | if ( SetFilePointer( hfile, header.mz.e_lfanew, NULL, SEEK_SET ) != -1 |
| 331 | && ReadFile( hfile, &ne, sizeof(ne), &len, NULL ) |
| 332 | && len == sizeof(ne) ) |
| 333 | { |
| 334 | switch ( ne.ne_exetyp ) |
| 335 | { |
| 336 | case 2: return BINARY_WIN16; |
| 337 | case 5: return BINARY_DOS; |
| 338 | default: return MODULE_Decide_OS2_OldWin(hfile, &header.mz, &ne); |
| 339 | } |
| 340 | } |
| 341 | /* Couldn't read header, so abort. */ |
Dmitry Timoshkov | 1467bbd | 2002-08-27 00:34:41 +0000 | [diff] [blame] | 342 | return BINARY_DOS; |
Alexandre Julliard | 3536316 | 2002-05-22 21:32:49 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | /* Unknown extended header, but this file is nonetheless DOS-executable. */ |
| 346 | return BINARY_DOS; |
| 347 | } |
| 348 | |
| 349 | return BINARY_UNKNOWN; |
| 350 | } |
| 351 | |
| 352 | /*********************************************************************** |
| 353 | * GetBinaryTypeA [KERNEL32.@] |
| 354 | * GetBinaryType [KERNEL32.@] |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 355 | * |
| 356 | * The GetBinaryType function determines whether a file is executable |
| 357 | * or not and if it is it returns what type of executable it is. |
| 358 | * The type of executable is a property that determines in which |
| 359 | * subsystem an executable file runs under. |
| 360 | * |
| 361 | * Binary types returned: |
| 362 | * SCS_32BIT_BINARY: A Win32 based application |
| 363 | * SCS_DOS_BINARY: An MS-Dos based application |
| 364 | * SCS_WOW_BINARY: A Win16 based application |
| 365 | * SCS_PIF_BINARY: A PIF file that executes an MS-Dos based app |
| 366 | * SCS_POSIX_BINARY: A POSIX based application ( Not implemented ) |
Ulrich Weigand | d523e4d | 1999-06-07 17:37:43 +0000 | [diff] [blame] | 367 | * SCS_OS216_BINARY: A 16bit OS/2 based application |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 368 | * |
| 369 | * Returns TRUE if the file is an executable in which case |
| 370 | * the value pointed by lpBinaryType is set. |
| 371 | * Returns FALSE if the file is not an executable or if the function fails. |
| 372 | * |
| 373 | * To do so it opens the file and reads in the header information |
Andreas Mohr | 8952dea | 1999-12-12 20:16:42 +0000 | [diff] [blame] | 374 | * if the extended header information is not present it will |
| 375 | * assume that the file is a DOS executable. |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 376 | * If the extended header information is present it will |
Andreas Mohr | 8952dea | 1999-12-12 20:16:42 +0000 | [diff] [blame] | 377 | * determine if the file is a 16 or 32 bit Windows executable |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 378 | * by check the flags in the header. |
| 379 | * |
| 380 | * Note that .COM and .PIF files are only recognized by their |
| 381 | * file name extension; but Windows does it the same way ... |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 382 | */ |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 383 | BOOL WINAPI GetBinaryTypeA( LPCSTR lpApplicationName, LPDWORD lpBinaryType ) |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 384 | { |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 385 | BOOL ret = FALSE; |
Ulrich Weigand | 237e8e9 | 1999-12-04 04:04:58 +0000 | [diff] [blame] | 386 | HANDLE hfile; |
Alexandre Julliard | 3536316 | 2002-05-22 21:32:49 +0000 | [diff] [blame] | 387 | char *ptr; |
Alexandre Julliard | a0d7731 | 1998-09-13 16:32:00 +0000 | [diff] [blame] | 388 | |
Alexandre Julliard | 06c275a | 1999-05-02 14:32:27 +0000 | [diff] [blame] | 389 | TRACE_(win32)("%s\n", lpApplicationName ); |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 390 | |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 391 | /* Sanity check. |
| 392 | */ |
| 393 | if ( lpApplicationName == NULL || lpBinaryType == NULL ) |
| 394 | return FALSE; |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 395 | |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 396 | /* Open the file indicated by lpApplicationName for reading. |
| 397 | */ |
Alexandre Julliard | e333212 | 2000-06-08 01:00:16 +0000 | [diff] [blame] | 398 | hfile = CreateFileA( lpApplicationName, GENERIC_READ, FILE_SHARE_READ, |
François Gouget | da2b6a9 | 2001-01-06 01:29:18 +0000 | [diff] [blame] | 399 | NULL, OPEN_EXISTING, 0, 0 ); |
Ulrich Weigand | 237e8e9 | 1999-12-04 04:04:58 +0000 | [diff] [blame] | 400 | if ( hfile == INVALID_HANDLE_VALUE ) |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 401 | return FALSE; |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 402 | |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 403 | /* Check binary type |
| 404 | */ |
Alexandre Julliard | 3536316 | 2002-05-22 21:32:49 +0000 | [diff] [blame] | 405 | switch(MODULE_GetBinaryType( hfile )) |
| 406 | { |
| 407 | case BINARY_UNKNOWN: |
| 408 | /* try to determine from file name */ |
| 409 | ptr = strrchr( lpApplicationName, '.' ); |
| 410 | if (!ptr) break; |
| 411 | if (!FILE_strcasecmp( ptr, ".COM" )) |
| 412 | { |
| 413 | *lpBinaryType = SCS_DOS_BINARY; |
| 414 | ret = TRUE; |
| 415 | } |
| 416 | else if (!FILE_strcasecmp( ptr, ".PIF" )) |
| 417 | { |
| 418 | *lpBinaryType = SCS_PIF_BINARY; |
| 419 | ret = TRUE; |
| 420 | } |
| 421 | break; |
| 422 | case BINARY_PE_EXE: |
| 423 | case BINARY_PE_DLL: |
| 424 | *lpBinaryType = SCS_32BIT_BINARY; |
| 425 | ret = TRUE; |
| 426 | break; |
| 427 | case BINARY_WIN16: |
| 428 | *lpBinaryType = SCS_WOW_BINARY; |
| 429 | ret = TRUE; |
| 430 | break; |
| 431 | case BINARY_OS216: |
| 432 | *lpBinaryType = SCS_OS216_BINARY; |
| 433 | ret = TRUE; |
| 434 | break; |
| 435 | case BINARY_DOS: |
| 436 | *lpBinaryType = SCS_DOS_BINARY; |
| 437 | ret = TRUE; |
| 438 | break; |
| 439 | case BINARY_UNIX_EXE: |
| 440 | case BINARY_UNIX_LIB: |
| 441 | ret = FALSE; |
| 442 | break; |
| 443 | } |
Alexandre Julliard | a0d7731 | 1998-09-13 16:32:00 +0000 | [diff] [blame] | 444 | |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 445 | CloseHandle( hfile ); |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 446 | return ret; |
| 447 | } |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 448 | |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 449 | /*********************************************************************** |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 450 | * GetBinaryTypeW [KERNEL32.@] |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 451 | */ |
| 452 | BOOL WINAPI GetBinaryTypeW( LPCWSTR lpApplicationName, LPDWORD lpBinaryType ) |
| 453 | { |
| 454 | BOOL ret = FALSE; |
| 455 | LPSTR strNew = NULL; |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 456 | |
Alexandre Julliard | 06c275a | 1999-05-02 14:32:27 +0000 | [diff] [blame] | 457 | TRACE_(win32)("%s\n", debugstr_w(lpApplicationName) ); |
Alexandre Julliard | a0d7731 | 1998-09-13 16:32:00 +0000 | [diff] [blame] | 458 | |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 459 | /* Sanity check. |
| 460 | */ |
| 461 | if ( lpApplicationName == NULL || lpBinaryType == NULL ) |
| 462 | return FALSE; |
| 463 | |
| 464 | /* Convert the wide string to a ascii string. |
| 465 | */ |
| 466 | strNew = HEAP_strdupWtoA( GetProcessHeap(), 0, lpApplicationName ); |
| 467 | |
| 468 | if ( strNew != NULL ) |
Alexandre Julliard | a0d7731 | 1998-09-13 16:32:00 +0000 | [diff] [blame] | 469 | { |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 470 | ret = GetBinaryTypeA( strNew, lpBinaryType ); |
| 471 | |
| 472 | /* Free the allocated string. |
| 473 | */ |
| 474 | HeapFree( GetProcessHeap(), 0, strNew ); |
Alexandre Julliard | a0d7731 | 1998-09-13 16:32:00 +0000 | [diff] [blame] | 475 | } |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 476 | |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 477 | return ret; |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 480 | |
| 481 | /*********************************************************************** |
Patrik Stridvall | 044855c | 2001-07-11 18:56:41 +0000 | [diff] [blame] | 482 | * WinExec (KERNEL.166) |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 483 | */ |
| 484 | HINSTANCE16 WINAPI WinExec16( LPCSTR lpCmdLine, UINT16 nCmdShow ) |
| 485 | { |
Andreas Mohr | 7096384 | 2000-09-22 22:08:28 +0000 | [diff] [blame] | 486 | LPCSTR p, args = NULL; |
| 487 | LPCSTR name_beg, name_end; |
Alexandre Julliard | c192ba2 | 2000-05-29 21:25:10 +0000 | [diff] [blame] | 488 | LPSTR name, cmdline; |
Andreas Mohr | 7096384 | 2000-09-22 22:08:28 +0000 | [diff] [blame] | 489 | int arglen; |
Alexandre Julliard | c192ba2 | 2000-05-29 21:25:10 +0000 | [diff] [blame] | 490 | HINSTANCE16 ret; |
| 491 | char buffer[MAX_PATH]; |
Ulrich Weigand | 6ce4006 | 1999-05-03 09:22:55 +0000 | [diff] [blame] | 492 | |
Andreas Mohr | 7096384 | 2000-09-22 22:08:28 +0000 | [diff] [blame] | 493 | if (*lpCmdLine == '"') /* has to be only one and only at beginning ! */ |
Alexandre Julliard | c192ba2 | 2000-05-29 21:25:10 +0000 | [diff] [blame] | 494 | { |
Andreas Mohr | 7096384 | 2000-09-22 22:08:28 +0000 | [diff] [blame] | 495 | name_beg = lpCmdLine+1; |
| 496 | p = strchr ( lpCmdLine+1, '"' ); |
| 497 | if (p) |
| 498 | { |
| 499 | name_end = p; |
| 500 | args = strchr ( p, ' ' ); |
| 501 | } |
| 502 | else /* yes, even valid with trailing '"' missing */ |
| 503 | name_end = lpCmdLine+strlen(lpCmdLine); |
| 504 | } |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 505 | else |
Andreas Mohr | 7096384 | 2000-09-22 22:08:28 +0000 | [diff] [blame] | 506 | { |
| 507 | name_beg = lpCmdLine; |
| 508 | args = strchr( lpCmdLine, ' ' ); |
| 509 | name_end = args ? args : lpCmdLine+strlen(lpCmdLine); |
| 510 | } |
| 511 | |
| 512 | if ((name_beg == lpCmdLine) && (!args)) |
| 513 | { /* just use the original cmdline string as file name */ |
| 514 | name = (LPSTR)lpCmdLine; |
Alexandre Julliard | c192ba2 | 2000-05-29 21:25:10 +0000 | [diff] [blame] | 515 | } |
| 516 | else |
| 517 | { |
Andreas Mohr | 7096384 | 2000-09-22 22:08:28 +0000 | [diff] [blame] | 518 | if (!(name = HeapAlloc( GetProcessHeap(), 0, name_end - name_beg + 1 ))) |
| 519 | return ERROR_NOT_ENOUGH_MEMORY; |
| 520 | memcpy( name, name_beg, name_end - name_beg ); |
| 521 | name[name_end - name_beg] = '\0'; |
Alexandre Julliard | c192ba2 | 2000-05-29 21:25:10 +0000 | [diff] [blame] | 522 | } |
Ulrich Weigand | 6ce4006 | 1999-05-03 09:22:55 +0000 | [diff] [blame] | 523 | |
Andreas Mohr | 7096384 | 2000-09-22 22:08:28 +0000 | [diff] [blame] | 524 | if (args) |
| 525 | { |
| 526 | args++; |
| 527 | arglen = strlen(args); |
Alexandre Julliard | d7b7682 | 2001-12-20 00:19:40 +0000 | [diff] [blame] | 528 | cmdline = HeapAlloc( GetProcessHeap(), 0, 2 + arglen ); |
Andreas Mohr | 7096384 | 2000-09-22 22:08:28 +0000 | [diff] [blame] | 529 | cmdline[0] = (BYTE)arglen; |
| 530 | strcpy( cmdline + 1, args ); |
| 531 | } |
| 532 | else |
| 533 | { |
Alexandre Julliard | d7b7682 | 2001-12-20 00:19:40 +0000 | [diff] [blame] | 534 | cmdline = HeapAlloc( GetProcessHeap(), 0, 2 ); |
Andreas Mohr | 7096384 | 2000-09-22 22:08:28 +0000 | [diff] [blame] | 535 | cmdline[0] = cmdline[1] = 0; |
| 536 | } |
| 537 | |
Andreas Mohr | cabee39 | 2000-10-25 21:22:27 +0000 | [diff] [blame] | 538 | TRACE("name: '%s', cmdline: '%.*s'\n", name, cmdline[0], &cmdline[1]); |
Andreas Mohr | 7096384 | 2000-09-22 22:08:28 +0000 | [diff] [blame] | 539 | |
Alexandre Julliard | c192ba2 | 2000-05-29 21:25:10 +0000 | [diff] [blame] | 540 | if (SearchPathA( NULL, name, ".exe", sizeof(buffer), buffer, NULL )) |
| 541 | { |
| 542 | LOADPARAMS16 params; |
Alexandre Julliard | d7b7682 | 2001-12-20 00:19:40 +0000 | [diff] [blame] | 543 | WORD showCmd[2]; |
Alexandre Julliard | c192ba2 | 2000-05-29 21:25:10 +0000 | [diff] [blame] | 544 | showCmd[0] = 2; |
| 545 | showCmd[1] = nCmdShow; |
| 546 | |
| 547 | params.hEnvironment = 0; |
Alexandre Julliard | d7b7682 | 2001-12-20 00:19:40 +0000 | [diff] [blame] | 548 | params.cmdLine = MapLS( cmdline ); |
| 549 | params.showCmd = MapLS( showCmd ); |
Alexandre Julliard | c192ba2 | 2000-05-29 21:25:10 +0000 | [diff] [blame] | 550 | params.reserved = 0; |
| 551 | |
| 552 | ret = LoadModule16( buffer, ¶ms ); |
Alexandre Julliard | d7b7682 | 2001-12-20 00:19:40 +0000 | [diff] [blame] | 553 | UnMapLS( params.cmdLine ); |
| 554 | UnMapLS( params.showCmd ); |
Alexandre Julliard | c192ba2 | 2000-05-29 21:25:10 +0000 | [diff] [blame] | 555 | } |
| 556 | else ret = GetLastError(); |
| 557 | |
Alexandre Julliard | d7b7682 | 2001-12-20 00:19:40 +0000 | [diff] [blame] | 558 | HeapFree( GetProcessHeap(), 0, cmdline ); |
Alexandre Julliard | c192ba2 | 2000-05-29 21:25:10 +0000 | [diff] [blame] | 559 | if (name != lpCmdLine) HeapFree( GetProcessHeap(), 0, name ); |
| 560 | |
| 561 | if (ret == 21) /* 32-bit module */ |
| 562 | { |
Alexandre Julliard | ab68797 | 2000-11-15 23:41:46 +0000 | [diff] [blame] | 563 | DWORD count; |
| 564 | ReleaseThunkLock( &count ); |
Alexandre Julliard | 8ff37b8 | 2001-06-06 20:24:12 +0000 | [diff] [blame] | 565 | ret = LOWORD( WinExec( lpCmdLine, nCmdShow ) ); |
Alexandre Julliard | ab68797 | 2000-11-15 23:41:46 +0000 | [diff] [blame] | 566 | RestoreThunkLock( count ); |
Alexandre Julliard | c192ba2 | 2000-05-29 21:25:10 +0000 | [diff] [blame] | 567 | } |
| 568 | return ret; |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | /*********************************************************************** |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 572 | * WinExec (KERNEL32.@) |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 573 | */ |
Francois Gouget | 2204b50 | 2002-05-19 22:21:45 +0000 | [diff] [blame] | 574 | UINT WINAPI WinExec( LPCSTR lpCmdLine, UINT nCmdShow ) |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 575 | { |
Alexandre Julliard | 8e8f0f5 | 2000-04-15 21:30:33 +0000 | [diff] [blame] | 576 | PROCESS_INFORMATION info; |
| 577 | STARTUPINFOA startup; |
Alexandre Julliard | 596921d | 2000-06-24 20:53:47 +0000 | [diff] [blame] | 578 | char *cmdline; |
Alexandre Julliard | 267ca68 | 2002-07-31 17:20:00 +0000 | [diff] [blame] | 579 | UINT ret; |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 580 | |
Alexandre Julliard | 8e8f0f5 | 2000-04-15 21:30:33 +0000 | [diff] [blame] | 581 | memset( &startup, 0, sizeof(startup) ); |
| 582 | startup.cb = sizeof(startup); |
| 583 | startup.dwFlags = STARTF_USESHOWWINDOW; |
| 584 | startup.wShowWindow = nCmdShow; |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 585 | |
Alexandre Julliard | 596921d | 2000-06-24 20:53:47 +0000 | [diff] [blame] | 586 | /* cmdline needs to be writeable for CreateProcess */ |
Alexandre Julliard | 5f728ca | 2001-07-24 21:45:22 +0000 | [diff] [blame] | 587 | if (!(cmdline = HeapAlloc( GetProcessHeap(), 0, strlen(lpCmdLine)+1 ))) return 0; |
| 588 | strcpy( cmdline, lpCmdLine ); |
Alexandre Julliard | 596921d | 2000-06-24 20:53:47 +0000 | [diff] [blame] | 589 | |
| 590 | if (CreateProcessA( NULL, cmdline, NULL, NULL, FALSE, |
Alexandre Julliard | 8e8f0f5 | 2000-04-15 21:30:33 +0000 | [diff] [blame] | 591 | 0, NULL, NULL, &startup, &info )) |
| 592 | { |
| 593 | /* Give 30 seconds to the app to come up */ |
Alexandre Julliard | 5edf4e1 | 2001-07-26 20:12:54 +0000 | [diff] [blame] | 594 | if (wait_input_idle( info.hProcess, 30000 ) == 0xFFFFFFFF) |
Alexandre Julliard | 8e8f0f5 | 2000-04-15 21:30:33 +0000 | [diff] [blame] | 595 | WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() ); |
Alexandre Julliard | 267ca68 | 2002-07-31 17:20:00 +0000 | [diff] [blame] | 596 | ret = 33; |
Alexandre Julliard | 8e8f0f5 | 2000-04-15 21:30:33 +0000 | [diff] [blame] | 597 | /* Close off the handles */ |
| 598 | CloseHandle( info.hThread ); |
| 599 | CloseHandle( info.hProcess ); |
| 600 | } |
Alexandre Julliard | 267ca68 | 2002-07-31 17:20:00 +0000 | [diff] [blame] | 601 | else if ((ret = GetLastError()) >= 32) |
Alexandre Julliard | 8e8f0f5 | 2000-04-15 21:30:33 +0000 | [diff] [blame] | 602 | { |
Alexandre Julliard | 267ca68 | 2002-07-31 17:20:00 +0000 | [diff] [blame] | 603 | FIXME("Strange error set by CreateProcess: %d\n", ret ); |
| 604 | ret = 11; |
Alexandre Julliard | 8e8f0f5 | 2000-04-15 21:30:33 +0000 | [diff] [blame] | 605 | } |
Alexandre Julliard | 596921d | 2000-06-24 20:53:47 +0000 | [diff] [blame] | 606 | HeapFree( GetProcessHeap(), 0, cmdline ); |
Alexandre Julliard | 267ca68 | 2002-07-31 17:20:00 +0000 | [diff] [blame] | 607 | return ret; |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | /********************************************************************** |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 611 | * LoadModule (KERNEL32.@) |
Alexandre Julliard | 491502b | 1997-11-01 19:08:16 +0000 | [diff] [blame] | 612 | */ |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 613 | HINSTANCE WINAPI LoadModule( LPCSTR name, LPVOID paramBlock ) |
Alexandre Julliard | 491502b | 1997-11-01 19:08:16 +0000 | [diff] [blame] | 614 | { |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 615 | LOADPARAMS *params = (LOADPARAMS *)paramBlock; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 616 | PROCESS_INFORMATION info; |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 617 | STARTUPINFOA startup; |
| 618 | HINSTANCE hInstance; |
Alexandre Julliard | 8e8f0f5 | 2000-04-15 21:30:33 +0000 | [diff] [blame] | 619 | LPSTR cmdline, p; |
| 620 | char filename[MAX_PATH]; |
| 621 | BYTE len; |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 622 | |
Alexandre Julliard | 8ff37b8 | 2001-06-06 20:24:12 +0000 | [diff] [blame] | 623 | if (!name) return (HINSTANCE)ERROR_FILE_NOT_FOUND; |
Alexandre Julliard | 8e8f0f5 | 2000-04-15 21:30:33 +0000 | [diff] [blame] | 624 | |
| 625 | if (!SearchPathA( NULL, name, ".exe", sizeof(filename), filename, NULL ) && |
| 626 | !SearchPathA( NULL, name, NULL, sizeof(filename), filename, NULL )) |
Alexandre Julliard | 8ff37b8 | 2001-06-06 20:24:12 +0000 | [diff] [blame] | 627 | return (HINSTANCE)GetLastError(); |
Alexandre Julliard | 8e8f0f5 | 2000-04-15 21:30:33 +0000 | [diff] [blame] | 628 | |
| 629 | len = (BYTE)params->lpCmdLine[0]; |
| 630 | if (!(cmdline = HeapAlloc( GetProcessHeap(), 0, strlen(filename) + len + 2 ))) |
Alexandre Julliard | 8ff37b8 | 2001-06-06 20:24:12 +0000 | [diff] [blame] | 631 | return (HINSTANCE)ERROR_NOT_ENOUGH_MEMORY; |
Alexandre Julliard | 8e8f0f5 | 2000-04-15 21:30:33 +0000 | [diff] [blame] | 632 | |
| 633 | strcpy( cmdline, filename ); |
| 634 | p = cmdline + strlen(cmdline); |
| 635 | *p++ = ' '; |
| 636 | memcpy( p, params->lpCmdLine + 1, len ); |
| 637 | p[len] = 0; |
| 638 | |
| 639 | memset( &startup, 0, sizeof(startup) ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 640 | startup.cb = sizeof(startup); |
Alexandre Julliard | 8e8f0f5 | 2000-04-15 21:30:33 +0000 | [diff] [blame] | 641 | if (params->lpCmdShow) |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 642 | { |
Alexandre Julliard | 8e8f0f5 | 2000-04-15 21:30:33 +0000 | [diff] [blame] | 643 | startup.dwFlags = STARTF_USESHOWWINDOW; |
| 644 | startup.wShowWindow = params->lpCmdShow[1]; |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 645 | } |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 646 | |
Alexandre Julliard | 8e8f0f5 | 2000-04-15 21:30:33 +0000 | [diff] [blame] | 647 | if (CreateProcessA( filename, cmdline, NULL, NULL, FALSE, 0, |
| 648 | params->lpEnvAddress, NULL, &startup, &info )) |
| 649 | { |
| 650 | /* Give 30 seconds to the app to come up */ |
Alexandre Julliard | 5edf4e1 | 2001-07-26 20:12:54 +0000 | [diff] [blame] | 651 | if (wait_input_idle( info.hProcess, 30000 ) == 0xFFFFFFFF ) |
Alexandre Julliard | 8e8f0f5 | 2000-04-15 21:30:33 +0000 | [diff] [blame] | 652 | WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() ); |
Alexandre Julliard | 8ff37b8 | 2001-06-06 20:24:12 +0000 | [diff] [blame] | 653 | hInstance = (HINSTANCE)33; |
Alexandre Julliard | 8e8f0f5 | 2000-04-15 21:30:33 +0000 | [diff] [blame] | 654 | /* Close off the handles */ |
| 655 | CloseHandle( info.hThread ); |
| 656 | CloseHandle( info.hProcess ); |
| 657 | } |
Alexandre Julliard | 8ff37b8 | 2001-06-06 20:24:12 +0000 | [diff] [blame] | 658 | else if ((hInstance = (HINSTANCE)GetLastError()) >= (HINSTANCE)32) |
Alexandre Julliard | 8e8f0f5 | 2000-04-15 21:30:33 +0000 | [diff] [blame] | 659 | { |
Andrew John Hughes | ed800c6 | 2002-11-21 03:45:01 +0000 | [diff] [blame] | 660 | FIXME("Strange error set by CreateProcess: %p\n", hInstance ); |
Alexandre Julliard | 8ff37b8 | 2001-06-06 20:24:12 +0000 | [diff] [blame] | 661 | hInstance = (HINSTANCE)11; |
Alexandre Julliard | 8e8f0f5 | 2000-04-15 21:30:33 +0000 | [diff] [blame] | 662 | } |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 663 | |
Alexandre Julliard | 8e8f0f5 | 2000-04-15 21:30:33 +0000 | [diff] [blame] | 664 | HeapFree( GetProcessHeap(), 0, cmdline ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 665 | return hInstance; |
Alexandre Julliard | 491502b | 1997-11-01 19:08:16 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Alexandre Julliard | 8e8f0f5 | 2000-04-15 21:30:33 +0000 | [diff] [blame] | 668 | |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 669 | /*********************************************************************** |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 670 | * GetModuleHandleA (KERNEL32.@) |
Patrik Stridvall | 044855c | 2001-07-11 18:56:41 +0000 | [diff] [blame] | 671 | * GetModuleHandle32 (KERNEL.488) |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 672 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 673 | HMODULE WINAPI GetModuleHandleA(LPCSTR module) |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 674 | { |
Eric Pouech | f3a73ef | 2003-03-06 23:41:37 +0000 | [diff] [blame] | 675 | NTSTATUS nts; |
| 676 | HMODULE ret; |
Alexandre Julliard | 5980477 | 2003-04-05 05:13:33 +0000 | [diff] [blame] | 677 | UNICODE_STRING wstr; |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 678 | |
Alexandre Julliard | 5980477 | 2003-04-05 05:13:33 +0000 | [diff] [blame] | 679 | if (!module) return get_exe_module(); |
Eric Pouech | f3a73ef | 2003-03-06 23:41:37 +0000 | [diff] [blame] | 680 | |
Alexandre Julliard | 5980477 | 2003-04-05 05:13:33 +0000 | [diff] [blame] | 681 | RtlCreateUnicodeStringFromAsciiz(&wstr, module); |
| 682 | nts = LdrGetDllHandle(0, 0, &wstr, &ret); |
| 683 | RtlFreeUnicodeString( &wstr ); |
Eric Pouech | f3a73ef | 2003-03-06 23:41:37 +0000 | [diff] [blame] | 684 | if (nts != STATUS_SUCCESS) |
| 685 | { |
| 686 | ret = 0; |
| 687 | SetLastError( RtlNtStatusToDosError( nts ) ); |
| 688 | } |
Eric Pouech | f3a73ef | 2003-03-06 23:41:37 +0000 | [diff] [blame] | 689 | return ret; |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 690 | } |
| 691 | |
Patrik Stridvall | 2d6457c | 2000-03-28 20:22:59 +0000 | [diff] [blame] | 692 | /*********************************************************************** |
Patrik Stridvall | 3ca9823 | 2001-06-20 23:03:14 +0000 | [diff] [blame] | 693 | * GetModuleHandleW (KERNEL32.@) |
Patrik Stridvall | 2d6457c | 2000-03-28 20:22:59 +0000 | [diff] [blame] | 694 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 695 | HMODULE WINAPI GetModuleHandleW(LPCWSTR module) |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 696 | { |
Eric Pouech | f3a73ef | 2003-03-06 23:41:37 +0000 | [diff] [blame] | 697 | NTSTATUS nts; |
| 698 | HMODULE ret; |
Alexandre Julliard | 5980477 | 2003-04-05 05:13:33 +0000 | [diff] [blame] | 699 | UNICODE_STRING wstr; |
Eric Pouech | f3a73ef | 2003-03-06 23:41:37 +0000 | [diff] [blame] | 700 | |
Alexandre Julliard | 5980477 | 2003-04-05 05:13:33 +0000 | [diff] [blame] | 701 | if (!module) return get_exe_module(); |
Eric Pouech | f3a73ef | 2003-03-06 23:41:37 +0000 | [diff] [blame] | 702 | |
Alexandre Julliard | 5980477 | 2003-04-05 05:13:33 +0000 | [diff] [blame] | 703 | RtlInitUnicodeString( &wstr, module ); |
| 704 | nts = LdrGetDllHandle( 0, 0, &wstr, &ret); |
Eric Pouech | f3a73ef | 2003-03-06 23:41:37 +0000 | [diff] [blame] | 705 | if (nts != STATUS_SUCCESS) |
| 706 | { |
| 707 | SetLastError( RtlNtStatusToDosError( nts ) ); |
| 708 | ret = 0; |
| 709 | } |
| 710 | return ret; |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 713 | |
Alexandre Julliard | b1bac32 | 1996-12-15 19:45:59 +0000 | [diff] [blame] | 714 | /*********************************************************************** |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 715 | * GetModuleFileNameA (KERNEL32.@) |
Patrik Stridvall | 044855c | 2001-07-11 18:56:41 +0000 | [diff] [blame] | 716 | * GetModuleFileName32 (KERNEL.487) |
Andreas Mohr | 4654c32 | 2000-02-20 19:15:34 +0000 | [diff] [blame] | 717 | * |
| 718 | * GetModuleFileNameA seems to *always* return the long path; |
| 719 | * it's only GetModuleFileName16 that decides between short/long path |
| 720 | * by checking if exe version >= 4.0. |
| 721 | * (SDK docu doesn't mention this) |
Alexandre Julliard | b1bac32 | 1996-12-15 19:45:59 +0000 | [diff] [blame] | 722 | */ |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 723 | DWORD WINAPI GetModuleFileNameA( |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 724 | HMODULE hModule, /* [in] module handle (32bit) */ |
Alexandre Julliard | dadf78f | 1998-05-17 17:13:43 +0000 | [diff] [blame] | 725 | LPSTR lpFileName, /* [out] filenamebuffer */ |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 726 | DWORD size ) /* [in] size of filenamebuffer */ |
| 727 | { |
Alexandre Julliard | 03e4ea1 | 2003-03-18 20:07:12 +0000 | [diff] [blame] | 728 | LPWSTR filenameW = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ); |
Alexandre Julliard | dadf78f | 1998-05-17 17:13:43 +0000 | [diff] [blame] | 729 | |
Alexandre Julliard | 03e4ea1 | 2003-03-18 20:07:12 +0000 | [diff] [blame] | 730 | if (!filenameW) |
Joshua Thielen | 1cef297 | 2002-07-05 00:16:41 +0000 | [diff] [blame] | 731 | { |
Alexandre Julliard | 03e4ea1 | 2003-03-18 20:07:12 +0000 | [diff] [blame] | 732 | SetLastError( ERROR_NOT_ENOUGH_MEMORY ); |
| 733 | return 0; |
Joshua Thielen | 1cef297 | 2002-07-05 00:16:41 +0000 | [diff] [blame] | 734 | } |
Alexandre Julliard | 03e4ea1 | 2003-03-18 20:07:12 +0000 | [diff] [blame] | 735 | GetModuleFileNameW( hModule, filenameW, size ); |
| 736 | WideCharToMultiByte( CP_ACP, 0, filenameW, -1, lpFileName, size, NULL, NULL ); |
| 737 | HeapFree( GetProcessHeap(), 0, filenameW ); |
| 738 | return strlen( lpFileName ); |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 739 | } |
| 740 | |
Alexandre Julliard | b1bac32 | 1996-12-15 19:45:59 +0000 | [diff] [blame] | 741 | /*********************************************************************** |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 742 | * GetModuleFileNameW (KERNEL32.@) |
Alexandre Julliard | b1bac32 | 1996-12-15 19:45:59 +0000 | [diff] [blame] | 743 | */ |
Alexandre Julliard | 83886f2 | 2002-07-05 01:27:19 +0000 | [diff] [blame] | 744 | DWORD WINAPI GetModuleFileNameW( HMODULE hModule, LPWSTR lpFileName, DWORD size ) |
Alexandre Julliard | b1bac32 | 1996-12-15 19:45:59 +0000 | [diff] [blame] | 745 | { |
Alexandre Julliard | 500a2f9 | 2003-03-20 04:29:21 +0000 | [diff] [blame] | 746 | ULONG magic; |
| 747 | |
Eric Pouech | 84d1a8f | 2003-03-18 18:29:13 +0000 | [diff] [blame] | 748 | lpFileName[0] = 0; |
| 749 | |
Alexandre Julliard | 500a2f9 | 2003-03-20 04:29:21 +0000 | [diff] [blame] | 750 | LdrLockLoaderLock( 0, NULL, &magic ); |
Eric Pouech | 84d1a8f | 2003-03-18 18:29:13 +0000 | [diff] [blame] | 751 | if (!hModule && !(NtCurrentTeb()->tibflags & TEBF_WIN32)) |
| 752 | { |
| 753 | /* 16-bit task - get current NE module name */ |
| 754 | NE_MODULE *pModule = NE_GetPtr( GetCurrentTask() ); |
| 755 | if (pModule) |
| 756 | { |
| 757 | WCHAR path[MAX_PATH]; |
| 758 | |
Alexandre Julliard | 03e4ea1 | 2003-03-18 20:07:12 +0000 | [diff] [blame] | 759 | MultiByteToWideChar( CP_ACP, 0, NE_MODULE_NAME(pModule), -1, path, MAX_PATH ); |
| 760 | GetLongPathNameW(path, lpFileName, size); |
Eric Pouech | 84d1a8f | 2003-03-18 18:29:13 +0000 | [diff] [blame] | 761 | } |
| 762 | } |
Alexandre Julliard | 03e4ea1 | 2003-03-18 20:07:12 +0000 | [diff] [blame] | 763 | else |
Eric Pouech | 84d1a8f | 2003-03-18 18:29:13 +0000 | [diff] [blame] | 764 | { |
| 765 | LDR_MODULE* pldr; |
| 766 | NTSTATUS nts; |
| 767 | |
Alexandre Julliard | 03e4ea1 | 2003-03-18 20:07:12 +0000 | [diff] [blame] | 768 | if (!hModule) hModule = get_exe_module(); |
Eric Pouech | 84d1a8f | 2003-03-18 18:29:13 +0000 | [diff] [blame] | 769 | nts = LdrFindEntryForAddress( hModule, &pldr ); |
Alexandre Julliard | 03e4ea1 | 2003-03-18 20:07:12 +0000 | [diff] [blame] | 770 | if (nts == STATUS_SUCCESS) lstrcpynW(lpFileName, pldr->FullDllName.Buffer, size); |
Eric Pouech | 84d1a8f | 2003-03-18 18:29:13 +0000 | [diff] [blame] | 771 | else SetLastError( RtlNtStatusToDosError( nts ) ); |
| 772 | |
| 773 | } |
Alexandre Julliard | 500a2f9 | 2003-03-20 04:29:21 +0000 | [diff] [blame] | 774 | LdrUnlockLoaderLock( 0, magic ); |
Eric Pouech | 84d1a8f | 2003-03-18 18:29:13 +0000 | [diff] [blame] | 775 | |
Alexandre Julliard | 03e4ea1 | 2003-03-18 20:07:12 +0000 | [diff] [blame] | 776 | TRACE( "%s\n", debugstr_w(lpFileName) ); |
| 777 | return strlenW(lpFileName); |
Alexandre Julliard | b1bac32 | 1996-12-15 19:45:59 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Eric Pouech | 32872b1 | 2003-03-14 05:01:20 +0000 | [diff] [blame] | 780 | /****************************************************************** |
| 781 | * load_library_as_datafile |
Bill Medland | 65fc1c9 | 2001-08-24 21:13:02 +0000 | [diff] [blame] | 782 | */ |
Alexandre Julliard | 803b568 | 2003-03-14 23:17:45 +0000 | [diff] [blame] | 783 | static BOOL load_library_as_datafile( LPCWSTR name, HMODULE* hmod) |
Bill Medland | 65fc1c9 | 2001-08-24 21:13:02 +0000 | [diff] [blame] | 784 | { |
Alexandre Julliard | 803b568 | 2003-03-14 23:17:45 +0000 | [diff] [blame] | 785 | static const WCHAR dotDLL[] = {'.','d','l','l',0}; |
| 786 | |
| 787 | WCHAR filenameW[MAX_PATH]; |
Eric Pouech | 32872b1 | 2003-03-14 05:01:20 +0000 | [diff] [blame] | 788 | HANDLE hFile = INVALID_HANDLE_VALUE; |
| 789 | HANDLE mapping; |
Alexandre Julliard | 803b568 | 2003-03-14 23:17:45 +0000 | [diff] [blame] | 790 | |
Eric Pouech | 32872b1 | 2003-03-14 05:01:20 +0000 | [diff] [blame] | 791 | *hmod = 0; |
Bill Medland | 65fc1c9 | 2001-08-24 21:13:02 +0000 | [diff] [blame] | 792 | |
Alexandre Julliard | 803b568 | 2003-03-14 23:17:45 +0000 | [diff] [blame] | 793 | if (SearchPathW( NULL, (LPCWSTR)name, dotDLL, sizeof(filenameW) / sizeof(filenameW[0]), |
| 794 | filenameW, NULL )) |
Bill Medland | 65fc1c9 | 2001-08-24 21:13:02 +0000 | [diff] [blame] | 795 | { |
Alexandre Julliard | 803b568 | 2003-03-14 23:17:45 +0000 | [diff] [blame] | 796 | hFile = CreateFileW( filenameW, GENERIC_READ, FILE_SHARE_READ, |
| 797 | NULL, OPEN_EXISTING, 0, 0 ); |
Eric Pouech | 32872b1 | 2003-03-14 05:01:20 +0000 | [diff] [blame] | 798 | } |
Eric Pouech | 32872b1 | 2003-03-14 05:01:20 +0000 | [diff] [blame] | 799 | if (hFile == INVALID_HANDLE_VALUE) return FALSE; |
| 800 | switch (MODULE_GetBinaryType( hFile )) |
| 801 | { |
| 802 | case BINARY_PE_EXE: |
| 803 | case BINARY_PE_DLL: |
Alexandre Julliard | 803b568 | 2003-03-14 23:17:45 +0000 | [diff] [blame] | 804 | mapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL ); |
Eric Pouech | 32872b1 | 2003-03-14 05:01:20 +0000 | [diff] [blame] | 805 | if (mapping) |
| 806 | { |
Alexandre Julliard | 803b568 | 2003-03-14 23:17:45 +0000 | [diff] [blame] | 807 | *hmod = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 ); |
Eric Pouech | 32872b1 | 2003-03-14 05:01:20 +0000 | [diff] [blame] | 808 | CloseHandle( mapping ); |
| 809 | } |
| 810 | break; |
| 811 | default: |
| 812 | break; |
| 813 | } |
| 814 | CloseHandle( hFile ); |
| 815 | |
| 816 | return *hmod != 0; |
Bill Medland | 65fc1c9 | 2001-08-24 21:13:02 +0000 | [diff] [blame] | 817 | } |
| 818 | |
Eric Pouech | 32872b1 | 2003-03-14 05:01:20 +0000 | [diff] [blame] | 819 | /****************************************************************** |
| 820 | * LoadLibraryExA (KERNEL32.@) |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 821 | * |
| 822 | * The HFILE parameter is not used and marked reserved in the SDK. I can |
| 823 | * only guess that it should force a file to be mapped, but I rather |
| 824 | * ignore the parameter because it would be extremely difficult to |
Andreas Mohr | 01c8ec3 | 2002-04-02 19:47:30 +0000 | [diff] [blame] | 825 | * integrate this with different types of module representations. |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 826 | * |
| 827 | */ |
Eric Pouech | 32872b1 | 2003-03-14 05:01:20 +0000 | [diff] [blame] | 828 | HMODULE WINAPI LoadLibraryExA(LPCSTR libname, HANDLE hfile, DWORD flags) |
Marcus Meissner | 8220bc9 | 1998-10-11 11:10:27 +0000 | [diff] [blame] | 829 | { |
Eric Pouech | 32872b1 | 2003-03-14 05:01:20 +0000 | [diff] [blame] | 830 | UNICODE_STRING wstr; |
| 831 | NTSTATUS nts; |
| 832 | HMODULE hModule; |
Peter Ganten | c7c4246 | 2000-08-28 21:33:28 +0000 | [diff] [blame] | 833 | |
Eric Pouech | 32872b1 | 2003-03-14 05:01:20 +0000 | [diff] [blame] | 834 | if (!libname) |
| 835 | { |
| 836 | SetLastError(ERROR_INVALID_PARAMETER); |
| 837 | return 0; |
| 838 | } |
Alexandre Julliard | 803b568 | 2003-03-14 23:17:45 +0000 | [diff] [blame] | 839 | RtlCreateUnicodeStringFromAsciiz( &wstr, libname ); |
Peter Ganten | c7c4246 | 2000-08-28 21:33:28 +0000 | [diff] [blame] | 840 | |
Eric Pouech | 32872b1 | 2003-03-14 05:01:20 +0000 | [diff] [blame] | 841 | if (flags & LOAD_LIBRARY_AS_DATAFILE) |
| 842 | { |
Alexandre Julliard | 803b568 | 2003-03-14 23:17:45 +0000 | [diff] [blame] | 843 | /* The method in load_library_as_datafile allows searching for the |
| 844 | * 'native' libraries only |
Eric Pouech | 32872b1 | 2003-03-14 05:01:20 +0000 | [diff] [blame] | 845 | */ |
Alexandre Julliard | 803b568 | 2003-03-14 23:17:45 +0000 | [diff] [blame] | 846 | if (load_library_as_datafile( wstr.Buffer, &hModule)) |
| 847 | { |
| 848 | RtlFreeUnicodeString( &wstr ); |
Eric Pouech | 32872b1 | 2003-03-14 05:01:20 +0000 | [diff] [blame] | 849 | return (HMODULE)((ULONG_PTR)hModule + 1); |
Alexandre Julliard | 803b568 | 2003-03-14 23:17:45 +0000 | [diff] [blame] | 850 | } |
Eric Pouech | 32872b1 | 2003-03-14 05:01:20 +0000 | [diff] [blame] | 851 | flags |= DONT_RESOLVE_DLL_REFERENCES; /* Just in case */ |
| 852 | /* Fallback to normal behaviour */ |
| 853 | } |
Peter Ganten | c7c4246 | 2000-08-28 21:33:28 +0000 | [diff] [blame] | 854 | |
Eric Pouech | 32872b1 | 2003-03-14 05:01:20 +0000 | [diff] [blame] | 855 | nts = LdrLoadDll(NULL, flags, &wstr, &hModule); |
| 856 | if (nts != STATUS_SUCCESS) |
| 857 | { |
| 858 | hModule = 0; |
| 859 | SetLastError( RtlNtStatusToDosError( nts ) ); |
| 860 | } |
| 861 | RtlFreeUnicodeString( &wstr ); |
Bill Medland | 65fc1c9 | 2001-08-24 21:13:02 +0000 | [diff] [blame] | 862 | |
Eric Pouech | 32872b1 | 2003-03-14 05:01:20 +0000 | [diff] [blame] | 863 | return hModule; |
| 864 | } |
Bill Medland | 65fc1c9 | 2001-08-24 21:13:02 +0000 | [diff] [blame] | 865 | |
Eric Pouech | 32872b1 | 2003-03-14 05:01:20 +0000 | [diff] [blame] | 866 | /*********************************************************************** |
| 867 | * LoadLibraryExW (KERNEL32.@) |
| 868 | */ |
| 869 | HMODULE WINAPI LoadLibraryExW(LPCWSTR libnameW, HANDLE hfile, DWORD flags) |
| 870 | { |
| 871 | UNICODE_STRING wstr; |
| 872 | NTSTATUS nts; |
| 873 | HMODULE hModule; |
Bill Medland | 65fc1c9 | 2001-08-24 21:13:02 +0000 | [diff] [blame] | 874 | |
Eric Pouech | 32872b1 | 2003-03-14 05:01:20 +0000 | [diff] [blame] | 875 | if (!libnameW) |
| 876 | { |
| 877 | SetLastError(ERROR_INVALID_PARAMETER); |
| 878 | return 0; |
| 879 | } |
Marcus Meissner | 8220bc9 | 1998-10-11 11:10:27 +0000 | [diff] [blame] | 880 | |
Eric Pouech | 32872b1 | 2003-03-14 05:01:20 +0000 | [diff] [blame] | 881 | if (flags & LOAD_LIBRARY_AS_DATAFILE) |
| 882 | { |
Alexandre Julliard | 803b568 | 2003-03-14 23:17:45 +0000 | [diff] [blame] | 883 | /* The method in load_library_as_datafile allows searching for the |
| 884 | * 'native' libraries only |
Eric Pouech | 32872b1 | 2003-03-14 05:01:20 +0000 | [diff] [blame] | 885 | */ |
Alexandre Julliard | 803b568 | 2003-03-14 23:17:45 +0000 | [diff] [blame] | 886 | if (load_library_as_datafile(libnameW, &hModule)) |
Eric Pouech | 32872b1 | 2003-03-14 05:01:20 +0000 | [diff] [blame] | 887 | return (HMODULE)((ULONG_PTR)hModule + 1); |
| 888 | flags |= DONT_RESOLVE_DLL_REFERENCES; /* Just in case */ |
| 889 | /* Fallback to normal behaviour */ |
| 890 | } |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 891 | |
Eric Pouech | 32872b1 | 2003-03-14 05:01:20 +0000 | [diff] [blame] | 892 | RtlInitUnicodeString( &wstr, libnameW ); |
| 893 | nts = LdrLoadDll(NULL, flags, &wstr, &hModule); |
| 894 | if (nts != STATUS_SUCCESS) |
| 895 | { |
| 896 | hModule = 0; |
| 897 | SetLastError( RtlNtStatusToDosError( nts ) ); |
| 898 | } |
| 899 | return hModule; |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | /*********************************************************************** |
Patrik Stridvall | 3ca9823 | 2001-06-20 23:03:14 +0000 | [diff] [blame] | 903 | * LoadLibraryA (KERNEL32.@) |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 904 | */ |
Eric Pouech | 32872b1 | 2003-03-14 05:01:20 +0000 | [diff] [blame] | 905 | HMODULE WINAPI LoadLibraryA(LPCSTR libname) |
| 906 | { |
| 907 | return LoadLibraryExA(libname, 0, 0); |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | /*********************************************************************** |
Patrik Stridvall | 3ca9823 | 2001-06-20 23:03:14 +0000 | [diff] [blame] | 911 | * LoadLibraryW (KERNEL32.@) |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 912 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 913 | HMODULE WINAPI LoadLibraryW(LPCWSTR libnameW) |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 914 | { |
Eric Pouech | 32872b1 | 2003-03-14 05:01:20 +0000 | [diff] [blame] | 915 | return LoadLibraryExW(libnameW, 0, 0); |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | /*********************************************************************** |
Patrik Stridvall | 01d5e5b | 2001-07-02 19:59:40 +0000 | [diff] [blame] | 919 | * LoadLibrary32 (KERNEL.452) |
| 920 | * LoadSystemLibrary32 (KERNEL.482) |
Ulrich Weigand | c3d9f28 | 1999-08-18 18:31:26 +0000 | [diff] [blame] | 921 | */ |
| 922 | HMODULE WINAPI LoadLibrary32_16( LPCSTR libname ) |
| 923 | { |
| 924 | HMODULE hModule; |
Alexandre Julliard | ab68797 | 2000-11-15 23:41:46 +0000 | [diff] [blame] | 925 | DWORD count; |
Ulrich Weigand | c3d9f28 | 1999-08-18 18:31:26 +0000 | [diff] [blame] | 926 | |
Alexandre Julliard | ab68797 | 2000-11-15 23:41:46 +0000 | [diff] [blame] | 927 | ReleaseThunkLock( &count ); |
Ulrich Weigand | c3d9f28 | 1999-08-18 18:31:26 +0000 | [diff] [blame] | 928 | hModule = LoadLibraryA( libname ); |
Alexandre Julliard | ab68797 | 2000-11-15 23:41:46 +0000 | [diff] [blame] | 929 | RestoreThunkLock( count ); |
Ulrich Weigand | c3d9f28 | 1999-08-18 18:31:26 +0000 | [diff] [blame] | 930 | return hModule; |
| 931 | } |
| 932 | |
| 933 | /*********************************************************************** |
Patrik Stridvall | 044855c | 2001-07-11 18:56:41 +0000 | [diff] [blame] | 934 | * FreeLibrary (KERNEL32.@) |
| 935 | * FreeLibrary32 (KERNEL.486) |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 936 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 937 | BOOL WINAPI FreeLibrary(HINSTANCE hLibModule) |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 938 | { |
Eric Pouech | 1efa50e | 2003-03-14 04:00:52 +0000 | [diff] [blame] | 939 | BOOL retv = FALSE; |
| 940 | NTSTATUS nts; |
Alexandre Julliard | 8ff37b8 | 2001-06-06 20:24:12 +0000 | [diff] [blame] | 941 | |
Eric Pouech | 32872b1 | 2003-03-14 05:01:20 +0000 | [diff] [blame] | 942 | if (!hLibModule) |
| 943 | { |
| 944 | SetLastError( ERROR_INVALID_HANDLE ); |
| 945 | return FALSE; |
| 946 | } |
| 947 | |
Alexandre Julliard | 8ff37b8 | 2001-06-06 20:24:12 +0000 | [diff] [blame] | 948 | if ((ULONG_PTR)hLibModule & 1) |
| 949 | { |
| 950 | /* this is a LOAD_LIBRARY_AS_DATAFILE module */ |
| 951 | char *ptr = (char *)hLibModule - 1; |
| 952 | UnmapViewOfFile( ptr ); |
| 953 | return TRUE; |
| 954 | } |
Bertho Stultiens | 1b34697 | 1999-04-11 14:52:24 +0000 | [diff] [blame] | 955 | |
Eric Pouech | 1efa50e | 2003-03-14 04:00:52 +0000 | [diff] [blame] | 956 | if ((nts = LdrUnloadDll( hLibModule )) == STATUS_SUCCESS) retv = TRUE; |
| 957 | else SetLastError( RtlNtStatusToDosError( nts ) ); |
Bertho Stultiens | 1b34697 | 1999-04-11 14:52:24 +0000 | [diff] [blame] | 958 | |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 959 | return retv; |
| 960 | } |
| 961 | |
| 962 | /*********************************************************************** |
Patrik Stridvall | 3ca9823 | 2001-06-20 23:03:14 +0000 | [diff] [blame] | 963 | * FreeLibraryAndExitThread (KERNEL32.@) |
Sergey Turchanov | 5ffd2df | 1999-03-22 12:35:48 +0000 | [diff] [blame] | 964 | */ |
| 965 | VOID WINAPI FreeLibraryAndExitThread(HINSTANCE hLibModule, DWORD dwExitCode) |
| 966 | { |
| 967 | FreeLibrary(hLibModule); |
| 968 | ExitThread(dwExitCode); |
| 969 | } |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 970 | |
| 971 | /*********************************************************************** |
Patrik Stridvall | 3ca9823 | 2001-06-20 23:03:14 +0000 | [diff] [blame] | 972 | * PrivateLoadLibrary (KERNEL32.@) |
Alexandre Julliard | e658d82 | 1997-11-30 17:45:40 +0000 | [diff] [blame] | 973 | * |
| 974 | * FIXME: rough guesswork, don't know what "Private" means |
| 975 | */ |
Alexandre Julliard | 8ff37b8 | 2001-06-06 20:24:12 +0000 | [diff] [blame] | 976 | HINSTANCE16 WINAPI PrivateLoadLibrary(LPCSTR libname) |
Alexandre Julliard | e658d82 | 1997-11-30 17:45:40 +0000 | [diff] [blame] | 977 | { |
Alexandre Julliard | 8ff37b8 | 2001-06-06 20:24:12 +0000 | [diff] [blame] | 978 | return LoadLibrary16(libname); |
Alexandre Julliard | e658d82 | 1997-11-30 17:45:40 +0000 | [diff] [blame] | 979 | } |
| 980 | |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 981 | /*********************************************************************** |
Patrik Stridvall | 3ca9823 | 2001-06-20 23:03:14 +0000 | [diff] [blame] | 982 | * PrivateFreeLibrary (KERNEL32.@) |
Alexandre Julliard | e658d82 | 1997-11-30 17:45:40 +0000 | [diff] [blame] | 983 | * |
| 984 | * FIXME: rough guesswork, don't know what "Private" means |
| 985 | */ |
Alexandre Julliard | 8ff37b8 | 2001-06-06 20:24:12 +0000 | [diff] [blame] | 986 | void WINAPI PrivateFreeLibrary(HINSTANCE16 handle) |
Alexandre Julliard | e658d82 | 1997-11-30 17:45:40 +0000 | [diff] [blame] | 987 | { |
Alexandre Julliard | 8ff37b8 | 2001-06-06 20:24:12 +0000 | [diff] [blame] | 988 | FreeLibrary16(handle); |
Alexandre Julliard | e658d82 | 1997-11-30 17:45:40 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | |
| 992 | /*********************************************************************** |
Patrik Stridvall | 01d5e5b | 2001-07-02 19:59:40 +0000 | [diff] [blame] | 993 | * GetProcAddress16 (KERNEL32.37) |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 994 | * Get procaddress in 16bit module from win32... (kernel32 undoc. ordinal func) |
| 995 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 996 | FARPROC16 WINAPI WIN32_GetProcAddress16( HMODULE hModule, LPCSTR name ) |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 997 | { |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 998 | if (!hModule) { |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 999 | WARN("hModule may not be 0!\n"); |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 1000 | return (FARPROC16)0; |
| 1001 | } |
Alexandre Julliard | dadf78f | 1998-05-17 17:13:43 +0000 | [diff] [blame] | 1002 | if (HIWORD(hModule)) |
| 1003 | { |
Andrew John Hughes | ed800c6 | 2002-11-21 03:45:01 +0000 | [diff] [blame] | 1004 | WARN("hModule is Win32 handle (%p)\n", hModule ); |
Alexandre Julliard | dadf78f | 1998-05-17 17:13:43 +0000 | [diff] [blame] | 1005 | return (FARPROC16)0; |
| 1006 | } |
Alexandre Julliard | 8ff37b8 | 2001-06-06 20:24:12 +0000 | [diff] [blame] | 1007 | return GetProcAddress16( LOWORD(hModule), name ); |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | /*********************************************************************** |
Patrik Stridvall | 01d5e5b | 2001-07-02 19:59:40 +0000 | [diff] [blame] | 1011 | * GetProcAddress (KERNEL.50) |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 1012 | */ |
Alexandre Julliard | ac7efef | 2000-11-27 21:54:01 +0000 | [diff] [blame] | 1013 | FARPROC16 WINAPI GetProcAddress16( HMODULE16 hModule, LPCSTR name ) |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 1014 | { |
| 1015 | WORD ordinal; |
Alexandre Julliard | ca22b33 | 1996-07-12 19:02:39 +0000 | [diff] [blame] | 1016 | FARPROC16 ret; |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 1017 | |
| 1018 | if (!hModule) hModule = GetCurrentTask(); |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 1019 | hModule = GetExePtr( hModule ); |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 1020 | |
| 1021 | if (HIWORD(name) != 0) |
| 1022 | { |
Alexandre Julliard | ac7efef | 2000-11-27 21:54:01 +0000 | [diff] [blame] | 1023 | ordinal = NE_GetOrdinal( hModule, name ); |
| 1024 | TRACE("%04x '%s'\n", hModule, name ); |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 1025 | } |
| 1026 | else |
| 1027 | { |
| 1028 | ordinal = LOWORD(name); |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 1029 | TRACE("%04x %04x\n", hModule, ordinal ); |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 1030 | } |
Alexandre Julliard | ca22b33 | 1996-07-12 19:02:39 +0000 | [diff] [blame] | 1031 | if (!ordinal) return (FARPROC16)0; |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 1032 | |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 1033 | ret = NE_GetEntryPoint( hModule, ordinal ); |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 1034 | |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 1035 | TRACE("returning %08x\n", (UINT)ret ); |
Alexandre Julliard | ca22b33 | 1996-07-12 19:02:39 +0000 | [diff] [blame] | 1036 | return ret; |
| 1037 | } |
| 1038 | |
| 1039 | |
| 1040 | /*********************************************************************** |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 1041 | * GetProcAddress (KERNEL32.@) |
Alexandre Julliard | ca22b33 | 1996-07-12 19:02:39 +0000 | [diff] [blame] | 1042 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1043 | FARPROC WINAPI GetProcAddress( HMODULE hModule, LPCSTR function ) |
Alexandre Julliard | ca22b33 | 1996-07-12 19:02:39 +0000 | [diff] [blame] | 1044 | { |
Eric Pouech | f3a73ef | 2003-03-06 23:41:37 +0000 | [diff] [blame] | 1045 | NTSTATUS nts; |
| 1046 | FARPROC fp; |
| 1047 | |
| 1048 | if (HIWORD(function)) |
| 1049 | { |
| 1050 | ANSI_STRING str; |
| 1051 | |
| 1052 | RtlInitAnsiString( &str, function ); |
| 1053 | nts = LdrGetProcedureAddress( hModule, &str, 0, (void**)&fp ); |
| 1054 | } |
| 1055 | else |
| 1056 | nts = LdrGetProcedureAddress( hModule, NULL, (DWORD)function, (void**)&fp ); |
| 1057 | if (nts != STATUS_SUCCESS) |
| 1058 | { |
| 1059 | SetLastError( RtlNtStatusToDosError( nts ) ); |
| 1060 | fp = NULL; |
| 1061 | } |
| 1062 | return fp; |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
Ulrich Weigand | a3527cf | 1998-10-11 19:31:10 +0000 | [diff] [blame] | 1065 | /*********************************************************************** |
Patrik Stridvall | 54fe838 | 2000-04-06 20:21:16 +0000 | [diff] [blame] | 1066 | * GetProcAddress32 (KERNEL.453) |
Ulrich Weigand | a3527cf | 1998-10-11 19:31:10 +0000 | [diff] [blame] | 1067 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1068 | FARPROC WINAPI GetProcAddress32_16( HMODULE hModule, LPCSTR function ) |
Ulrich Weigand | a3527cf | 1998-10-11 19:31:10 +0000 | [diff] [blame] | 1069 | { |
Eric Pouech | f3a73ef | 2003-03-06 23:41:37 +0000 | [diff] [blame] | 1070 | /* FIXME: we used to disable snoop when returning proc for Win16 subsystem */ |
| 1071 | return GetProcAddress( hModule, function ); |
Ulrich Weigand | a3527cf | 1998-10-11 19:31:10 +0000 | [diff] [blame] | 1072 | } |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 1073 | |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1074 | /*************************************************************************** |
| 1075 | * HasGPHandler (KERNEL.338) |
| 1076 | */ |
| 1077 | |
Patrik Stridvall | c7a8dde | 1999-04-25 12:36:53 +0000 | [diff] [blame] | 1078 | #include "pshpack1.h" |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1079 | typedef struct _GPHANDLERDEF |
| 1080 | { |
| 1081 | WORD selector; |
| 1082 | WORD rangeStart; |
| 1083 | WORD rangeEnd; |
| 1084 | WORD handler; |
| 1085 | } GPHANDLERDEF; |
Patrik Stridvall | c7a8dde | 1999-04-25 12:36:53 +0000 | [diff] [blame] | 1086 | #include "poppack.h" |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1087 | |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1088 | SEGPTR WINAPI HasGPHandler16( SEGPTR address ) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1089 | { |
| 1090 | HMODULE16 hModule; |
Ulrich Weigand | a3527cf | 1998-10-11 19:31:10 +0000 | [diff] [blame] | 1091 | int gpOrdinal; |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1092 | SEGPTR gpPtr; |
| 1093 | GPHANDLERDEF *gpHandler; |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 1094 | |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1095 | if ( (hModule = FarGetOwner16( SELECTOROF(address) )) != 0 |
Ulrich Weigand | a3527cf | 1998-10-11 19:31:10 +0000 | [diff] [blame] | 1096 | && (gpOrdinal = NE_GetOrdinal( hModule, "__GP" )) != 0 |
| 1097 | && (gpPtr = (SEGPTR)NE_GetEntryPointEx( hModule, gpOrdinal, FALSE )) != 0 |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1098 | && !IsBadReadPtr16( gpPtr, sizeof(GPHANDLERDEF) ) |
Alexandre Julliard | 982a223 | 2000-12-13 20:20:09 +0000 | [diff] [blame] | 1099 | && (gpHandler = MapSL( gpPtr )) != NULL ) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1100 | { |
| 1101 | while (gpHandler->selector) |
| 1102 | { |
| 1103 | if ( SELECTOROF(address) == gpHandler->selector |
| 1104 | && OFFSETOF(address) >= gpHandler->rangeStart |
| 1105 | && OFFSETOF(address) < gpHandler->rangeEnd ) |
Alexandre Julliard | 982a223 | 2000-12-13 20:20:09 +0000 | [diff] [blame] | 1106 | return MAKESEGPTR( gpHandler->selector, gpHandler->handler ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1107 | gpHandler++; |
| 1108 | } |
| 1109 | } |
| 1110 | |
| 1111 | return 0; |
| 1112 | } |