| 1 | /* $Id: wprocess.cpp,v 1.99 2000-10-03 17:28:32 sandervl Exp $ */
|
|---|
| 2 |
|
|---|
| 3 | /*
|
|---|
| 4 | * Win32 process functions
|
|---|
| 5 | *
|
|---|
| 6 | * Copyright 1998-2000 Sander van Leeuwen (sandervl@xs4all.nl)
|
|---|
| 7 | * Copyright 2000 knut st. osmundsen (knut.stange.osmundsen@mynd.no)
|
|---|
| 8 | *
|
|---|
| 9 | * NOTE: Even though Odin32 OS/2 apps don't switch FS selectors,
|
|---|
| 10 | * we still allocate a TEB to store misc information.
|
|---|
| 11 | *
|
|---|
| 12 | * Project Odin Software License can be found in LICENSE.TXT
|
|---|
| 13 | *
|
|---|
| 14 | */
|
|---|
| 15 | #include <odin.h>
|
|---|
| 16 | #include <odinwrap.h>
|
|---|
| 17 | #include <os2win.h>
|
|---|
| 18 | #include <stdio.h>
|
|---|
| 19 | #include <stdlib.h>
|
|---|
| 20 | #include <string.h>
|
|---|
| 21 |
|
|---|
| 22 | #include <unicode.h>
|
|---|
| 23 | #include "windllbase.h"
|
|---|
| 24 | #include "winexebase.h"
|
|---|
| 25 | #include "windllpeldr.h"
|
|---|
| 26 | #include "winexepeldr.h"
|
|---|
| 27 | #include "winfakepeldr.h"
|
|---|
| 28 | #include "windlllx.h"
|
|---|
| 29 | #include <vmutex.h>
|
|---|
| 30 | #include <handlemanager.h>
|
|---|
| 31 |
|
|---|
| 32 | #ifdef __IBMCPP__
|
|---|
| 33 | #include <builtin.h>
|
|---|
| 34 | #endif
|
|---|
| 35 |
|
|---|
| 36 | #include "odin32validate.h"
|
|---|
| 37 | #include "exceptutil.h"
|
|---|
| 38 | #include "oslibdos.h"
|
|---|
| 39 | #include "oslibmisc.h"
|
|---|
| 40 | #include "oslibdebug.h"
|
|---|
| 41 |
|
|---|
| 42 | #include "console.h"
|
|---|
| 43 | #include "cio.h"
|
|---|
| 44 | #include "versionos2.h" /*PLF Wed 98-03-18 02:36:51*/
|
|---|
| 45 | #include <wprocess.h>
|
|---|
| 46 | #include "mmap.h"
|
|---|
| 47 |
|
|---|
| 48 | #define DBG_LOCALLOG DBG_wprocess
|
|---|
| 49 | #include "dbglocal.h"
|
|---|
| 50 |
|
|---|
| 51 | ODINDEBUGCHANNEL(KERNEL32-WPROCESS)
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 | /*******************************************************************************
|
|---|
| 55 | * Global Variables *
|
|---|
| 56 | *******************************************************************************/
|
|---|
| 57 | BOOL fIsOS2Image = FALSE; /* TRUE -> Odin32 OS/2 application (not converted!) */
|
|---|
| 58 | /* FALSE -> otherwise */
|
|---|
| 59 | BOOL fExitProcess = FALSE;
|
|---|
| 60 |
|
|---|
| 61 | //Commandlines
|
|---|
| 62 | PCSTR pszCmdLineA; /* ASCII/ANSII commandline. */
|
|---|
| 63 | PCWSTR pszCmdLineW; /* Unicode commandline. */
|
|---|
| 64 |
|
|---|
| 65 | //Process database
|
|---|
| 66 | PDB ProcessPDB = {0};
|
|---|
| 67 | USHORT ProcessTIBSel = 0;
|
|---|
| 68 | DWORD *TIBFlatPtr = 0;
|
|---|
| 69 |
|
|---|
| 70 | //list of thread database structures
|
|---|
| 71 | static THDB *threadList = 0;
|
|---|
| 72 | static VMutex threadListMutex;
|
|---|
| 73 |
|
|---|
| 74 | //TODO: This should not be here: (need to rearrange NTDLL; kernel32 can't depend on ntdll)
|
|---|
| 75 | BOOLEAN (* WINAPI RtlAllocateAndInitializeSid) ( PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority,
|
|---|
| 76 | BYTE nSubAuthorityCount,
|
|---|
| 77 | DWORD nSubAuthority0,
|
|---|
| 78 | DWORD nSubAuthority1,
|
|---|
| 79 | DWORD nSubAuthority2,
|
|---|
| 80 | DWORD nSubAuthority3,
|
|---|
| 81 | DWORD nSubAuthority4,
|
|---|
| 82 | DWORD nSubAuthority5,
|
|---|
| 83 | DWORD nSubAuthority6,
|
|---|
| 84 | DWORD nSubAuthority7,
|
|---|
| 85 | PSID *pSid);
|
|---|
| 86 | static HINSTANCE hInstNTDll = 0;
|
|---|
| 87 |
|
|---|
| 88 | //******************************************************************************
|
|---|
| 89 | //******************************************************************************
|
|---|
| 90 | TEB *WIN32API GetThreadTEB()
|
|---|
| 91 | {
|
|---|
| 92 | if(TIBFlatPtr == NULL) {
|
|---|
| 93 | DebugInt3();
|
|---|
| 94 | return 0;
|
|---|
| 95 | }
|
|---|
| 96 | return (TEB *)*TIBFlatPtr;
|
|---|
| 97 | }
|
|---|
| 98 | //******************************************************************************
|
|---|
| 99 | //******************************************************************************
|
|---|
| 100 | THDB *WIN32API GetThreadTHDB()
|
|---|
| 101 | {
|
|---|
| 102 | TEB *winteb;
|
|---|
| 103 | THDB *thdb;
|
|---|
| 104 |
|
|---|
| 105 | if(TIBFlatPtr == NULL)
|
|---|
| 106 | return 0;
|
|---|
| 107 |
|
|---|
| 108 | winteb = (TEB *)*TIBFlatPtr;
|
|---|
| 109 | if(winteb == NULL) {
|
|---|
| 110 | return NULL;
|
|---|
| 111 | }
|
|---|
| 112 | thdb = (THDB *)(winteb+1);
|
|---|
| 113 |
|
|---|
| 114 | return thdb;
|
|---|
| 115 | }
|
|---|
| 116 | //******************************************************************************
|
|---|
| 117 | //******************************************************************************
|
|---|
| 118 | THDB *WIN32API GetTHDBFromThreadId(ULONG threadId)
|
|---|
| 119 | {
|
|---|
| 120 | THDB *thdb = threadList;
|
|---|
| 121 |
|
|---|
| 122 | threadListMutex.enter();
|
|---|
| 123 | while(thdb) {
|
|---|
| 124 | if(thdb->threadId == threadId) {
|
|---|
| 125 | break;
|
|---|
| 126 | }
|
|---|
| 127 | thdb = thdb->next;
|
|---|
| 128 | }
|
|---|
| 129 | threadListMutex.leave();
|
|---|
| 130 | return thdb;
|
|---|
| 131 | }
|
|---|
| 132 | //******************************************************************************
|
|---|
| 133 | //******************************************************************************
|
|---|
| 134 | THDB *WIN32API GetTHDBFromThreadHandle(HANDLE hThread)
|
|---|
| 135 | {
|
|---|
| 136 | THDB *thdb = threadList;
|
|---|
| 137 |
|
|---|
| 138 | threadListMutex.enter();
|
|---|
| 139 | while(thdb) {
|
|---|
| 140 | if(thdb->hThread == hThread) {
|
|---|
| 141 | break;
|
|---|
| 142 | }
|
|---|
| 143 | thdb = thdb->next;
|
|---|
| 144 | }
|
|---|
| 145 | threadListMutex.leave();
|
|---|
| 146 | return thdb;
|
|---|
| 147 | }
|
|---|
| 148 | //******************************************************************************
|
|---|
| 149 | // Set up the TIB selector and memory for the current thread
|
|---|
| 150 | //******************************************************************************
|
|---|
| 151 | TEB *InitializeTIB(BOOL fMainThread)
|
|---|
| 152 | {
|
|---|
| 153 | TEB *winteb;
|
|---|
| 154 | THDB *thdb;
|
|---|
| 155 | ULONG hThreadMain;
|
|---|
| 156 | USHORT tibsel;
|
|---|
| 157 |
|
|---|
| 158 | //Allocate one dword to store the flat address of our TEB
|
|---|
| 159 | if(fMainThread) {
|
|---|
| 160 | TIBFlatPtr = (DWORD *)OSLibAllocThreadLocalMemory(1);
|
|---|
| 161 | if(TIBFlatPtr == 0) {
|
|---|
| 162 | dprintf(("InitializeTIB: local thread memory alloc failed!!"));
|
|---|
| 163 | DebugInt3();
|
|---|
| 164 | return NULL;
|
|---|
| 165 | }
|
|---|
| 166 | //SvL: This doesn't really create a thread, but only sets up the
|
|---|
| 167 | // handle of thread 0
|
|---|
| 168 | hThreadMain = HMCreateThread(NULL, 0, 0, 0, 0, 0, TRUE);
|
|---|
| 169 | }
|
|---|
| 170 | if(OSLibAllocSel(PAGE_SIZE, &tibsel) == FALSE)
|
|---|
| 171 | {
|
|---|
| 172 | dprintf(("InitializeTIB: selector alloc failed!!"));
|
|---|
| 173 | DebugInt3();
|
|---|
| 174 | return NULL;
|
|---|
| 175 | }
|
|---|
| 176 | winteb = (TEB *)OSLibSelToFlat(tibsel);
|
|---|
| 177 | if(winteb == NULL)
|
|---|
| 178 | {
|
|---|
| 179 | dprintf(("InitializeTIB: DosSelToFlat failed!!"));
|
|---|
| 180 | DebugInt3();
|
|---|
| 181 | return NULL;
|
|---|
| 182 | }
|
|---|
| 183 | memset(winteb, 0, PAGE_SIZE);
|
|---|
| 184 | thdb = (THDB *)(winteb+1);
|
|---|
| 185 | *TIBFlatPtr = (DWORD)winteb;
|
|---|
| 186 |
|
|---|
| 187 | winteb->except = (PVOID)-1; /* 00 Head of exception handling chain */
|
|---|
| 188 | winteb->stack_top = (PVOID)OSLibGetTIB(TIB_STACKTOP); /* 04 Top of thread stack */
|
|---|
| 189 | winteb->stack_low = (PVOID)OSLibGetTIB(TIB_STACKLOW); /* 08 Stack low-water mark */
|
|---|
| 190 | winteb->htask16 = (USHORT)OSLibGetPIB(PIB_TASKHNDL); /* 0c Win16 task handle */
|
|---|
| 191 | winteb->stack_sel = getSS(); /* 0e 16-bit stack selector */
|
|---|
| 192 | winteb->self = winteb; /* 18 Pointer to this structure */
|
|---|
| 193 | winteb->flags = TEBF_WIN32; /* 1c Flags */
|
|---|
| 194 | winteb->queue = 0; /* 28 Message queue */
|
|---|
| 195 | winteb->tls_ptr = &thdb->tls_array[0]; /* 2c Pointer to TLS array */
|
|---|
| 196 | winteb->process = &ProcessPDB; /* 30 owning process (used by NT3.51 applets)*/
|
|---|
| 197 |
|
|---|
| 198 | memcpy(&thdb->teb, winteb, sizeof(TEB));
|
|---|
| 199 | thdb->process = &ProcessPDB;
|
|---|
| 200 | thdb->exit_code = 0x103; /* STILL_ACTIVE */
|
|---|
| 201 | thdb->teb_sel = tibsel;
|
|---|
| 202 | thdb->OrgTIBSel = GetFS();
|
|---|
| 203 | thdb->pWsockData = NULL;
|
|---|
| 204 | thdb->threadId = GetCurrentThreadId();
|
|---|
| 205 | if(fMainThread) {
|
|---|
| 206 | thdb->hThread = hThreadMain;
|
|---|
| 207 | }
|
|---|
| 208 | else thdb->hThread = GetCurrentThread();
|
|---|
| 209 | thdb->lcid = GetUserDefaultLCID();
|
|---|
| 210 |
|
|---|
| 211 | threadListMutex.enter();
|
|---|
| 212 | THDB *thdblast = threadList;
|
|---|
| 213 | if(!thdblast) {
|
|---|
| 214 | threadList = thdb;
|
|---|
| 215 | }
|
|---|
| 216 | else {
|
|---|
| 217 | while(thdblast->next) {
|
|---|
| 218 | thdblast = thdblast->next;
|
|---|
| 219 | }
|
|---|
| 220 | thdblast->next = thdb;
|
|---|
| 221 | }
|
|---|
| 222 | thdb->next = NULL;
|
|---|
| 223 | threadListMutex.leave();
|
|---|
| 224 |
|
|---|
| 225 | if(OSLibGetPIB(PIB_TASKTYPE) == TASKTYPE_PM)
|
|---|
| 226 | {
|
|---|
| 227 | thdb->flags = 0; //todo gui
|
|---|
| 228 | }
|
|---|
| 229 | else thdb->flags = 0; //todo textmode
|
|---|
| 230 |
|
|---|
| 231 | //Initialize thread security objects (TODO: Not complete)
|
|---|
| 232 | if(hInstNTDll == 0) {
|
|---|
| 233 | hInstNTDll = LoadLibraryA("NTDLL.DLL");
|
|---|
| 234 | *(ULONG *)&RtlAllocateAndInitializeSid = (ULONG)GetProcAddress(hInstNTDll, "RtlAllocateAndInitializeSid");
|
|---|
| 235 | if(RtlAllocateAndInitializeSid == NULL) {
|
|---|
| 236 | DebugInt3();
|
|---|
| 237 | }
|
|---|
| 238 | }
|
|---|
| 239 | SID_IDENTIFIER_AUTHORITY sidIdAuth = {0};
|
|---|
| 240 | thdb->threadinfo.dwType = SECTYPE_PROCESS | SECTYPE_INITIALIZED;
|
|---|
| 241 | RtlAllocateAndInitializeSid(&sidIdAuth, 1, 0, 0, 0, 0, 0, 0, 0, 0, &thdb->threadinfo.SidUser.User.Sid);
|
|---|
| 242 | thdb->threadinfo.SidUser.User.Attributes = 0; //?????????
|
|---|
| 243 |
|
|---|
| 244 | thdb->threadinfo.pTokenGroups = (TOKEN_GROUPS*)malloc(sizeof(TOKEN_GROUPS));
|
|---|
| 245 | thdb->threadinfo.pTokenGroups->GroupCount = 1;
|
|---|
| 246 | RtlAllocateAndInitializeSid(&sidIdAuth, 1, 0, 0, 0, 0, 0, 0, 0, 0, &thdb->threadinfo.PrimaryGroup.PrimaryGroup);
|
|---|
| 247 | thdb->threadinfo.pTokenGroups->Groups[0].Sid = thdb->threadinfo.PrimaryGroup.PrimaryGroup;
|
|---|
| 248 | thdb->threadinfo.pTokenGroups->Groups[0].Attributes = 0; //????
|
|---|
| 249 | // pPrivilegeSet = NULL;
|
|---|
| 250 | // pTokenPrivileges= NULL;
|
|---|
| 251 | // TokenOwner = {0};
|
|---|
| 252 | // DefaultDACL = {0};
|
|---|
| 253 | // TokenSource = {0};
|
|---|
| 254 | thdb->threadinfo.TokenType = TokenPrimary;
|
|---|
| 255 |
|
|---|
| 256 | if(fMainThread)
|
|---|
| 257 | {
|
|---|
| 258 | //todo initialize PDB during process creation
|
|---|
| 259 | //todo: initialize TLS array if required
|
|---|
| 260 | //TLS in executable always TLS index 0?
|
|---|
| 261 | ProcessTIBSel = tibsel;
|
|---|
| 262 | ProcessPDB.exit_code = 0x103; /* STILL_ACTIVE */
|
|---|
| 263 | ProcessPDB.threads = 1;
|
|---|
| 264 | ProcessPDB.running_threads = 1;
|
|---|
| 265 | ProcessPDB.ring0_threads = 1;
|
|---|
| 266 | ProcessPDB.system_heap = GetProcessHeap();
|
|---|
| 267 | ProcessPDB.parent = 0;
|
|---|
| 268 | ProcessPDB.group = &ProcessPDB;
|
|---|
| 269 | ProcessPDB.priority = 8; /* Normal */
|
|---|
| 270 | ProcessPDB.heap = ProcessPDB.system_heap; /* will be changed later on */
|
|---|
| 271 | ProcessPDB.next = NULL;
|
|---|
| 272 | ProcessPDB.winver = 0xffff; /* to be determined */
|
|---|
| 273 | ProcessPDB.server_pid = (void *)GetCurrentProcessId();
|
|---|
| 274 | ProcessPDB.tls_bits[0] = 0; //all tls slots are free
|
|---|
| 275 | ProcessPDB.tls_bits[1] = 0;
|
|---|
| 276 |
|
|---|
| 277 | GetSystemTime(&ProcessPDB.creationTime) ;
|
|---|
| 278 |
|
|---|
| 279 | /* Initialize the critical section */
|
|---|
| 280 | InitializeCriticalSection( &ProcessPDB.crit_section );
|
|---|
| 281 | }
|
|---|
| 282 | dprintf(("InitializeTIB setup TEB with selector %x", tibsel));
|
|---|
| 283 | dprintf(("InitializeTIB: FS(%x):[0] = %x", GetFS(), QueryExceptionChain()));
|
|---|
| 284 | return winteb;
|
|---|
| 285 | }
|
|---|
| 286 | //******************************************************************************
|
|---|
| 287 | // Destroy the TIB selector and memory for the current thread
|
|---|
| 288 | //******************************************************************************
|
|---|
| 289 | void DestroyTIB()
|
|---|
| 290 | {
|
|---|
| 291 | SHORT orgtibsel;
|
|---|
| 292 | TEB *winteb;
|
|---|
| 293 | THDB *thdb;
|
|---|
| 294 |
|
|---|
| 295 | dprintf(("DestroyTIB: FS = %x", GetFS()));
|
|---|
| 296 | dprintf(("DestroyTIB: FS:[0] = %x", QueryExceptionChain()));
|
|---|
| 297 |
|
|---|
| 298 | winteb = (TEB *)*TIBFlatPtr;
|
|---|
| 299 | if(winteb) {
|
|---|
| 300 | thdb = (THDB *)(winteb+1);
|
|---|
| 301 | orgtibsel = thdb->OrgTIBSel;
|
|---|
| 302 |
|
|---|
| 303 | threadListMutex.enter();
|
|---|
| 304 | THDB *curthdb = threadList;
|
|---|
| 305 | if(curthdb == thdb) {
|
|---|
| 306 | threadList = thdb->next;
|
|---|
| 307 | }
|
|---|
| 308 | else {
|
|---|
| 309 | while(curthdb->next != thdb) {
|
|---|
| 310 | curthdb = curthdb->next;
|
|---|
| 311 | if(curthdb == NULL) {
|
|---|
| 312 | dprintf(("DestroyTIB: couldn't find thdb %x", thdb));
|
|---|
| 313 | DebugInt3();
|
|---|
| 314 | break;
|
|---|
| 315 | }
|
|---|
| 316 | }
|
|---|
| 317 | if(curthdb) {
|
|---|
| 318 | curthdb->next = thdb->next;
|
|---|
| 319 | }
|
|---|
| 320 | }
|
|---|
| 321 | threadListMutex.leave();
|
|---|
| 322 |
|
|---|
| 323 | //Restore our original FS selector
|
|---|
| 324 | SetFS(orgtibsel);
|
|---|
| 325 |
|
|---|
| 326 | //And free our own
|
|---|
| 327 | OSLibFreeSel(thdb->teb_sel);
|
|---|
| 328 |
|
|---|
| 329 | *TIBFlatPtr = 0;
|
|---|
| 330 | }
|
|---|
| 331 | else dprintf(("Already destroyed TIB"));
|
|---|
| 332 |
|
|---|
| 333 | dprintf(("DestroyTIB: FS(%x):[0] = %x", GetFS(), QueryExceptionChain()));
|
|---|
| 334 | return;
|
|---|
| 335 | }
|
|---|
| 336 | /******************************************************************************/
|
|---|
| 337 | /******************************************************************************/
|
|---|
| 338 | void SetPDBInstance(HINSTANCE hInstance)
|
|---|
| 339 | {
|
|---|
| 340 | ProcessPDB.hInstance = hInstance;
|
|---|
| 341 | }
|
|---|
| 342 | /******************************************************************************/
|
|---|
| 343 | /******************************************************************************/
|
|---|
| 344 | void WIN32API RestoreOS2TIB()
|
|---|
| 345 | {
|
|---|
| 346 | SHORT orgtibsel;
|
|---|
| 347 | TEB *winteb;
|
|---|
| 348 | THDB *thdb;
|
|---|
| 349 |
|
|---|
| 350 | //If we're running an Odin32 OS/2 application (not converted!), then we
|
|---|
| 351 | //we don't switch FS selectors
|
|---|
| 352 | if(fIsOS2Image) {
|
|---|
| 353 | return;
|
|---|
| 354 | }
|
|---|
| 355 |
|
|---|
| 356 | winteb = (TEB *)*TIBFlatPtr;
|
|---|
| 357 | if(winteb) {
|
|---|
| 358 | thdb = (THDB *)(winteb+1);
|
|---|
| 359 | orgtibsel = thdb->OrgTIBSel;
|
|---|
| 360 |
|
|---|
| 361 | //Restore our original FS selector
|
|---|
| 362 | SetFS(orgtibsel);
|
|---|
| 363 | }
|
|---|
| 364 | }
|
|---|
| 365 | /******************************************************************************/
|
|---|
| 366 | /******************************************************************************/
|
|---|
| 367 | USHORT WIN32API SetWin32TIB()
|
|---|
| 368 | {
|
|---|
| 369 | SHORT win32tibsel;
|
|---|
| 370 | TEB *winteb;
|
|---|
| 371 | THDB *thdb;
|
|---|
| 372 |
|
|---|
| 373 | //If we're running an Odin32 OS/2 application (not converted!), then we
|
|---|
| 374 | //we don't switch FS selectors
|
|---|
| 375 | if(fIsOS2Image) {
|
|---|
| 376 | return GetFS();
|
|---|
| 377 | }
|
|---|
| 378 |
|
|---|
| 379 | winteb = (TEB *)*TIBFlatPtr;
|
|---|
| 380 | if(winteb) {
|
|---|
| 381 | thdb = (THDB *)(winteb+1);
|
|---|
| 382 | win32tibsel = thdb->teb_sel;
|
|---|
| 383 |
|
|---|
| 384 | //Restore our win32 FS selector
|
|---|
| 385 | return SetReturnFS(win32tibsel);
|
|---|
| 386 | }
|
|---|
| 387 | else {
|
|---|
| 388 | //we didn't create this thread, so allocate a selector now
|
|---|
| 389 | //NOTE: Possible memory leak (i.e. DART threads in WINMM)
|
|---|
| 390 | winteb = InitializeTIB();
|
|---|
| 391 | if(winteb == NULL) {
|
|---|
| 392 | DebugInt3();
|
|---|
| 393 | return GetFS();
|
|---|
| 394 | }
|
|---|
| 395 | thdb = (THDB *)(winteb+1);
|
|---|
| 396 | win32tibsel = thdb->teb_sel;
|
|---|
| 397 |
|
|---|
| 398 | //Restore our win32 FS selector
|
|---|
| 399 | return SetReturnFS(win32tibsel);
|
|---|
| 400 | }
|
|---|
| 401 | // nested calls are OK, OS2ToWinCallback for instance
|
|---|
| 402 | //else DebugInt3();
|
|---|
| 403 |
|
|---|
| 404 | return GetFS();
|
|---|
| 405 | }
|
|---|
| 406 | //******************************************************************************
|
|---|
| 407 | //******************************************************************************
|
|---|
| 408 | VOID WIN32API ExitProcess(DWORD exitcode)
|
|---|
| 409 | {
|
|---|
| 410 | dprintf(("KERNEL32: ExitProcess %d\n", exitcode));
|
|---|
| 411 | dprintf(("KERNEL32: ExitProcess FS = %x\n", GetFS()));
|
|---|
| 412 |
|
|---|
| 413 | fExitProcess = TRUE;
|
|---|
| 414 |
|
|---|
| 415 | SetOS2ExceptionChain(-1);
|
|---|
| 416 |
|
|---|
| 417 | if(WinExe) {
|
|---|
| 418 | delete(WinExe);
|
|---|
| 419 | WinExe = NULL;
|
|---|
| 420 | }
|
|---|
| 421 |
|
|---|
| 422 | //Note: Needs to be done after deleting WinExe (destruction of exe + dll objects)
|
|---|
| 423 | //Flush and delete all open memory mapped files
|
|---|
| 424 | Win32MemMap::deleteAll();
|
|---|
| 425 |
|
|---|
| 426 | //Restore original OS/2 TIB selector
|
|---|
| 427 | DestroyTIB();
|
|---|
| 428 | SetExceptionChain((ULONG)-1);
|
|---|
| 429 |
|
|---|
| 430 | //avoid crashes since win32 & OS/2 exception handler aren't identical
|
|---|
| 431 | //(terminate process generates two exceptions)
|
|---|
| 432 | /* @@@PH 1998/02/12 Added Console Support */
|
|---|
| 433 | if (iConsoleIsActive())
|
|---|
| 434 | iConsoleWaitClose();
|
|---|
| 435 |
|
|---|
| 436 | O32_ExitProcess(exitcode);
|
|---|
| 437 | }
|
|---|
| 438 | //******************************************************************************
|
|---|
| 439 | //******************************************************************************
|
|---|
| 440 | BOOL WIN32API FreeLibrary(HINSTANCE hinstance)
|
|---|
| 441 | {
|
|---|
| 442 | Win32DllBase *winmod;
|
|---|
| 443 | BOOL rc;
|
|---|
| 444 |
|
|---|
| 445 | //SvL: Ignore FreeLibary for executable
|
|---|
| 446 | if(WinExe && hinstance == WinExe->getInstanceHandle()) {
|
|---|
| 447 | return TRUE;
|
|---|
| 448 | }
|
|---|
| 449 |
|
|---|
| 450 | winmod = Win32DllBase::findModule(hinstance);
|
|---|
| 451 | if(winmod) {
|
|---|
| 452 | dprintf(("FreeLibrary %s", winmod->getName()));
|
|---|
| 453 | //Only free it when the nrDynamicLibRef != 0
|
|---|
| 454 | //This prevent problems after ExitProcess:
|
|---|
| 455 | //i.e. dll A is referenced by our exe and loaded with LoadLibrary by dll B
|
|---|
| 456 | // During ExitProcess it's unloaded once (before dll B), dll B calls
|
|---|
| 457 | // FreeLibrary, but our exe also has a reference -> unloaded too many times
|
|---|
| 458 | if(winmod->isDynamicLib()) {
|
|---|
| 459 | winmod->decDynamicLib();
|
|---|
| 460 | winmod->Release();
|
|---|
| 461 | }
|
|---|
| 462 | else {
|
|---|
| 463 | dprintf(("Skipping dynamic unload as nrDynamicLibRef == 0"));
|
|---|
| 464 | }
|
|---|
| 465 | return(TRUE);
|
|---|
| 466 | }
|
|---|
| 467 | dprintf(("WARNING: KERNEL32: FreeLibrary %s %x NOT FOUND!", OSLibGetDllName(hinstance), hinstance));
|
|---|
| 468 | return(TRUE);
|
|---|
| 469 | }
|
|---|
| 470 | /******************************************************************************/
|
|---|
| 471 | /******************************************************************************/
|
|---|
| 472 | /**
|
|---|
| 473 | * LoadLibraryA can be used to map a DLL module into the calling process's
|
|---|
| 474 | * addressspace. It returns a handle that can be used with GetProcAddress to
|
|---|
| 475 | * get addresses of exported entry points (functions and variables).
|
|---|
| 476 | *
|
|---|
| 477 | * LoadLibraryA can also be used to map executable (.exe) modules into the
|
|---|
| 478 | * address to access resources in the module. However, LoadLibrary can't be
|
|---|
| 479 | * used to run an executable (.exe) module.
|
|---|
| 480 | *
|
|---|
| 481 | * @returns Handle to the library which was loaded.
|
|---|
| 482 | * @param lpszLibFile Pointer to zero ASCII string giving the name of the
|
|---|
| 483 | * executable image (either a Dll or an Exe) which is to be
|
|---|
| 484 | * loaded.
|
|---|
| 485 | *
|
|---|
| 486 | * If no extention is specified the default .DLL extention is
|
|---|
| 487 | * appended to the name. End the filename with an '.' if the
|
|---|
| 488 | * file does not have an extention (and don't want the .DLL
|
|---|
| 489 | * appended).
|
|---|
| 490 | *
|
|---|
| 491 | * If no path is specified, this API will use the Odin32
|
|---|
| 492 | * standard search strategy to find the file. This strategy
|
|---|
| 493 | * is described in the method Win32ImageBase::findDLL.
|
|---|
| 494 | *
|
|---|
| 495 | * This API likes to have backslashes (\), but will probably
|
|---|
| 496 | * accept forward slashes too. Win32 SDK docs says that it
|
|---|
| 497 | * should not contain forward slashes.
|
|---|
| 498 | *
|
|---|
| 499 | * Win32 SDK docs adds:
|
|---|
| 500 | * "The name specified is the file name of the module and
|
|---|
| 501 | * is not related to the name stored in the library module
|
|---|
| 502 | * itself, as specified by the LIBRARY keyword in the
|
|---|
| 503 | * module-definition (.def) file."
|
|---|
| 504 | *
|
|---|
| 505 | * @sketch Call LoadLibraryExA with flags set to 0.
|
|---|
| 506 | * @status Odin32 Completely Implemented.
|
|---|
| 507 | * @author Sander van Leeuwen (sandervl@xs4all.nl)
|
|---|
| 508 | * knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
|
|---|
| 509 | * @remark Forwards to LoadLibraryExA.
|
|---|
| 510 | */
|
|---|
| 511 | HINSTANCE WIN32API LoadLibraryA(LPCTSTR lpszLibFile)
|
|---|
| 512 | {
|
|---|
| 513 | HINSTANCE hDll;
|
|---|
| 514 |
|
|---|
| 515 | dprintf(("KERNEL32: LoadLibraryA(%s) --> LoadLibraryExA(lpszLibFile, 0, 0)",
|
|---|
| 516 | lpszLibFile));
|
|---|
| 517 | hDll = LoadLibraryExA(lpszLibFile, 0, 0);
|
|---|
| 518 | dprintf(("KERNEL32: LoadLibraryA(%s) returns 0x%x",
|
|---|
| 519 | lpszLibFile, hDll));
|
|---|
| 520 | return hDll;
|
|---|
| 521 | }
|
|---|
| 522 |
|
|---|
| 523 |
|
|---|
| 524 | /**
|
|---|
| 525 | * LoadLibraryW can be used to map a DLL module into the calling process's
|
|---|
| 526 | * addressspace. It returns a handle that can be used with GetProcAddress to
|
|---|
| 527 | * get addresses of exported entry points (functions and variables).
|
|---|
| 528 | *
|
|---|
| 529 | * LoadLibraryW can also be used to map executable (.exe) modules into the
|
|---|
| 530 | * address to access resources in the module. However, LoadLibrary can't be
|
|---|
| 531 | * used to run an executable (.exe) module.
|
|---|
| 532 | *
|
|---|
| 533 | * @returns Handle to the library which was loaded.
|
|---|
| 534 | * @param lpszLibFile Pointer to Unicode string giving the name of
|
|---|
| 535 | * the executable image (either a Dll or an Exe) which is to
|
|---|
| 536 | * be loaded.
|
|---|
| 537 | *
|
|---|
| 538 | * If no extention is specified the default .DLL extention is
|
|---|
| 539 | * appended to the name. End the filename with an '.' if the
|
|---|
| 540 | * file does not have an extention (and don't want the .DLL
|
|---|
| 541 | * appended).
|
|---|
| 542 | *
|
|---|
| 543 | * If no path is specified, this API will use the Odin32
|
|---|
| 544 | * standard search strategy to find the file. This strategy
|
|---|
| 545 | * is described in the method Win32ImageBase::findDLL.
|
|---|
| 546 | *
|
|---|
| 547 | * This API likes to have backslashes (\), but will probably
|
|---|
| 548 | * accept forward slashes too. Win32 SDK docs says that it
|
|---|
| 549 | * should not contain forward slashes.
|
|---|
| 550 | *
|
|---|
| 551 | * Win32 SDK docs adds:
|
|---|
| 552 | * "The name specified is the file name of the module and
|
|---|
| 553 | * is not related to the name stored in the library module
|
|---|
| 554 | * itself, as specified by the LIBRARY keyword in the
|
|---|
| 555 | * module-definition (.def) file."
|
|---|
| 556 | *
|
|---|
| 557 | * @sketch Convert Unicode name to ascii.
|
|---|
| 558 | * Call LoadLibraryExA with flags set to 0.
|
|---|
| 559 | * free ascii string.
|
|---|
| 560 | * @status Odin32 Completely Implemented.
|
|---|
| 561 | * @author Sander van Leeuwen (sandervl@xs4all.nl)
|
|---|
| 562 | * knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
|
|---|
| 563 | * @remark Forwards to LoadLibraryExA.
|
|---|
| 564 | */
|
|---|
| 565 | HINSTANCE WIN32API LoadLibraryW(LPCWSTR lpszLibFile)
|
|---|
| 566 | {
|
|---|
| 567 | char * pszAsciiLibFile;
|
|---|
| 568 | HINSTANCE hDll;
|
|---|
| 569 |
|
|---|
| 570 | pszAsciiLibFile = UnicodeToAsciiString(lpszLibFile);
|
|---|
| 571 | dprintf(("KERNEL32: LoadLibraryW(%s) --> LoadLibraryExA(lpszLibFile, 0, 0)",
|
|---|
| 572 | pszAsciiLibFile));
|
|---|
| 573 | hDll = LoadLibraryExA(pszAsciiLibFile, NULL, 0);
|
|---|
| 574 | dprintf(("KERNEL32: LoadLibraryW(%s) returns 0x%x",
|
|---|
| 575 | pszAsciiLibFile, hDll));
|
|---|
| 576 | free(pszAsciiLibFile);
|
|---|
| 577 |
|
|---|
| 578 | return hDll;
|
|---|
| 579 | }
|
|---|
| 580 |
|
|---|
| 581 |
|
|---|
| 582 | /**
|
|---|
| 583 | * LoadLibraryExA can be used to map a DLL module into the calling process's
|
|---|
| 584 | * addressspace. It returns a handle that can be used with GetProcAddress to
|
|---|
| 585 | * get addresses of exported entry points (functions and variables).
|
|---|
| 586 | *
|
|---|
| 587 | * LoadLibraryExA can also be used to map executable (.exe) modules into the
|
|---|
| 588 | * address to access resources in the module. However, LoadLibrary can't be
|
|---|
| 589 | * used to run an executable (.exe) module.
|
|---|
| 590 | *
|
|---|
| 591 | * @returns Handle to the library which was loaded.
|
|---|
| 592 | * @param lpszLibFile Pointer to Unicode string giving the name of
|
|---|
| 593 | * the executable image (either a Dll or an Exe) which is to
|
|---|
| 594 | * be loaded.
|
|---|
| 595 | *
|
|---|
| 596 | * If no extention is specified the default .DLL extention is
|
|---|
| 597 | * appended to the name. End the filename with an '.' if the
|
|---|
| 598 | * file does not have an extention (and don't want the .DLL
|
|---|
| 599 | * appended).
|
|---|
| 600 | *
|
|---|
| 601 | * If no path is specified, this API will use the Odin32
|
|---|
| 602 | * standard search strategy to find the file. This strategy
|
|---|
| 603 | * is described in the method Win32ImageBase::findDLL.
|
|---|
| 604 | * This may be alterned by the LOAD_WITH_ALTERED_SEARCH_PATH
|
|---|
| 605 | * flag, see below.
|
|---|
| 606 | *
|
|---|
| 607 | * This API likes to have backslashes (\), but will probably
|
|---|
| 608 | * accept forward slashes too. Win32 SDK docs says that it
|
|---|
| 609 | * should not contain forward slashes.
|
|---|
| 610 | *
|
|---|
| 611 | * Win32 SDK docs adds:
|
|---|
| 612 | * "The name specified is the file name of the module and
|
|---|
| 613 | * is not related to the name stored in the library module
|
|---|
| 614 | * itself, as specified by the LIBRARY keyword in the
|
|---|
| 615 | * module-definition (.def) file."
|
|---|
| 616 | *
|
|---|
| 617 | * @param hFile Reserved. Must be 0.
|
|---|
| 618 | *
|
|---|
| 619 | * @param dwFlags Flags which specifies the taken when loading the module.
|
|---|
| 620 | * The value 0 makes it identical to LoadLibraryA/W.
|
|---|
| 621 | *
|
|---|
| 622 | * Flags:
|
|---|
| 623 | *
|
|---|
| 624 | * DONT_RESOLVE_DLL_REFERENCES
|
|---|
| 625 | * (WinNT/2K feature): Don't load imported modules and
|
|---|
| 626 | * hence don't resolve imported symbols.
|
|---|
| 627 | * DllMain isn't called either. (Which is obvious since
|
|---|
| 628 | * it may use one of the importe symbols.)
|
|---|
| 629 | *
|
|---|
| 630 | * On the other hand, if this flag is NOT set, the system
|
|---|
| 631 | * load imported modules, resolves imported symbols, calls
|
|---|
| 632 | * DllMain for process and thread init and term (if wished
|
|---|
| 633 | * by the module).
|
|---|
| 634 | *
|
|---|
| 635 | * partially implemented yet - imports are resolved it seems.
|
|---|
| 636 | *
|
|---|
| 637 | * LOAD_LIBRARY_AS_DATAFILE
|
|---|
| 638 | * If this flag is set, the module is mapped into the
|
|---|
| 639 | * address space but is not prepared for execution. Though
|
|---|
| 640 | * it's preparted for resource API. Hence, you'll use this
|
|---|
| 641 | * flag when you want to load a DLL for extracting
|
|---|
| 642 | * messages or resources from it.
|
|---|
| 643 | *
|
|---|
| 644 | * The resulting handle can be used with any Odin32 API
|
|---|
| 645 | * which operates on resources.
|
|---|
| 646 | * (WinNt/2k supports all resource APIs while Win9x don't
|
|---|
| 647 | * support the specialized resource APIs: LoadBitmap,
|
|---|
| 648 | * LoadCursor, LoadIcon, LoadImage, LoadMenu.)
|
|---|
| 649 | *
|
|---|
| 650 | * not implemented yet.
|
|---|
| 651 | *
|
|---|
| 652 | * LOAD_WITH_ALTERED_SEARCH_PATH
|
|---|
| 653 | * If this flag is set and lpszLibFile specifies a path
|
|---|
| 654 | * we'll use an alternative file search strategy to find
|
|---|
| 655 | * imported modules. This stratgy is simply to use the
|
|---|
| 656 | * path of the module being loaded instead of the path
|
|---|
| 657 | * of the executable module as the first location
|
|---|
| 658 | * to search for imported modules.
|
|---|
| 659 | *
|
|---|
| 660 | * If this flag is clear, the standard Odin32 standard
|
|---|
| 661 | * search strategy. See Win32ImageBase::findDll for
|
|---|
| 662 | * further information.
|
|---|
| 663 | *
|
|---|
| 664 | * not implemented yet.
|
|---|
| 665 | *
|
|---|
| 666 | * @status Open32 Partially Implemented.
|
|---|
| 667 | * @author Sander van Leeuwen (sandervl@xs4all.nl)
|
|---|
| 668 | * knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
|
|---|
| 669 | * @remark Forwards to LoadLibraryExA.
|
|---|
| 670 | */
|
|---|
| 671 | HINSTANCE WIN32API LoadLibraryExA(LPCTSTR lpszLibFile, HANDLE hFile, DWORD dwFlags)
|
|---|
| 672 | {
|
|---|
| 673 | HINSTANCE hDll;
|
|---|
| 674 | Win32DllBase * pModule;
|
|---|
| 675 | char szModname[CCHMAXPATH];
|
|---|
| 676 | BOOL fPath; /* Flags which is set if the */
|
|---|
| 677 | /* lpszLibFile contains a path. */
|
|---|
| 678 | ULONG fPE; /* isPEImage return value. */
|
|---|
| 679 |
|
|---|
| 680 | /** @sketch
|
|---|
| 681 | * Some parameter validations is probably useful.
|
|---|
| 682 | */
|
|---|
| 683 | if (!VALID_PSZ(lpszLibFile))
|
|---|
| 684 | {
|
|---|
| 685 | dprintf(("KERNEL32: LoadLibraryExA(0x%x, 0x%x, 0x%x): invalid pointer lpszLibFile = 0x%x\n",
|
|---|
| 686 | lpszLibFile, hFile, dwFlags, lpszLibFile));
|
|---|
| 687 | SetLastError(ERROR_INVALID_PARAMETER); //or maybe ERROR_ACCESS_DENIED is more appropriate?
|
|---|
| 688 | return NULL;
|
|---|
| 689 | }
|
|---|
| 690 | if (!VALID_PSZMAXSIZE(lpszLibFile, CCHMAXPATH))
|
|---|
| 691 | {
|
|---|
| 692 | dprintf(("KERNEL32: LoadLibraryExA(%s, 0x%x, 0x%x): lpszLibFile string too long, %d\n",
|
|---|
| 693 | lpszLibFile, hFile, dwFlags, strlen(lpszLibFile)));
|
|---|
| 694 | SetLastError(ERROR_INVALID_PARAMETER);
|
|---|
| 695 | return NULL;
|
|---|
| 696 | }
|
|---|
| 697 | if ((dwFlags & ~(DONT_RESOLVE_DLL_REFERENCES | LOAD_WITH_ALTERED_SEARCH_PATH | LOAD_LIBRARY_AS_DATAFILE)) != 0)
|
|---|
| 698 | {
|
|---|
| 699 | dprintf(("KERNEL32: LoadLibraryExA(%s, 0x%x, 0x%x): dwFlags have invalid or unsupported flags\n",
|
|---|
| 700 | lpszLibFile, hFile, dwFlags));
|
|---|
| 701 | SetLastError(ERROR_INVALID_PARAMETER);
|
|---|
| 702 | return NULL;
|
|---|
| 703 | }
|
|---|
| 704 |
|
|---|
| 705 |
|
|---|
| 706 | /** @sketch
|
|---|
| 707 | * First we'll see if the module is allready loaded - either as the EXE or as DLL.
|
|---|
| 708 | * IF Executable present AND libfile matches the modname of the executable THEN
|
|---|
| 709 | * RETURN instance handle of executable.
|
|---|
| 710 | * Endif
|
|---|
| 711 | * IF allready loaded THEN
|
|---|
| 712 | * IF it's a LX dll which isn't loaded and we're using the PeLoader THEN
|
|---|
| 713 | * Set Load library.
|
|---|
| 714 | * Endif
|
|---|
| 715 | * Inc dynamic reference count.
|
|---|
| 716 | * Inc reference count.
|
|---|
| 717 | * RETURN instance handle.
|
|---|
| 718 | * Endif
|
|---|
| 719 | */
|
|---|
| 720 | if (WinExe != NULL && WinExe->matchModName(lpszLibFile))
|
|---|
| 721 | return WinExe->getInstanceHandle();
|
|---|
| 722 | pModule = Win32DllBase::findModule((LPSTR)lpszLibFile);
|
|---|
| 723 | if (pModule)
|
|---|
| 724 | {
|
|---|
| 725 | pModule->incDynamicLib();
|
|---|
| 726 | pModule->AddRef();
|
|---|
| 727 | dprintf(("KERNEL32: LoadLibraryExA(%s, 0x%x, 0x%x): returns 0x%x. Dll found %s",
|
|---|
| 728 | lpszLibFile, hFile, dwFlags, pModule->getInstanceHandle(), pModule->getFullPath()));
|
|---|
| 729 | return pModule->getInstanceHandle();
|
|---|
| 730 | }
|
|---|
| 731 |
|
|---|
| 732 |
|
|---|
| 733 | /** @sketch
|
|---|
| 734 | * Test if lpszLibFile has a path or not.
|
|---|
| 735 | * Copy the lpszLibFile to szModname, rename the dll and uppercase the name.
|
|---|
| 736 | * IF it hasn't a path THEN
|
|---|
| 737 | * Issue a findDll to find the dll/executable to be loaded.
|
|---|
| 738 | * IF the Dll isn't found THEN
|
|---|
| 739 | * Set last error and RETURN.
|
|---|
| 740 | * Endif.
|
|---|
| 741 | * Endif
|
|---|
| 742 | */
|
|---|
| 743 | fPath = strchr(lpszLibFile, '\\') || strchr(lpszLibFile, '/');
|
|---|
| 744 | strcpy(szModname, lpszLibFile);
|
|---|
| 745 | Win32DllBase::renameDll(szModname);
|
|---|
| 746 | strupr(szModname);
|
|---|
| 747 |
|
|---|
| 748 | if (!fPath)
|
|---|
| 749 | {
|
|---|
| 750 | char szModName2[CCHMAXPATH];
|
|---|
| 751 | strcpy(szModName2, szModname);
|
|---|
| 752 | if (!Win32ImageBase::findDll(szModName2, szModname, sizeof(szModname)))
|
|---|
| 753 | {
|
|---|
| 754 | dprintf(("KERNEL32: LoadLibraryExA(%s, 0x%x, 0x%x): module wasn't found. returns NULL",
|
|---|
| 755 | lpszLibFile, hFile, dwFlags));
|
|---|
| 756 | SetLastError(ERROR_FILE_NOT_FOUND);
|
|---|
| 757 | return NULL;
|
|---|
| 758 | }
|
|---|
| 759 | }
|
|---|
| 760 |
|
|---|
| 761 | //test if dll is in PE or LX format
|
|---|
| 762 | fPE = Win32ImageBase::isPEImage(szModname);
|
|---|
| 763 |
|
|---|
| 764 | /** @sketch
|
|---|
| 765 | * IF dwFlags == 0 && (!fPeLoader || !fPE) THEN
|
|---|
| 766 | * Try load the executable using LoadLibrary
|
|---|
| 767 | * IF successfully loaded THEN
|
|---|
| 768 | * IF LX dll and is using the PE Loader THEN
|
|---|
| 769 | * Set Load library.
|
|---|
| 770 | * Inc reference count.
|
|---|
| 771 | * Endif
|
|---|
| 772 | * Inc dynamic reference count.
|
|---|
| 773 | * RETURN successfully.
|
|---|
| 774 | * Endif
|
|---|
| 775 | * Endif
|
|---|
| 776 | */
|
|---|
| 777 | //only call Open32 if dwFlags == 0 and (LX binary or win32k process)
|
|---|
| 778 | if(dwFlags == 0 && (!fPeLoader || fPE != ERROR_SUCCESS))
|
|---|
| 779 | {
|
|---|
| 780 | hDll = O32_LoadLibrary(szModname);
|
|---|
| 781 | if (hDll)
|
|---|
| 782 | {
|
|---|
| 783 | /* OS/2 dll, system dll, converted dll or win32k took care of it. */
|
|---|
| 784 | pModule = (Win32DllBase *)Win32LxDll::findModuleByOS2Handle(hDll);
|
|---|
| 785 | if(pModule)
|
|---|
| 786 | {
|
|---|
| 787 |
|
|---|
| 788 | if(pModule->isLxDll())
|
|---|
| 789 | {
|
|---|
| 790 | ((Win32LxDll *)pModule)->setDllHandleOS2(hDll);
|
|---|
| 791 | if(fPeLoader)
|
|---|
| 792 | {
|
|---|
| 793 | if(pModule->AddRef() == -1) {//-1 -> load failed (attachProcess)
|
|---|
| 794 | dprintf(("Dll %s refused to be loaded; aborting", szModname));
|
|---|
| 795 | delete pModule;
|
|---|
| 796 | return 0;
|
|---|
| 797 | }
|
|---|
| 798 | }
|
|---|
| 799 | }
|
|---|
| 800 | pModule->incDynamicLib();
|
|---|
| 801 | }
|
|---|
| 802 | else
|
|---|
| 803 | return hDll; //happens when LoadLibrary is called in kernel32's initterm (nor harmful)
|
|---|
| 804 |
|
|---|
| 805 | dprintf(("KERNEL32: LoadLibraryExA(%s, 0x%x, 0x%x): returns 0x%x. Loaded %s using O32_LoadLibrary.",
|
|---|
| 806 | lpszLibFile, hFile, dwFlags, hDll, szModname));
|
|---|
| 807 | return pModule->getInstanceHandle();
|
|---|
| 808 | }
|
|---|
| 809 | dprintf(("KERNEL32: LoadLibraryExA(%s, 0x%x, 0x%x): O32_LoadLibrary(%s) failed. LastError=%d",
|
|---|
| 810 | lpszLibFile, hFile, dwFlags, szModname, GetLastError()));
|
|---|
| 811 | }
|
|---|
| 812 | else
|
|---|
| 813 | hDll = NULL;
|
|---|
| 814 |
|
|---|
| 815 |
|
|---|
| 816 | /** @sketch
|
|---|
| 817 | * If PE image THEN
|
|---|
| 818 | * IF LOAD_LIBRARY_AS_DATAFILE or Executable THEN
|
|---|
| 819 | *
|
|---|
| 820 | *
|
|---|
| 821 | * Try load the file using the Win32PeLdrDll class.
|
|---|
| 822 | * <sketch continued further down>
|
|---|
| 823 | * Else
|
|---|
| 824 | * Set last error.
|
|---|
| 825 | * (hDll is NULL)
|
|---|
| 826 | * Endif
|
|---|
| 827 | * return hDll.
|
|---|
| 828 | */
|
|---|
| 829 | if(fPE == ERROR_SUCCESS)
|
|---|
| 830 | {
|
|---|
| 831 | Win32PeLdrDll * peldrDll;
|
|---|
| 832 | /* TODO!
|
|---|
| 833 | * We might use the fake loader class to do the LOAD_LIBRARY_AS_DATAFILE and
|
|---|
| 834 | * executable image loading. These are both loaded for accessing resource.
|
|---|
| 835 | * But this requires that they behaves like Dlls...
|
|---|
| 836 | if (dwFlags & LOAD_LIBRARY_AS_DATAFILE || fPE == 2)
|
|---|
| 837 | {
|
|---|
| 838 | peldrDll = new Win32PeLdrRsrcImg(szModname);
|
|---|
| 839 | }
|
|---|
| 840 | else
|
|---|
| 841 | */
|
|---|
| 842 |
|
|---|
| 843 | peldrDll = new Win32PeLdrDll(szModname);
|
|---|
| 844 | if (peldrDll == NULL)
|
|---|
| 845 | {
|
|---|
| 846 | dprintf(("KERNEL32: LoadLibraryExA(%s, 0x%x, 0x%x): Failed to created instance of Win32PeLdrDll. returns NULL.",
|
|---|
| 847 | lpszLibFile, hFile, dwFlags));
|
|---|
| 848 | SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
|---|
| 849 | return NULL;
|
|---|
| 850 | }
|
|---|
| 851 |
|
|---|
| 852 | /** @sketch
|
|---|
| 853 | * Process dwFlags
|
|---|
| 854 | */
|
|---|
| 855 | if (dwFlags & (DONT_RESOLVE_DLL_REFERENCES | LOAD_LIBRARY_AS_DATAFILE))
|
|---|
| 856 | {
|
|---|
| 857 | peldrDll->disableThreadLibraryCalls();
|
|---|
| 858 | //peldrDll->setDontProcessImports(); not implemented?
|
|---|
| 859 | }
|
|---|
| 860 | if (dwFlags & LOAD_WITH_ALTERED_SEARCH_PATH)
|
|---|
| 861 | {
|
|---|
| 862 | dprintf(("KERNEL32: LoadLibraryExA(%s, 0x%x, 0x%x): Warning dwFlags LOAD_WITH_ALTERED_SEARCH_PATH is not implemented.",
|
|---|
| 863 | lpszLibFile, hFile, dwFlags));
|
|---|
| 864 | //peldrDll->setLoadWithAlteredSearchPath();
|
|---|
| 865 | }
|
|---|
| 866 | if (dwFlags & LOAD_LIBRARY_AS_DATAFILE)
|
|---|
| 867 | {
|
|---|
| 868 | dprintf(("KERNEL32: LoadLibraryExA(%s, 0x%x, 0x%x): Warning dwFlags LOAD_LIBRARY_AS_DATAFILE is not implemented.",
|
|---|
| 869 | lpszLibFile, hFile, dwFlags));
|
|---|
| 870 | //peldrDll->setLoadAsDatafile();
|
|---|
| 871 | }
|
|---|
| 872 |
|
|---|
| 873 | /** @sketch
|
|---|
| 874 | * Initiate the peldr DLL.
|
|---|
| 875 | * IF successful init THEN
|
|---|
| 876 | * Inc dynamic ref count.
|
|---|
| 877 | * Inc ref count.
|
|---|
| 878 | * Attach to process
|
|---|
| 879 | * IF successful THEN
|
|---|
| 880 | * hDLL <- instance handle.
|
|---|
| 881 | * ELSE
|
|---|
| 882 | * set last error
|
|---|
| 883 | * delete Win32PeLdrDll instance.
|
|---|
| 884 | * Endif
|
|---|
| 885 | * ELSE
|
|---|
| 886 | * set last error
|
|---|
| 887 | * delete Win32PeLdrDll instance.
|
|---|
| 888 | * Endif.
|
|---|
| 889 | */
|
|---|
| 890 | if (peldrDll->init(0))
|
|---|
| 891 | {
|
|---|
| 892 | peldrDll->AddRef();
|
|---|
| 893 | if (peldrDll->attachProcess())
|
|---|
| 894 | {
|
|---|
| 895 | hDll = peldrDll->getInstanceHandle();
|
|---|
| 896 | //Must be called *after* attachprocess, since attachprocess may also
|
|---|
| 897 | //trigger LoadLibrary calls
|
|---|
| 898 | //Those dlls must not be put in front of this dll in the dynamic
|
|---|
| 899 | //dll list; or else the unload order is wrong:
|
|---|
| 900 | //i.e. RPAP3260 loads PNRS3260 in DLL_PROCESS_ATTACH
|
|---|
| 901 | // this means that in ExitProcess, PNRS3260 needs to be removed
|
|---|
| 902 | // first since RPAP3260 depends on it
|
|---|
| 903 | peldrDll->incDynamicLib();
|
|---|
| 904 | }
|
|---|
| 905 | else
|
|---|
| 906 | {
|
|---|
| 907 | dprintf(("KERNEL32: LoadLibraryExA(%s, 0x%x, 0x%x): attachProcess call to Win32PeLdrDll instance failed. returns NULL.",
|
|---|
| 908 | lpszLibFile, hFile, dwFlags));
|
|---|
| 909 | SetLastError(ERROR_DLL_INIT_FAILED);
|
|---|
| 910 | delete peldrDll;
|
|---|
| 911 | return NULL;
|
|---|
| 912 | }
|
|---|
| 913 | }
|
|---|
| 914 | else
|
|---|
| 915 | {
|
|---|
| 916 | dprintf(("KERNEL32: LoadLibraryExA(%s, 0x%x, 0x%x): Failed to init Win32PeLdrDll instance. error=%d returns NULL.",
|
|---|
| 917 | lpszLibFile, hFile, dwFlags, peldrDll->getError()));
|
|---|
| 918 | SetLastError(ERROR_INVALID_EXE_SIGNATURE);
|
|---|
| 919 | delete peldrDll;
|
|---|
| 920 | return NULL;
|
|---|
| 921 | }
|
|---|
| 922 | }
|
|---|
| 923 | else
|
|---|
| 924 | {
|
|---|
| 925 | dprintf(("KERNEL32: LoadLibraryExA(%s, 0x%x, 0x%x) library wasn't found (%s) or isn't loadable; err %x",
|
|---|
| 926 | lpszLibFile, hFile, dwFlags, szModname, fPE));
|
|---|
| 927 | SetLastError(fPE);
|
|---|
| 928 | return NULL;
|
|---|
| 929 | }
|
|---|
| 930 |
|
|---|
| 931 | return hDll;
|
|---|
| 932 | }
|
|---|
| 933 |
|
|---|
| 934 |
|
|---|
| 935 | /**
|
|---|
| 936 | * LoadLibraryExW can be used to map a DLL module into the calling process's
|
|---|
| 937 | * addressspace. It returns a handle that can be used with GetProcAddress to
|
|---|
| 938 | * get addresses of exported entry points (functions and variables).
|
|---|
| 939 | *
|
|---|
| 940 | * LoadLibraryExW can also be used to map executable (.exe) modules into the
|
|---|
| 941 | * address to access resources in the module. However, LoadLibrary can't be
|
|---|
| 942 | * used to run an executable (.exe) module.
|
|---|
| 943 | *
|
|---|
| 944 | * @returns Handle to the library which was loaded.
|
|---|
| 945 | * @param lpszLibFile Pointer to Unicode string giving the name of
|
|---|
| 946 | * the executable image (either a Dll or an Exe) which is to
|
|---|
| 947 | * be loaded.
|
|---|
| 948 | *
|
|---|
| 949 | * If no extention is specified the default .DLL extention is
|
|---|
| 950 | * appended to the name. End the filename with an '.' if the
|
|---|
| 951 | * file does not have an extention (and don't want the .DLL
|
|---|
| 952 | * appended).
|
|---|
| 953 | *
|
|---|
| 954 | * If no path is specified, this API will use the Odin32
|
|---|
| 955 | * standard search strategy to find the file. This strategy
|
|---|
| 956 | * is described in the method Win32ImageBase::findDLL.
|
|---|
| 957 | * This may be alterned by the LOAD_WITH_ALTERED_SEARCH_PATH
|
|---|
| 958 | * flag, see below.
|
|---|
| 959 | *
|
|---|
| 960 | * This API likes to have backslashes (\), but will probably
|
|---|
| 961 | * accept forward slashes too. Win32 SDK docs says that it
|
|---|
| 962 | * should not contain forward slashes.
|
|---|
| 963 | *
|
|---|
| 964 | * Win32 SDK docs adds:
|
|---|
| 965 | * "The name specified is the file name of the module and
|
|---|
| 966 | * is not related to the name stored in the library module
|
|---|
| 967 | * itself, as specified by the LIBRARY keyword in the
|
|---|
| 968 | * module-definition (.def) file."
|
|---|
| 969 | *
|
|---|
| 970 | * @param hFile Reserved. Must be 0.
|
|---|
| 971 | *
|
|---|
| 972 | * @param dwFlags Flags which specifies the taken when loading the module.
|
|---|
| 973 | * The value 0 makes it identical to LoadLibraryA/W.
|
|---|
| 974 | *
|
|---|
| 975 | * Flags:
|
|---|
| 976 | *
|
|---|
| 977 | * DONT_RESOLVE_DLL_REFERENCES
|
|---|
| 978 | * (WinNT/2K feature): Don't load imported modules and
|
|---|
| 979 | * hence don't resolve imported symbols.
|
|---|
| 980 | * DllMain isn't called either. (Which is obvious since
|
|---|
| 981 | * it may use one of the importe symbols.)
|
|---|
| 982 | *
|
|---|
| 983 | * On the other hand, if this flag is NOT set, the system
|
|---|
| 984 | * load imported modules, resolves imported symbols, calls
|
|---|
| 985 | * DllMain for process and thread init and term (if wished
|
|---|
| 986 | * by the module).
|
|---|
| 987 | *
|
|---|
| 988 | * LOAD_LIBRARY_AS_DATAFILE
|
|---|
| 989 | * If this flag is set, the module is mapped into the
|
|---|
| 990 | * address space but is not prepared for execution. Though
|
|---|
| 991 | * it's preparted for resource API. Hence, you'll use this
|
|---|
| 992 | * flag when you want to load a DLL for extracting
|
|---|
| 993 | * messages or resources from it.
|
|---|
| 994 | *
|
|---|
| 995 | * The resulting handle can be used with any Odin32 API
|
|---|
| 996 | * which operates on resources.
|
|---|
| 997 | * (WinNt/2k supports all resource APIs while Win9x don't
|
|---|
| 998 | * support the specialized resource APIs: LoadBitmap,
|
|---|
| 999 | * LoadCursor, LoadIcon, LoadImage, LoadMenu.)
|
|---|
| 1000 | *
|
|---|
| 1001 | * LOAD_WITH_ALTERED_SEARCH_PATH
|
|---|
| 1002 | * If this flag is set and lpszLibFile specifies a path
|
|---|
| 1003 | * we'll use an alternative file search strategy to find
|
|---|
| 1004 | * imported modules. This stratgy is simply to use the
|
|---|
| 1005 | * path of the module being loaded instead of the path
|
|---|
| 1006 | * of the executable module as the first location
|
|---|
| 1007 | * to search for imported modules.
|
|---|
| 1008 | *
|
|---|
| 1009 | * If this flag is clear, the standard Odin32 standard
|
|---|
| 1010 | * search strategy. See Win32ImageBase::findDll for
|
|---|
| 1011 | * further information.
|
|---|
| 1012 | *
|
|---|
| 1013 | * @sketch Convert Unicode name to ascii.
|
|---|
| 1014 | * Call LoadLibraryExA.
|
|---|
| 1015 | * Free ascii string.
|
|---|
| 1016 | * return handle from LoadLibraryExA.
|
|---|
| 1017 | * @status Open32 Partially Implemented.
|
|---|
| 1018 | * @author Sander van Leeuwen (sandervl@xs4all.nl)
|
|---|
| 1019 | * knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
|
|---|
| 1020 | * @remark Forwards to LoadLibraryExA.
|
|---|
| 1021 | */
|
|---|
| 1022 | HINSTANCE WIN32API LoadLibraryExW(LPCWSTR lpszLibFile, HANDLE hFile, DWORD dwFlags)
|
|---|
| 1023 | {
|
|---|
| 1024 | char * pszAsciiLibFile;
|
|---|
| 1025 | HINSTANCE hDll;
|
|---|
| 1026 |
|
|---|
| 1027 | pszAsciiLibFile = UnicodeToAsciiString(lpszLibFile);
|
|---|
| 1028 | dprintf(("KERNEL32: LoadLibraryExW(%s, 0x%x, 0x%x) --> LoadLibraryExA",
|
|---|
| 1029 | pszAsciiLibFile, hFile, dwFlags));
|
|---|
| 1030 | hDll = LoadLibraryExA(pszAsciiLibFile, hFile, dwFlags);
|
|---|
| 1031 | dprintf(("KERNEL32: LoadLibraryExW(%s, 0x%x, 0x%x) returns 0x%x",
|
|---|
| 1032 | pszAsciiLibFile, hFile, dwFlags, hDll));
|
|---|
| 1033 | free(pszAsciiLibFile);
|
|---|
| 1034 |
|
|---|
| 1035 | return hDll;
|
|---|
| 1036 | }
|
|---|
| 1037 | //******************************************************************************
|
|---|
| 1038 | //******************************************************************************
|
|---|
| 1039 | HINSTANCE16 WIN32API LoadLibrary16(LPCTSTR lpszLibFile)
|
|---|
| 1040 | {
|
|---|
| 1041 | dprintf(("ERROR: LoadLibrary16 %s, not implemented", lpszLibFile));
|
|---|
| 1042 | return 0;
|
|---|
| 1043 | }
|
|---|
| 1044 | //******************************************************************************
|
|---|
| 1045 | //******************************************************************************
|
|---|
| 1046 | VOID WIN32API FreeLibrary16(HINSTANCE16 hinstance)
|
|---|
| 1047 | {
|
|---|
| 1048 | dprintf(("ERROR: FreeLibrary16 %x, not implemented", hinstance));
|
|---|
| 1049 | }
|
|---|
| 1050 | //******************************************************************************
|
|---|
| 1051 | //******************************************************************************
|
|---|
| 1052 | FARPROC WIN32API GetProcAddress16(HMODULE hModule, LPCSTR lpszProc)
|
|---|
| 1053 | {
|
|---|
| 1054 | dprintf(("ERROR: GetProcAddress16 %x %x, not implemented", hModule, lpszProc));
|
|---|
| 1055 | return 0;
|
|---|
| 1056 | }
|
|---|
| 1057 |
|
|---|
| 1058 |
|
|---|
| 1059 | /**
|
|---|
| 1060 | * Internal function which gets the commandline (string) used to start the current process.
|
|---|
| 1061 | * @returns OS/2 / Windows return code
|
|---|
| 1062 | * On successful return (NO_ERROR) the global variables
|
|---|
| 1063 | * pszCmdLineA and pszCmdLineW are set.
|
|---|
| 1064 | *
|
|---|
| 1065 | * @param pszPeExe Pass in the name of the PE exe of this process. We'll
|
|---|
| 1066 | * us this as exename and skip the first argument (ie. argv[1]).
|
|---|
| 1067 | * If NULL we'll use the commandline from OS/2 as it is.
|
|---|
| 1068 | * @status Completely implemented and tested.
|
|---|
| 1069 | * @author knut st. osmundsen (knut.stange.osmundsen@mynd.no)
|
|---|
| 1070 | */
|
|---|
| 1071 | ULONG InitCommandLine(const char *pszPeExe)
|
|---|
| 1072 | {
|
|---|
| 1073 | PCHAR pib_pchcmd; /* PIB pointer to commandline. */
|
|---|
| 1074 | CHAR szFilename[CCHMAXPATH]; /* Filename buffer used to get the exe filename in. */
|
|---|
| 1075 | ULONG cch; /* Commandline string length. (including terminator) */
|
|---|
| 1076 | PSZ psz; /* Temporary string pointer. */
|
|---|
| 1077 | PSZ psz2; /* Temporary string pointer. */
|
|---|
| 1078 | APIRET rc; /* OS/2 return code. */
|
|---|
| 1079 | BOOL fQuotes; /* Flag used to remember if the exe filename should be in quotes. */
|
|---|
| 1080 |
|
|---|
| 1081 | /** @sketch
|
|---|
| 1082 | * Get commandline from the PIB.
|
|---|
| 1083 | */
|
|---|
| 1084 | pib_pchcmd = (PCHAR)OSLibGetPIB(PIB_PCHCMD);
|
|---|
| 1085 |
|
|---|
| 1086 | /** @sketch
|
|---|
| 1087 | * Two methods of making the commandline:
|
|---|
| 1088 | * (1) The first argument is skipped and the second is used as exe filname.
|
|---|
| 1089 | * This applies to PE.EXE launched processes only.
|
|---|
| 1090 | * (2) No skipping. First argument is the exe filename.
|
|---|
| 1091 | * This applies to all but PE.EXE launched processes.
|
|---|
| 1092 | *
|
|---|
| 1093 | * Note: We could do some code size optimization here. Much of the code for
|
|---|
| 1094 | * the two methods are nearly identical.
|
|---|
| 1095 | *
|
|---|
| 1096 | */
|
|---|
| 1097 | if(pszPeExe)
|
|---|
| 1098 | {
|
|---|
| 1099 | /** @sketch
|
|---|
| 1100 | * Allocate memory for the commandline.
|
|---|
| 1101 | * Build commandline:
|
|---|
| 1102 | * Copy exe filename.
|
|---|
| 1103 | * Add arguments.
|
|---|
| 1104 | */
|
|---|
| 1105 | cch = strlen(pszPeExe)+1;
|
|---|
| 1106 | pszCmdLineA = psz = (PSZ)malloc(cch);
|
|---|
| 1107 | if (psz == NULL)
|
|---|
| 1108 | {
|
|---|
| 1109 | dprintf(("KERNEL32: InitCommandLine(%p): malloc(%d) failed\n", pszPeExe, cch));
|
|---|
| 1110 | return ERROR_NOT_ENOUGH_MEMORY;
|
|---|
| 1111 | }
|
|---|
| 1112 | strcpy((char *)pszCmdLineA, pszPeExe);
|
|---|
| 1113 |
|
|---|
| 1114 | rc = NO_ERROR;
|
|---|
| 1115 | }
|
|---|
| 1116 | else
|
|---|
| 1117 | {
|
|---|
| 1118 | /** @sketch Method (2):
|
|---|
| 1119 | * First we'll have to determin the size of the commandline.
|
|---|
| 1120 | *
|
|---|
| 1121 | * As we don't assume that OS/2 allways puts a fully qualified EXE name
|
|---|
| 1122 | * as the first string, we'll check if it's empty - and get the modulename
|
|---|
| 1123 | * in that case - and allways get the fully qualified filename.
|
|---|
| 1124 | */
|
|---|
| 1125 | if (pib_pchcmd == NULL || pib_pchcmd[0] == '\0')
|
|---|
| 1126 | {
|
|---|
| 1127 | rc = OSLibDosQueryModuleName(OSLibGetPIB(PIB_HMTE), sizeof(szFilename), szFilename);
|
|---|
| 1128 | if (rc != NO_ERROR)
|
|---|
| 1129 | {
|
|---|
| 1130 | dprintf(("KERNEL32: InitCommandLine(%p): OSLibQueryModuleName(0x%x,...) failed with rc=%d\n",
|
|---|
| 1131 | pszPeExe, OSLibGetPIB(PIB_HMTE), rc));
|
|---|
| 1132 | return rc;
|
|---|
| 1133 | }
|
|---|
| 1134 | }
|
|---|
| 1135 | else
|
|---|
| 1136 | {
|
|---|
| 1137 | rc = OSLibDosQueryPathInfo(pib_pchcmd, FIL_QUERYFULLNAME, szFilename, sizeof(szFilename));
|
|---|
| 1138 | if (rc != NO_ERROR)
|
|---|
| 1139 | {
|
|---|
| 1140 | dprintf(("KERNEL32: InitCommandLine(%p): (info) OSLibDosQueryPathInfo failed with rc=%d\n", pszPeExe, rc));
|
|---|
| 1141 | strcpy(szFilename, pib_pchcmd);
|
|---|
| 1142 | rc = NO_ERROR;
|
|---|
| 1143 | }
|
|---|
| 1144 | }
|
|---|
| 1145 |
|
|---|
| 1146 | /** @sketch
|
|---|
| 1147 | * We're still measuring the size of the commandline:
|
|---|
| 1148 | * Check if we have to quote the exe filename.
|
|---|
| 1149 | * Determin the length of the executable name including quotes and '\0'-terminator.
|
|---|
| 1150 | * Count the length of the arguments. (We here count's all argument strings.)
|
|---|
| 1151 | */
|
|---|
| 1152 | fQuotes = strchr(szFilename, ' ') != NULL;
|
|---|
| 1153 | cch = strlen(szFilename) + fQuotes*2 + 1;
|
|---|
| 1154 | if (pib_pchcmd != NULL)
|
|---|
| 1155 | {
|
|---|
| 1156 | psz2 = pib_pchcmd + strlen(pib_pchcmd) + 1;
|
|---|
| 1157 | while (*psz2 != '\0')
|
|---|
| 1158 | {
|
|---|
| 1159 | register int cchTmp = strlen(psz2) + 1; /* + 1 is for terminator (psz2) and space (cch). */
|
|---|
| 1160 | psz2 += cchTmp;
|
|---|
| 1161 | cch += cchTmp;
|
|---|
| 1162 | }
|
|---|
| 1163 | }
|
|---|
| 1164 |
|
|---|
| 1165 | /** @sketch
|
|---|
| 1166 | * Allocate memory for the commandline.
|
|---|
| 1167 | * Build commandline:
|
|---|
| 1168 | * Copy exe filename.
|
|---|
| 1169 | * Add arguments.
|
|---|
| 1170 | */
|
|---|
| 1171 | pszCmdLineA = psz = (PSZ)malloc(cch);
|
|---|
| 1172 | if (psz == NULL)
|
|---|
| 1173 | {
|
|---|
| 1174 | dprintf(("KERNEL32: InitCommandLine(%p): malloc(%d) failed\n", pszPeExe, cch));
|
|---|
| 1175 | return ERROR_NOT_ENOUGH_MEMORY;
|
|---|
| 1176 | }
|
|---|
| 1177 |
|
|---|
| 1178 | if (fQuotes)
|
|---|
| 1179 | *psz++ = '"';
|
|---|
| 1180 | strcpy(psz, szFilename);
|
|---|
| 1181 | psz += strlen(psz);
|
|---|
| 1182 | if (fQuotes)
|
|---|
| 1183 | {
|
|---|
| 1184 | *psz++ = '"';
|
|---|
| 1185 | *psz = '\0';
|
|---|
| 1186 | }
|
|---|
| 1187 |
|
|---|
| 1188 | if (pib_pchcmd != NULL)
|
|---|
| 1189 | {
|
|---|
| 1190 | psz2 = pib_pchcmd + strlen(pib_pchcmd) + 1;
|
|---|
| 1191 | while (*psz2 != '\0')
|
|---|
| 1192 | {
|
|---|
| 1193 | register int cchTmp = strlen(psz2) + 1; /* + 1 is for terminator (psz). */
|
|---|
| 1194 | *psz++ = ' '; /* add space */
|
|---|
| 1195 | memcpy(psz, psz2, cchTmp);
|
|---|
| 1196 | psz2 += cchTmp;
|
|---|
| 1197 | psz += cchTmp - 1;
|
|---|
| 1198 | }
|
|---|
| 1199 | }
|
|---|
| 1200 | }
|
|---|
| 1201 |
|
|---|
| 1202 | /** @sketch
|
|---|
| 1203 | * If successfully build ASCII commandline then convert it to UniCode.
|
|---|
| 1204 | */
|
|---|
| 1205 | if (rc == NO_ERROR)
|
|---|
| 1206 | {
|
|---|
| 1207 | pszCmdLineW = (WCHAR*)malloc(cch * 2);
|
|---|
| 1208 | if (pszCmdLineW != NULL)
|
|---|
| 1209 | AsciiToUnicode(pszCmdLineA, (WCHAR*)pszCmdLineW);
|
|---|
| 1210 | else
|
|---|
| 1211 | {
|
|---|
| 1212 | dprintf(("KERNEL32: InitCommandLine(%p): malloc(%d) failed (2)\n", pszPeExe, cch));
|
|---|
| 1213 | rc = ERROR_NOT_ENOUGH_MEMORY;
|
|---|
| 1214 | }
|
|---|
| 1215 | }
|
|---|
| 1216 |
|
|---|
| 1217 | return rc;
|
|---|
| 1218 | }
|
|---|
| 1219 |
|
|---|
| 1220 | /**
|
|---|
| 1221 | * Gets the command line of the current process.
|
|---|
| 1222 | * @returns On success:
|
|---|
| 1223 | * Command line of the current process. One single string.
|
|---|
| 1224 | * The first part of the command line string is the executable filename
|
|---|
| 1225 | * of the current process. It might be in quotes if it contains spaces.
|
|---|
| 1226 | * The rest of the string is arguments.
|
|---|
| 1227 | *
|
|---|
| 1228 | * On error:
|
|---|
| 1229 | * NULL. Last error set. (does Win32 set last error this?)
|
|---|
| 1230 | * @sketch IF not inited THEN
|
|---|
| 1231 | * Init commandline assuming !PE.EXE
|
|---|
| 1232 | * IF init failes THEN set last error.
|
|---|
| 1233 | * ENDIF
|
|---|
| 1234 | * return ASCII/ANSI commandline.
|
|---|
| 1235 | * @status Completely implemented and tested.
|
|---|
| 1236 | * @author knut st. osmundsen (knut.stange.osmundsen@mynd.no)
|
|---|
| 1237 | * @remark The Ring-3 PeLdr is resposible for calling InitCommandLine before anyone
|
|---|
| 1238 | * is able to call this function.
|
|---|
| 1239 | */
|
|---|
| 1240 | LPCSTR WIN32API GetCommandLineA(VOID)
|
|---|
| 1241 | {
|
|---|
| 1242 | /*
|
|---|
| 1243 | * Check if the commandline is initiated.
|
|---|
| 1244 | * If not we'll have to do it.
|
|---|
| 1245 | * ASSUMES that if not inited this isn't a PE.EXE lauched process.
|
|---|
| 1246 | */
|
|---|
| 1247 | if (pszCmdLineA == NULL)
|
|---|
| 1248 | {
|
|---|
| 1249 | APIRET rc;
|
|---|
| 1250 | rc = InitCommandLine(NULL);
|
|---|
| 1251 | if (rc != NULL)
|
|---|
| 1252 | SetLastError(rc);
|
|---|
| 1253 | }
|
|---|
| 1254 |
|
|---|
| 1255 | dprintf(("KERNEL32: GetCommandLineA: %s\n", pszCmdLineA));
|
|---|
| 1256 | return pszCmdLineA;
|
|---|
| 1257 | }
|
|---|
| 1258 |
|
|---|
| 1259 |
|
|---|
| 1260 | /**
|
|---|
| 1261 | * Gets the command line of the current process.
|
|---|
| 1262 | * @returns On success:
|
|---|
| 1263 | * Command line of the current process. One single string.
|
|---|
| 1264 | * The first part of the command line string is the executable filename
|
|---|
| 1265 | * of the current process. It might be in quotes if it contains spaces.
|
|---|
| 1266 | * The rest of the string is arguments.
|
|---|
| 1267 | *
|
|---|
| 1268 | * On error:
|
|---|
| 1269 | * NULL. Last error set. (does Win32 set last error this?)
|
|---|
| 1270 | * @sketch IF not inited THEN
|
|---|
| 1271 | * Init commandline assuming !PE.EXE
|
|---|
| 1272 | * IF init failes THEN set last error.
|
|---|
| 1273 | * ENDIF
|
|---|
| 1274 | * return Unicode commandline.
|
|---|
| 1275 | * @status Completely implemented and tested.
|
|---|
| 1276 | * @author knut st. osmundsen (knut.stange.osmundsen@mynd.no)
|
|---|
| 1277 | * @remark The Ring-3 PeLdr is resposible for calling InitCommandLine before anyone
|
|---|
| 1278 | * is able to call this function.
|
|---|
| 1279 | */
|
|---|
| 1280 | LPCWSTR WIN32API GetCommandLineW(void)
|
|---|
| 1281 | {
|
|---|
| 1282 | /*
|
|---|
| 1283 | * Check if the commandline is initiated.
|
|---|
| 1284 | * If not we'll have to do it.
|
|---|
| 1285 | * ASSUMES that if not inited this isn't a PE.EXE lauched process.
|
|---|
| 1286 | */
|
|---|
| 1287 | if (pszCmdLineW == NULL)
|
|---|
| 1288 | {
|
|---|
| 1289 | APIRET rc;
|
|---|
| 1290 | rc = InitCommandLine(NULL);
|
|---|
| 1291 | if (rc != NULL)
|
|---|
| 1292 | SetLastError(rc);
|
|---|
| 1293 | }
|
|---|
| 1294 |
|
|---|
| 1295 | dprintf(("KERNEL32: GetCommandLineW: %s\n", pszCmdLineA));
|
|---|
| 1296 | return pszCmdLineW;
|
|---|
| 1297 | }
|
|---|
| 1298 |
|
|---|
| 1299 |
|
|---|
| 1300 | /**
|
|---|
| 1301 | * GetModuleFileName gets the full path and file name for the specified module.
|
|---|
| 1302 | * @returns Bytes written to the buffer (lpszPath). This count includes the
|
|---|
| 1303 | * terminating '\0'.
|
|---|
| 1304 | * On error 0 is returned. Last error is set.
|
|---|
| 1305 | * @param hModule Handle to the module you like to get the file name to.
|
|---|
| 1306 | * @param lpszPath Output buffer for full path and file name.
|
|---|
| 1307 | * @param cchPath Size of the lpszPath buffer.
|
|---|
| 1308 | * @sketch Validate lpszPath.
|
|---|
| 1309 | * Find the module object using handle.
|
|---|
| 1310 | * If found Then
|
|---|
| 1311 | * Get full path from module object.
|
|---|
| 1312 | * If found path Then
|
|---|
| 1313 | * Copy path to buffer and set the number of bytes written.
|
|---|
| 1314 | * Else
|
|---|
| 1315 | * IPE!
|
|---|
| 1316 | * Else
|
|---|
| 1317 | * Call Open32 GetModuleFileName. (kernel32 initterm needs/needed this)
|
|---|
| 1318 | * Log result.
|
|---|
| 1319 | * Return number of bytes written to the buffer.
|
|---|
| 1320 | *
|
|---|
| 1321 | * @status Completely implemented, Open32.
|
|---|
| 1322 | * @author knut st. osmundsen (knut.stange.osmundsen@mynd.no)
|
|---|
| 1323 | * Sander van Leeuwen (sandervl@xs4all.nl)
|
|---|
| 1324 | * @remark - Do we still have to call Open32?
|
|---|
| 1325 | * - Do we set ERROR_BUFFER_OVERFLOW when cch > cchPath?
|
|---|
| 1326 | * - Does NT really set the last error?
|
|---|
| 1327 | */
|
|---|
| 1328 | DWORD WIN32API GetModuleFileNameA(HMODULE hModule, LPTSTR lpszPath, DWORD cchPath)
|
|---|
| 1329 | {
|
|---|
| 1330 | Win32ImageBase * pMod; /* Pointer to the module object. */
|
|---|
| 1331 | DWORD cch; /* Length of the */
|
|---|
| 1332 |
|
|---|
| 1333 | if (!VALID_PSZ(lpszPath))
|
|---|
| 1334 | {
|
|---|
| 1335 | dprintf(("KERNEL32: GetModuleFileNameA(0x%x, 0x%x, 0x%x): invalid pointer lpszLibFile = 0x%x\n",
|
|---|
| 1336 | hModule, lpszPath, cchPath, lpszPath));
|
|---|
| 1337 | SetLastError(ERROR_INVALID_PARAMETER); //or maybe ERROR_ACCESS_DENIED is more appropriate?
|
|---|
| 1338 | return 0;
|
|---|
| 1339 | }
|
|---|
| 1340 |
|
|---|
| 1341 | pMod = Win32ImageBase::findModule(hModule);
|
|---|
| 1342 | if (pMod != NULL)
|
|---|
| 1343 | {
|
|---|
| 1344 | const char *pszFn = pMod->getFullPath();
|
|---|
| 1345 | if (pszFn)
|
|---|
| 1346 | {
|
|---|
| 1347 | cch = strlen(pszFn) + 1;
|
|---|
| 1348 | if (cch > cchPath)
|
|---|
| 1349 | cch = cchPath;
|
|---|
| 1350 | memcpy(lpszPath, pszFn, cch);
|
|---|
| 1351 | lpszPath[cch - 1] = '\0';
|
|---|
| 1352 | }
|
|---|
| 1353 | else
|
|---|
| 1354 | {
|
|---|
| 1355 | dprintf(("KERNEL32: GetModuleFileNameA(%x,...): IPE - getFullPath returned NULL or empty string\n"));
|
|---|
| 1356 | DebugInt3();
|
|---|
| 1357 | SetLastError(ERROR_INVALID_HANDLE);
|
|---|
| 1358 | }
|
|---|
| 1359 | }
|
|---|
| 1360 | else
|
|---|
| 1361 | {
|
|---|
| 1362 | SetLastError(ERROR_INVALID_HANDLE);
|
|---|
| 1363 | //only needed for call inside kernel32's initterm (profile init)
|
|---|
| 1364 | //(console init only it seems...)
|
|---|
| 1365 | cch = O32_GetModuleFileName(hModule, lpszPath, cchPath);
|
|---|
| 1366 | if (cch > 0) cch++; /* Open32 doesn't count the terminator. */
|
|---|
| 1367 | }
|
|---|
| 1368 |
|
|---|
| 1369 | if (cch > 0)
|
|---|
| 1370 | dprintf(("KERNEL32: GetModuleFileNameA(%x,...): %s %d\n", hModule, lpszPath, cch));
|
|---|
| 1371 | else
|
|---|
| 1372 | dprintf(("KERNEL32: WARNING: GetModuleFileNameA(%x,...) - not found!", hModule));
|
|---|
| 1373 |
|
|---|
| 1374 | return cch;
|
|---|
| 1375 | }
|
|---|
| 1376 |
|
|---|
| 1377 |
|
|---|
| 1378 | /**
|
|---|
| 1379 | * GetModuleFileName gets the full path and file name for the specified module.
|
|---|
| 1380 | * @returns Bytes written to the buffer (lpszPath). This count includes the
|
|---|
| 1381 | * terminating '\0'.
|
|---|
| 1382 | * On error 0 is returned. Last error is set.
|
|---|
| 1383 | * @param hModule Handle to the module you like to get the file name to.
|
|---|
| 1384 | * @param lpszPath Output buffer for full path and file name.
|
|---|
| 1385 | * @param cchPath Size of the lpszPath buffer.
|
|---|
| 1386 | * @sketch Find the module object using handle.
|
|---|
| 1387 | * If found Then
|
|---|
| 1388 | * get full path from module object.
|
|---|
| 1389 | * If found path Then
|
|---|
| 1390 | * Determin path length.
|
|---|
| 1391 | * Translate the path to into the buffer.
|
|---|
| 1392 | * Else
|
|---|
| 1393 | * IPE.
|
|---|
| 1394 | * else
|
|---|
| 1395 | * SetLastError to invalid handle.
|
|---|
| 1396 | * Log result.
|
|---|
| 1397 | * return number of bytes written to the buffer.
|
|---|
| 1398 | *
|
|---|
| 1399 | * @status Completely implemented.
|
|---|
| 1400 | * @author knut st. osmundsen (knut.stange.osmundsen@mynd.no)
|
|---|
| 1401 | * @remark - We do _NOT_ call Open32.
|
|---|
| 1402 | * - Do we set ERROR_BUFFER_OVERFLOW when cch > cchPath?
|
|---|
| 1403 | * - Does NT really set the last error?
|
|---|
| 1404 | */
|
|---|
| 1405 | DWORD WIN32API GetModuleFileNameW(HMODULE hModule, LPWSTR lpszPath, DWORD cchPath)
|
|---|
| 1406 | {
|
|---|
| 1407 | Win32ImageBase * pMod;
|
|---|
| 1408 | DWORD cch = 0;
|
|---|
| 1409 |
|
|---|
| 1410 | if (!VALID_PSZ(lpszPath))
|
|---|
| 1411 | {
|
|---|
| 1412 | dprintf(("KERNEL32: GetModuleFileNameW(0x%x, 0x%x, 0x%x): invalid pointer lpszLibFile = 0x%x\n",
|
|---|
| 1413 | hModule, lpszPath, cchPath, lpszPath));
|
|---|
| 1414 | SetLastError(ERROR_INVALID_PARAMETER); //or maybe ERROR_ACCESS_DENIED is more appropriate?
|
|---|
| 1415 | return 0;
|
|---|
| 1416 | }
|
|---|
| 1417 |
|
|---|
| 1418 | pMod = Win32ImageBase::findModule(hModule);
|
|---|
| 1419 | if (pMod != NULL)
|
|---|
| 1420 | {
|
|---|
| 1421 | const char *pszFn = pMod->getFullPath();
|
|---|
| 1422 | if (pszFn || *pszFn != '\0')
|
|---|
| 1423 | {
|
|---|
| 1424 | cch = strlen(pszFn) + 1;
|
|---|
| 1425 | if (cch > cchPath)
|
|---|
| 1426 | cch = cchPath;
|
|---|
| 1427 | AsciiToUnicodeN(pszFn, lpszPath, cch);
|
|---|
| 1428 | }
|
|---|
| 1429 | else
|
|---|
| 1430 | {
|
|---|
| 1431 | dprintf(("KERNEL32: GetModuleFileNameW(%x,...): IPE - getFullPath returned NULL or empty string\n"));
|
|---|
| 1432 | DebugInt3();
|
|---|
| 1433 | SetLastError(ERROR_INVALID_HANDLE);
|
|---|
| 1434 | }
|
|---|
| 1435 | }
|
|---|
| 1436 | else
|
|---|
| 1437 | SetLastError(ERROR_INVALID_HANDLE);
|
|---|
| 1438 |
|
|---|
| 1439 | if (cch > 0)
|
|---|
| 1440 | dprintf(("KERNEL32: GetModuleFileNameW(%x,...): %s %d\n", hModule, lpszPath, cch));
|
|---|
| 1441 | else
|
|---|
| 1442 | dprintf(("KERNEL32: WARNING: GetModuleFileNameW(%x,...) - not found!", hModule));
|
|---|
| 1443 |
|
|---|
| 1444 | return cch;
|
|---|
| 1445 | }
|
|---|
| 1446 |
|
|---|
| 1447 |
|
|---|
| 1448 | //******************************************************************************
|
|---|
| 1449 | //NOTE: GetModuleHandleA does NOT support files with multiple dots (i.e.
|
|---|
| 1450 | // very.weird.exe)
|
|---|
| 1451 | //******************************************************************************
|
|---|
| 1452 | HANDLE WIN32API GetModuleHandleA(LPCTSTR lpszModule)
|
|---|
| 1453 | {
|
|---|
| 1454 | HANDLE hMod = 0;
|
|---|
| 1455 | Win32DllBase *windll;
|
|---|
| 1456 | char szModule[CCHMAXPATH];
|
|---|
| 1457 | BOOL fDllModule = FALSE;
|
|---|
| 1458 |
|
|---|
| 1459 | if(lpszModule == NULL) {
|
|---|
| 1460 | if(WinExe)
|
|---|
| 1461 | hMod = WinExe->getInstanceHandle();
|
|---|
| 1462 | else hMod = -1;
|
|---|
| 1463 | }
|
|---|
| 1464 | else
|
|---|
| 1465 | {
|
|---|
| 1466 | strcpy(szModule, OSLibStripPath((char *)lpszModule));
|
|---|
| 1467 | strupr(szModule);
|
|---|
| 1468 | if(strstr(szModule, ".DLL")) {
|
|---|
| 1469 | fDllModule = TRUE;
|
|---|
| 1470 | }
|
|---|
| 1471 | else {
|
|---|
| 1472 | if(!strstr(szModule, ".")) {
|
|---|
| 1473 | //if there's no extension or trainling dot, we
|
|---|
| 1474 | //assume it's a dll (see Win32 SDK docs)
|
|---|
| 1475 | fDllModule = TRUE;
|
|---|
| 1476 | }
|
|---|
| 1477 | }
|
|---|
| 1478 | char *dot = strstr(szModule, ".");
|
|---|
| 1479 | if(dot)
|
|---|
| 1480 | *dot = 0;
|
|---|
| 1481 |
|
|---|
| 1482 | if(!fDllModule && WinExe && !strcmpi(szModule, WinExe->getModuleName())) {
|
|---|
| 1483 | hMod = WinExe->getInstanceHandle();
|
|---|
| 1484 | }
|
|---|
| 1485 | else {
|
|---|
| 1486 | windll = Win32DllBase::findModule(szModule);
|
|---|
| 1487 | if(windll) {
|
|---|
| 1488 | hMod = windll->getInstanceHandle();
|
|---|
| 1489 | }
|
|---|
| 1490 | }
|
|---|
| 1491 | }
|
|---|
| 1492 |
|
|---|
| 1493 | dprintf(("KERNEL32: GetModuleHandle %s returned %X\n", lpszModule, hMod));
|
|---|
| 1494 | return(hMod);
|
|---|
| 1495 | }
|
|---|
| 1496 | //******************************************************************************
|
|---|
| 1497 | //******************************************************************************
|
|---|
| 1498 | HMODULE WIN32API GetModuleHandleW(LPCWSTR arg1)
|
|---|
| 1499 | {
|
|---|
| 1500 | HMODULE rc;
|
|---|
| 1501 | char *astring;
|
|---|
| 1502 |
|
|---|
| 1503 | astring = UnicodeToAsciiString((LPWSTR)arg1);
|
|---|
| 1504 | rc = GetModuleHandleA(astring);
|
|---|
| 1505 | dprintf(("KERNEL32: OS2GetModuleHandleW %s returned %X\n", astring, rc));
|
|---|
| 1506 | FreeAsciiString(astring);
|
|---|
| 1507 | return(rc);
|
|---|
| 1508 | }
|
|---|
| 1509 | //******************************************************************************
|
|---|
| 1510 | //******************************************************************************
|
|---|
| 1511 | BOOL WINAPI CreateProcessA( LPCSTR lpApplicationName, LPSTR lpCommandLine,
|
|---|
| 1512 | LPSECURITY_ATTRIBUTES lpProcessAttributes,
|
|---|
| 1513 | LPSECURITY_ATTRIBUTES lpThreadAttributes,
|
|---|
| 1514 | BOOL bInheritHandles, DWORD dwCreationFlags,
|
|---|
| 1515 | LPVOID lpEnvironment, LPCSTR lpCurrentDirectory,
|
|---|
| 1516 | LPSTARTUPINFOA lpStartupInfo,
|
|---|
| 1517 | LPPROCESS_INFORMATION lpProcessInfo )
|
|---|
| 1518 | {
|
|---|
| 1519 | THDB *pThreadDB = (THDB*)GetThreadTHDB();
|
|---|
| 1520 | char *cmdline = NULL;
|
|---|
| 1521 | BOOL rc;
|
|---|
| 1522 |
|
|---|
| 1523 | dprintf(("KERNEL32: CreateProcessA %s cline:%s inherit:%d cFlags:%x Env:%x CurDir:%s StartupFlags:%x\n",
|
|---|
| 1524 | lpApplicationName, lpCommandLine, bInheritHandles, dwCreationFlags,
|
|---|
| 1525 | lpEnvironment, lpCurrentDirectory, lpStartupInfo));
|
|---|
| 1526 |
|
|---|
| 1527 | // open32 does not support DEBUG_ONLY_THIS_PROCESS
|
|---|
| 1528 | if(dwCreationFlags & DEBUG_ONLY_THIS_PROCESS)
|
|---|
| 1529 | dwCreationFlags |= DEBUG_PROCESS;
|
|---|
| 1530 |
|
|---|
| 1531 | if(O32_CreateProcess(lpApplicationName, lpCommandLine, lpProcessAttributes,
|
|---|
| 1532 | lpThreadAttributes, bInheritHandles, dwCreationFlags,
|
|---|
| 1533 | lpEnvironment, lpCurrentDirectory, lpStartupInfo,
|
|---|
| 1534 | lpProcessInfo) == TRUE)
|
|---|
| 1535 | {
|
|---|
| 1536 | if (dwCreationFlags & DEBUG_PROCESS && pThreadDB != NULL)
|
|---|
| 1537 | {
|
|---|
| 1538 | if(pThreadDB->pidDebuggee != 0)
|
|---|
| 1539 | {
|
|---|
| 1540 | // TODO: handle this
|
|---|
| 1541 | dprintf(("KERNEL32: CreateProcess ERROR: This thread is already a debugger\n"));
|
|---|
| 1542 | }
|
|---|
| 1543 | else
|
|---|
| 1544 | {
|
|---|
| 1545 | pThreadDB->pidDebuggee = lpProcessInfo->dwProcessId;
|
|---|
| 1546 | OSLibStartDebugger((ULONG*)&pThreadDB->pidDebuggee);
|
|---|
| 1547 | }
|
|---|
| 1548 | }
|
|---|
| 1549 | else pThreadDB->pidDebuggee = 0;
|
|---|
| 1550 |
|
|---|
| 1551 | return(TRUE);
|
|---|
| 1552 | }
|
|---|
| 1553 | //probably a win32 exe, so run it in the pe loader
|
|---|
| 1554 | if(lpApplicationName) {
|
|---|
| 1555 | if(lpCommandLine) {
|
|---|
| 1556 | //skip exe name in lpCommandLine
|
|---|
| 1557 | while(*lpCommandLine != 0 && *lpCommandLine != ' ')
|
|---|
| 1558 | lpCommandLine++;
|
|---|
| 1559 |
|
|---|
| 1560 | if(*lpCommandLine != 0) {
|
|---|
| 1561 | lpCommandLine++;
|
|---|
| 1562 | }
|
|---|
| 1563 | cmdline = (char *)malloc(strlen(lpApplicationName)+strlen(lpCommandLine) + 16);
|
|---|
| 1564 | sprintf(cmdline, "PE.EXE %s %s", lpApplicationName, lpCommandLine);
|
|---|
| 1565 | }
|
|---|
| 1566 | else {
|
|---|
| 1567 | cmdline = (char *)malloc(strlen(lpApplicationName) + 16);
|
|---|
| 1568 | sprintf(cmdline, "PE.EXE %s", lpApplicationName);
|
|---|
| 1569 | }
|
|---|
| 1570 | }
|
|---|
| 1571 | else {
|
|---|
| 1572 | cmdline = (char *)malloc(strlen(lpCommandLine) + 16);
|
|---|
| 1573 | sprintf(cmdline, "PE.EXE %s", lpCommandLine);
|
|---|
| 1574 | }
|
|---|
| 1575 | dprintf(("KERNEL32: CreateProcess %s\n", cmdline));
|
|---|
| 1576 | rc = O32_CreateProcess("PE.EXE", (LPCSTR)cmdline,lpProcessAttributes,
|
|---|
| 1577 | lpThreadAttributes, bInheritHandles, dwCreationFlags,
|
|---|
| 1578 | lpEnvironment, lpCurrentDirectory, lpStartupInfo,
|
|---|
| 1579 | lpProcessInfo);
|
|---|
| 1580 | if(rc == TRUE) {
|
|---|
| 1581 | if (dwCreationFlags & DEBUG_PROCESS && pThreadDB != NULL)
|
|---|
| 1582 | {
|
|---|
| 1583 | if(pThreadDB->pidDebuggee != 0)
|
|---|
| 1584 | {
|
|---|
| 1585 | // TODO: handle this
|
|---|
| 1586 | dprintf(("KERNEL32: CreateProcess ERROR: This thread is already a debugger\n"));
|
|---|
| 1587 | }
|
|---|
| 1588 | else
|
|---|
| 1589 | {
|
|---|
| 1590 | pThreadDB->pidDebuggee = lpProcessInfo->dwProcessId;
|
|---|
| 1591 | OSLibStartDebugger((ULONG*)&pThreadDB->pidDebuggee);
|
|---|
| 1592 | }
|
|---|
| 1593 | }
|
|---|
| 1594 | else
|
|---|
| 1595 | pThreadDB->pidDebuggee = 0;
|
|---|
| 1596 | }
|
|---|
| 1597 | if(cmdline)
|
|---|
| 1598 | free(cmdline);
|
|---|
| 1599 |
|
|---|
| 1600 | if(lpProcessInfo)
|
|---|
| 1601 | dprintf(("KERNEL32: CreateProcess returned %d hPro:%x hThr:%x pid:%x tid:%x\n",
|
|---|
| 1602 | rc, lpProcessInfo->hProcess, lpProcessInfo->hThread,
|
|---|
| 1603 | lpProcessInfo->dwProcessId,lpProcessInfo->dwThreadId));
|
|---|
| 1604 | else
|
|---|
| 1605 | dprintf(("KERNEL32: CreateProcess returned %d\n", rc));
|
|---|
| 1606 | return(rc);
|
|---|
| 1607 | }
|
|---|
| 1608 | //******************************************************************************
|
|---|
| 1609 | //******************************************************************************
|
|---|
| 1610 | BOOL WIN32API CreateProcessW(LPCWSTR lpApplicationName, LPWSTR lpCommandLine,
|
|---|
| 1611 | PSECURITY_ATTRIBUTES lpProcessAttributes,
|
|---|
| 1612 | PSECURITY_ATTRIBUTES lpThreadAttributes,
|
|---|
| 1613 | BOOL bInheritHandles, DWORD dwCreationFlags,
|
|---|
| 1614 | LPVOID lpEnvironment,
|
|---|
| 1615 | LPCWSTR lpCurrentDirectory, LPSTARTUPINFOW lpStartupInfo,
|
|---|
| 1616 | LPPROCESS_INFORMATION lpProcessInfo)
|
|---|
| 1617 | {
|
|---|
| 1618 | BOOL rc;
|
|---|
| 1619 | char *astring1 = 0, *astring2 = 0, *astring3 = 0;
|
|---|
| 1620 |
|
|---|
| 1621 | dprintf(("KERNEL32: CreateProcessW"));
|
|---|
| 1622 | if(lpApplicationName)
|
|---|
| 1623 | astring1 = UnicodeToAsciiString((LPWSTR)lpApplicationName);
|
|---|
| 1624 | if(lpCommandLine)
|
|---|
| 1625 | astring2 = UnicodeToAsciiString(lpCommandLine);
|
|---|
| 1626 | if(lpCurrentDirectory)
|
|---|
| 1627 | astring3 = UnicodeToAsciiString((LPWSTR)lpCurrentDirectory);
|
|---|
| 1628 | rc = CreateProcessA(astring1, astring2, lpProcessAttributes, lpThreadAttributes,
|
|---|
| 1629 | bInheritHandles, dwCreationFlags, lpEnvironment,
|
|---|
| 1630 | astring3, (LPSTARTUPINFOA)lpStartupInfo,
|
|---|
| 1631 | lpProcessInfo);
|
|---|
| 1632 | if(astring3) FreeAsciiString(astring3);
|
|---|
| 1633 | if(astring2) FreeAsciiString(astring2);
|
|---|
| 1634 | if(astring1) FreeAsciiString(astring1);
|
|---|
| 1635 | return(rc);
|
|---|
| 1636 | }
|
|---|
| 1637 | //******************************************************************************
|
|---|
| 1638 | //******************************************************************************
|
|---|
| 1639 | HINSTANCE WIN32API WinExec(LPCSTR lpCmdLine, UINT nCmdShow)
|
|---|
| 1640 | {
|
|---|
| 1641 | STARTUPINFOA startinfo = {0};
|
|---|
| 1642 | PROCESS_INFORMATION procinfo;
|
|---|
| 1643 | DWORD rc;
|
|---|
| 1644 |
|
|---|
| 1645 | dprintf(("KERNEL32: WinExec %s\n", lpCmdLine));
|
|---|
| 1646 | startinfo.dwFlags = nCmdShow;
|
|---|
| 1647 | if(CreateProcessA(NULL, (LPSTR)lpCmdLine, NULL, NULL, FALSE, 0, NULL, NULL,
|
|---|
| 1648 | &startinfo, &procinfo) == FALSE)
|
|---|
| 1649 | {
|
|---|
| 1650 | return 0;
|
|---|
| 1651 | }
|
|---|
| 1652 | //block until the launched app waits for input (or a timeout of 15 seconds)
|
|---|
| 1653 | //TODO: Shouldn't call Open32, but the api in user32..
|
|---|
| 1654 | rc = O32_WaitForInputIdle(procinfo.hProcess, 15000);
|
|---|
| 1655 | if(rc != 0) {
|
|---|
| 1656 | dprintf(("WinExec: WaitForInputIdle %x returned %x", procinfo.hProcess, rc));
|
|---|
| 1657 | }
|
|---|
| 1658 | return procinfo.hProcess; //correct?
|
|---|
| 1659 | }
|
|---|
| 1660 | /**********************************************************************
|
|---|
| 1661 | * LoadModule (KERNEL32.499)
|
|---|
| 1662 | *
|
|---|
| 1663 | * Wine: 20000909
|
|---|
| 1664 | *
|
|---|
| 1665 | * Copyright 1995 Alexandre Julliard
|
|---|
| 1666 | */
|
|---|
| 1667 | HINSTANCE WINAPI LoadModule( LPCSTR name, LPVOID paramBlock )
|
|---|
| 1668 | {
|
|---|
| 1669 | LOADPARAMS *params = (LOADPARAMS *)paramBlock;
|
|---|
| 1670 | PROCESS_INFORMATION info;
|
|---|
| 1671 | STARTUPINFOA startup;
|
|---|
| 1672 | HINSTANCE hInstance;
|
|---|
| 1673 | LPSTR cmdline, p;
|
|---|
| 1674 | char filename[MAX_PATH];
|
|---|
| 1675 | BYTE len;
|
|---|
| 1676 |
|
|---|
| 1677 | dprintf(("LoadModule %s %x", name, paramBlock));
|
|---|
| 1678 |
|
|---|
| 1679 | if (!name) return ERROR_FILE_NOT_FOUND;
|
|---|
| 1680 |
|
|---|
| 1681 | if (!SearchPathA( NULL, name, ".exe", sizeof(filename), filename, NULL ) &&
|
|---|
| 1682 | !SearchPathA( NULL, name, NULL, sizeof(filename), filename, NULL ))
|
|---|
| 1683 | return GetLastError();
|
|---|
| 1684 |
|
|---|
| 1685 | len = (BYTE)params->lpCmdLine[0];
|
|---|
| 1686 | if (!(cmdline = (LPSTR)HeapAlloc( GetProcessHeap(), 0, strlen(filename) + len + 2 )))
|
|---|
| 1687 | return ERROR_NOT_ENOUGH_MEMORY;
|
|---|
| 1688 |
|
|---|
| 1689 | strcpy( cmdline, filename );
|
|---|
| 1690 | p = cmdline + strlen(cmdline);
|
|---|
| 1691 | *p++ = ' ';
|
|---|
| 1692 | memcpy( p, params->lpCmdLine + 1, len );
|
|---|
| 1693 | p[len] = 0;
|
|---|
| 1694 |
|
|---|
| 1695 | memset( &startup, 0, sizeof(startup) );
|
|---|
| 1696 | startup.cb = sizeof(startup);
|
|---|
| 1697 | if (params->lpCmdShow)
|
|---|
| 1698 | {
|
|---|
| 1699 | startup.dwFlags = STARTF_USESHOWWINDOW;
|
|---|
| 1700 | startup.wShowWindow = params->lpCmdShow[1];
|
|---|
| 1701 | }
|
|---|
| 1702 |
|
|---|
| 1703 | if (CreateProcessA( filename, cmdline, NULL, NULL, FALSE, 0,
|
|---|
| 1704 | params->lpEnvAddress, NULL, &startup, &info ))
|
|---|
| 1705 | {
|
|---|
| 1706 | /* Give 15 seconds to the app to come up */
|
|---|
| 1707 | if ( O32_WaitForInputIdle ( info.hProcess, 15000 ) == 0xFFFFFFFF )
|
|---|
| 1708 | dprintf(("ERROR: WaitForInputIdle failed: Error %ld\n", GetLastError() ));
|
|---|
| 1709 | hInstance = 33;
|
|---|
| 1710 | /* Close off the handles */
|
|---|
| 1711 | CloseHandle( info.hThread );
|
|---|
| 1712 | CloseHandle( info.hProcess );
|
|---|
| 1713 | }
|
|---|
| 1714 | else if ((hInstance = GetLastError()) >= 32)
|
|---|
| 1715 | {
|
|---|
| 1716 | dprintf(("ERROR: Strange error set by CreateProcess: %d\n", hInstance ));
|
|---|
| 1717 | hInstance = 11;
|
|---|
| 1718 | }
|
|---|
| 1719 |
|
|---|
| 1720 | HeapFree( GetProcessHeap(), 0, cmdline );
|
|---|
| 1721 | return hInstance;
|
|---|
| 1722 | }
|
|---|
| 1723 | //******************************************************************************
|
|---|
| 1724 | //******************************************************************************
|
|---|
| 1725 | FARPROC WIN32API GetProcAddress(HMODULE hModule, LPCSTR lpszProc)
|
|---|
| 1726 | {
|
|---|
| 1727 | Win32ImageBase *winmod;
|
|---|
| 1728 | FARPROC proc;
|
|---|
| 1729 | ULONG ulAPIOrdinal;
|
|---|
| 1730 |
|
|---|
| 1731 | if(hModule == 0 || hModule == -1 || (WinExe && hModule == WinExe->getInstanceHandle())) {
|
|---|
| 1732 | winmod = WinExe;
|
|---|
| 1733 | }
|
|---|
| 1734 | else winmod = (Win32ImageBase *)Win32DllBase::findModule((HINSTANCE)hModule);
|
|---|
| 1735 |
|
|---|
| 1736 | if(winmod) {
|
|---|
| 1737 | ulAPIOrdinal = (ULONG)lpszProc;
|
|---|
| 1738 | if (ulAPIOrdinal <= 0x0000FFFF) {
|
|---|
| 1739 | proc = (FARPROC)winmod->getApi((int)ulAPIOrdinal);
|
|---|
| 1740 | }
|
|---|
| 1741 | else proc = (FARPROC)winmod->getApi((char *)lpszProc);
|
|---|
| 1742 | if(proc == 0) {
|
|---|
| 1743 | #ifdef DEBUG
|
|---|
| 1744 | if(ulAPIOrdinal <= 0x0000FFFF) {
|
|---|
| 1745 | dprintf(("GetProcAddress %x %x not found!", hModule, ulAPIOrdinal));
|
|---|
| 1746 | }
|
|---|
| 1747 | else dprintf(("GetProcAddress %x %s not found!", hModule, lpszProc));
|
|---|
| 1748 | #endif
|
|---|
| 1749 | SetLastError(ERROR_PROC_NOT_FOUND);
|
|---|
| 1750 | }
|
|---|
| 1751 | if(HIWORD(lpszProc))
|
|---|
| 1752 | dprintf(("KERNEL32: GetProcAddress %s from %X returned %X\n", lpszProc, hModule, proc));
|
|---|
| 1753 | else dprintf(("KERNEL32: GetProcAddress %x from %X returned %X\n", lpszProc, hModule, proc));
|
|---|
| 1754 | return proc;
|
|---|
| 1755 | }
|
|---|
| 1756 | proc = O32_GetProcAddress(hModule, lpszProc);
|
|---|
| 1757 | if(HIWORD(lpszProc))
|
|---|
| 1758 | dprintf(("KERNEL32: GetProcAddress %s from %X returned %X\n", lpszProc, hModule, proc));
|
|---|
| 1759 | else dprintf(("KERNEL32: GetProcAddress %x from %X returned %X\n", lpszProc, hModule, proc));
|
|---|
| 1760 | return(proc);
|
|---|
| 1761 | }
|
|---|
| 1762 | //******************************************************************************
|
|---|
| 1763 | //Retrieve the version
|
|---|
| 1764 | //******************************************************************************
|
|---|
| 1765 | BOOL SYSTEM GetVersionStruct(char *lpszModName, char *verstruct, ULONG bufLength)
|
|---|
| 1766 | {
|
|---|
| 1767 | Win32ImageBase *winimage;
|
|---|
| 1768 | Win32PeLdrRsrcImg *rsrcimg;
|
|---|
| 1769 |
|
|---|
| 1770 | dprintf(("GetVersionStruct of module %s", lpszModName));
|
|---|
| 1771 | if(WinExe && !stricmp(WinExe->getFullPath(), lpszModName)) {
|
|---|
| 1772 | winimage = (Win32ImageBase *)WinExe;
|
|---|
| 1773 | }
|
|---|
| 1774 | else {
|
|---|
| 1775 | winimage = (Win32ImageBase *)Win32DllBase::findModule(lpszModName);
|
|---|
| 1776 | if(winimage == NULL)
|
|---|
| 1777 | {
|
|---|
| 1778 | char modname[CCHMAXPATH];
|
|---|
| 1779 |
|
|---|
| 1780 | strcpy(modname, lpszModName);
|
|---|
| 1781 | //rename dll if necessary (i.e. OLE32 -> OLE32OS2)
|
|---|
| 1782 | Win32DllBase::renameDll(modname);
|
|---|
| 1783 |
|
|---|
| 1784 | if(Win32ImageBase::isPEImage(modname) != ERROR_SUCCESS)
|
|---|
| 1785 | {
|
|---|
| 1786 | HINSTANCE hInstance;
|
|---|
| 1787 |
|
|---|
| 1788 | //must be an LX dll, just load it (app will probably load it anyway)
|
|---|
| 1789 | hInstance = LoadLibraryA(modname);
|
|---|
| 1790 | if(hInstance == 0)
|
|---|
| 1791 | return 0;
|
|---|
| 1792 | winimage = (Win32ImageBase *)Win32DllBase::findModule(hInstance);
|
|---|
| 1793 | if(winimage) {
|
|---|
| 1794 | return winimage->getVersionStruct(verstruct, bufLength);
|
|---|
| 1795 | }
|
|---|
| 1796 | return 0;
|
|---|
| 1797 | }
|
|---|
| 1798 | //SvL: Try to load it
|
|---|
| 1799 | rsrcimg = new Win32PeLdrRsrcImg(modname);
|
|---|
| 1800 | if(rsrcimg == NULL)
|
|---|
| 1801 | return 0;
|
|---|
| 1802 |
|
|---|
| 1803 | rsrcimg->init(0);
|
|---|
| 1804 | if(rsrcimg->getError() != NO_ERROR)
|
|---|
| 1805 | {
|
|---|
| 1806 | dprintf(("GetVersionStruct can't load %s\n", modname));
|
|---|
| 1807 | delete rsrcimg;
|
|---|
| 1808 | return(FALSE);
|
|---|
| 1809 | }
|
|---|
| 1810 | BOOL rc = rsrcimg->getVersionStruct(verstruct, bufLength);
|
|---|
| 1811 | delete rsrcimg;
|
|---|
| 1812 | return rc;
|
|---|
| 1813 | }
|
|---|
| 1814 | }
|
|---|
| 1815 | return winimage->getVersionStruct(verstruct, bufLength);
|
|---|
| 1816 | }
|
|---|
| 1817 | //******************************************************************************
|
|---|
| 1818 | //******************************************************************************
|
|---|
| 1819 | ULONG SYSTEM GetVersionSize(char *lpszModName)
|
|---|
| 1820 | {
|
|---|
| 1821 | Win32ImageBase *winimage;
|
|---|
| 1822 | Win32PeLdrRsrcImg *rsrcimg;
|
|---|
| 1823 |
|
|---|
| 1824 | dprintf(("GetVersionSize of %s\n", lpszModName));
|
|---|
| 1825 |
|
|---|
| 1826 | if(WinExe && !stricmp(WinExe->getFullPath(), lpszModName)) {
|
|---|
| 1827 | winimage = (Win32ImageBase *)WinExe;
|
|---|
| 1828 | }
|
|---|
| 1829 | else {
|
|---|
| 1830 | winimage = (Win32ImageBase *)Win32DllBase::findModule(lpszModName);
|
|---|
| 1831 | if(winimage == NULL)
|
|---|
| 1832 | {
|
|---|
| 1833 | char modname[CCHMAXPATH];
|
|---|
| 1834 |
|
|---|
| 1835 | strcpy(modname, lpszModName);
|
|---|
| 1836 | //rename dll if necessary (i.e. OLE32 -> OLE32OS2)
|
|---|
| 1837 | Win32DllBase::renameDll(modname);
|
|---|
| 1838 |
|
|---|
| 1839 | if(Win32ImageBase::isPEImage(modname) != ERROR_SUCCESS)
|
|---|
| 1840 | {
|
|---|
| 1841 | HINSTANCE hInstance;
|
|---|
| 1842 |
|
|---|
| 1843 | //must be an LX dll, just load it (app will probably load it anyway)
|
|---|
| 1844 | hInstance = LoadLibraryA(modname);
|
|---|
| 1845 | if(hInstance == 0)
|
|---|
| 1846 | return 0;
|
|---|
| 1847 | winimage = (Win32ImageBase *)Win32DllBase::findModule(hInstance);
|
|---|
| 1848 | if(winimage) {
|
|---|
| 1849 | return winimage->getVersionSize();
|
|---|
| 1850 | }
|
|---|
| 1851 | return 0;
|
|---|
| 1852 | }
|
|---|
| 1853 |
|
|---|
| 1854 | //SvL: Try to load it
|
|---|
| 1855 | rsrcimg = new Win32PeLdrRsrcImg(modname);
|
|---|
| 1856 | if(rsrcimg == NULL)
|
|---|
| 1857 | return 0;
|
|---|
| 1858 |
|
|---|
| 1859 | rsrcimg->init(0);
|
|---|
| 1860 | if(rsrcimg->getError() != NO_ERROR)
|
|---|
| 1861 | {
|
|---|
| 1862 | dprintf(("GetVersionSize can't load %s\n", modname));
|
|---|
| 1863 | delete rsrcimg;
|
|---|
| 1864 | return(FALSE);
|
|---|
| 1865 | }
|
|---|
| 1866 | int size = rsrcimg->getVersionSize();
|
|---|
| 1867 | delete rsrcimg;
|
|---|
| 1868 | return size;
|
|---|
| 1869 | }
|
|---|
| 1870 | }
|
|---|
| 1871 | return winimage->getVersionSize();
|
|---|
| 1872 | }
|
|---|
| 1873 | //******************************************************************************
|
|---|
| 1874 | //TODO:What does this do exactly??
|
|---|
| 1875 | //******************************************************************************
|
|---|
| 1876 | ODINFUNCTION1(BOOL,DisableThreadLibraryCalls,HMODULE,hModule)
|
|---|
| 1877 | {
|
|---|
| 1878 | Win32DllBase *winmod;
|
|---|
| 1879 | FARPROC proc;
|
|---|
| 1880 | ULONG ulAPIOrdinal;
|
|---|
| 1881 |
|
|---|
| 1882 | winmod = Win32DllBase::findModule((HINSTANCE)hModule);
|
|---|
| 1883 | if(winmod)
|
|---|
| 1884 | {
|
|---|
| 1885 | // don't call ATTACH/DETACH thread functions in DLL
|
|---|
| 1886 | winmod->disableThreadLibraryCalls();
|
|---|
| 1887 | return TRUE;
|
|---|
| 1888 | }
|
|---|
| 1889 | else
|
|---|
| 1890 | {
|
|---|
| 1891 | // raise error condition
|
|---|
| 1892 | SetLastError(ERROR_INVALID_HANDLE);
|
|---|
| 1893 | return FALSE;
|
|---|
| 1894 | }
|
|---|
| 1895 | }
|
|---|
| 1896 | //******************************************************************************
|
|---|
| 1897 | //******************************************************************************
|
|---|