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" |
| 22 | |
Alexandre Julliard | a0b2b1d | 1997-11-16 17:38:29 +0000 | [diff] [blame] | 23 | #include <assert.h> |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 24 | #include <fcntl.h> |
| 25 | #include <stdlib.h> |
Jeremy White | d3e22d9 | 2000-02-10 19:03:02 +0000 | [diff] [blame] | 26 | #include <stdio.h> |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 27 | #include <string.h> |
| 28 | #include <sys/types.h> |
Patrik Stridvall | d016f81 | 2002-08-17 00:43:16 +0000 | [diff] [blame] | 29 | #ifdef HAVE_UNISTD_H |
| 30 | # include <unistd.h> |
| 31 | #endif |
Jeremy White | d3e22d9 | 2000-02-10 19:03:02 +0000 | [diff] [blame] | 32 | #include "wine/winbase16.h" |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 33 | #include "winerror.h" |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 34 | #include "heap.h" |
Aric Stewart | e4d0932 | 2000-12-03 03:14:29 +0000 | [diff] [blame] | 35 | #include "file.h" |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 36 | #include "module.h" |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 37 | #include "wine/debug.h" |
Alexandre Julliard | 37e9503 | 2001-07-19 00:39:09 +0000 | [diff] [blame] | 38 | #include "wine/server.h" |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 39 | |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 40 | WINE_DEFAULT_DEBUG_CHANNEL(module); |
| 41 | WINE_DECLARE_DEBUG_CHANNEL(win32); |
| 42 | WINE_DECLARE_DEBUG_CHANNEL(loaddll); |
Patrik Stridvall | b4b9fae | 1999-04-19 14:56:29 +0000 | [diff] [blame] | 43 | |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 44 | WINE_MODREF *MODULE_modref_list = NULL; |
| 45 | |
| 46 | static WINE_MODREF *exe_modref; |
| 47 | static int free_lib_count; /* recursion depth of FreeLibrary calls */ |
Alexandre Julliard | 014a8bb | 2000-12-20 18:41:34 +0000 | [diff] [blame] | 48 | static int process_detaching; /* set on process detach to avoid deadlocks with thread detach */ |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 49 | |
Alexandre Julliard | 6478164 | 2002-02-02 18:13:50 +0000 | [diff] [blame] | 50 | static CRITICAL_SECTION loader_section = CRITICAL_SECTION_INIT( "loader_section" ); |
| 51 | |
Alexandre Julliard | 5edf4e1 | 2001-07-26 20:12:54 +0000 | [diff] [blame] | 52 | /*********************************************************************** |
| 53 | * wait_input_idle |
| 54 | * |
| 55 | * Wrapper to call WaitForInputIdle USER function |
| 56 | */ |
| 57 | typedef DWORD (WINAPI *WaitForInputIdle_ptr)( HANDLE hProcess, DWORD dwTimeOut ); |
| 58 | |
| 59 | static DWORD wait_input_idle( HANDLE process, DWORD timeout ) |
| 60 | { |
| 61 | HMODULE mod = GetModuleHandleA( "user32.dll" ); |
| 62 | if (mod) |
| 63 | { |
| 64 | WaitForInputIdle_ptr ptr = (WaitForInputIdle_ptr)GetProcAddress( mod, "WaitForInputIdle" ); |
| 65 | if (ptr) return ptr( process, timeout ); |
| 66 | } |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 71 | /************************************************************************* |
| 72 | * MODULE32_LookupHMODULE |
| 73 | * looks for the referenced HMODULE in the current process |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 74 | * NOTE: Assumes that the process critical section is held! |
Alexandre Julliard | 329f068 | 1996-04-14 13:21:20 +0000 | [diff] [blame] | 75 | */ |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 76 | static WINE_MODREF *MODULE32_LookupHMODULE( HMODULE hmod ) |
Ulrich Weigand | 1d90d69 | 1999-02-24 14:27:07 +0000 | [diff] [blame] | 77 | { |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 78 | WINE_MODREF *wm; |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 79 | |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 80 | if (!hmod) |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 81 | return exe_modref; |
Ulrich Weigand | 1d90d69 | 1999-02-24 14:27:07 +0000 | [diff] [blame] | 82 | |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 83 | if (!HIWORD(hmod)) { |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 84 | ERR("tried to lookup 0x%04x in win32 module handler!\n",hmod); |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 85 | SetLastError( ERROR_INVALID_HANDLE ); |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 86 | return NULL; |
| 87 | } |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 88 | for ( wm = MODULE_modref_list; wm; wm=wm->next ) |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 89 | if (wm->module == hmod) |
| 90 | return wm; |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 91 | SetLastError( ERROR_INVALID_HANDLE ); |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 92 | return NULL; |
| 93 | } |
| 94 | |
Ulrich Weigand | a3527cf | 1998-10-11 19:31:10 +0000 | [diff] [blame] | 95 | /************************************************************************* |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 96 | * MODULE_AllocModRef |
| 97 | * |
| 98 | * Allocate a WINE_MODREF structure and add it to the process list |
| 99 | * NOTE: Assumes that the process critical section is held! |
| 100 | */ |
| 101 | WINE_MODREF *MODULE_AllocModRef( HMODULE hModule, LPCSTR filename ) |
| 102 | { |
| 103 | WINE_MODREF *wm; |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 104 | |
Alexandre Julliard | 5f728ca | 2001-07-24 21:45:22 +0000 | [diff] [blame] | 105 | DWORD long_len = strlen( filename ); |
| 106 | DWORD short_len = GetShortPathNameA( filename, NULL, 0 ); |
| 107 | |
| 108 | if ((wm = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, |
| 109 | sizeof(*wm) + long_len + short_len + 1 ))) |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 110 | { |
| 111 | wm->module = hModule; |
| 112 | wm->tlsindex = -1; |
| 113 | |
Alexandre Julliard | 5f728ca | 2001-07-24 21:45:22 +0000 | [diff] [blame] | 114 | wm->filename = wm->data; |
| 115 | memcpy( wm->filename, filename, long_len + 1 ); |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 116 | if ((wm->modname = strrchr( wm->filename, '\\' ))) wm->modname++; |
| 117 | else wm->modname = wm->filename; |
| 118 | |
Alexandre Julliard | 5f728ca | 2001-07-24 21:45:22 +0000 | [diff] [blame] | 119 | wm->short_filename = wm->filename + long_len + 1; |
| 120 | GetShortPathNameA( wm->filename, wm->short_filename, short_len + 1 ); |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 121 | if ((wm->short_modname = strrchr( wm->short_filename, '\\' ))) wm->short_modname++; |
| 122 | else wm->short_modname = wm->short_filename; |
| 123 | |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 124 | wm->next = MODULE_modref_list; |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 125 | if (wm->next) wm->next->prev = wm; |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 126 | MODULE_modref_list = wm; |
| 127 | |
Alexandre Julliard | a5dea21 | 2002-08-09 19:57:38 +0000 | [diff] [blame] | 128 | if (!(RtlImageNtHeader(hModule)->FileHeader.Characteristics & IMAGE_FILE_DLL)) |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 129 | { |
| 130 | if (!exe_modref) exe_modref = wm; |
| 131 | else FIXME( "Trying to load second .EXE file: %s\n", filename ); |
| 132 | } |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 133 | } |
| 134 | return wm; |
| 135 | } |
| 136 | |
| 137 | /************************************************************************* |
Andreas Mohr | 96293d4 | 2000-07-08 18:28:03 +0000 | [diff] [blame] | 138 | * MODULE_InitDLL |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 139 | */ |
Andreas Mohr | 96293d4 | 2000-07-08 18:28:03 +0000 | [diff] [blame] | 140 | static BOOL MODULE_InitDLL( WINE_MODREF *wm, DWORD type, LPVOID lpReserved ) |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 141 | { |
| 142 | BOOL retv = TRUE; |
| 143 | |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 144 | static LPCSTR typeName[] = { "PROCESS_DETACH", "PROCESS_ATTACH", |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 145 | "THREAD_ATTACH", "THREAD_DETACH" }; |
| 146 | assert( wm ); |
| 147 | |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 148 | /* Skip calls for modules loaded with special load flags */ |
| 149 | |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 150 | if (wm->flags & WINE_MODREF_DONT_RESOLVE_REFS) return TRUE; |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 151 | |
Alexandre Julliard | f93eb3e | 2000-04-28 20:26:35 +0000 | [diff] [blame] | 152 | TRACE("(%s,%s,%p) - CALL\n", wm->modname, typeName[type], lpReserved ); |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 153 | |
| 154 | /* Call the initialization routine */ |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 155 | retv = PE_InitDLL( wm->module, type, lpReserved ); |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 156 | |
Alexandre Julliard | f93eb3e | 2000-04-28 20:26:35 +0000 | [diff] [blame] | 157 | /* The state of the module list may have changed due to the call |
| 158 | to PE_InitDLL. We cannot assume that this module has not been |
| 159 | deleted. */ |
| 160 | TRACE("(%p,%s,%p) - RETURN %d\n", wm, typeName[type], lpReserved, retv ); |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 161 | |
| 162 | return retv; |
| 163 | } |
| 164 | |
| 165 | /************************************************************************* |
| 166 | * MODULE_DllProcessAttach |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 167 | * |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 168 | * Send the process attach notification to all DLLs the given module |
| 169 | * depends on (recursively). This is somewhat complicated due to the fact that |
Ulrich Weigand | a3527cf | 1998-10-11 19:31:10 +0000 | [diff] [blame] | 170 | * |
| 171 | * - we have to respect the module dependencies, i.e. modules implicitly |
| 172 | * referenced by another module have to be initialized before the module |
| 173 | * itself can be initialized |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 174 | * |
Ulrich Weigand | a3527cf | 1998-10-11 19:31:10 +0000 | [diff] [blame] | 175 | * - the initialization routine of a DLL can itself call LoadLibrary, |
| 176 | * thereby introducing a whole new set of dependencies (even involving |
| 177 | * the 'old' modules) at any time during the whole process |
| 178 | * |
| 179 | * (Note that this routine can be recursively entered not only directly |
| 180 | * from itself, but also via LoadLibrary from one of the called initialization |
| 181 | * routines.) |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 182 | * |
| 183 | * Furthermore, we need to rearrange the main WINE_MODREF list to allow |
| 184 | * the process *detach* notifications to be sent in the correct order. |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 185 | * This must not only take into account module dependencies, but also |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 186 | * 'hidden' dependencies created by modules calling LoadLibrary in their |
| 187 | * attach notification routine. |
| 188 | * |
| 189 | * The strategy is rather simple: we move a WINE_MODREF to the head of the |
| 190 | * list after the attach notification has returned. This implies that the |
| 191 | * detach notifications are called in the reverse of the sequence the attach |
| 192 | * notifications *returned*. |
Ulrich Weigand | a3527cf | 1998-10-11 19:31:10 +0000 | [diff] [blame] | 193 | */ |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 194 | BOOL MODULE_DllProcessAttach( WINE_MODREF *wm, LPVOID lpReserved ) |
Ulrich Weigand | a3527cf | 1998-10-11 19:31:10 +0000 | [diff] [blame] | 195 | { |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 196 | BOOL retv = TRUE; |
| 197 | int i; |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 198 | |
Alexandre Julliard | 6478164 | 2002-02-02 18:13:50 +0000 | [diff] [blame] | 199 | RtlEnterCriticalSection( &loader_section ); |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 200 | |
Alexandre Julliard | 6478164 | 2002-02-02 18:13:50 +0000 | [diff] [blame] | 201 | if (!wm) |
| 202 | { |
| 203 | wm = exe_modref; |
| 204 | PE_InitTls(); |
| 205 | } |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 206 | assert( wm ); |
Ulrich Weigand | a3527cf | 1998-10-11 19:31:10 +0000 | [diff] [blame] | 207 | |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 208 | /* prevent infinite recursion in case of cyclical dependencies */ |
| 209 | if ( ( wm->flags & WINE_MODREF_MARKER ) |
| 210 | || ( wm->flags & WINE_MODREF_PROCESS_ATTACHED ) ) |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 211 | goto done; |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 212 | |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 213 | TRACE("(%s,%p) - START\n", wm->modname, lpReserved ); |
Ulrich Weigand | a3527cf | 1998-10-11 19:31:10 +0000 | [diff] [blame] | 214 | |
| 215 | /* Tag current MODREF to prevent recursive loop */ |
Ulrich Weigand | e469a58 | 1999-03-27 16:45:57 +0000 | [diff] [blame] | 216 | wm->flags |= WINE_MODREF_MARKER; |
Ulrich Weigand | a3527cf | 1998-10-11 19:31:10 +0000 | [diff] [blame] | 217 | |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 218 | /* Recursively attach all DLLs this one depends on */ |
| 219 | for ( i = 0; retv && i < wm->nDeps; i++ ) |
| 220 | if ( wm->deps[i] ) |
| 221 | retv = MODULE_DllProcessAttach( wm->deps[i], lpReserved ); |
| 222 | |
| 223 | /* Call DLL entry point */ |
| 224 | if ( retv ) |
Ulrich Weigand | a3527cf | 1998-10-11 19:31:10 +0000 | [diff] [blame] | 225 | { |
Andreas Mohr | 96293d4 | 2000-07-08 18:28:03 +0000 | [diff] [blame] | 226 | retv = MODULE_InitDLL( wm, DLL_PROCESS_ATTACH, lpReserved ); |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 227 | if ( retv ) |
Ulrich Weigand | e469a58 | 1999-03-27 16:45:57 +0000 | [diff] [blame] | 228 | wm->flags |= WINE_MODREF_PROCESS_ATTACHED; |
Kevin Holbrook | a8f8bef | 1999-04-18 09:33:20 +0000 | [diff] [blame] | 229 | } |
Ulrich Weigand | e469a58 | 1999-03-27 16:45:57 +0000 | [diff] [blame] | 230 | |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 231 | /* Re-insert MODREF at head of list */ |
| 232 | if ( retv && wm->prev ) |
Kevin Holbrook | a8f8bef | 1999-04-18 09:33:20 +0000 | [diff] [blame] | 233 | { |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 234 | wm->prev->next = wm->next; |
| 235 | if ( wm->next ) wm->next->prev = wm->prev; |
| 236 | |
| 237 | wm->prev = NULL; |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 238 | wm->next = MODULE_modref_list; |
| 239 | MODULE_modref_list = wm->next->prev = wm; |
Kevin Holbrook | a8f8bef | 1999-04-18 09:33:20 +0000 | [diff] [blame] | 240 | } |
Ulrich Weigand | e469a58 | 1999-03-27 16:45:57 +0000 | [diff] [blame] | 241 | |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 242 | /* Remove recursion flag */ |
| 243 | wm->flags &= ~WINE_MODREF_MARKER; |
Ulrich Weigand | e469a58 | 1999-03-27 16:45:57 +0000 | [diff] [blame] | 244 | |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 245 | TRACE("(%s,%p) - END\n", wm->modname, lpReserved ); |
Ulrich Weigand | e469a58 | 1999-03-27 16:45:57 +0000 | [diff] [blame] | 246 | |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 247 | done: |
Alexandre Julliard | 6478164 | 2002-02-02 18:13:50 +0000 | [diff] [blame] | 248 | RtlLeaveCriticalSection( &loader_section ); |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 249 | return retv; |
Ulrich Weigand | ebc543c | 1998-10-23 09:37:23 +0000 | [diff] [blame] | 250 | } |
Ulrich Weigand | a3527cf | 1998-10-11 19:31:10 +0000 | [diff] [blame] | 251 | |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 252 | /************************************************************************* |
| 253 | * MODULE_DllProcessDetach |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 254 | * |
| 255 | * Send DLL process detach notifications. See the comment about calling |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 256 | * sequence at MODULE_DllProcessAttach. Unless the bForceDetach flag |
| 257 | * is set, only DLLs with zero refcount are notified. |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 258 | */ |
| 259 | void MODULE_DllProcessDetach( BOOL bForceDetach, LPVOID lpReserved ) |
Ulrich Weigand | ebc543c | 1998-10-23 09:37:23 +0000 | [diff] [blame] | 260 | { |
Ulrich Weigand | ebc543c | 1998-10-23 09:37:23 +0000 | [diff] [blame] | 261 | WINE_MODREF *wm; |
| 262 | |
Alexandre Julliard | 6478164 | 2002-02-02 18:13:50 +0000 | [diff] [blame] | 263 | RtlEnterCriticalSection( &loader_section ); |
Alexandre Julliard | 014a8bb | 2000-12-20 18:41:34 +0000 | [diff] [blame] | 264 | if (bForceDetach) process_detaching = 1; |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 265 | do |
| 266 | { |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 267 | for ( wm = MODULE_modref_list; wm; wm = wm->next ) |
Ulrich Weigand | ebc543c | 1998-10-23 09:37:23 +0000 | [diff] [blame] | 268 | { |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 269 | /* Check whether to detach this DLL */ |
| 270 | if ( !(wm->flags & WINE_MODREF_PROCESS_ATTACHED) ) |
| 271 | continue; |
| 272 | if ( wm->refCount > 0 && !bForceDetach ) |
| 273 | continue; |
| 274 | |
| 275 | /* Call detach notification */ |
| 276 | wm->flags &= ~WINE_MODREF_PROCESS_ATTACHED; |
Andreas Mohr | 96293d4 | 2000-07-08 18:28:03 +0000 | [diff] [blame] | 277 | MODULE_InitDLL( wm, DLL_PROCESS_DETACH, lpReserved ); |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 278 | |
| 279 | /* Restart at head of WINE_MODREF list, as entries might have |
| 280 | been added and/or removed while performing the call ... */ |
Ulrich Weigand | ebc543c | 1998-10-23 09:37:23 +0000 | [diff] [blame] | 281 | break; |
Ulrich Weigand | a3527cf | 1998-10-11 19:31:10 +0000 | [diff] [blame] | 282 | } |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 283 | } while ( wm ); |
Alexandre Julliard | 12f29b5 | 2000-03-17 15:16:57 +0000 | [diff] [blame] | 284 | |
Alexandre Julliard | 6478164 | 2002-02-02 18:13:50 +0000 | [diff] [blame] | 285 | RtlLeaveCriticalSection( &loader_section ); |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 286 | } |
Ulrich Weigand | ebc543c | 1998-10-23 09:37:23 +0000 | [diff] [blame] | 287 | |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 288 | /************************************************************************* |
| 289 | * MODULE_DllThreadAttach |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 290 | * |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 291 | * Send DLL thread attach notifications. These are sent in the |
| 292 | * reverse sequence of process detach notification. |
| 293 | * |
| 294 | */ |
| 295 | void MODULE_DllThreadAttach( LPVOID lpReserved ) |
| 296 | { |
| 297 | WINE_MODREF *wm; |
| 298 | |
Alexandre Julliard | 014a8bb | 2000-12-20 18:41:34 +0000 | [diff] [blame] | 299 | /* don't do any attach calls if process is exiting */ |
| 300 | if (process_detaching) return; |
| 301 | /* FIXME: there is still a race here */ |
| 302 | |
Alexandre Julliard | 6478164 | 2002-02-02 18:13:50 +0000 | [diff] [blame] | 303 | RtlEnterCriticalSection( &loader_section ); |
| 304 | |
| 305 | PE_InitTls(); |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 306 | |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 307 | for ( wm = MODULE_modref_list; wm; wm = wm->next ) |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 308 | if ( !wm->next ) |
| 309 | break; |
| 310 | |
| 311 | for ( ; wm; wm = wm->prev ) |
Ulrich Weigand | ebc543c | 1998-10-23 09:37:23 +0000 | [diff] [blame] | 312 | { |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 313 | if ( !(wm->flags & WINE_MODREF_PROCESS_ATTACHED) ) |
| 314 | continue; |
| 315 | if ( wm->flags & WINE_MODREF_NO_DLL_CALLS ) |
| 316 | continue; |
Ulrich Weigand | e469a58 | 1999-03-27 16:45:57 +0000 | [diff] [blame] | 317 | |
Andreas Mohr | 96293d4 | 2000-07-08 18:28:03 +0000 | [diff] [blame] | 318 | MODULE_InitDLL( wm, DLL_THREAD_ATTACH, lpReserved ); |
Ulrich Weigand | ebc543c | 1998-10-23 09:37:23 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Alexandre Julliard | 6478164 | 2002-02-02 18:13:50 +0000 | [diff] [blame] | 321 | RtlLeaveCriticalSection( &loader_section ); |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 322 | } |
Ulrich Weigand | ebc543c | 1998-10-23 09:37:23 +0000 | [diff] [blame] | 323 | |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 324 | /************************************************************************* |
| 325 | * MODULE_DllThreadDetach |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 326 | * |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 327 | * Send DLL thread detach notifications. These are sent in the |
| 328 | * same sequence as process detach notification. |
| 329 | * |
| 330 | */ |
| 331 | void MODULE_DllThreadDetach( LPVOID lpReserved ) |
| 332 | { |
| 333 | WINE_MODREF *wm; |
| 334 | |
Alexandre Julliard | 014a8bb | 2000-12-20 18:41:34 +0000 | [diff] [blame] | 335 | /* don't do any detach calls if process is exiting */ |
| 336 | if (process_detaching) return; |
| 337 | /* FIXME: there is still a race here */ |
| 338 | |
Alexandre Julliard | 6478164 | 2002-02-02 18:13:50 +0000 | [diff] [blame] | 339 | RtlEnterCriticalSection( &loader_section ); |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 340 | |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 341 | for ( wm = MODULE_modref_list; wm; wm = wm->next ) |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 342 | { |
| 343 | if ( !(wm->flags & WINE_MODREF_PROCESS_ATTACHED) ) |
| 344 | continue; |
| 345 | if ( wm->flags & WINE_MODREF_NO_DLL_CALLS ) |
| 346 | continue; |
| 347 | |
Andreas Mohr | 96293d4 | 2000-07-08 18:28:03 +0000 | [diff] [blame] | 348 | MODULE_InitDLL( wm, DLL_THREAD_DETACH, lpReserved ); |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Alexandre Julliard | 6478164 | 2002-02-02 18:13:50 +0000 | [diff] [blame] | 351 | RtlLeaveCriticalSection( &loader_section ); |
Ulrich Weigand | a3527cf | 1998-10-11 19:31:10 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Ulrich Weigand | e469a58 | 1999-03-27 16:45:57 +0000 | [diff] [blame] | 354 | /**************************************************************************** |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 355 | * DisableThreadLibraryCalls (KERNEL32.@) |
Ulrich Weigand | e469a58 | 1999-03-27 16:45:57 +0000 | [diff] [blame] | 356 | * |
| 357 | * Don't call DllEntryPoint for DLL_THREAD_{ATTACH,DETACH} if set. |
| 358 | */ |
| 359 | BOOL WINAPI DisableThreadLibraryCalls( HMODULE hModule ) |
| 360 | { |
Bertho Stultiens | 1b34697 | 1999-04-11 14:52:24 +0000 | [diff] [blame] | 361 | WINE_MODREF *wm; |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 362 | BOOL retval = TRUE; |
Ulrich Weigand | e469a58 | 1999-03-27 16:45:57 +0000 | [diff] [blame] | 363 | |
Alexandre Julliard | 6478164 | 2002-02-02 18:13:50 +0000 | [diff] [blame] | 364 | RtlEnterCriticalSection( &loader_section ); |
Ulrich Weigand | e469a58 | 1999-03-27 16:45:57 +0000 | [diff] [blame] | 365 | |
Bertho Stultiens | 1b34697 | 1999-04-11 14:52:24 +0000 | [diff] [blame] | 366 | wm = MODULE32_LookupHMODULE( hModule ); |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 367 | if ( !wm ) |
| 368 | retval = FALSE; |
| 369 | else |
| 370 | wm->flags |= WINE_MODREF_NO_DLL_CALLS; |
| 371 | |
Alexandre Julliard | 6478164 | 2002-02-02 18:13:50 +0000 | [diff] [blame] | 372 | RtlLeaveCriticalSection( &loader_section ); |
Ulrich Weigand | e469a58 | 1999-03-27 16:45:57 +0000 | [diff] [blame] | 373 | |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 374 | return retval; |
Ulrich Weigand | e469a58 | 1999-03-27 16:45:57 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Alexandre Julliard | a2f2e01 | 1995-06-06 16:40:35 +0000 | [diff] [blame] | 377 | |
| 378 | /*********************************************************************** |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 379 | * MODULE_CreateDummyModule |
| 380 | * |
| 381 | * Create a dummy NE module for Win32 or Winelib. |
| 382 | */ |
Alexandre Julliard | 8ff37b8 | 2001-06-06 20:24:12 +0000 | [diff] [blame] | 383 | HMODULE16 MODULE_CreateDummyModule( LPCSTR filename, HMODULE module32 ) |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 384 | { |
Alexandre Julliard | 8ff37b8 | 2001-06-06 20:24:12 +0000 | [diff] [blame] | 385 | HMODULE16 hModule; |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 386 | NE_MODULE *pModule; |
| 387 | SEGTABLEENTRY *pSegment; |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 388 | char *pStr,*s; |
Francois Gouget | baa9bf9 | 1999-12-27 05:24:06 +0000 | [diff] [blame] | 389 | unsigned int len; |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 390 | const char* basename; |
Ulrich Weigand | 237e8e9 | 1999-12-04 04:04:58 +0000 | [diff] [blame] | 391 | OFSTRUCT *ofs; |
| 392 | int of_size, size; |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 393 | |
Ulrich Weigand | 237e8e9 | 1999-12-04 04:04:58 +0000 | [diff] [blame] | 394 | /* Extract base filename */ |
| 395 | basename = strrchr(filename, '\\'); |
| 396 | if (!basename) basename = filename; |
| 397 | else basename++; |
| 398 | len = strlen(basename); |
| 399 | if ((s = strchr(basename, '.'))) len = s - basename; |
| 400 | |
| 401 | /* Allocate module */ |
| 402 | of_size = sizeof(OFSTRUCT) - sizeof(ofs->szPathName) |
| 403 | + strlen(filename) + 1; |
| 404 | size = sizeof(NE_MODULE) + |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 405 | /* loaded file info */ |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 406 | ((of_size + 3) & ~3) + |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 407 | /* segment table: DS,CS */ |
| 408 | 2 * sizeof(SEGTABLEENTRY) + |
| 409 | /* name table */ |
Ulrich Weigand | 237e8e9 | 1999-12-04 04:04:58 +0000 | [diff] [blame] | 410 | len + 2 + |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 411 | /* several empty tables */ |
| 412 | 8; |
| 413 | |
| 414 | hModule = GlobalAlloc16( GMEM_MOVEABLE | GMEM_ZEROINIT, size ); |
Alexandre Julliard | 8ff37b8 | 2001-06-06 20:24:12 +0000 | [diff] [blame] | 415 | if (!hModule) return (HMODULE16)11; /* invalid exe */ |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 416 | |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 417 | FarSetOwner16( hModule, hModule ); |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 418 | pModule = (NE_MODULE *)GlobalLock16( hModule ); |
| 419 | |
| 420 | /* Set all used entries */ |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 421 | pModule->magic = IMAGE_OS2_SIGNATURE; |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 422 | pModule->count = 1; |
| 423 | pModule->next = 0; |
| 424 | pModule->flags = 0; |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 425 | pModule->dgroup = 0; |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 426 | pModule->ss = 1; |
| 427 | pModule->cs = 2; |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 428 | pModule->heap_size = 0; |
| 429 | pModule->stack_size = 0; |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 430 | pModule->seg_count = 2; |
| 431 | pModule->modref_count = 0; |
| 432 | pModule->nrname_size = 0; |
| 433 | pModule->fileinfo = sizeof(NE_MODULE); |
| 434 | pModule->os_flags = NE_OSFLAGS_WINDOWS; |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 435 | pModule->self = hModule; |
Alexandre Julliard | b445952 | 2000-04-15 21:00:55 +0000 | [diff] [blame] | 436 | pModule->module32 = module32; |
| 437 | |
| 438 | /* Set version and flags */ |
| 439 | if (module32) |
| 440 | { |
Alexandre Julliard | a5dea21 | 2002-08-09 19:57:38 +0000 | [diff] [blame] | 441 | IMAGE_NT_HEADERS *nt = RtlImageNtHeader( module32 ); |
| 442 | pModule->expected_version = ((nt->OptionalHeader.MajorSubsystemVersion & 0xff) << 8 ) | |
| 443 | (nt->OptionalHeader.MinorSubsystemVersion & 0xff); |
Alexandre Julliard | b445952 | 2000-04-15 21:00:55 +0000 | [diff] [blame] | 444 | pModule->flags |= NE_FFLAGS_WIN32; |
Alexandre Julliard | a5dea21 | 2002-08-09 19:57:38 +0000 | [diff] [blame] | 445 | if (nt->FileHeader.Characteristics & IMAGE_FILE_DLL) |
Alexandre Julliard | b445952 | 2000-04-15 21:00:55 +0000 | [diff] [blame] | 446 | pModule->flags |= NE_FFLAGS_LIBMODULE | NE_FFLAGS_SINGLEDATA; |
| 447 | } |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 448 | |
| 449 | /* Set loaded file information */ |
Ulrich Weigand | 237e8e9 | 1999-12-04 04:04:58 +0000 | [diff] [blame] | 450 | ofs = (OFSTRUCT *)(pModule + 1); |
| 451 | memset( ofs, 0, of_size ); |
| 452 | ofs->cBytes = of_size < 256 ? of_size : 255; /* FIXME */ |
| 453 | strcpy( ofs->szPathName, filename ); |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 454 | |
Ulrich Weigand | acefd16 | 2000-12-29 05:09:15 +0000 | [diff] [blame] | 455 | pSegment = (SEGTABLEENTRY*)((char*)(pModule + 1) + ((of_size + 3) & ~3)); |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 456 | pModule->seg_table = (int)pSegment - (int)pModule; |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 457 | /* Data segment */ |
| 458 | pSegment->size = 0; |
| 459 | pSegment->flags = NE_SEGFLAGS_DATA; |
| 460 | pSegment->minsize = 0x1000; |
| 461 | pSegment++; |
| 462 | /* Code segment */ |
| 463 | pSegment->flags = 0; |
| 464 | pSegment++; |
| 465 | |
| 466 | /* Module name */ |
| 467 | pStr = (char *)pSegment; |
| 468 | pModule->name_table = (int)pStr - (int)pModule; |
Francois Gouget | baa9bf9 | 1999-12-27 05:24:06 +0000 | [diff] [blame] | 469 | assert(len<256); |
Alexandre Julliard | 17216f5 | 1997-10-12 16:30:17 +0000 | [diff] [blame] | 470 | *pStr = len; |
Francois Gouget | baa9bf9 | 1999-12-27 05:24:06 +0000 | [diff] [blame] | 471 | lstrcpynA( pStr+1, basename, len+1 ); |
Ulrich Weigand | 237e8e9 | 1999-12-04 04:04:58 +0000 | [diff] [blame] | 472 | pStr += len+2; |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 473 | |
| 474 | /* All tables zero terminated */ |
| 475 | pModule->res_table = pModule->import_table = pModule->entry_table = |
| 476 | (int)pStr - (int)pModule; |
| 477 | |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 478 | NE_RegisterModule( pModule ); |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 479 | return hModule; |
| 480 | } |
| 481 | |
| 482 | |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 483 | /********************************************************************** |
Peter Ganten | c7c4246 | 2000-08-28 21:33:28 +0000 | [diff] [blame] | 484 | * MODULE_FindModule |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 485 | * |
| 486 | * Find a (loaded) win32 module depending on path |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 487 | * |
| 488 | * RETURNS |
| 489 | * the module handle if found |
| 490 | * 0 if not |
| 491 | */ |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 492 | WINE_MODREF *MODULE_FindModule( |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 493 | LPCSTR path /* [in] pathname of module/library to be found */ |
| 494 | ) { |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 495 | WINE_MODREF *wm; |
Ulrich Weigand | 237e8e9 | 1999-12-04 04:04:58 +0000 | [diff] [blame] | 496 | char dllname[260], *p; |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 497 | |
Ulrich Weigand | 237e8e9 | 1999-12-04 04:04:58 +0000 | [diff] [blame] | 498 | /* Append .DLL to name if no extension present */ |
| 499 | strcpy( dllname, path ); |
| 500 | if (!(p = strrchr( dllname, '.')) || strchr( p, '/' ) || strchr( p, '\\')) |
| 501 | strcat( dllname, ".DLL" ); |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 502 | |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 503 | for ( wm = MODULE_modref_list; wm; wm = wm->next ) |
Ulrich Weigand | 237e8e9 | 1999-12-04 04:04:58 +0000 | [diff] [blame] | 504 | { |
Aric Stewart | e4d0932 | 2000-12-03 03:14:29 +0000 | [diff] [blame] | 505 | if ( !FILE_strcasecmp( dllname, wm->modname ) ) |
Ulrich Weigand | 237e8e9 | 1999-12-04 04:04:58 +0000 | [diff] [blame] | 506 | break; |
Aric Stewart | e4d0932 | 2000-12-03 03:14:29 +0000 | [diff] [blame] | 507 | if ( !FILE_strcasecmp( dllname, wm->filename ) ) |
Ulrich Weigand | 237e8e9 | 1999-12-04 04:04:58 +0000 | [diff] [blame] | 508 | break; |
Aric Stewart | e4d0932 | 2000-12-03 03:14:29 +0000 | [diff] [blame] | 509 | if ( !FILE_strcasecmp( dllname, wm->short_modname ) ) |
Ulrich Weigand | 237e8e9 | 1999-12-04 04:04:58 +0000 | [diff] [blame] | 510 | break; |
Aric Stewart | e4d0932 | 2000-12-03 03:14:29 +0000 | [diff] [blame] | 511 | if ( !FILE_strcasecmp( dllname, wm->short_filename ) ) |
Ulrich Weigand | 237e8e9 | 1999-12-04 04:04:58 +0000 | [diff] [blame] | 512 | break; |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 513 | } |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 514 | |
Ulrich Weigand | 237e8e9 | 1999-12-04 04:04:58 +0000 | [diff] [blame] | 515 | return wm; |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Andreas Mohr | b021fe2 | 2000-07-26 18:02:28 +0000 | [diff] [blame] | 518 | |
| 519 | /* Check whether a file is an OS/2 or a very old Windows executable |
| 520 | * by testing on import of KERNEL. |
| 521 | * |
| 522 | * FIXME: is reading the module imports the only way of discerning |
| 523 | * old Windows binaries from OS/2 ones ? At least it seems so... |
| 524 | */ |
Alexandre Julliard | 3536316 | 2002-05-22 21:32:49 +0000 | [diff] [blame] | 525 | static enum binary_type MODULE_Decide_OS2_OldWin(HANDLE hfile, const IMAGE_DOS_HEADER *mz, |
| 526 | const IMAGE_OS2_HEADER *ne) |
Andreas Mohr | b021fe2 | 2000-07-26 18:02:28 +0000 | [diff] [blame] | 527 | { |
| 528 | DWORD currpos = SetFilePointer( hfile, 0, NULL, SEEK_CUR); |
Alexandre Julliard | 3536316 | 2002-05-22 21:32:49 +0000 | [diff] [blame] | 529 | enum binary_type ret = BINARY_OS216; |
Andreas Mohr | b021fe2 | 2000-07-26 18:02:28 +0000 | [diff] [blame] | 530 | LPWORD modtab = NULL; |
| 531 | LPSTR nametab = NULL; |
| 532 | DWORD len; |
| 533 | int i; |
| 534 | |
| 535 | /* read modref table */ |
| 536 | if ( (SetFilePointer( hfile, mz->e_lfanew + ne->ne_modtab, NULL, SEEK_SET ) == -1) |
| 537 | || (!(modtab = HeapAlloc( GetProcessHeap(), 0, ne->ne_cmod*sizeof(WORD)))) |
| 538 | || (!(ReadFile(hfile, modtab, ne->ne_cmod*sizeof(WORD), &len, NULL))) |
| 539 | || (len != ne->ne_cmod*sizeof(WORD)) ) |
| 540 | goto broken; |
| 541 | |
| 542 | /* read imported names table */ |
| 543 | if ( (SetFilePointer( hfile, mz->e_lfanew + ne->ne_imptab, NULL, SEEK_SET ) == -1) |
| 544 | || (!(nametab = HeapAlloc( GetProcessHeap(), 0, ne->ne_enttab - ne->ne_imptab))) |
| 545 | || (!(ReadFile(hfile, nametab, ne->ne_enttab - ne->ne_imptab, &len, NULL))) |
| 546 | || (len != ne->ne_enttab - ne->ne_imptab) ) |
| 547 | goto broken; |
| 548 | |
| 549 | for (i=0; i < ne->ne_cmod; i++) |
| 550 | { |
| 551 | LPSTR module = &nametab[modtab[i]]; |
| 552 | TRACE("modref: %.*s\n", module[0], &module[1]); |
| 553 | if (!(strncmp(&module[1], "KERNEL", module[0]))) |
| 554 | { /* very old Windows file */ |
| 555 | 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] | 556 | ret = BINARY_WIN16; |
Andreas Mohr | b021fe2 | 2000-07-26 18:02:28 +0000 | [diff] [blame] | 557 | goto good; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | broken: |
| 562 | ERR("Hmm, an error occurred. Is this binary file broken ?\n"); |
| 563 | |
| 564 | good: |
| 565 | HeapFree( GetProcessHeap(), 0, modtab); |
| 566 | HeapFree( GetProcessHeap(), 0, nametab); |
| 567 | SetFilePointer( hfile, currpos, NULL, SEEK_SET); /* restore filepos */ |
Alexandre Julliard | 3536316 | 2002-05-22 21:32:49 +0000 | [diff] [blame] | 568 | return ret; |
Andreas Mohr | b021fe2 | 2000-07-26 18:02:28 +0000 | [diff] [blame] | 569 | } |
| 570 | |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 571 | /*********************************************************************** |
| 572 | * MODULE_GetBinaryType |
Alexandre Julliard | 3536316 | 2002-05-22 21:32:49 +0000 | [diff] [blame] | 573 | */ |
| 574 | enum binary_type MODULE_GetBinaryType( HANDLE hfile ) |
| 575 | { |
| 576 | union |
| 577 | { |
| 578 | struct |
| 579 | { |
| 580 | unsigned char magic[4]; |
| 581 | unsigned char ignored[12]; |
| 582 | unsigned short type; |
| 583 | } elf; |
| 584 | IMAGE_DOS_HEADER mz; |
| 585 | } header; |
| 586 | |
| 587 | char magic[4]; |
| 588 | DWORD len; |
| 589 | |
| 590 | /* Seek to the start of the file and read the header information. */ |
| 591 | if (SetFilePointer( hfile, 0, NULL, SEEK_SET ) == -1) |
| 592 | return BINARY_UNKNOWN; |
| 593 | if (!ReadFile( hfile, &header, sizeof(header), &len, NULL ) || len != sizeof(header)) |
| 594 | return BINARY_UNKNOWN; |
| 595 | |
| 596 | if (!memcmp( header.elf.magic, "\177ELF", 4 )) |
| 597 | { |
| 598 | /* FIXME: we don't bother to check byte order, architecture, etc. */ |
| 599 | switch(header.elf.type) |
| 600 | { |
| 601 | case 2: return BINARY_UNIX_EXE; |
| 602 | case 3: return BINARY_UNIX_LIB; |
| 603 | } |
| 604 | return BINARY_UNKNOWN; |
| 605 | } |
| 606 | |
| 607 | /* Not ELF, try DOS */ |
| 608 | |
| 609 | if (header.mz.e_magic == IMAGE_DOS_SIGNATURE) |
| 610 | { |
| 611 | /* We do have a DOS image so we will now try to seek into |
| 612 | * the file by the amount indicated by the field |
| 613 | * "Offset to extended header" and read in the |
| 614 | * "magic" field information at that location. |
| 615 | * This will tell us if there is more header information |
| 616 | * to read or not. |
| 617 | */ |
| 618 | /* But before we do we will make sure that header |
| 619 | * structure encompasses the "Offset to extended header" |
| 620 | * field. |
| 621 | */ |
| 622 | if ((header.mz.e_cparhdr << 4) < sizeof(IMAGE_DOS_HEADER)) |
| 623 | return BINARY_DOS; |
| 624 | if (header.mz.e_crlc && (header.mz.e_lfarlc < sizeof(IMAGE_DOS_HEADER))) |
| 625 | return BINARY_DOS; |
| 626 | if (header.mz.e_lfanew < sizeof(IMAGE_DOS_HEADER)) |
| 627 | return BINARY_DOS; |
| 628 | if (SetFilePointer( hfile, header.mz.e_lfanew, NULL, SEEK_SET ) == -1) |
| 629 | return BINARY_DOS; |
| 630 | if (!ReadFile( hfile, magic, sizeof(magic), &len, NULL ) || len != sizeof(magic)) |
| 631 | return BINARY_DOS; |
| 632 | |
| 633 | /* Reading the magic field succeeded so |
| 634 | * we will try to determine what type it is. |
| 635 | */ |
| 636 | if (!memcmp( magic, "PE\0\0", 4 )) |
| 637 | { |
Dmitry Timoshkov | 1467bbd | 2002-08-27 00:34:41 +0000 | [diff] [blame] | 638 | IMAGE_FILE_HEADER FileHeader; |
Alexandre Julliard | 3536316 | 2002-05-22 21:32:49 +0000 | [diff] [blame] | 639 | |
Dmitry Timoshkov | 1467bbd | 2002-08-27 00:34:41 +0000 | [diff] [blame] | 640 | if (ReadFile( hfile, &FileHeader, sizeof(FileHeader), &len, NULL ) && len == sizeof(FileHeader)) |
Alexandre Julliard | 3536316 | 2002-05-22 21:32:49 +0000 | [diff] [blame] | 641 | { |
Dmitry Timoshkov | 1467bbd | 2002-08-27 00:34:41 +0000 | [diff] [blame] | 642 | if (FileHeader.Characteristics & IMAGE_FILE_DLL) return BINARY_PE_DLL; |
Alexandre Julliard | 3536316 | 2002-05-22 21:32:49 +0000 | [diff] [blame] | 643 | return BINARY_PE_EXE; |
| 644 | } |
Dmitry Timoshkov | 1467bbd | 2002-08-27 00:34:41 +0000 | [diff] [blame] | 645 | return BINARY_DOS; |
Alexandre Julliard | 3536316 | 2002-05-22 21:32:49 +0000 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | if (!memcmp( magic, "NE", 2 )) |
| 649 | { |
| 650 | /* This is a Windows executable (NE) header. This can |
| 651 | * mean either a 16-bit OS/2 or a 16-bit Windows or even a |
| 652 | * DOS program (running under a DOS extender). To decide |
| 653 | * which, we'll have to read the NE header. |
| 654 | */ |
| 655 | IMAGE_OS2_HEADER ne; |
| 656 | if ( SetFilePointer( hfile, header.mz.e_lfanew, NULL, SEEK_SET ) != -1 |
| 657 | && ReadFile( hfile, &ne, sizeof(ne), &len, NULL ) |
| 658 | && len == sizeof(ne) ) |
| 659 | { |
| 660 | switch ( ne.ne_exetyp ) |
| 661 | { |
| 662 | case 2: return BINARY_WIN16; |
| 663 | case 5: return BINARY_DOS; |
| 664 | default: return MODULE_Decide_OS2_OldWin(hfile, &header.mz, &ne); |
| 665 | } |
| 666 | } |
| 667 | /* Couldn't read header, so abort. */ |
Dmitry Timoshkov | 1467bbd | 2002-08-27 00:34:41 +0000 | [diff] [blame] | 668 | return BINARY_DOS; |
Alexandre Julliard | 3536316 | 2002-05-22 21:32:49 +0000 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | /* Unknown extended header, but this file is nonetheless DOS-executable. */ |
| 672 | return BINARY_DOS; |
| 673 | } |
| 674 | |
| 675 | return BINARY_UNKNOWN; |
| 676 | } |
| 677 | |
| 678 | /*********************************************************************** |
| 679 | * GetBinaryTypeA [KERNEL32.@] |
| 680 | * GetBinaryType [KERNEL32.@] |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 681 | * |
| 682 | * The GetBinaryType function determines whether a file is executable |
| 683 | * or not and if it is it returns what type of executable it is. |
| 684 | * The type of executable is a property that determines in which |
| 685 | * subsystem an executable file runs under. |
| 686 | * |
| 687 | * Binary types returned: |
| 688 | * SCS_32BIT_BINARY: A Win32 based application |
| 689 | * SCS_DOS_BINARY: An MS-Dos based application |
| 690 | * SCS_WOW_BINARY: A Win16 based application |
| 691 | * SCS_PIF_BINARY: A PIF file that executes an MS-Dos based app |
| 692 | * SCS_POSIX_BINARY: A POSIX based application ( Not implemented ) |
Ulrich Weigand | d523e4d | 1999-06-07 17:37:43 +0000 | [diff] [blame] | 693 | * SCS_OS216_BINARY: A 16bit OS/2 based application |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 694 | * |
| 695 | * Returns TRUE if the file is an executable in which case |
| 696 | * the value pointed by lpBinaryType is set. |
| 697 | * Returns FALSE if the file is not an executable or if the function fails. |
| 698 | * |
| 699 | * 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] | 700 | * if the extended header information is not present it will |
| 701 | * assume that the file is a DOS executable. |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 702 | * If the extended header information is present it will |
Andreas Mohr | 8952dea | 1999-12-12 20:16:42 +0000 | [diff] [blame] | 703 | * determine if the file is a 16 or 32 bit Windows executable |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 704 | * by check the flags in the header. |
| 705 | * |
| 706 | * Note that .COM and .PIF files are only recognized by their |
| 707 | * file name extension; but Windows does it the same way ... |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 708 | */ |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 709 | BOOL WINAPI GetBinaryTypeA( LPCSTR lpApplicationName, LPDWORD lpBinaryType ) |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 710 | { |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 711 | BOOL ret = FALSE; |
Ulrich Weigand | 237e8e9 | 1999-12-04 04:04:58 +0000 | [diff] [blame] | 712 | HANDLE hfile; |
Alexandre Julliard | 3536316 | 2002-05-22 21:32:49 +0000 | [diff] [blame] | 713 | char *ptr; |
Alexandre Julliard | a0d7731 | 1998-09-13 16:32:00 +0000 | [diff] [blame] | 714 | |
Alexandre Julliard | 06c275a | 1999-05-02 14:32:27 +0000 | [diff] [blame] | 715 | TRACE_(win32)("%s\n", lpApplicationName ); |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 716 | |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 717 | /* Sanity check. |
| 718 | */ |
| 719 | if ( lpApplicationName == NULL || lpBinaryType == NULL ) |
| 720 | return FALSE; |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 721 | |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 722 | /* Open the file indicated by lpApplicationName for reading. |
| 723 | */ |
Alexandre Julliard | e333212 | 2000-06-08 01:00:16 +0000 | [diff] [blame] | 724 | hfile = CreateFileA( lpApplicationName, GENERIC_READ, FILE_SHARE_READ, |
François Gouget | da2b6a9 | 2001-01-06 01:29:18 +0000 | [diff] [blame] | 725 | NULL, OPEN_EXISTING, 0, 0 ); |
Ulrich Weigand | 237e8e9 | 1999-12-04 04:04:58 +0000 | [diff] [blame] | 726 | if ( hfile == INVALID_HANDLE_VALUE ) |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 727 | return FALSE; |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 728 | |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 729 | /* Check binary type |
| 730 | */ |
Alexandre Julliard | 3536316 | 2002-05-22 21:32:49 +0000 | [diff] [blame] | 731 | switch(MODULE_GetBinaryType( hfile )) |
| 732 | { |
| 733 | case BINARY_UNKNOWN: |
| 734 | /* try to determine from file name */ |
| 735 | ptr = strrchr( lpApplicationName, '.' ); |
| 736 | if (!ptr) break; |
| 737 | if (!FILE_strcasecmp( ptr, ".COM" )) |
| 738 | { |
| 739 | *lpBinaryType = SCS_DOS_BINARY; |
| 740 | ret = TRUE; |
| 741 | } |
| 742 | else if (!FILE_strcasecmp( ptr, ".PIF" )) |
| 743 | { |
| 744 | *lpBinaryType = SCS_PIF_BINARY; |
| 745 | ret = TRUE; |
| 746 | } |
| 747 | break; |
| 748 | case BINARY_PE_EXE: |
| 749 | case BINARY_PE_DLL: |
| 750 | *lpBinaryType = SCS_32BIT_BINARY; |
| 751 | ret = TRUE; |
| 752 | break; |
| 753 | case BINARY_WIN16: |
| 754 | *lpBinaryType = SCS_WOW_BINARY; |
| 755 | ret = TRUE; |
| 756 | break; |
| 757 | case BINARY_OS216: |
| 758 | *lpBinaryType = SCS_OS216_BINARY; |
| 759 | ret = TRUE; |
| 760 | break; |
| 761 | case BINARY_DOS: |
| 762 | *lpBinaryType = SCS_DOS_BINARY; |
| 763 | ret = TRUE; |
| 764 | break; |
| 765 | case BINARY_UNIX_EXE: |
| 766 | case BINARY_UNIX_LIB: |
| 767 | ret = FALSE; |
| 768 | break; |
| 769 | } |
Alexandre Julliard | a0d7731 | 1998-09-13 16:32:00 +0000 | [diff] [blame] | 770 | |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 771 | CloseHandle( hfile ); |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 772 | return ret; |
| 773 | } |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 774 | |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 775 | /*********************************************************************** |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 776 | * GetBinaryTypeW [KERNEL32.@] |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 777 | */ |
| 778 | BOOL WINAPI GetBinaryTypeW( LPCWSTR lpApplicationName, LPDWORD lpBinaryType ) |
| 779 | { |
| 780 | BOOL ret = FALSE; |
| 781 | LPSTR strNew = NULL; |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 782 | |
Alexandre Julliard | 06c275a | 1999-05-02 14:32:27 +0000 | [diff] [blame] | 783 | TRACE_(win32)("%s\n", debugstr_w(lpApplicationName) ); |
Alexandre Julliard | a0d7731 | 1998-09-13 16:32:00 +0000 | [diff] [blame] | 784 | |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 785 | /* Sanity check. |
| 786 | */ |
| 787 | if ( lpApplicationName == NULL || lpBinaryType == NULL ) |
| 788 | return FALSE; |
| 789 | |
| 790 | /* Convert the wide string to a ascii string. |
| 791 | */ |
| 792 | strNew = HEAP_strdupWtoA( GetProcessHeap(), 0, lpApplicationName ); |
| 793 | |
| 794 | if ( strNew != NULL ) |
Alexandre Julliard | a0d7731 | 1998-09-13 16:32:00 +0000 | [diff] [blame] | 795 | { |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 796 | ret = GetBinaryTypeA( strNew, lpBinaryType ); |
| 797 | |
| 798 | /* Free the allocated string. |
| 799 | */ |
| 800 | HeapFree( GetProcessHeap(), 0, strNew ); |
Alexandre Julliard | a0d7731 | 1998-09-13 16:32:00 +0000 | [diff] [blame] | 801 | } |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 802 | |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 803 | return ret; |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 804 | } |
| 805 | |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 806 | |
| 807 | /*********************************************************************** |
Patrik Stridvall | 044855c | 2001-07-11 18:56:41 +0000 | [diff] [blame] | 808 | * WinExec (KERNEL.166) |
| 809 | * WinExec16 (KERNEL32.@) |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 810 | */ |
| 811 | HINSTANCE16 WINAPI WinExec16( LPCSTR lpCmdLine, UINT16 nCmdShow ) |
| 812 | { |
Andreas Mohr | 7096384 | 2000-09-22 22:08:28 +0000 | [diff] [blame] | 813 | LPCSTR p, args = NULL; |
| 814 | LPCSTR name_beg, name_end; |
Alexandre Julliard | c192ba2 | 2000-05-29 21:25:10 +0000 | [diff] [blame] | 815 | LPSTR name, cmdline; |
Andreas Mohr | 7096384 | 2000-09-22 22:08:28 +0000 | [diff] [blame] | 816 | int arglen; |
Alexandre Julliard | c192ba2 | 2000-05-29 21:25:10 +0000 | [diff] [blame] | 817 | HINSTANCE16 ret; |
| 818 | char buffer[MAX_PATH]; |
Ulrich Weigand | 6ce4006 | 1999-05-03 09:22:55 +0000 | [diff] [blame] | 819 | |
Andreas Mohr | 7096384 | 2000-09-22 22:08:28 +0000 | [diff] [blame] | 820 | if (*lpCmdLine == '"') /* has to be only one and only at beginning ! */ |
Alexandre Julliard | c192ba2 | 2000-05-29 21:25:10 +0000 | [diff] [blame] | 821 | { |
Andreas Mohr | 7096384 | 2000-09-22 22:08:28 +0000 | [diff] [blame] | 822 | name_beg = lpCmdLine+1; |
| 823 | p = strchr ( lpCmdLine+1, '"' ); |
| 824 | if (p) |
| 825 | { |
| 826 | name_end = p; |
| 827 | args = strchr ( p, ' ' ); |
| 828 | } |
| 829 | else /* yes, even valid with trailing '"' missing */ |
| 830 | name_end = lpCmdLine+strlen(lpCmdLine); |
| 831 | } |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 832 | else |
Andreas Mohr | 7096384 | 2000-09-22 22:08:28 +0000 | [diff] [blame] | 833 | { |
| 834 | name_beg = lpCmdLine; |
| 835 | args = strchr( lpCmdLine, ' ' ); |
| 836 | name_end = args ? args : lpCmdLine+strlen(lpCmdLine); |
| 837 | } |
| 838 | |
| 839 | if ((name_beg == lpCmdLine) && (!args)) |
| 840 | { /* just use the original cmdline string as file name */ |
| 841 | name = (LPSTR)lpCmdLine; |
Alexandre Julliard | c192ba2 | 2000-05-29 21:25:10 +0000 | [diff] [blame] | 842 | } |
| 843 | else |
| 844 | { |
Andreas Mohr | 7096384 | 2000-09-22 22:08:28 +0000 | [diff] [blame] | 845 | if (!(name = HeapAlloc( GetProcessHeap(), 0, name_end - name_beg + 1 ))) |
| 846 | return ERROR_NOT_ENOUGH_MEMORY; |
| 847 | memcpy( name, name_beg, name_end - name_beg ); |
| 848 | name[name_end - name_beg] = '\0'; |
Alexandre Julliard | c192ba2 | 2000-05-29 21:25:10 +0000 | [diff] [blame] | 849 | } |
Ulrich Weigand | 6ce4006 | 1999-05-03 09:22:55 +0000 | [diff] [blame] | 850 | |
Andreas Mohr | 7096384 | 2000-09-22 22:08:28 +0000 | [diff] [blame] | 851 | if (args) |
| 852 | { |
| 853 | args++; |
| 854 | arglen = strlen(args); |
Alexandre Julliard | d7b7682 | 2001-12-20 00:19:40 +0000 | [diff] [blame] | 855 | cmdline = HeapAlloc( GetProcessHeap(), 0, 2 + arglen ); |
Andreas Mohr | 7096384 | 2000-09-22 22:08:28 +0000 | [diff] [blame] | 856 | cmdline[0] = (BYTE)arglen; |
| 857 | strcpy( cmdline + 1, args ); |
| 858 | } |
| 859 | else |
| 860 | { |
Alexandre Julliard | d7b7682 | 2001-12-20 00:19:40 +0000 | [diff] [blame] | 861 | cmdline = HeapAlloc( GetProcessHeap(), 0, 2 ); |
Andreas Mohr | 7096384 | 2000-09-22 22:08:28 +0000 | [diff] [blame] | 862 | cmdline[0] = cmdline[1] = 0; |
| 863 | } |
| 864 | |
Andreas Mohr | cabee39 | 2000-10-25 21:22:27 +0000 | [diff] [blame] | 865 | TRACE("name: '%s', cmdline: '%.*s'\n", name, cmdline[0], &cmdline[1]); |
Andreas Mohr | 7096384 | 2000-09-22 22:08:28 +0000 | [diff] [blame] | 866 | |
Alexandre Julliard | c192ba2 | 2000-05-29 21:25:10 +0000 | [diff] [blame] | 867 | if (SearchPathA( NULL, name, ".exe", sizeof(buffer), buffer, NULL )) |
| 868 | { |
| 869 | LOADPARAMS16 params; |
Alexandre Julliard | d7b7682 | 2001-12-20 00:19:40 +0000 | [diff] [blame] | 870 | WORD showCmd[2]; |
Alexandre Julliard | c192ba2 | 2000-05-29 21:25:10 +0000 | [diff] [blame] | 871 | showCmd[0] = 2; |
| 872 | showCmd[1] = nCmdShow; |
| 873 | |
| 874 | params.hEnvironment = 0; |
Alexandre Julliard | d7b7682 | 2001-12-20 00:19:40 +0000 | [diff] [blame] | 875 | params.cmdLine = MapLS( cmdline ); |
| 876 | params.showCmd = MapLS( showCmd ); |
Alexandre Julliard | c192ba2 | 2000-05-29 21:25:10 +0000 | [diff] [blame] | 877 | params.reserved = 0; |
| 878 | |
| 879 | ret = LoadModule16( buffer, ¶ms ); |
Alexandre Julliard | d7b7682 | 2001-12-20 00:19:40 +0000 | [diff] [blame] | 880 | UnMapLS( params.cmdLine ); |
| 881 | UnMapLS( params.showCmd ); |
Alexandre Julliard | c192ba2 | 2000-05-29 21:25:10 +0000 | [diff] [blame] | 882 | } |
| 883 | else ret = GetLastError(); |
| 884 | |
Alexandre Julliard | d7b7682 | 2001-12-20 00:19:40 +0000 | [diff] [blame] | 885 | HeapFree( GetProcessHeap(), 0, cmdline ); |
Alexandre Julliard | c192ba2 | 2000-05-29 21:25:10 +0000 | [diff] [blame] | 886 | if (name != lpCmdLine) HeapFree( GetProcessHeap(), 0, name ); |
| 887 | |
| 888 | if (ret == 21) /* 32-bit module */ |
| 889 | { |
Alexandre Julliard | ab68797 | 2000-11-15 23:41:46 +0000 | [diff] [blame] | 890 | DWORD count; |
| 891 | ReleaseThunkLock( &count ); |
Alexandre Julliard | 8ff37b8 | 2001-06-06 20:24:12 +0000 | [diff] [blame] | 892 | ret = LOWORD( WinExec( lpCmdLine, nCmdShow ) ); |
Alexandre Julliard | ab68797 | 2000-11-15 23:41:46 +0000 | [diff] [blame] | 893 | RestoreThunkLock( count ); |
Alexandre Julliard | c192ba2 | 2000-05-29 21:25:10 +0000 | [diff] [blame] | 894 | } |
| 895 | return ret; |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | /*********************************************************************** |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 899 | * WinExec (KERNEL32.@) |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 900 | */ |
Francois Gouget | 2204b50 | 2002-05-19 22:21:45 +0000 | [diff] [blame] | 901 | UINT WINAPI WinExec( LPCSTR lpCmdLine, UINT nCmdShow ) |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 902 | { |
Alexandre Julliard | 8e8f0f5 | 2000-04-15 21:30:33 +0000 | [diff] [blame] | 903 | PROCESS_INFORMATION info; |
| 904 | STARTUPINFOA startup; |
Alexandre Julliard | 596921d | 2000-06-24 20:53:47 +0000 | [diff] [blame] | 905 | char *cmdline; |
Alexandre Julliard | 267ca68 | 2002-07-31 17:20:00 +0000 | [diff] [blame] | 906 | UINT ret; |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 907 | |
Alexandre Julliard | 8e8f0f5 | 2000-04-15 21:30:33 +0000 | [diff] [blame] | 908 | memset( &startup, 0, sizeof(startup) ); |
| 909 | startup.cb = sizeof(startup); |
| 910 | startup.dwFlags = STARTF_USESHOWWINDOW; |
| 911 | startup.wShowWindow = nCmdShow; |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 912 | |
Alexandre Julliard | 596921d | 2000-06-24 20:53:47 +0000 | [diff] [blame] | 913 | /* cmdline needs to be writeable for CreateProcess */ |
Alexandre Julliard | 5f728ca | 2001-07-24 21:45:22 +0000 | [diff] [blame] | 914 | if (!(cmdline = HeapAlloc( GetProcessHeap(), 0, strlen(lpCmdLine)+1 ))) return 0; |
| 915 | strcpy( cmdline, lpCmdLine ); |
Alexandre Julliard | 596921d | 2000-06-24 20:53:47 +0000 | [diff] [blame] | 916 | |
| 917 | if (CreateProcessA( NULL, cmdline, NULL, NULL, FALSE, |
Alexandre Julliard | 8e8f0f5 | 2000-04-15 21:30:33 +0000 | [diff] [blame] | 918 | 0, NULL, NULL, &startup, &info )) |
| 919 | { |
| 920 | /* Give 30 seconds to the app to come up */ |
Alexandre Julliard | 5edf4e1 | 2001-07-26 20:12:54 +0000 | [diff] [blame] | 921 | if (wait_input_idle( info.hProcess, 30000 ) == 0xFFFFFFFF) |
Alexandre Julliard | 8e8f0f5 | 2000-04-15 21:30:33 +0000 | [diff] [blame] | 922 | WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() ); |
Alexandre Julliard | 267ca68 | 2002-07-31 17:20:00 +0000 | [diff] [blame] | 923 | ret = 33; |
Alexandre Julliard | 8e8f0f5 | 2000-04-15 21:30:33 +0000 | [diff] [blame] | 924 | /* Close off the handles */ |
| 925 | CloseHandle( info.hThread ); |
| 926 | CloseHandle( info.hProcess ); |
| 927 | } |
Alexandre Julliard | 267ca68 | 2002-07-31 17:20:00 +0000 | [diff] [blame] | 928 | else if ((ret = GetLastError()) >= 32) |
Alexandre Julliard | 8e8f0f5 | 2000-04-15 21:30:33 +0000 | [diff] [blame] | 929 | { |
Alexandre Julliard | 267ca68 | 2002-07-31 17:20:00 +0000 | [diff] [blame] | 930 | FIXME("Strange error set by CreateProcess: %d\n", ret ); |
| 931 | ret = 11; |
Alexandre Julliard | 8e8f0f5 | 2000-04-15 21:30:33 +0000 | [diff] [blame] | 932 | } |
Alexandre Julliard | 596921d | 2000-06-24 20:53:47 +0000 | [diff] [blame] | 933 | HeapFree( GetProcessHeap(), 0, cmdline ); |
Alexandre Julliard | 267ca68 | 2002-07-31 17:20:00 +0000 | [diff] [blame] | 934 | return ret; |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | /********************************************************************** |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 938 | * LoadModule (KERNEL32.@) |
Alexandre Julliard | 491502b | 1997-11-01 19:08:16 +0000 | [diff] [blame] | 939 | */ |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 940 | HINSTANCE WINAPI LoadModule( LPCSTR name, LPVOID paramBlock ) |
Alexandre Julliard | 491502b | 1997-11-01 19:08:16 +0000 | [diff] [blame] | 941 | { |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 942 | LOADPARAMS *params = (LOADPARAMS *)paramBlock; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 943 | PROCESS_INFORMATION info; |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 944 | STARTUPINFOA startup; |
| 945 | HINSTANCE hInstance; |
Alexandre Julliard | 8e8f0f5 | 2000-04-15 21:30:33 +0000 | [diff] [blame] | 946 | LPSTR cmdline, p; |
| 947 | char filename[MAX_PATH]; |
| 948 | BYTE len; |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 949 | |
Alexandre Julliard | 8ff37b8 | 2001-06-06 20:24:12 +0000 | [diff] [blame] | 950 | if (!name) return (HINSTANCE)ERROR_FILE_NOT_FOUND; |
Alexandre Julliard | 8e8f0f5 | 2000-04-15 21:30:33 +0000 | [diff] [blame] | 951 | |
| 952 | if (!SearchPathA( NULL, name, ".exe", sizeof(filename), filename, NULL ) && |
| 953 | !SearchPathA( NULL, name, NULL, sizeof(filename), filename, NULL )) |
Alexandre Julliard | 8ff37b8 | 2001-06-06 20:24:12 +0000 | [diff] [blame] | 954 | return (HINSTANCE)GetLastError(); |
Alexandre Julliard | 8e8f0f5 | 2000-04-15 21:30:33 +0000 | [diff] [blame] | 955 | |
| 956 | len = (BYTE)params->lpCmdLine[0]; |
| 957 | if (!(cmdline = HeapAlloc( GetProcessHeap(), 0, strlen(filename) + len + 2 ))) |
Alexandre Julliard | 8ff37b8 | 2001-06-06 20:24:12 +0000 | [diff] [blame] | 958 | return (HINSTANCE)ERROR_NOT_ENOUGH_MEMORY; |
Alexandre Julliard | 8e8f0f5 | 2000-04-15 21:30:33 +0000 | [diff] [blame] | 959 | |
| 960 | strcpy( cmdline, filename ); |
| 961 | p = cmdline + strlen(cmdline); |
| 962 | *p++ = ' '; |
| 963 | memcpy( p, params->lpCmdLine + 1, len ); |
| 964 | p[len] = 0; |
| 965 | |
| 966 | memset( &startup, 0, sizeof(startup) ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 967 | startup.cb = sizeof(startup); |
Alexandre Julliard | 8e8f0f5 | 2000-04-15 21:30:33 +0000 | [diff] [blame] | 968 | if (params->lpCmdShow) |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 969 | { |
Alexandre Julliard | 8e8f0f5 | 2000-04-15 21:30:33 +0000 | [diff] [blame] | 970 | startup.dwFlags = STARTF_USESHOWWINDOW; |
| 971 | startup.wShowWindow = params->lpCmdShow[1]; |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 972 | } |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 973 | |
Alexandre Julliard | 8e8f0f5 | 2000-04-15 21:30:33 +0000 | [diff] [blame] | 974 | if (CreateProcessA( filename, cmdline, NULL, NULL, FALSE, 0, |
| 975 | params->lpEnvAddress, NULL, &startup, &info )) |
| 976 | { |
| 977 | /* Give 30 seconds to the app to come up */ |
Alexandre Julliard | 5edf4e1 | 2001-07-26 20:12:54 +0000 | [diff] [blame] | 978 | if (wait_input_idle( info.hProcess, 30000 ) == 0xFFFFFFFF ) |
Alexandre Julliard | 8e8f0f5 | 2000-04-15 21:30:33 +0000 | [diff] [blame] | 979 | WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() ); |
Alexandre Julliard | 8ff37b8 | 2001-06-06 20:24:12 +0000 | [diff] [blame] | 980 | hInstance = (HINSTANCE)33; |
Alexandre Julliard | 8e8f0f5 | 2000-04-15 21:30:33 +0000 | [diff] [blame] | 981 | /* Close off the handles */ |
| 982 | CloseHandle( info.hThread ); |
| 983 | CloseHandle( info.hProcess ); |
| 984 | } |
Alexandre Julliard | 8ff37b8 | 2001-06-06 20:24:12 +0000 | [diff] [blame] | 985 | else if ((hInstance = (HINSTANCE)GetLastError()) >= (HINSTANCE)32) |
Alexandre Julliard | 8e8f0f5 | 2000-04-15 21:30:33 +0000 | [diff] [blame] | 986 | { |
| 987 | FIXME("Strange error set by CreateProcess: %d\n", hInstance ); |
Alexandre Julliard | 8ff37b8 | 2001-06-06 20:24:12 +0000 | [diff] [blame] | 988 | hInstance = (HINSTANCE)11; |
Alexandre Julliard | 8e8f0f5 | 2000-04-15 21:30:33 +0000 | [diff] [blame] | 989 | } |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 990 | |
Alexandre Julliard | 8e8f0f5 | 2000-04-15 21:30:33 +0000 | [diff] [blame] | 991 | HeapFree( GetProcessHeap(), 0, cmdline ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 992 | return hInstance; |
Alexandre Julliard | 491502b | 1997-11-01 19:08:16 +0000 | [diff] [blame] | 993 | } |
| 994 | |
Alexandre Julliard | 8e8f0f5 | 2000-04-15 21:30:33 +0000 | [diff] [blame] | 995 | |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 996 | /*********************************************************************** |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 997 | * GetModuleHandleA (KERNEL32.@) |
Patrik Stridvall | 044855c | 2001-07-11 18:56:41 +0000 | [diff] [blame] | 998 | * GetModuleHandle32 (KERNEL.488) |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 999 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1000 | HMODULE WINAPI GetModuleHandleA(LPCSTR module) |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 1001 | { |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 1002 | WINE_MODREF *wm; |
| 1003 | |
| 1004 | if ( module == NULL ) |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 1005 | wm = exe_modref; |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 1006 | else |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 1007 | wm = MODULE_FindModule( module ); |
| 1008 | |
| 1009 | return wm? wm->module : 0; |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 1010 | } |
| 1011 | |
Patrik Stridvall | 2d6457c | 2000-03-28 20:22:59 +0000 | [diff] [blame] | 1012 | /*********************************************************************** |
Patrik Stridvall | 3ca9823 | 2001-06-20 23:03:14 +0000 | [diff] [blame] | 1013 | * GetModuleHandleW (KERNEL32.@) |
Patrik Stridvall | 2d6457c | 2000-03-28 20:22:59 +0000 | [diff] [blame] | 1014 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1015 | HMODULE WINAPI GetModuleHandleW(LPCWSTR module) |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 1016 | { |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1017 | HMODULE hModule; |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 1018 | LPSTR modulea = HEAP_strdupWtoA( GetProcessHeap(), 0, module ); |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1019 | hModule = GetModuleHandleA( modulea ); |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 1020 | HeapFree( GetProcessHeap(), 0, modulea ); |
| 1021 | return hModule; |
| 1022 | } |
| 1023 | |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1024 | |
Alexandre Julliard | b1bac32 | 1996-12-15 19:45:59 +0000 | [diff] [blame] | 1025 | /*********************************************************************** |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 1026 | * GetModuleFileNameA (KERNEL32.@) |
Patrik Stridvall | 044855c | 2001-07-11 18:56:41 +0000 | [diff] [blame] | 1027 | * GetModuleFileName32 (KERNEL.487) |
Andreas Mohr | 4654c32 | 2000-02-20 19:15:34 +0000 | [diff] [blame] | 1028 | * |
| 1029 | * GetModuleFileNameA seems to *always* return the long path; |
| 1030 | * it's only GetModuleFileName16 that decides between short/long path |
| 1031 | * by checking if exe version >= 4.0. |
| 1032 | * (SDK docu doesn't mention this) |
Alexandre Julliard | b1bac32 | 1996-12-15 19:45:59 +0000 | [diff] [blame] | 1033 | */ |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 1034 | DWORD WINAPI GetModuleFileNameA( |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1035 | HMODULE hModule, /* [in] module handle (32bit) */ |
Alexandre Julliard | dadf78f | 1998-05-17 17:13:43 +0000 | [diff] [blame] | 1036 | LPSTR lpFileName, /* [out] filenamebuffer */ |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 1037 | DWORD size ) /* [in] size of filenamebuffer */ |
| 1038 | { |
Alexandre Julliard | 6478164 | 2002-02-02 18:13:50 +0000 | [diff] [blame] | 1039 | RtlEnterCriticalSection( &loader_section ); |
Alexandre Julliard | dadf78f | 1998-05-17 17:13:43 +0000 | [diff] [blame] | 1040 | |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 1041 | lpFileName[0] = 0; |
Joshua Thielen | 1cef297 | 2002-07-05 00:16:41 +0000 | [diff] [blame] | 1042 | if (!hModule && !(NtCurrentTeb()->tibflags & TEBF_WIN32)) |
| 1043 | { |
| 1044 | /* 16-bit task - get current NE module name */ |
| 1045 | NE_MODULE *pModule = NE_GetPtr( GetCurrentTask() ); |
| 1046 | if (pModule) GetLongPathNameA(NE_MODULE_NAME(pModule), lpFileName, size); |
| 1047 | } |
| 1048 | else |
| 1049 | { |
| 1050 | WINE_MODREF *wm = MODULE32_LookupHMODULE( hModule ); |
| 1051 | if (wm) lstrcpynA( lpFileName, wm->filename, size ); |
| 1052 | } |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 1053 | |
Alexandre Julliard | 6478164 | 2002-02-02 18:13:50 +0000 | [diff] [blame] | 1054 | RtlLeaveCriticalSection( &loader_section ); |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 1055 | TRACE("%s\n", lpFileName ); |
Alexandre Julliard | b1bac32 | 1996-12-15 19:45:59 +0000 | [diff] [blame] | 1056 | return strlen(lpFileName); |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
Alexandre Julliard | b1bac32 | 1996-12-15 19:45:59 +0000 | [diff] [blame] | 1059 | |
| 1060 | /*********************************************************************** |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 1061 | * GetModuleFileNameW (KERNEL32.@) |
Alexandre Julliard | b1bac32 | 1996-12-15 19:45:59 +0000 | [diff] [blame] | 1062 | */ |
Alexandre Julliard | 83886f2 | 2002-07-05 01:27:19 +0000 | [diff] [blame] | 1063 | DWORD WINAPI GetModuleFileNameW( HMODULE hModule, LPWSTR lpFileName, DWORD size ) |
Alexandre Julliard | b1bac32 | 1996-12-15 19:45:59 +0000 | [diff] [blame] | 1064 | { |
Alexandre Julliard | 83886f2 | 2002-07-05 01:27:19 +0000 | [diff] [blame] | 1065 | LPSTR fnA = HeapAlloc( GetProcessHeap(), 0, size * 2 ); |
| 1066 | if (!fnA) return 0; |
| 1067 | GetModuleFileNameA( hModule, fnA, size * 2 ); |
Alexandre Julliard | 24a62ab | 2000-11-28 22:40:56 +0000 | [diff] [blame] | 1068 | if (size > 0 && !MultiByteToWideChar( CP_ACP, 0, fnA, -1, lpFileName, size )) |
| 1069 | lpFileName[size-1] = 0; |
Alexandre Julliard | b1bac32 | 1996-12-15 19:45:59 +0000 | [diff] [blame] | 1070 | HeapFree( GetProcessHeap(), 0, fnA ); |
Alexandre Julliard | 83886f2 | 2002-07-05 01:27:19 +0000 | [diff] [blame] | 1071 | return strlenW(lpFileName); |
Alexandre Julliard | b1bac32 | 1996-12-15 19:45:59 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 1075 | /*********************************************************************** |
Patrik Stridvall | 3ca9823 | 2001-06-20 23:03:14 +0000 | [diff] [blame] | 1076 | * LoadLibraryExA (KERNEL32.@) |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 1077 | */ |
Jim Aston | 031f4fa | 1999-10-23 19:00:02 +0000 | [diff] [blame] | 1078 | HMODULE WINAPI LoadLibraryExA(LPCSTR libname, HANDLE hfile, DWORD flags) |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 1079 | { |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 1080 | WINE_MODREF *wm; |
Marcus Meissner | 574ef76 | 1999-04-03 16:23:47 +0000 | [diff] [blame] | 1081 | |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 1082 | if(!libname) |
| 1083 | { |
| 1084 | SetLastError(ERROR_INVALID_PARAMETER); |
| 1085 | return 0; |
| 1086 | } |
Ulrich Weigand | ebc543c | 1998-10-23 09:37:23 +0000 | [diff] [blame] | 1087 | |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 1088 | if (flags & LOAD_LIBRARY_AS_DATAFILE) |
| 1089 | { |
| 1090 | char filename[256]; |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 1091 | HMODULE hmod = 0; |
| 1092 | |
Dmitry Timoshkov | b77afe7 | 2001-03-21 03:38:03 +0000 | [diff] [blame] | 1093 | /* This method allows searching for the 'native' libraries only */ |
Ove Kaaven | 10b3402 | 2001-04-16 19:32:48 +0000 | [diff] [blame] | 1094 | if (SearchPathA( NULL, libname, ".dll", sizeof(filename), filename, NULL )) |
Dmitry Timoshkov | b77afe7 | 2001-03-21 03:38:03 +0000 | [diff] [blame] | 1095 | { |
Ove Kaaven | 10b3402 | 2001-04-16 19:32:48 +0000 | [diff] [blame] | 1096 | /* FIXME: maybe we should use the hfile parameter instead */ |
| 1097 | HANDLE hFile = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ, |
| 1098 | NULL, OPEN_EXISTING, 0, 0 ); |
| 1099 | if (hFile != INVALID_HANDLE_VALUE) |
| 1100 | { |
Alexandre Julliard | 3536316 | 2002-05-22 21:32:49 +0000 | [diff] [blame] | 1101 | HANDLE mapping; |
| 1102 | switch (MODULE_GetBinaryType( hFile )) |
Alexandre Julliard | cd3afa8 | 2002-01-31 23:32:57 +0000 | [diff] [blame] | 1103 | { |
Alexandre Julliard | 3536316 | 2002-05-22 21:32:49 +0000 | [diff] [blame] | 1104 | case BINARY_PE_EXE: |
| 1105 | case BINARY_PE_DLL: |
| 1106 | mapping = CreateFileMappingA( hFile, NULL, PAGE_READONLY, 0, 0, NULL ); |
Alexandre Julliard | cd3afa8 | 2002-01-31 23:32:57 +0000 | [diff] [blame] | 1107 | if (mapping) |
| 1108 | { |
| 1109 | hmod = (HMODULE)MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 ); |
| 1110 | CloseHandle( mapping ); |
| 1111 | } |
Alexandre Julliard | 3536316 | 2002-05-22 21:32:49 +0000 | [diff] [blame] | 1112 | break; |
| 1113 | default: |
| 1114 | break; |
Alexandre Julliard | cd3afa8 | 2002-01-31 23:32:57 +0000 | [diff] [blame] | 1115 | } |
Ove Kaaven | 10b3402 | 2001-04-16 19:32:48 +0000 | [diff] [blame] | 1116 | CloseHandle( hFile ); |
| 1117 | } |
Alexandre Julliard | 8ff37b8 | 2001-06-06 20:24:12 +0000 | [diff] [blame] | 1118 | if (hmod) return (HMODULE)((ULONG_PTR)hmod + 1); |
Dmitry Timoshkov | b77afe7 | 2001-03-21 03:38:03 +0000 | [diff] [blame] | 1119 | } |
Ove Kaaven | 10b3402 | 2001-04-16 19:32:48 +0000 | [diff] [blame] | 1120 | flags |= DONT_RESOLVE_DLL_REFERENCES; /* Just in case */ |
| 1121 | /* Fallback to normal behaviour */ |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 1122 | } |
Ove Kaaven | 10b3402 | 2001-04-16 19:32:48 +0000 | [diff] [blame] | 1123 | |
Alexandre Julliard | 6478164 | 2002-02-02 18:13:50 +0000 | [diff] [blame] | 1124 | RtlEnterCriticalSection( &loader_section ); |
Bertho Stultiens | af5745f | 1999-04-19 16:32:31 +0000 | [diff] [blame] | 1125 | |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 1126 | wm = MODULE_LoadLibraryExA( libname, hfile, flags ); |
Ulrich Weigand | 0106f85 | 1999-12-08 03:28:14 +0000 | [diff] [blame] | 1127 | if ( wm ) |
Bertho Stultiens | af5745f | 1999-04-19 16:32:31 +0000 | [diff] [blame] | 1128 | { |
Ulrich Weigand | 0106f85 | 1999-12-08 03:28:14 +0000 | [diff] [blame] | 1129 | if ( !MODULE_DllProcessAttach( wm, NULL ) ) |
| 1130 | { |
Andreas Mohr | 01c8ec3 | 2002-04-02 19:47:30 +0000 | [diff] [blame] | 1131 | WARN_(module)("Attach failed for module '%s'.\n", libname); |
Ulrich Weigand | 0106f85 | 1999-12-08 03:28:14 +0000 | [diff] [blame] | 1132 | MODULE_FreeLibrary(wm); |
| 1133 | SetLastError(ERROR_DLL_INIT_FAILED); |
| 1134 | wm = NULL; |
| 1135 | } |
Bertho Stultiens | af5745f | 1999-04-19 16:32:31 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
Alexandre Julliard | 6478164 | 2002-02-02 18:13:50 +0000 | [diff] [blame] | 1138 | RtlLeaveCriticalSection( &loader_section ); |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 1139 | return wm ? wm->module : 0; |
Marcus Meissner | 8220bc9 | 1998-10-11 11:10:27 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 1142 | /*********************************************************************** |
Bill Medland | 65fc1c9 | 2001-08-24 21:13:02 +0000 | [diff] [blame] | 1143 | * allocate_lib_dir |
| 1144 | * |
| 1145 | * helper for MODULE_LoadLibraryExA. Allocate space to hold the directory |
| 1146 | * portion of the provided name and put the name in it. |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 1147 | * |
Bill Medland | 65fc1c9 | 2001-08-24 21:13:02 +0000 | [diff] [blame] | 1148 | */ |
| 1149 | static LPCSTR allocate_lib_dir(LPCSTR libname) |
| 1150 | { |
| 1151 | LPCSTR p, pmax; |
| 1152 | LPSTR result; |
| 1153 | int length; |
| 1154 | |
| 1155 | pmax = libname; |
| 1156 | if ((p = strrchr( pmax, '\\' ))) pmax = p + 1; |
| 1157 | if ((p = strrchr( pmax, '/' ))) pmax = p + 1; /* Naughty. MSDN says don't */ |
| 1158 | if (pmax == libname && pmax[0] && pmax[1] == ':') pmax += 2; |
| 1159 | |
| 1160 | length = pmax - libname; |
| 1161 | |
| 1162 | result = HeapAlloc (GetProcessHeap(), 0, length+1); |
| 1163 | |
| 1164 | if (result) |
| 1165 | { |
| 1166 | strncpy (result, libname, length); |
| 1167 | result [length] = '\0'; |
| 1168 | } |
| 1169 | |
| 1170 | return result; |
| 1171 | } |
| 1172 | |
| 1173 | /*********************************************************************** |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 1174 | * MODULE_LoadLibraryExA (internal) |
| 1175 | * |
| 1176 | * Load a PE style module according to the load order. |
| 1177 | * |
| 1178 | * The HFILE parameter is not used and marked reserved in the SDK. I can |
| 1179 | * only guess that it should force a file to be mapped, but I rather |
| 1180 | * ignore the parameter because it would be extremely difficult to |
Andreas Mohr | 01c8ec3 | 2002-04-02 19:47:30 +0000 | [diff] [blame] | 1181 | * integrate this with different types of module representations. |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 1182 | * |
Bill Medland | 65fc1c9 | 2001-08-24 21:13:02 +0000 | [diff] [blame] | 1183 | * libdir is used to support LOAD_WITH_ALTERED_SEARCH_PATH during the recursion |
| 1184 | * on this function. When first called from LoadLibraryExA it will be |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 1185 | * NULL but thereafter it may point to a buffer containing the path |
| 1186 | * portion of the library name. Note that the recursion all occurs |
| 1187 | * within a Critical section (see LoadLibraryExA) so the use of a |
Bill Medland | 65fc1c9 | 2001-08-24 21:13:02 +0000 | [diff] [blame] | 1188 | * static is acceptable. |
| 1189 | * (We have to use a static variable at some point anyway, to pass the |
| 1190 | * information from BUILTIN32_dlopen through dlopen and the builtin's |
| 1191 | * init function into load_library). |
| 1192 | * allocated_libdir is TRUE in the stack frame that allocated libdir |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 1193 | */ |
Alexandre Julliard | 267ca68 | 2002-07-31 17:20:00 +0000 | [diff] [blame] | 1194 | WINE_MODREF *MODULE_LoadLibraryExA( LPCSTR libname, HANDLE hfile, DWORD flags ) |
Marcus Meissner | 8220bc9 | 1998-10-11 11:10:27 +0000 | [diff] [blame] | 1195 | { |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame] | 1196 | DWORD err = GetLastError(); |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 1197 | WINE_MODREF *pwm; |
| 1198 | int i; |
Alexandre Julliard | b9c9cdc | 2001-03-20 02:11:08 +0000 | [diff] [blame] | 1199 | enum loadorder_type loadorder[LOADORDER_NTYPES]; |
Alexandre Julliard | befbb0e | 2002-08-14 21:12:58 +0000 | [diff] [blame] | 1200 | LPSTR filename; |
Gerard Patel | a77fd7d | 2001-01-10 22:54:02 +0000 | [diff] [blame] | 1201 | const char *filetype = ""; |
Bill Medland | 65fc1c9 | 2001-08-24 21:13:02 +0000 | [diff] [blame] | 1202 | DWORD found; |
| 1203 | BOOL allocated_libdir = FALSE; |
| 1204 | static LPCSTR libdir = NULL; /* See above */ |
Peter Ganten | c7c4246 | 2000-08-28 21:33:28 +0000 | [diff] [blame] | 1205 | |
| 1206 | if ( !libname ) return NULL; |
| 1207 | |
| 1208 | filename = HeapAlloc ( GetProcessHeap(), 0, MAX_PATH + 1 ); |
| 1209 | if ( !filename ) return NULL; |
Bill Medland | ca5b201 | 2002-01-18 18:58:08 +0000 | [diff] [blame] | 1210 | *filename = 0; /* Just in case we don't set it before goto error */ |
Peter Ganten | c7c4246 | 2000-08-28 21:33:28 +0000 | [diff] [blame] | 1211 | |
Alexandre Julliard | 6478164 | 2002-02-02 18:13:50 +0000 | [diff] [blame] | 1212 | RtlEnterCriticalSection( &loader_section ); |
Bill Medland | 65fc1c9 | 2001-08-24 21:13:02 +0000 | [diff] [blame] | 1213 | |
| 1214 | if ((flags & LOAD_WITH_ALTERED_SEARCH_PATH) && FILE_contains_path(libname)) |
| 1215 | { |
| 1216 | if (!(libdir = allocate_lib_dir(libname))) goto error; |
| 1217 | allocated_libdir = TRUE; |
| 1218 | } |
| 1219 | |
| 1220 | if (!libdir || allocated_libdir) |
| 1221 | found = SearchPathA(NULL, libname, ".dll", MAX_PATH, filename, NULL); |
| 1222 | else |
| 1223 | found = DIR_SearchAlternatePath(libdir, libname, ".dll", MAX_PATH, filename, NULL); |
| 1224 | |
Peter Ganten | c7c4246 | 2000-08-28 21:33:28 +0000 | [diff] [blame] | 1225 | /* build the modules filename */ |
Bill Medland | 65fc1c9 | 2001-08-24 21:13:02 +0000 | [diff] [blame] | 1226 | if (!found) |
Peter Ganten | c7c4246 | 2000-08-28 21:33:28 +0000 | [diff] [blame] | 1227 | { |
Alexandre Julliard | befbb0e | 2002-08-14 21:12:58 +0000 | [diff] [blame] | 1228 | if (!MODULE_GetBuiltinPath( libname, ".dll", filename, MAX_PATH )) goto error; |
Peter Ganten | c7c4246 | 2000-08-28 21:33:28 +0000 | [diff] [blame] | 1229 | } |
Marcus Meissner | 8220bc9 | 1998-10-11 11:10:27 +0000 | [diff] [blame] | 1230 | |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 1231 | /* Check for already loaded module */ |
Bill Medland | 65fc1c9 | 2001-08-24 21:13:02 +0000 | [diff] [blame] | 1232 | if (!(pwm = MODULE_FindModule(filename)) && !FILE_contains_path(libname)) |
Eric Pouech | 208955c | 2000-09-10 03:14:00 +0000 | [diff] [blame] | 1233 | { |
| 1234 | LPSTR fn = HeapAlloc ( GetProcessHeap(), 0, MAX_PATH + 1 ); |
| 1235 | if (fn) |
| 1236 | { |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 1237 | /* since the default loading mechanism uses a more detailed algorithm |
Eric Pouech | 208955c | 2000-09-10 03:14:00 +0000 | [diff] [blame] | 1238 | * than SearchPath (like using PATH, which can even be modified between |
| 1239 | * two attempts of loading the same DLL), the look-up above (with |
| 1240 | * SearchPath) can have put the file in system directory, whereas it |
| 1241 | * has already been loaded but with a different path. So do a specific |
| 1242 | * look-up with filename (without any path) |
| 1243 | */ |
| 1244 | strcpy ( fn, libname ); |
| 1245 | /* if the filename doesn't have an extension append .DLL */ |
| 1246 | if (!strrchr( fn, '.')) strcat( fn, ".dll" ); |
| 1247 | if ((pwm = MODULE_FindModule( fn )) != NULL) |
| 1248 | strcpy( filename, fn ); |
| 1249 | HeapFree( GetProcessHeap(), 0, fn ); |
| 1250 | } |
| 1251 | } |
| 1252 | if (pwm) |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 1253 | { |
Bertho Stultiens | f4b6e82 | 1999-04-22 08:56:40 +0000 | [diff] [blame] | 1254 | if(!(pwm->flags & WINE_MODREF_MARKER)) |
| 1255 | pwm->refCount++; |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 1256 | |
| 1257 | if ((pwm->flags & WINE_MODREF_DONT_RESOLVE_REFS) && |
| 1258 | !(flags & DONT_RESOLVE_DLL_REFERENCES)) |
| 1259 | { |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 1260 | pwm->flags &= ~WINE_MODREF_DONT_RESOLVE_REFS; |
Bill Medland | 65fc1c9 | 2001-08-24 21:13:02 +0000 | [diff] [blame] | 1261 | PE_fixup_imports( pwm ); |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 1262 | } |
Francois Gouget | 070e749 | 2001-11-06 21:01:32 +0000 | [diff] [blame] | 1263 | TRACE("Already loaded module '%s' at 0x%08x, count=%d\n", filename, pwm->module, pwm->refCount); |
Bill Medland | 65fc1c9 | 2001-08-24 21:13:02 +0000 | [diff] [blame] | 1264 | if (allocated_libdir) |
| 1265 | { |
| 1266 | HeapFree ( GetProcessHeap(), 0, (LPSTR)libdir ); |
| 1267 | libdir = NULL; |
| 1268 | } |
Alexandre Julliard | 6478164 | 2002-02-02 18:13:50 +0000 | [diff] [blame] | 1269 | RtlLeaveCriticalSection( &loader_section ); |
Peter Ganten | c7c4246 | 2000-08-28 21:33:28 +0000 | [diff] [blame] | 1270 | HeapFree ( GetProcessHeap(), 0, filename ); |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 1271 | return pwm; |
| 1272 | } |
| 1273 | |
Alexandre Julliard | b9c9cdc | 2001-03-20 02:11:08 +0000 | [diff] [blame] | 1274 | MODULE_GetLoadOrder( loadorder, filename, TRUE); |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 1275 | |
Alexandre Julliard | b9c9cdc | 2001-03-20 02:11:08 +0000 | [diff] [blame] | 1276 | for(i = 0; i < LOADORDER_NTYPES; i++) |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 1277 | { |
Alexandre Julliard | b9c9cdc | 2001-03-20 02:11:08 +0000 | [diff] [blame] | 1278 | if (loadorder[i] == LOADORDER_INVALID) break; |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame] | 1279 | SetLastError( ERROR_FILE_NOT_FOUND ); |
Alexandre Julliard | b9c9cdc | 2001-03-20 02:11:08 +0000 | [diff] [blame] | 1280 | |
| 1281 | switch(loadorder[i]) |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 1282 | { |
Alexandre Julliard | b9c9cdc | 2001-03-20 02:11:08 +0000 | [diff] [blame] | 1283 | case LOADORDER_DLL: |
Peter Ganten | c7c4246 | 2000-08-28 21:33:28 +0000 | [diff] [blame] | 1284 | TRACE("Trying native dll '%s'\n", filename); |
| 1285 | pwm = PE_LoadLibraryExA(filename, flags); |
Gerard Patel | a77fd7d | 2001-01-10 22:54:02 +0000 | [diff] [blame] | 1286 | filetype = "native"; |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 1287 | break; |
| 1288 | |
Alexandre Julliard | b9c9cdc | 2001-03-20 02:11:08 +0000 | [diff] [blame] | 1289 | case LOADORDER_SO: |
Peter Ganten | c7c4246 | 2000-08-28 21:33:28 +0000 | [diff] [blame] | 1290 | TRACE("Trying so-library '%s'\n", filename); |
Alexandre Julliard | e441d3c | 2000-12-22 22:50:12 +0000 | [diff] [blame] | 1291 | pwm = ELF_LoadLibraryExA(filename, flags); |
Gerard Patel | a77fd7d | 2001-01-10 22:54:02 +0000 | [diff] [blame] | 1292 | filetype = "so"; |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 1293 | break; |
| 1294 | |
Alexandre Julliard | b9c9cdc | 2001-03-20 02:11:08 +0000 | [diff] [blame] | 1295 | case LOADORDER_BI: |
Peter Ganten | c7c4246 | 2000-08-28 21:33:28 +0000 | [diff] [blame] | 1296 | TRACE("Trying built-in '%s'\n", filename); |
| 1297 | pwm = BUILTIN32_LoadLibraryExA(filename, flags); |
Gerard Patel | a77fd7d | 2001-01-10 22:54:02 +0000 | [diff] [blame] | 1298 | filetype = "builtin"; |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 1299 | break; |
| 1300 | |
Alexandre Julliard | b9c9cdc | 2001-03-20 02:11:08 +0000 | [diff] [blame] | 1301 | default: |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 1302 | pwm = NULL; |
| 1303 | break; |
| 1304 | } |
| 1305 | |
| 1306 | if(pwm) |
| 1307 | { |
| 1308 | /* Initialize DLL just loaded */ |
Francois Gouget | 070e749 | 2001-11-06 21:01:32 +0000 | [diff] [blame] | 1309 | TRACE("Loaded module '%s' at 0x%08x\n", filename, pwm->module); |
Gerard Patel | a77fd7d | 2001-01-10 22:54:02 +0000 | [diff] [blame] | 1310 | if (!TRACE_ON(module)) |
| 1311 | TRACE_(loaddll)("Loaded module '%s' : %s\n", filename, filetype); |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 1312 | /* Set the refCount here so that an attach failure will */ |
| 1313 | /* decrement the dependencies through the MODULE_FreeLibrary call. */ |
| 1314 | pwm->refCount++; |
| 1315 | |
Bill Medland | 65fc1c9 | 2001-08-24 21:13:02 +0000 | [diff] [blame] | 1316 | if (allocated_libdir) |
| 1317 | { |
| 1318 | HeapFree ( GetProcessHeap(), 0, (LPSTR)libdir ); |
| 1319 | libdir = NULL; |
| 1320 | } |
Alexandre Julliard | 6478164 | 2002-02-02 18:13:50 +0000 | [diff] [blame] | 1321 | RtlLeaveCriticalSection( &loader_section ); |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame] | 1322 | SetLastError( err ); /* restore last error */ |
Peter Ganten | c7c4246 | 2000-08-28 21:33:28 +0000 | [diff] [blame] | 1323 | HeapFree ( GetProcessHeap(), 0, filename ); |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 1324 | return pwm; |
| 1325 | } |
| 1326 | |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame] | 1327 | if(GetLastError() != ERROR_FILE_NOT_FOUND) |
Andreas Mohr | 01c8ec3 | 2002-04-02 19:47:30 +0000 | [diff] [blame] | 1328 | { |
Alexandre Julliard | 83886f2 | 2002-07-05 01:27:19 +0000 | [diff] [blame] | 1329 | WARN("Loading of %s DLL %s failed (error %ld).\n", |
| 1330 | filetype, filename, GetLastError()); |
Andreas Mohr | 01c8ec3 | 2002-04-02 19:47:30 +0000 | [diff] [blame] | 1331 | break; |
| 1332 | } |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
Peter Ganten | c7c4246 | 2000-08-28 21:33:28 +0000 | [diff] [blame] | 1335 | error: |
Bill Medland | 65fc1c9 | 2001-08-24 21:13:02 +0000 | [diff] [blame] | 1336 | if (allocated_libdir) |
| 1337 | { |
| 1338 | HeapFree ( GetProcessHeap(), 0, (LPSTR)libdir ); |
| 1339 | libdir = NULL; |
| 1340 | } |
Alexandre Julliard | 6478164 | 2002-02-02 18:13:50 +0000 | [diff] [blame] | 1341 | RtlLeaveCriticalSection( &loader_section ); |
Andreas Mohr | 01c8ec3 | 2002-04-02 19:47:30 +0000 | [diff] [blame] | 1342 | WARN("Failed to load module '%s'; error=%ld\n", filename, GetLastError()); |
Peter Ganten | c7c4246 | 2000-08-28 21:33:28 +0000 | [diff] [blame] | 1343 | HeapFree ( GetProcessHeap(), 0, filename ); |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 1344 | return NULL; |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | /*********************************************************************** |
Patrik Stridvall | 3ca9823 | 2001-06-20 23:03:14 +0000 | [diff] [blame] | 1348 | * LoadLibraryA (KERNEL32.@) |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 1349 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1350 | HMODULE WINAPI LoadLibraryA(LPCSTR libname) { |
| 1351 | return LoadLibraryExA(libname,0,0); |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | /*********************************************************************** |
Patrik Stridvall | 3ca9823 | 2001-06-20 23:03:14 +0000 | [diff] [blame] | 1355 | * LoadLibraryW (KERNEL32.@) |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 1356 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1357 | HMODULE WINAPI LoadLibraryW(LPCWSTR libnameW) |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 1358 | { |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1359 | return LoadLibraryExW(libnameW,0,0); |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 1360 | } |
| 1361 | |
| 1362 | /*********************************************************************** |
Patrik Stridvall | 01d5e5b | 2001-07-02 19:59:40 +0000 | [diff] [blame] | 1363 | * LoadLibrary32 (KERNEL.452) |
| 1364 | * LoadSystemLibrary32 (KERNEL.482) |
Ulrich Weigand | c3d9f28 | 1999-08-18 18:31:26 +0000 | [diff] [blame] | 1365 | */ |
| 1366 | HMODULE WINAPI LoadLibrary32_16( LPCSTR libname ) |
| 1367 | { |
| 1368 | HMODULE hModule; |
Alexandre Julliard | ab68797 | 2000-11-15 23:41:46 +0000 | [diff] [blame] | 1369 | DWORD count; |
Ulrich Weigand | c3d9f28 | 1999-08-18 18:31:26 +0000 | [diff] [blame] | 1370 | |
Alexandre Julliard | ab68797 | 2000-11-15 23:41:46 +0000 | [diff] [blame] | 1371 | ReleaseThunkLock( &count ); |
Ulrich Weigand | c3d9f28 | 1999-08-18 18:31:26 +0000 | [diff] [blame] | 1372 | hModule = LoadLibraryA( libname ); |
Alexandre Julliard | ab68797 | 2000-11-15 23:41:46 +0000 | [diff] [blame] | 1373 | RestoreThunkLock( count ); |
Ulrich Weigand | c3d9f28 | 1999-08-18 18:31:26 +0000 | [diff] [blame] | 1374 | return hModule; |
| 1375 | } |
| 1376 | |
| 1377 | /*********************************************************************** |
Patrik Stridvall | 3ca9823 | 2001-06-20 23:03:14 +0000 | [diff] [blame] | 1378 | * LoadLibraryExW (KERNEL32.@) |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 1379 | */ |
Jim Aston | 031f4fa | 1999-10-23 19:00:02 +0000 | [diff] [blame] | 1380 | HMODULE WINAPI LoadLibraryExW(LPCWSTR libnameW,HANDLE hfile,DWORD flags) |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 1381 | { |
| 1382 | LPSTR libnameA = HEAP_strdupWtoA( GetProcessHeap(), 0, libnameW ); |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1383 | HMODULE ret = LoadLibraryExA( libnameA , hfile, flags ); |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 1384 | |
| 1385 | HeapFree( GetProcessHeap(), 0, libnameA ); |
| 1386 | return ret; |
| 1387 | } |
| 1388 | |
Bertho Stultiens | 1b34697 | 1999-04-11 14:52:24 +0000 | [diff] [blame] | 1389 | /*********************************************************************** |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 1390 | * MODULE_FlushModrefs |
Bertho Stultiens | 1b34697 | 1999-04-11 14:52:24 +0000 | [diff] [blame] | 1391 | * |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 1392 | * NOTE: Assumes that the process critical section is held! |
Bertho Stultiens | 1b34697 | 1999-04-11 14:52:24 +0000 | [diff] [blame] | 1393 | * |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 1394 | * Remove all unused modrefs and call the internal unloading routines |
| 1395 | * for the library type. |
Bertho Stultiens | 1b34697 | 1999-04-11 14:52:24 +0000 | [diff] [blame] | 1396 | */ |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 1397 | static void MODULE_FlushModrefs(void) |
Bertho Stultiens | 1b34697 | 1999-04-11 14:52:24 +0000 | [diff] [blame] | 1398 | { |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 1399 | WINE_MODREF *wm, *next; |
Bertho Stultiens | 1b34697 | 1999-04-11 14:52:24 +0000 | [diff] [blame] | 1400 | |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 1401 | for(wm = MODULE_modref_list; wm; wm = next) |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 1402 | { |
| 1403 | next = wm->next; |
Bertho Stultiens | 1b34697 | 1999-04-11 14:52:24 +0000 | [diff] [blame] | 1404 | |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 1405 | if(wm->refCount) |
| 1406 | continue; |
Bertho Stultiens | 1b34697 | 1999-04-11 14:52:24 +0000 | [diff] [blame] | 1407 | |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 1408 | /* Unlink this modref from the chain */ |
| 1409 | if(wm->next) |
| 1410 | wm->next->prev = wm->prev; |
| 1411 | if(wm->prev) |
| 1412 | wm->prev->next = wm->next; |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 1413 | if(wm == MODULE_modref_list) |
| 1414 | MODULE_modref_list = wm->next; |
Bertho Stultiens | 1b34697 | 1999-04-11 14:52:24 +0000 | [diff] [blame] | 1415 | |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 1416 | TRACE(" unloading %s\n", wm->filename); |
Alexandre Julliard | 69622db | 2002-06-25 00:23:23 +0000 | [diff] [blame] | 1417 | if (!TRACE_ON(module)) |
| 1418 | TRACE_(loaddll)("Unloaded module '%s' : %s\n", wm->filename, |
| 1419 | wm->dlhandle ? "builtin" : "native" ); |
| 1420 | |
Alexandre Julliard | c9cf68d | 2001-05-02 01:12:27 +0000 | [diff] [blame] | 1421 | if (wm->dlhandle) wine_dll_unload( wm->dlhandle ); |
Alexandre Julliard | 2418edb | 2001-05-10 19:17:54 +0000 | [diff] [blame] | 1422 | else UnmapViewOfFile( (LPVOID)wm->module ); |
Andreas Mohr | cabee39 | 2000-10-25 21:22:27 +0000 | [diff] [blame] | 1423 | FreeLibrary16(wm->hDummyMod); |
| 1424 | HeapFree( GetProcessHeap(), 0, wm->deps ); |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 1425 | HeapFree( GetProcessHeap(), 0, wm ); |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 1426 | } |
Bertho Stultiens | 1b34697 | 1999-04-11 14:52:24 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 1429 | /*********************************************************************** |
Patrik Stridvall | 044855c | 2001-07-11 18:56:41 +0000 | [diff] [blame] | 1430 | * FreeLibrary (KERNEL32.@) |
| 1431 | * FreeLibrary32 (KERNEL.486) |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 1432 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1433 | BOOL WINAPI FreeLibrary(HINSTANCE hLibModule) |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 1434 | { |
Ulrich Weigand | 6315a7f | 1999-05-22 10:44:39 +0000 | [diff] [blame] | 1435 | BOOL retv = FALSE; |
Bertho Stultiens | 1b34697 | 1999-04-11 14:52:24 +0000 | [diff] [blame] | 1436 | WINE_MODREF *wm; |
Ulrich Weigand | e469a58 | 1999-03-27 16:45:57 +0000 | [diff] [blame] | 1437 | |
Alexandre Julliard | 8ff37b8 | 2001-06-06 20:24:12 +0000 | [diff] [blame] | 1438 | if (!hLibModule) |
| 1439 | { |
| 1440 | SetLastError( ERROR_INVALID_HANDLE ); |
| 1441 | return FALSE; |
| 1442 | } |
| 1443 | |
| 1444 | if ((ULONG_PTR)hLibModule & 1) |
| 1445 | { |
| 1446 | /* this is a LOAD_LIBRARY_AS_DATAFILE module */ |
| 1447 | char *ptr = (char *)hLibModule - 1; |
| 1448 | UnmapViewOfFile( ptr ); |
| 1449 | return TRUE; |
| 1450 | } |
| 1451 | |
Alexandre Julliard | 6478164 | 2002-02-02 18:13:50 +0000 | [diff] [blame] | 1452 | RtlEnterCriticalSection( &loader_section ); |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 1453 | free_lib_count++; |
Bertho Stultiens | 1b34697 | 1999-04-11 14:52:24 +0000 | [diff] [blame] | 1454 | |
Alexandre Julliard | 8ff37b8 | 2001-06-06 20:24:12 +0000 | [diff] [blame] | 1455 | if ((wm = MODULE32_LookupHMODULE( hLibModule ))) retv = MODULE_FreeLibrary( wm ); |
Bertho Stultiens | 1b34697 | 1999-04-11 14:52:24 +0000 | [diff] [blame] | 1456 | |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 1457 | free_lib_count--; |
Alexandre Julliard | 6478164 | 2002-02-02 18:13:50 +0000 | [diff] [blame] | 1458 | RtlLeaveCriticalSection( &loader_section ); |
Bertho Stultiens | 1b34697 | 1999-04-11 14:52:24 +0000 | [diff] [blame] | 1459 | |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 1460 | return retv; |
| 1461 | } |
| 1462 | |
| 1463 | /*********************************************************************** |
| 1464 | * MODULE_DecRefCount |
| 1465 | * |
| 1466 | * NOTE: Assumes that the process critical section is held! |
| 1467 | */ |
| 1468 | static void MODULE_DecRefCount( WINE_MODREF *wm ) |
| 1469 | { |
| 1470 | int i; |
| 1471 | |
| 1472 | if ( wm->flags & WINE_MODREF_MARKER ) |
| 1473 | return; |
| 1474 | |
| 1475 | if ( wm->refCount <= 0 ) |
| 1476 | return; |
| 1477 | |
| 1478 | --wm->refCount; |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 1479 | TRACE("(%s) refCount: %d\n", wm->modname, wm->refCount ); |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 1480 | |
| 1481 | if ( wm->refCount == 0 ) |
| 1482 | { |
| 1483 | wm->flags |= WINE_MODREF_MARKER; |
| 1484 | |
| 1485 | for ( i = 0; i < wm->nDeps; i++ ) |
| 1486 | if ( wm->deps[i] ) |
| 1487 | MODULE_DecRefCount( wm->deps[i] ); |
| 1488 | |
| 1489 | wm->flags &= ~WINE_MODREF_MARKER; |
| 1490 | } |
| 1491 | } |
| 1492 | |
| 1493 | /*********************************************************************** |
| 1494 | * MODULE_FreeLibrary |
| 1495 | * |
| 1496 | * NOTE: Assumes that the process critical section is held! |
| 1497 | */ |
| 1498 | BOOL MODULE_FreeLibrary( WINE_MODREF *wm ) |
| 1499 | { |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 1500 | TRACE("(%s) - START\n", wm->modname ); |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 1501 | |
| 1502 | /* Recursively decrement reference counts */ |
| 1503 | MODULE_DecRefCount( wm ); |
| 1504 | |
| 1505 | /* Call process detach notifications */ |
Alexandre Julliard | becb9a3 | 2000-12-11 03:48:15 +0000 | [diff] [blame] | 1506 | if ( free_lib_count <= 1 ) |
Alexandre Julliard | d131a17 | 1999-05-23 20:02:04 +0000 | [diff] [blame] | 1507 | { |
Ulrich Weigand | 6315a7f | 1999-05-22 10:44:39 +0000 | [diff] [blame] | 1508 | MODULE_DllProcessDetach( FALSE, NULL ); |
Alexandre Julliard | 67a7499 | 2001-02-27 02:09:16 +0000 | [diff] [blame] | 1509 | SERVER_START_REQ( unload_dll ) |
Alexandre Julliard | 9c2370b | 2000-08-30 00:00:48 +0000 | [diff] [blame] | 1510 | { |
Alexandre Julliard | 9c2370b | 2000-08-30 00:00:48 +0000 | [diff] [blame] | 1511 | req->base = (void *)wm->module; |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1512 | wine_server_call( req ); |
Alexandre Julliard | 9c2370b | 2000-08-30 00:00:48 +0000 | [diff] [blame] | 1513 | } |
| 1514 | SERVER_END_REQ; |
Alexandre Julliard | f93eb3e | 2000-04-28 20:26:35 +0000 | [diff] [blame] | 1515 | MODULE_FlushModrefs(); |
Alexandre Julliard | d131a17 | 1999-05-23 20:02:04 +0000 | [diff] [blame] | 1516 | } |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 1517 | |
Ulrich Czekalla | cc27998 | 2000-03-08 18:41:22 +0000 | [diff] [blame] | 1518 | TRACE("END\n"); |
Bertho Stultiens | c1d1cfe | 1999-04-18 12:14:06 +0000 | [diff] [blame] | 1519 | |
Ulrich Weigand | 6315a7f | 1999-05-22 10:44:39 +0000 | [diff] [blame] | 1520 | return TRUE; |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
Bertho Stultiens | 1b34697 | 1999-04-11 14:52:24 +0000 | [diff] [blame] | 1523 | |
Sergey Turchanov | 5ffd2df | 1999-03-22 12:35:48 +0000 | [diff] [blame] | 1524 | /*********************************************************************** |
Patrik Stridvall | 3ca9823 | 2001-06-20 23:03:14 +0000 | [diff] [blame] | 1525 | * FreeLibraryAndExitThread (KERNEL32.@) |
Sergey Turchanov | 5ffd2df | 1999-03-22 12:35:48 +0000 | [diff] [blame] | 1526 | */ |
| 1527 | VOID WINAPI FreeLibraryAndExitThread(HINSTANCE hLibModule, DWORD dwExitCode) |
| 1528 | { |
| 1529 | FreeLibrary(hLibModule); |
| 1530 | ExitThread(dwExitCode); |
| 1531 | } |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 1532 | |
| 1533 | /*********************************************************************** |
Patrik Stridvall | 3ca9823 | 2001-06-20 23:03:14 +0000 | [diff] [blame] | 1534 | * PrivateLoadLibrary (KERNEL32.@) |
Alexandre Julliard | e658d82 | 1997-11-30 17:45:40 +0000 | [diff] [blame] | 1535 | * |
| 1536 | * FIXME: rough guesswork, don't know what "Private" means |
| 1537 | */ |
Alexandre Julliard | 8ff37b8 | 2001-06-06 20:24:12 +0000 | [diff] [blame] | 1538 | HINSTANCE16 WINAPI PrivateLoadLibrary(LPCSTR libname) |
Alexandre Julliard | e658d82 | 1997-11-30 17:45:40 +0000 | [diff] [blame] | 1539 | { |
Alexandre Julliard | 8ff37b8 | 2001-06-06 20:24:12 +0000 | [diff] [blame] | 1540 | return LoadLibrary16(libname); |
Alexandre Julliard | e658d82 | 1997-11-30 17:45:40 +0000 | [diff] [blame] | 1541 | } |
| 1542 | |
| 1543 | |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 1544 | |
| 1545 | /*********************************************************************** |
Patrik Stridvall | 3ca9823 | 2001-06-20 23:03:14 +0000 | [diff] [blame] | 1546 | * PrivateFreeLibrary (KERNEL32.@) |
Alexandre Julliard | e658d82 | 1997-11-30 17:45:40 +0000 | [diff] [blame] | 1547 | * |
| 1548 | * FIXME: rough guesswork, don't know what "Private" means |
| 1549 | */ |
Alexandre Julliard | 8ff37b8 | 2001-06-06 20:24:12 +0000 | [diff] [blame] | 1550 | void WINAPI PrivateFreeLibrary(HINSTANCE16 handle) |
Alexandre Julliard | e658d82 | 1997-11-30 17:45:40 +0000 | [diff] [blame] | 1551 | { |
Alexandre Julliard | 8ff37b8 | 2001-06-06 20:24:12 +0000 | [diff] [blame] | 1552 | FreeLibrary16(handle); |
Alexandre Julliard | e658d82 | 1997-11-30 17:45:40 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
| 1555 | |
| 1556 | /*********************************************************************** |
Patrik Stridvall | 01d5e5b | 2001-07-02 19:59:40 +0000 | [diff] [blame] | 1557 | * GetProcAddress16 (KERNEL32.37) |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 1558 | * Get procaddress in 16bit module from win32... (kernel32 undoc. ordinal func) |
| 1559 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1560 | FARPROC16 WINAPI WIN32_GetProcAddress16( HMODULE hModule, LPCSTR name ) |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 1561 | { |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 1562 | if (!hModule) { |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 1563 | WARN("hModule may not be 0!\n"); |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 1564 | return (FARPROC16)0; |
| 1565 | } |
Alexandre Julliard | dadf78f | 1998-05-17 17:13:43 +0000 | [diff] [blame] | 1566 | if (HIWORD(hModule)) |
| 1567 | { |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 1568 | WARN("hModule is Win32 handle (%08x)\n", hModule ); |
Alexandre Julliard | dadf78f | 1998-05-17 17:13:43 +0000 | [diff] [blame] | 1569 | return (FARPROC16)0; |
| 1570 | } |
Alexandre Julliard | 8ff37b8 | 2001-06-06 20:24:12 +0000 | [diff] [blame] | 1571 | return GetProcAddress16( LOWORD(hModule), name ); |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 1572 | } |
| 1573 | |
| 1574 | /*********************************************************************** |
Patrik Stridvall | 01d5e5b | 2001-07-02 19:59:40 +0000 | [diff] [blame] | 1575 | * GetProcAddress (KERNEL.50) |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 1576 | */ |
Alexandre Julliard | ac7efef | 2000-11-27 21:54:01 +0000 | [diff] [blame] | 1577 | FARPROC16 WINAPI GetProcAddress16( HMODULE16 hModule, LPCSTR name ) |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 1578 | { |
| 1579 | WORD ordinal; |
Alexandre Julliard | ca22b33 | 1996-07-12 19:02:39 +0000 | [diff] [blame] | 1580 | FARPROC16 ret; |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 1581 | |
| 1582 | if (!hModule) hModule = GetCurrentTask(); |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 1583 | hModule = GetExePtr( hModule ); |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 1584 | |
| 1585 | if (HIWORD(name) != 0) |
| 1586 | { |
Alexandre Julliard | ac7efef | 2000-11-27 21:54:01 +0000 | [diff] [blame] | 1587 | ordinal = NE_GetOrdinal( hModule, name ); |
| 1588 | TRACE("%04x '%s'\n", hModule, name ); |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 1589 | } |
| 1590 | else |
| 1591 | { |
| 1592 | ordinal = LOWORD(name); |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 1593 | TRACE("%04x %04x\n", hModule, ordinal ); |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 1594 | } |
Alexandre Julliard | ca22b33 | 1996-07-12 19:02:39 +0000 | [diff] [blame] | 1595 | if (!ordinal) return (FARPROC16)0; |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 1596 | |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 1597 | ret = NE_GetEntryPoint( hModule, ordinal ); |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 1598 | |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 1599 | TRACE("returning %08x\n", (UINT)ret ); |
Alexandre Julliard | ca22b33 | 1996-07-12 19:02:39 +0000 | [diff] [blame] | 1600 | return ret; |
| 1601 | } |
| 1602 | |
| 1603 | |
| 1604 | /*********************************************************************** |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 1605 | * GetProcAddress (KERNEL32.@) |
Alexandre Julliard | ca22b33 | 1996-07-12 19:02:39 +0000 | [diff] [blame] | 1606 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1607 | FARPROC WINAPI GetProcAddress( HMODULE hModule, LPCSTR function ) |
Alexandre Julliard | ca22b33 | 1996-07-12 19:02:39 +0000 | [diff] [blame] | 1608 | { |
Alexandre Julliard | 891d23e | 2002-07-24 19:04:41 +0000 | [diff] [blame] | 1609 | return MODULE_GetProcAddress( hModule, function, -1, TRUE ); |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 1610 | } |
| 1611 | |
Ulrich Weigand | a3527cf | 1998-10-11 19:31:10 +0000 | [diff] [blame] | 1612 | /*********************************************************************** |
Patrik Stridvall | 54fe838 | 2000-04-06 20:21:16 +0000 | [diff] [blame] | 1613 | * GetProcAddress32 (KERNEL.453) |
Ulrich Weigand | a3527cf | 1998-10-11 19:31:10 +0000 | [diff] [blame] | 1614 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1615 | FARPROC WINAPI GetProcAddress32_16( HMODULE hModule, LPCSTR function ) |
Ulrich Weigand | a3527cf | 1998-10-11 19:31:10 +0000 | [diff] [blame] | 1616 | { |
Alexandre Julliard | 891d23e | 2002-07-24 19:04:41 +0000 | [diff] [blame] | 1617 | return MODULE_GetProcAddress( hModule, function, -1, FALSE ); |
Ulrich Weigand | a3527cf | 1998-10-11 19:31:10 +0000 | [diff] [blame] | 1618 | } |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 1619 | |
| 1620 | /*********************************************************************** |
Patrik Stridvall | 2d6457c | 2000-03-28 20:22:59 +0000 | [diff] [blame] | 1621 | * MODULE_GetProcAddress (internal) |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 1622 | */ |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 1623 | FARPROC MODULE_GetProcAddress( |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1624 | HMODULE hModule, /* [in] current module handle */ |
Ulrich Weigand | a3527cf | 1998-10-11 19:31:10 +0000 | [diff] [blame] | 1625 | LPCSTR function, /* [in] function to be looked up */ |
Alexandre Julliard | 891d23e | 2002-07-24 19:04:41 +0000 | [diff] [blame] | 1626 | int hint, |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1627 | BOOL snoop ) |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 1628 | { |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 1629 | WINE_MODREF *wm; |
| 1630 | FARPROC retproc = 0; |
Alexandre Julliard | ca22b33 | 1996-07-12 19:02:39 +0000 | [diff] [blame] | 1631 | |
Alexandre Julliard | 84c70f5 | 1997-05-09 08:40:27 +0000 | [diff] [blame] | 1632 | if (HIWORD(function)) |
Alexandre Julliard | 891d23e | 2002-07-24 19:04:41 +0000 | [diff] [blame] | 1633 | TRACE_(win32)("(%08lx,%s (%d))\n",(DWORD)hModule,function,hint); |
Alexandre Julliard | 84c70f5 | 1997-05-09 08:40:27 +0000 | [diff] [blame] | 1634 | else |
Alexandre Julliard | 06c275a | 1999-05-02 14:32:27 +0000 | [diff] [blame] | 1635 | TRACE_(win32)("(%08lx,%p)\n",(DWORD)hModule,function); |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 1636 | |
Alexandre Julliard | 6478164 | 2002-02-02 18:13:50 +0000 | [diff] [blame] | 1637 | RtlEnterCriticalSection( &loader_section ); |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 1638 | if ((wm = MODULE32_LookupHMODULE( hModule ))) |
Alexandre Julliard | e658d82 | 1997-11-30 17:45:40 +0000 | [diff] [blame] | 1639 | { |
Alexandre Julliard | 891d23e | 2002-07-24 19:04:41 +0000 | [diff] [blame] | 1640 | retproc = wm->find_export( wm, function, hint, snoop ); |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 1641 | if (!retproc) SetLastError(ERROR_PROC_NOT_FOUND); |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 1642 | } |
Alexandre Julliard | 6478164 | 2002-02-02 18:13:50 +0000 | [diff] [blame] | 1643 | RtlLeaveCriticalSection( &loader_section ); |
Alexandre Julliard | 081ee94 | 2000-08-07 04:12:41 +0000 | [diff] [blame] | 1644 | return retproc; |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 1645 | } |
| 1646 | |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 1647 | |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1648 | /*************************************************************************** |
| 1649 | * HasGPHandler (KERNEL.338) |
| 1650 | */ |
| 1651 | |
Patrik Stridvall | c7a8dde | 1999-04-25 12:36:53 +0000 | [diff] [blame] | 1652 | #include "pshpack1.h" |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1653 | typedef struct _GPHANDLERDEF |
| 1654 | { |
| 1655 | WORD selector; |
| 1656 | WORD rangeStart; |
| 1657 | WORD rangeEnd; |
| 1658 | WORD handler; |
| 1659 | } GPHANDLERDEF; |
Patrik Stridvall | c7a8dde | 1999-04-25 12:36:53 +0000 | [diff] [blame] | 1660 | #include "poppack.h" |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1661 | |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1662 | SEGPTR WINAPI HasGPHandler16( SEGPTR address ) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1663 | { |
| 1664 | HMODULE16 hModule; |
Ulrich Weigand | a3527cf | 1998-10-11 19:31:10 +0000 | [diff] [blame] | 1665 | int gpOrdinal; |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1666 | SEGPTR gpPtr; |
| 1667 | GPHANDLERDEF *gpHandler; |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 1668 | |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1669 | if ( (hModule = FarGetOwner16( SELECTOROF(address) )) != 0 |
Ulrich Weigand | a3527cf | 1998-10-11 19:31:10 +0000 | [diff] [blame] | 1670 | && (gpOrdinal = NE_GetOrdinal( hModule, "__GP" )) != 0 |
| 1671 | && (gpPtr = (SEGPTR)NE_GetEntryPointEx( hModule, gpOrdinal, FALSE )) != 0 |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1672 | && !IsBadReadPtr16( gpPtr, sizeof(GPHANDLERDEF) ) |
Alexandre Julliard | 982a223 | 2000-12-13 20:20:09 +0000 | [diff] [blame] | 1673 | && (gpHandler = MapSL( gpPtr )) != NULL ) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1674 | { |
| 1675 | while (gpHandler->selector) |
| 1676 | { |
| 1677 | if ( SELECTOROF(address) == gpHandler->selector |
| 1678 | && OFFSETOF(address) >= gpHandler->rangeStart |
| 1679 | && OFFSETOF(address) < gpHandler->rangeEnd ) |
Alexandre Julliard | 982a223 | 2000-12-13 20:20:09 +0000 | [diff] [blame] | 1680 | return MAKESEGPTR( gpHandler->selector, gpHandler->handler ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1681 | gpHandler++; |
| 1682 | } |
| 1683 | } |
| 1684 | |
| 1685 | return 0; |
| 1686 | } |