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