| 1 | /* $Id: wprocess.cpp,v 1.40 1999-10-23 12:34:48 sandervl Exp $ */
|
|---|
| 2 |
|
|---|
| 3 | /*
|
|---|
| 4 | * Win32 process functions
|
|---|
| 5 | *
|
|---|
| 6 | * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl)
|
|---|
| 7 | *
|
|---|
| 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 <winfakepeldr.h>
|
|---|
| 27 |
|
|---|
| 28 | #ifdef __IBMCPP__
|
|---|
| 29 | #include <builtin.h>
|
|---|
| 30 | #endif
|
|---|
| 31 |
|
|---|
| 32 | #include "exceptutil.h"
|
|---|
| 33 | #include "oslibmisc.h"
|
|---|
| 34 |
|
|---|
| 35 | #include "console.h"
|
|---|
| 36 | #include "cio.h"
|
|---|
| 37 | #include "versionos2.h" /*PLF Wed 98-03-18 02:36:51*/
|
|---|
| 38 | #include <wprocess.h>
|
|---|
| 39 | #include "mmap.h"
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 | ODINDEBUGCHANNEL(KERNEL32-WPROCESS)
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 | //******************************************************************************
|
|---|
| 46 | //******************************************************************************
|
|---|
| 47 | BOOL fFreeLibrary = FALSE;
|
|---|
| 48 | BOOL fIsOS2Image = FALSE; //TRUE -> Odin32 OS/2 application (not converted!)
|
|---|
| 49 | //FALSE -> otherwise
|
|---|
| 50 | //Process database
|
|---|
| 51 | PDB ProcessPDB = {0};
|
|---|
| 52 | USHORT ProcessTIBSel = 0;
|
|---|
| 53 | DWORD *TIBFlatPtr = 0;
|
|---|
| 54 |
|
|---|
| 55 | //******************************************************************************
|
|---|
| 56 | //******************************************************************************
|
|---|
| 57 | TEB *WIN32API GetThreadTEB()
|
|---|
| 58 | {
|
|---|
| 59 | return (TEB *)*TIBFlatPtr;
|
|---|
| 60 | }
|
|---|
| 61 | //******************************************************************************
|
|---|
| 62 | //******************************************************************************
|
|---|
| 63 | THDB *WIN32API GetThreadTHDB()
|
|---|
| 64 | {
|
|---|
| 65 | TEB *winteb;
|
|---|
| 66 | THDB *thdb;
|
|---|
| 67 |
|
|---|
| 68 | winteb = (TEB *)*TIBFlatPtr;
|
|---|
| 69 | if(winteb == NULL) {
|
|---|
| 70 | return NULL;
|
|---|
| 71 | }
|
|---|
| 72 | thdb = (THDB *)(winteb+1);
|
|---|
| 73 |
|
|---|
| 74 | return thdb;
|
|---|
| 75 | }
|
|---|
| 76 | //******************************************************************************
|
|---|
| 77 | // Set up the TIB selector and memory for the current thread
|
|---|
| 78 | //******************************************************************************
|
|---|
| 79 | TEB *InitializeTIB(BOOL fMainThread)
|
|---|
| 80 | {
|
|---|
| 81 | TEB *winteb;
|
|---|
| 82 | THDB *thdb;
|
|---|
| 83 |
|
|---|
| 84 | USHORT tibsel;
|
|---|
| 85 |
|
|---|
| 86 | //Allocate one dword to store the flat address of our TEB
|
|---|
| 87 | if(fMainThread) {
|
|---|
| 88 | TIBFlatPtr = (DWORD *)OSLibAllocThreadLocalMemory(1);
|
|---|
| 89 | if(TIBFlatPtr == 0) {
|
|---|
| 90 | dprintf(("InitializeTIB: local thread memory alloc failed!!"));
|
|---|
| 91 | DebugInt3();
|
|---|
| 92 | return NULL;
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 | if(OSLibAllocSel(PAGE_SIZE, &tibsel) == FALSE)
|
|---|
| 96 | {
|
|---|
| 97 | dprintf(("InitializeTIB: selector alloc failed!!"));
|
|---|
| 98 | DebugInt3();
|
|---|
| 99 | return NULL;
|
|---|
| 100 | }
|
|---|
| 101 | winteb = (TEB *)OSLibSelToFlat(tibsel);
|
|---|
| 102 | if(winteb == NULL)
|
|---|
| 103 | {
|
|---|
| 104 | dprintf(("InitializeTIB: DosSelToFlat failed!!"));
|
|---|
| 105 | DebugInt3();
|
|---|
| 106 | return NULL;
|
|---|
| 107 | }
|
|---|
| 108 | memset(winteb, 0, PAGE_SIZE);
|
|---|
| 109 | thdb = (THDB *)(winteb+1);
|
|---|
| 110 | *TIBFlatPtr = (DWORD)winteb;
|
|---|
| 111 |
|
|---|
| 112 | winteb->except = (PVOID)-1; /* 00 Head of exception handling chain */
|
|---|
| 113 | winteb->stack_top = (PVOID)OSLibGetTIB(TIB_STACKTOP); /* 04 Top of thread stack */
|
|---|
| 114 | winteb->stack_low = (PVOID)OSLibGetTIB(TIB_STACKLOW); /* 08 Stack low-water mark */
|
|---|
| 115 | winteb->htask16 = (USHORT)OSLibGetPIB(PIB_TASKHNDL); /* 0c Win16 task handle */
|
|---|
| 116 | winteb->stack_sel = getSS(); /* 0e 16-bit stack selector */
|
|---|
| 117 | winteb->self = winteb; /* 18 Pointer to this structure */
|
|---|
| 118 | winteb->flags = TEBF_WIN32; /* 1c Flags */
|
|---|
| 119 | winteb->queue = 0; /* 28 Message queue */
|
|---|
| 120 | winteb->tls_ptr = &thdb->tls_array[0]; /* 2c Pointer to TLS array */
|
|---|
| 121 | winteb->process = &ProcessPDB; /* 30 owning process (used by NT3.51 applets)*/
|
|---|
| 122 |
|
|---|
| 123 | memcpy(&thdb->teb, winteb, sizeof(TEB));
|
|---|
| 124 | thdb->process = &ProcessPDB;
|
|---|
| 125 | thdb->exit_code = 0x103; /* STILL_ACTIVE */
|
|---|
| 126 | thdb->teb_sel = tibsel;
|
|---|
| 127 | thdb->OrgTIBSel = GetFS();
|
|---|
| 128 |
|
|---|
| 129 | if(OSLibGetPIB(PIB_TASKTYPE) == TASKTYPE_PM)
|
|---|
| 130 | {
|
|---|
| 131 | thdb->flags = 0; //todo gui
|
|---|
| 132 | }
|
|---|
| 133 | else thdb->flags = 0; //todo textmode
|
|---|
| 134 |
|
|---|
| 135 | if(fMainThread)
|
|---|
| 136 | {
|
|---|
| 137 | //todo initialize PDB during process creation
|
|---|
| 138 | //todo: initialize TLS array if required
|
|---|
| 139 | //TLS in executable always TLS index 0?
|
|---|
| 140 | ProcessTIBSel = tibsel;
|
|---|
| 141 | }
|
|---|
| 142 | dprintf(("InitializeTIB setup TEB with selector %x", tibsel));
|
|---|
| 143 | dprintf(("InitializeTIB: FS(%x):[0] = %x", GetFS(), QueryExceptionChain()));
|
|---|
| 144 | return winteb;
|
|---|
| 145 | }
|
|---|
| 146 | //******************************************************************************
|
|---|
| 147 | // Destroy the TIB selector and memory for the current thread
|
|---|
| 148 | //******************************************************************************
|
|---|
| 149 | void DestroyTIB()
|
|---|
| 150 | {
|
|---|
| 151 | SHORT orgtibsel;
|
|---|
| 152 | TEB *winteb;
|
|---|
| 153 | THDB *thdb;
|
|---|
| 154 |
|
|---|
| 155 | dprintf(("DestroyTIB: FS = %x", GetFS()));
|
|---|
| 156 | dprintf(("DestroyTIB: FS:[0] = %x", QueryExceptionChain()));
|
|---|
| 157 |
|
|---|
| 158 | winteb = (TEB *)*TIBFlatPtr;
|
|---|
| 159 | if(winteb) {
|
|---|
| 160 | thdb = (THDB *)(winteb+1);
|
|---|
| 161 | orgtibsel = thdb->OrgTIBSel;
|
|---|
| 162 |
|
|---|
| 163 | //Restore our original FS selector
|
|---|
| 164 | SetFS(orgtibsel);
|
|---|
| 165 |
|
|---|
| 166 | //And free our own
|
|---|
| 167 | OSLibFreeSel(thdb->teb_sel);
|
|---|
| 168 | }
|
|---|
| 169 | else dprintf(("Already destroyed TIB"));
|
|---|
| 170 |
|
|---|
| 171 | dprintf(("DestroyTIB: FS(%x):[0] = %x", GetFS(), QueryExceptionChain()));
|
|---|
| 172 | *TIBFlatPtr = 0;
|
|---|
| 173 | return;
|
|---|
| 174 | }
|
|---|
| 175 | /******************************************************************************/
|
|---|
| 176 | /******************************************************************************/
|
|---|
| 177 | void WIN32API RestoreOS2TIB()
|
|---|
| 178 | {
|
|---|
| 179 | SHORT orgtibsel;
|
|---|
| 180 | TEB *winteb;
|
|---|
| 181 | THDB *thdb;
|
|---|
| 182 |
|
|---|
| 183 | //If we're running an Odin32 OS/2 application (not converted!), then we
|
|---|
| 184 | //we don't switch FS selectors
|
|---|
| 185 | if(fIsOS2Image) {
|
|---|
| 186 | return;
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | winteb = (TEB *)*TIBFlatPtr;
|
|---|
| 190 | if(winteb) {
|
|---|
| 191 | thdb = (THDB *)(winteb+1);
|
|---|
| 192 | orgtibsel = thdb->OrgTIBSel;
|
|---|
| 193 |
|
|---|
| 194 | //Restore our original FS selector
|
|---|
| 195 | SetFS(orgtibsel);
|
|---|
| 196 | }
|
|---|
| 197 | }
|
|---|
| 198 | /******************************************************************************/
|
|---|
| 199 | /******************************************************************************/
|
|---|
| 200 | USHORT WIN32API SetWin32TIB()
|
|---|
| 201 | {
|
|---|
| 202 | SHORT win32tibsel;
|
|---|
| 203 | TEB *winteb;
|
|---|
| 204 | THDB *thdb;
|
|---|
| 205 |
|
|---|
| 206 | //If we're running an Odin32 OS/2 application (not converted!), then we
|
|---|
| 207 | //we don't switch FS selectors
|
|---|
| 208 | if(fIsOS2Image) {
|
|---|
| 209 | return GetFS();
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | winteb = (TEB *)*TIBFlatPtr;
|
|---|
| 213 | if(winteb) {
|
|---|
| 214 | thdb = (THDB *)(winteb+1);
|
|---|
| 215 | win32tibsel = thdb->teb_sel;
|
|---|
| 216 |
|
|---|
| 217 | //Restore our win32 FS selector
|
|---|
| 218 | return SetReturnFS(win32tibsel);
|
|---|
| 219 | }
|
|---|
| 220 | else {
|
|---|
| 221 | //we didn't create this thread, so allocate a selector now
|
|---|
| 222 | //NOTE: Possible memory leak (i.e. DART threads in WINMM)
|
|---|
| 223 | winteb = InitializeTIB();
|
|---|
| 224 | if(winteb == NULL) {
|
|---|
| 225 | DebugInt3();
|
|---|
| 226 | return GetFS();
|
|---|
| 227 | }
|
|---|
| 228 | thdb = (THDB *)(winteb+1);
|
|---|
| 229 | win32tibsel = thdb->teb_sel;
|
|---|
| 230 |
|
|---|
| 231 | //Restore our win32 FS selector
|
|---|
| 232 | return SetReturnFS(win32tibsel);
|
|---|
| 233 | }
|
|---|
| 234 | // nested calls are OK, OS2ToWinCallback for instance
|
|---|
| 235 | //else DebugInt3();
|
|---|
| 236 |
|
|---|
| 237 | return GetFS();
|
|---|
| 238 | }
|
|---|
| 239 | //******************************************************************************
|
|---|
| 240 | //******************************************************************************
|
|---|
| 241 | void _System Win32DllExitList(ULONG reason)
|
|---|
| 242 | {
|
|---|
| 243 | dprintf(("Win32DllExitList %d\n", reason));
|
|---|
| 244 |
|
|---|
| 245 | if(WinExe) {
|
|---|
| 246 | delete(WinExe);
|
|---|
| 247 | WinExe = NULL;
|
|---|
| 248 | }
|
|---|
| 249 | return;
|
|---|
| 250 | }
|
|---|
| 251 | //******************************************************************************
|
|---|
| 252 | //******************************************************************************
|
|---|
| 253 | VOID WIN32API ExitProcess(DWORD exitcode)
|
|---|
| 254 | {
|
|---|
| 255 | dprintf(("KERNEL32: ExitProcess %d\n", exitcode));
|
|---|
| 256 | dprintf(("KERNEL32: ExitProcess FS = %x\n", GetFS()));
|
|---|
| 257 |
|
|---|
| 258 | SetOS2ExceptionChain(-1);
|
|---|
| 259 |
|
|---|
| 260 | //Flush and delete all open memory mapped files
|
|---|
| 261 | Win32MemMap::deleteAll();
|
|---|
| 262 |
|
|---|
| 263 | Win32DllExitList(0);
|
|---|
| 264 |
|
|---|
| 265 | //Restore original OS/2 TIB selector
|
|---|
| 266 | DestroyTIB();
|
|---|
| 267 | SetExceptionChain((ULONG)-1);
|
|---|
| 268 |
|
|---|
| 269 | //avoid crashes since win32 & OS/2 exception handler aren't identical
|
|---|
| 270 | //(terminate process generates two exceptions)
|
|---|
| 271 | /* @@@PH 1998/02/12 Added Console Support */
|
|---|
| 272 | if (iConsoleIsActive())
|
|---|
| 273 | iConsoleWaitClose();
|
|---|
| 274 |
|
|---|
| 275 | O32_ExitProcess(exitcode);
|
|---|
| 276 | }
|
|---|
| 277 | //******************************************************************************
|
|---|
| 278 | //******************************************************************************
|
|---|
| 279 | BOOL WIN32API FreeLibrary(HINSTANCE hinstance)
|
|---|
| 280 | {
|
|---|
| 281 | Win32DllBase *winmod;
|
|---|
| 282 | BOOL rc;
|
|---|
| 283 |
|
|---|
| 284 | dprintf(("FreeLibrary"));
|
|---|
| 285 | winmod = Win32DllBase::findModule(hinstance);
|
|---|
| 286 | if(winmod) {
|
|---|
| 287 | winmod->Release();
|
|---|
| 288 | return(TRUE);
|
|---|
| 289 | }
|
|---|
| 290 | dprintf(("KERNEL32: FreeLibrary %s %X\n", OSLibGetDllName(hinstance), hinstance));
|
|---|
| 291 |
|
|---|
| 292 | //TODO: Not thread safe
|
|---|
| 293 | fFreeLibrary = TRUE; //ditch dll
|
|---|
| 294 | rc = O32_FreeLibrary(hinstance);
|
|---|
| 295 | fFreeLibrary = FALSE;
|
|---|
| 296 | dprintf(("FreeLibrary returned %X\n", rc));
|
|---|
| 297 | return(TRUE);
|
|---|
| 298 | }
|
|---|
| 299 | /******************************************************************************/
|
|---|
| 300 | /******************************************************************************/
|
|---|
| 301 | static HINSTANCE iLoadLibraryA(LPCTSTR lpszLibFile, DWORD dwFlags)
|
|---|
| 302 | {
|
|---|
| 303 | char modname[CCHMAXPATH];
|
|---|
| 304 | HINSTANCE hDll;
|
|---|
| 305 | Win32DllBase *module;
|
|---|
| 306 |
|
|---|
| 307 | module = Win32DllBase::findModule((LPSTR)lpszLibFile);
|
|---|
| 308 | if(module) {
|
|---|
| 309 | module->AddRef();
|
|---|
| 310 | return module->getInstanceHandle();
|
|---|
| 311 | }
|
|---|
| 312 |
|
|---|
| 313 | hDll = O32_LoadLibrary(lpszLibFile);
|
|---|
| 314 | dprintf(("KERNEL32: iLoadLibraryA %s returned %X (%d)\n",
|
|---|
| 315 | lpszLibFile,
|
|---|
| 316 | hDll,
|
|---|
| 317 | GetLastError()));
|
|---|
| 318 | if(hDll)
|
|---|
| 319 | {
|
|---|
| 320 | return hDll; //converted dll or win32k took care of it
|
|---|
| 321 | }
|
|---|
| 322 |
|
|---|
| 323 | strcpy(modname, lpszLibFile);
|
|---|
| 324 | strupr(modname);
|
|---|
| 325 | if(!strstr(modname, ".DLL")) {
|
|---|
| 326 | strcat(modname,".DLL");
|
|---|
| 327 | }
|
|---|
| 328 |
|
|---|
| 329 | if(Win32ImageBase::isPEImage((char *)modname))
|
|---|
| 330 | {
|
|---|
| 331 | module = Win32DllBase::findModule((char *)modname);
|
|---|
| 332 | if(module) {//don't load it again
|
|---|
| 333 | module->AddRef();
|
|---|
| 334 | return module->getInstanceHandle();
|
|---|
| 335 | }
|
|---|
| 336 |
|
|---|
| 337 | Win32PeLdrDll *peldrDll = new Win32PeLdrDll((char *)modname);
|
|---|
| 338 | if(peldrDll == NULL)
|
|---|
| 339 | return(0);
|
|---|
| 340 |
|
|---|
| 341 | peldrDll->init(0);
|
|---|
| 342 | if(peldrDll->getError() != NO_ERROR) {
|
|---|
| 343 | dprintf(("LoadLibary %s failed (::init)\n", lpszLibFile));
|
|---|
| 344 | delete(peldrDll);
|
|---|
| 345 | return(0);
|
|---|
| 346 | }
|
|---|
| 347 | if(dwFlags & DONT_RESOLVE_DLL_REFERENCES) {
|
|---|
| 348 | peldrDll->setNoEntryCalls();
|
|---|
| 349 | }
|
|---|
| 350 |
|
|---|
| 351 | if(peldrDll->attachProcess() == FALSE) {
|
|---|
| 352 | dprintf(("LoadLibary %s failed (::attachProcess)\n", lpszLibFile));
|
|---|
| 353 | delete(peldrDll);
|
|---|
| 354 | return(0);
|
|---|
| 355 | }
|
|---|
| 356 | peldrDll->AddRef();
|
|---|
| 357 | return peldrDll->getInstanceHandle();
|
|---|
| 358 | }
|
|---|
| 359 | else return(0);
|
|---|
| 360 | }
|
|---|
| 361 | //******************************************************************************
|
|---|
| 362 | //******************************************************************************
|
|---|
| 363 | HINSTANCE WIN32API LoadLibraryA(LPCTSTR lpszLibFile)
|
|---|
| 364 | {
|
|---|
| 365 | HINSTANCE hDll;
|
|---|
| 366 |
|
|---|
| 367 | dprintf(("KERNEL32: LoadLibraryA(%s)\n",
|
|---|
| 368 | lpszLibFile));
|
|---|
| 369 |
|
|---|
| 370 | hDll = iLoadLibraryA(lpszLibFile, 0);
|
|---|
| 371 | if (hDll == 0)
|
|---|
| 372 | {
|
|---|
| 373 | char * pszName;
|
|---|
| 374 |
|
|---|
| 375 | // remove path from the image name
|
|---|
| 376 | pszName = strrchr((char *)lpszLibFile,
|
|---|
| 377 | '\\');
|
|---|
| 378 | if (pszName != NULL)
|
|---|
| 379 | {
|
|---|
| 380 | pszName++; // skip backslash
|
|---|
| 381 |
|
|---|
| 382 | // now try again without fully qualified path
|
|---|
| 383 | hDll = iLoadLibraryA(pszName, 0);
|
|---|
| 384 | }
|
|---|
| 385 | }
|
|---|
| 386 |
|
|---|
| 387 | return hDll;
|
|---|
| 388 | }
|
|---|
| 389 | //******************************************************************************
|
|---|
| 390 | //******************************************************************************
|
|---|
| 391 | HINSTANCE WIN32API LoadLibraryExA(LPCTSTR lpszLibFile, HANDLE hFile, DWORD dwFlags)
|
|---|
| 392 | {
|
|---|
| 393 | HINSTANCE hDll;
|
|---|
| 394 |
|
|---|
| 395 | dprintf(("KERNEL32: LoadLibraryExA %s (%X)\n", lpszLibFile, dwFlags));
|
|---|
| 396 | hDll = iLoadLibraryA(lpszLibFile, dwFlags);
|
|---|
| 397 | if (hDll == 0)
|
|---|
| 398 | {
|
|---|
| 399 | char * pszName;
|
|---|
| 400 |
|
|---|
| 401 | // remove path from the image name
|
|---|
| 402 | pszName = strrchr((char *)lpszLibFile,
|
|---|
| 403 | '\\');
|
|---|
| 404 | if (pszName != NULL)
|
|---|
| 405 | {
|
|---|
| 406 | pszName++; // skip backslash
|
|---|
| 407 |
|
|---|
| 408 | // now try again without fully qualified path
|
|---|
| 409 | hDll = iLoadLibraryA(pszName, dwFlags);
|
|---|
| 410 | }
|
|---|
| 411 | }
|
|---|
| 412 |
|
|---|
| 413 | return hDll;
|
|---|
| 414 | }
|
|---|
| 415 | //******************************************************************************
|
|---|
| 416 | //******************************************************************************
|
|---|
| 417 | HINSTANCE WIN32API LoadLibraryW(LPCWSTR lpModule)
|
|---|
| 418 | {
|
|---|
| 419 | char *asciimodule;
|
|---|
| 420 | HINSTANCE rc;
|
|---|
| 421 |
|
|---|
| 422 | asciimodule = UnicodeToAsciiString((LPWSTR)lpModule);
|
|---|
| 423 | dprintf(("KERNEL32: OS2LoadLibraryW %s\n", asciimodule));
|
|---|
| 424 | rc = LoadLibraryA(asciimodule);
|
|---|
| 425 | free(asciimodule);
|
|---|
| 426 | return(rc);
|
|---|
| 427 | }
|
|---|
| 428 | //******************************************************************************
|
|---|
| 429 | //******************************************************************************
|
|---|
| 430 | HINSTANCE WIN32API LoadLibraryExW(LPCWSTR lpModule, HANDLE hFile, DWORD dwFlags)
|
|---|
| 431 | {
|
|---|
| 432 | char *asciimodule;
|
|---|
| 433 | HINSTANCE rc;
|
|---|
| 434 |
|
|---|
| 435 | asciimodule = UnicodeToAsciiString((LPWSTR)lpModule);
|
|---|
| 436 | dprintf(("KERNEL32: OS2LoadLibraryExW %s (%d)\n", asciimodule, dwFlags));
|
|---|
| 437 | rc = LoadLibraryExA(asciimodule, hFile, dwFlags);
|
|---|
| 438 | free(asciimodule);
|
|---|
| 439 | return(rc);
|
|---|
| 440 | }
|
|---|
| 441 | //******************************************************************************
|
|---|
| 442 | //******************************************************************************
|
|---|
| 443 | LPCSTR WIN32API GetCommandLineA()
|
|---|
| 444 | {
|
|---|
| 445 | LPTSTR cmdline = NULL;
|
|---|
| 446 |
|
|---|
| 447 | if(WinExe) {
|
|---|
| 448 | cmdline = WinExe->getCommandLine();
|
|---|
| 449 | }
|
|---|
| 450 | if(cmdline == NULL) //not used for converted exes
|
|---|
| 451 | cmdline = O32_GetCommandLine();
|
|---|
| 452 |
|
|---|
| 453 | dprintf(("KERNEL32: GetCommandLine %s\n", cmdline));
|
|---|
| 454 | dprintf(("KERNEL32: FS = %x\n", GetFS()));
|
|---|
| 455 | return(cmdline);
|
|---|
| 456 | }
|
|---|
| 457 | //******************************************************************************
|
|---|
| 458 | //******************************************************************************
|
|---|
| 459 | LPCWSTR WIN32API GetCommandLineW(void)
|
|---|
| 460 | {
|
|---|
| 461 | static WCHAR *UnicodeCmdLine = NULL;
|
|---|
| 462 | char *asciicmdline = NULL;
|
|---|
| 463 |
|
|---|
| 464 | dprintf(("KERNEL32: FS = %x\n", GetFS()));
|
|---|
| 465 |
|
|---|
| 466 | if(UnicodeCmdLine)
|
|---|
| 467 | return(UnicodeCmdLine); //already called before
|
|---|
| 468 |
|
|---|
| 469 | if(WinExe) {
|
|---|
| 470 | asciicmdline = WinExe->getCommandLine();
|
|---|
| 471 | }
|
|---|
| 472 | if(asciicmdline == NULL) //not used for converted exes
|
|---|
| 473 | asciicmdline = O32_GetCommandLine();
|
|---|
| 474 |
|
|---|
| 475 | if(asciicmdline) {
|
|---|
| 476 | UnicodeCmdLine = (WCHAR *)malloc(strlen(asciicmdline)*2 + 2);
|
|---|
| 477 | AsciiToUnicode(asciicmdline, UnicodeCmdLine);
|
|---|
| 478 | dprintf(("KERNEL32: OS2GetCommandLineW: %s\n", asciicmdline));
|
|---|
| 479 | return(UnicodeCmdLine);
|
|---|
| 480 | }
|
|---|
| 481 | dprintf(("KERNEL32: OS2GetCommandLineW: asciicmdline == NULL\n"));
|
|---|
| 482 | return NULL;
|
|---|
| 483 | }
|
|---|
| 484 | //******************************************************************************
|
|---|
| 485 | //******************************************************************************
|
|---|
| 486 | DWORD WIN32API GetModuleFileNameA(HMODULE hinstModule, LPTSTR lpszPath, DWORD cchPath)
|
|---|
| 487 | {
|
|---|
| 488 | DWORD rc;
|
|---|
| 489 | Win32ImageBase *module;
|
|---|
| 490 | char *fpath = NULL;
|
|---|
| 491 |
|
|---|
| 492 | dprintf(("GetModuleFileName %X", hinstModule));
|
|---|
| 493 | if(hinstModule == 0 || hinstModule == -1 || (WinExe && hinstModule == WinExe->getInstanceHandle())) {
|
|---|
| 494 | module = (Win32ImageBase *)WinExe;
|
|---|
| 495 | }
|
|---|
| 496 | else {
|
|---|
| 497 | module = (Win32ImageBase *)Win32DllBase::findModule(hinstModule);
|
|---|
| 498 | }
|
|---|
| 499 |
|
|---|
| 500 | if(module) {
|
|---|
| 501 | fpath = module->getFullPath();
|
|---|
| 502 | }
|
|---|
| 503 | if(fpath) {
|
|---|
| 504 | //SvL: 13-9-98: +1
|
|---|
| 505 | rc = min(strlen(fpath)+1, cchPath);
|
|---|
| 506 | strncpy(lpszPath, fpath, rc);
|
|---|
| 507 | }
|
|---|
| 508 | else rc = O32_GetModuleFileName(hinstModule, lpszPath, cchPath);
|
|---|
| 509 |
|
|---|
| 510 | if(rc) {
|
|---|
| 511 | dprintf(("KERNEL32: GetModuleFileName %s %d\n", lpszPath, hinstModule));
|
|---|
| 512 | }
|
|---|
| 513 | return(rc);
|
|---|
| 514 | }
|
|---|
| 515 | //******************************************************************************
|
|---|
| 516 | //******************************************************************************
|
|---|
| 517 | DWORD WIN32API GetModuleFileNameW(HMODULE hModule, LPWSTR lpFileName, DWORD nSize)
|
|---|
| 518 | {
|
|---|
| 519 | char *asciifilename = (char *)malloc(nSize+1);
|
|---|
| 520 | DWORD rc;
|
|---|
| 521 |
|
|---|
| 522 | dprintf(("KERNEL32: OSLibGetModuleFileNameW\n"));
|
|---|
| 523 | rc = GetModuleFileNameA(hModule, asciifilename, nSize);
|
|---|
| 524 | if(rc) AsciiToUnicode(asciifilename, lpFileName);
|
|---|
| 525 | free(asciifilename);
|
|---|
| 526 | return(rc);
|
|---|
| 527 | }
|
|---|
| 528 | //******************************************************************************
|
|---|
| 529 | //NOTE: GetModuleHandleA does NOT support files with multiple dots (i.e.
|
|---|
| 530 | // very.weird.exe)
|
|---|
| 531 | //******************************************************************************
|
|---|
| 532 | HANDLE WIN32API GetModuleHandleA(LPCTSTR lpszModule)
|
|---|
| 533 | {
|
|---|
| 534 | HANDLE hMod;
|
|---|
| 535 | Win32DllBase *windll;
|
|---|
| 536 | char szModule[CCHMAXPATH];
|
|---|
| 537 | BOOL fDllModule = FALSE;
|
|---|
| 538 |
|
|---|
| 539 | if(lpszModule == NULL) {
|
|---|
| 540 | if(WinExe)
|
|---|
| 541 | hMod = WinExe->getInstanceHandle();
|
|---|
| 542 | else hMod = -1;
|
|---|
| 543 | }
|
|---|
| 544 | else {
|
|---|
| 545 | strcpy(szModule, OSLibStripPath((char *)lpszModule));
|
|---|
| 546 | strupr(szModule);
|
|---|
| 547 | if(strstr(szModule, ".DLL")) {
|
|---|
| 548 | fDllModule = TRUE;
|
|---|
| 549 | }
|
|---|
| 550 | else {
|
|---|
| 551 | if(!strstr(szModule, ".")) {
|
|---|
| 552 | //if there's no extension or trainling dot, we
|
|---|
| 553 | //assume it's a dll (see Win32 SDK docs)
|
|---|
| 554 | fDllModule = TRUE;
|
|---|
| 555 | }
|
|---|
| 556 | }
|
|---|
| 557 | char *dot = strstr(szModule, ".");
|
|---|
| 558 | if(dot)
|
|---|
| 559 | *dot = 0;
|
|---|
| 560 |
|
|---|
| 561 | if(!fDllModule && WinExe && !strcmpi(lpszModule, WinExe->getModuleName())) {
|
|---|
| 562 | hMod = WinExe->getInstanceHandle();
|
|---|
| 563 | }
|
|---|
| 564 | else {
|
|---|
| 565 | windll = Win32DllBase::findModule(szModule);
|
|---|
| 566 | if(windll) {
|
|---|
| 567 | hMod = windll->getInstanceHandle();
|
|---|
| 568 | }
|
|---|
| 569 | else hMod = OSLibiGetModuleHandleA((char *)lpszModule);
|
|---|
| 570 | }
|
|---|
| 571 | }
|
|---|
| 572 |
|
|---|
| 573 | eprintf(("KERNEL32: GetModuleHandle %s returned %X\n", lpszModule, hMod));
|
|---|
| 574 | return(hMod);
|
|---|
| 575 | }
|
|---|
| 576 | //******************************************************************************
|
|---|
| 577 | //******************************************************************************
|
|---|
| 578 | HMODULE WIN32API GetModuleHandleW(LPCWSTR arg1)
|
|---|
| 579 | {
|
|---|
| 580 | HMODULE rc;
|
|---|
| 581 | char *astring;
|
|---|
| 582 |
|
|---|
| 583 | astring = UnicodeToAsciiString((LPWSTR)arg1);
|
|---|
| 584 | rc = GetModuleHandleA(astring);
|
|---|
| 585 | dprintf(("KERNEL32: OS2GetModuleHandleW %s returned %X\n", astring, rc));
|
|---|
| 586 | FreeAsciiString(astring);
|
|---|
| 587 | return(rc);
|
|---|
| 588 | }
|
|---|
| 589 | //******************************************************************************
|
|---|
| 590 | //******************************************************************************
|
|---|
| 591 | BOOL WIN32API CreateProcessA(LPCSTR lpszImageName, LPSTR lpszCommandLine,
|
|---|
| 592 | PSECURITY_ATTRIBUTES arg3,
|
|---|
| 593 | PSECURITY_ATTRIBUTES arg4, BOOL arg5, DWORD arg6,
|
|---|
| 594 | PVOID arg7, LPCSTR arg8, LPSTARTUPINFOA arg9,
|
|---|
| 595 | LPPROCESS_INFORMATION arg10)
|
|---|
| 596 | {
|
|---|
| 597 | BOOL rc;
|
|---|
| 598 | char *cmdline;
|
|---|
| 599 | BOOL fAllocStr = FALSE;
|
|---|
| 600 |
|
|---|
| 601 | if(O32_CreateProcess(lpszImageName, lpszCommandLine, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10) == TRUE)
|
|---|
| 602 | return(TRUE);
|
|---|
| 603 |
|
|---|
| 604 | //probably a win32 exe, so run it in the pe loader
|
|---|
| 605 | if(lpszImageName) {
|
|---|
| 606 | if(lpszCommandLine) {
|
|---|
| 607 | cmdline = (char *)malloc(strlen(lpszImageName)+strlen(lpszCommandLine) + 16);
|
|---|
| 608 | sprintf(cmdline, "PE.EXE %s %s", lpszImageName, lpszCommandLine);
|
|---|
| 609 | fAllocStr = TRUE;
|
|---|
| 610 | }
|
|---|
| 611 | else {
|
|---|
| 612 | cmdline = (char *)malloc(strlen(lpszImageName) + 16);
|
|---|
| 613 | sprintf(cmdline, "PE.EXE %s", lpszImageName);
|
|---|
| 614 | fAllocStr = TRUE;
|
|---|
| 615 | }
|
|---|
| 616 | }
|
|---|
| 617 | else {
|
|---|
| 618 | cmdline = (char *)malloc(strlen(lpszCommandLine) + 16);
|
|---|
| 619 | sprintf(cmdline, "PE.EXE %s", lpszCommandLine);
|
|---|
| 620 | fAllocStr = TRUE;
|
|---|
| 621 | }
|
|---|
| 622 | dprintf(("KERNEL32: CreateProcess %s\n", cmdline));
|
|---|
| 623 | rc = O32_CreateProcess("PE.EXE", (LPCSTR)cmdline, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10);
|
|---|
| 624 | if(fAllocStr)
|
|---|
| 625 | free(cmdline);
|
|---|
| 626 |
|
|---|
| 627 | dprintf(("KERNEL32: CreateProcess returned %d\n", rc));
|
|---|
| 628 | return(rc);
|
|---|
| 629 | }
|
|---|
| 630 | //******************************************************************************
|
|---|
| 631 | //******************************************************************************
|
|---|
| 632 | BOOL WIN32API CreateProcessW(LPCWSTR arg1, LPWSTR arg2,
|
|---|
| 633 | PSECURITY_ATTRIBUTES arg3,
|
|---|
| 634 | PSECURITY_ATTRIBUTES arg4,
|
|---|
| 635 | BOOL arg5, DWORD arg6, PVOID arg7,
|
|---|
| 636 | LPCWSTR arg8, LPSTARTUPINFOW arg9,
|
|---|
| 637 | LPPROCESS_INFORMATION arg10)
|
|---|
| 638 | {
|
|---|
| 639 | BOOL rc;
|
|---|
| 640 | char *astring1, *astring2, *astring3;
|
|---|
| 641 |
|
|---|
| 642 | dprintf(("KERNEL32: OS2CreateProcessW DOESN't WORK"));
|
|---|
| 643 | astring1 = UnicodeToAsciiString((LPWSTR)arg1);
|
|---|
| 644 | astring2 = UnicodeToAsciiString(arg2);
|
|---|
| 645 | astring3 = UnicodeToAsciiString((LPWSTR)arg8);
|
|---|
| 646 | // NOTE: This will not work as is (needs UNICODE support)
|
|---|
| 647 | rc = CreateProcessA(astring1, astring2, arg3, arg4, arg5, arg6, arg7,
|
|---|
| 648 | astring3, (LPSTARTUPINFOA)arg9, arg10);
|
|---|
| 649 | FreeAsciiString(astring3);
|
|---|
| 650 | FreeAsciiString(astring2);
|
|---|
| 651 | FreeAsciiString(astring1);
|
|---|
| 652 | return(rc);
|
|---|
| 653 | }
|
|---|
| 654 | //******************************************************************************
|
|---|
| 655 | //******************************************************************************
|
|---|
| 656 | FARPROC WIN32API GetProcAddress(HMODULE hModule, LPCSTR lpszProc)
|
|---|
| 657 | {
|
|---|
| 658 | Win32DllBase *winmod;
|
|---|
| 659 | FARPROC proc;
|
|---|
| 660 | ULONG ulAPIOrdinal;
|
|---|
| 661 |
|
|---|
| 662 | winmod = Win32DllBase::findModule((HINSTANCE)hModule);
|
|---|
| 663 | if(winmod) {
|
|---|
| 664 | ulAPIOrdinal = (ULONG)lpszProc;
|
|---|
| 665 | if (ulAPIOrdinal <= 0x0000FFFF) {
|
|---|
| 666 | proc = (FARPROC)winmod->getApi((int)ulAPIOrdinal);
|
|---|
| 667 | }
|
|---|
| 668 | else proc = (FARPROC)winmod->getApi((char *)lpszProc);
|
|---|
| 669 | if(proc == 0) {
|
|---|
| 670 | SetLastError(ERROR_PROC_NOT_FOUND);
|
|---|
| 671 | }
|
|---|
| 672 | return proc;
|
|---|
| 673 | }
|
|---|
| 674 | proc = O32_GetProcAddress(hModule, lpszProc);
|
|---|
| 675 | dprintf(("KERNEL32: GetProcAddress %s from %X returned %X\n", lpszProc, hModule, proc));
|
|---|
| 676 | return(proc);
|
|---|
| 677 | }
|
|---|
| 678 | //******************************************************************************
|
|---|
| 679 | //Retrieve the version
|
|---|
| 680 | //******************************************************************************
|
|---|
| 681 | BOOL SYSTEM GetVersionStruct(char *modname, char *verstruct, ULONG bufLength)
|
|---|
| 682 | {
|
|---|
| 683 | Win32ImageBase *winimage;
|
|---|
| 684 | Win32PeLdrRsrcImg *rsrcimg;
|
|---|
| 685 |
|
|---|
| 686 | dprintf(("GetVersionStruct"));
|
|---|
| 687 | if(WinExe && !stricmp(WinExe->getFullPath(), modname)) {
|
|---|
| 688 | winimage = (Win32ImageBase *)WinExe;
|
|---|
| 689 | }
|
|---|
| 690 | else {
|
|---|
| 691 | winimage = (Win32ImageBase *)Win32DllBase::findModule(modname);
|
|---|
| 692 | if(winimage == NULL)
|
|---|
| 693 | {
|
|---|
| 694 | if(Win32ImageBase::isPEImage(modname) == FALSE)
|
|---|
| 695 | {
|
|---|
| 696 | HINSTANCE hInstance;
|
|---|
| 697 |
|
|---|
| 698 | //must be an LX dll, just load it (app will probably load it anyway)
|
|---|
| 699 | hInstance = LoadLibraryA(modname);
|
|---|
| 700 | if(hInstance == 0)
|
|---|
| 701 | return 0;
|
|---|
| 702 | winimage = (Win32ImageBase *)Win32DllBase::findModule(hInstance);
|
|---|
| 703 | if(winimage) {
|
|---|
| 704 | return winimage->getVersionStruct(verstruct, bufLength);
|
|---|
| 705 | }
|
|---|
| 706 | return 0;
|
|---|
| 707 | }
|
|---|
| 708 | //SvL: Try to load it
|
|---|
| 709 | rsrcimg = new Win32PeLdrRsrcImg(modname);
|
|---|
| 710 | if(rsrcimg == NULL)
|
|---|
| 711 | return 0;
|
|---|
| 712 |
|
|---|
| 713 | rsrcimg->init(0);
|
|---|
| 714 | if(rsrcimg->getError() != NO_ERROR)
|
|---|
| 715 | {
|
|---|
| 716 | dprintf(("GetVersionStruct can't load %s\n", modname));
|
|---|
| 717 | delete rsrcimg;
|
|---|
| 718 | return(FALSE);
|
|---|
| 719 | }
|
|---|
| 720 | BOOL rc = rsrcimg->getVersionStruct(verstruct, bufLength);
|
|---|
| 721 | delete rsrcimg;
|
|---|
| 722 | return rc;
|
|---|
| 723 | }
|
|---|
| 724 | }
|
|---|
| 725 | return winimage->getVersionStruct(verstruct, bufLength);
|
|---|
| 726 | }
|
|---|
| 727 | //******************************************************************************
|
|---|
| 728 | //******************************************************************************
|
|---|
| 729 | ULONG SYSTEM GetVersionSize(char *modname)
|
|---|
| 730 | {
|
|---|
| 731 | Win32ImageBase *winimage;
|
|---|
| 732 | Win32PeLdrRsrcImg *rsrcimg;
|
|---|
| 733 |
|
|---|
| 734 | dprintf(("GetVersionSize of %s\n", modname));
|
|---|
| 735 |
|
|---|
| 736 | if(WinExe && !stricmp(WinExe->getFullPath(), modname)) {
|
|---|
| 737 | winimage = (Win32ImageBase *)WinExe;
|
|---|
| 738 | }
|
|---|
| 739 | else {
|
|---|
| 740 | winimage = (Win32ImageBase *)Win32DllBase::findModule(modname);
|
|---|
| 741 | if(winimage == NULL)
|
|---|
| 742 | {
|
|---|
| 743 | if(Win32ImageBase::isPEImage(modname) == FALSE)
|
|---|
| 744 | {
|
|---|
| 745 | HINSTANCE hInstance;
|
|---|
| 746 |
|
|---|
| 747 | //must be an LX dll, just load it (app will probably load it anyway)
|
|---|
| 748 | hInstance = LoadLibraryA(modname);
|
|---|
| 749 | if(hInstance == 0)
|
|---|
| 750 | return 0;
|
|---|
| 751 | winimage = (Win32ImageBase *)Win32DllBase::findModule(hInstance);
|
|---|
| 752 | if(winimage) {
|
|---|
| 753 | return winimage->getVersionSize();
|
|---|
| 754 | }
|
|---|
| 755 | return 0;
|
|---|
| 756 | }
|
|---|
| 757 |
|
|---|
| 758 | //SvL: Try to load it
|
|---|
| 759 | rsrcimg = new Win32PeLdrRsrcImg(modname);
|
|---|
| 760 | if(rsrcimg == NULL)
|
|---|
| 761 | return 0;
|
|---|
| 762 |
|
|---|
| 763 | rsrcimg->init(0);
|
|---|
| 764 | if(rsrcimg->getError() != NO_ERROR)
|
|---|
| 765 | {
|
|---|
| 766 | dprintf(("GetVersionSize can't load %s\n", modname));
|
|---|
| 767 | delete rsrcimg;
|
|---|
| 768 | return(FALSE);
|
|---|
| 769 | }
|
|---|
| 770 | rsrcimg->init(0);
|
|---|
| 771 | int size = rsrcimg->getVersionSize();
|
|---|
| 772 | delete rsrcimg;
|
|---|
| 773 | return size;
|
|---|
| 774 | }
|
|---|
| 775 | }
|
|---|
| 776 | return winimage->getVersionSize();
|
|---|
| 777 | }
|
|---|
| 778 | //******************************************************************************
|
|---|
| 779 | //******************************************************************************
|
|---|
| 780 |
|
|---|
| 781 |
|
|---|
| 782 | /***********************************************************************
|
|---|
| 783 | * RegisterServiceProcess (KERNEL, KERNEL32)
|
|---|
| 784 | *
|
|---|
| 785 | * A service process calls this function to ensure that it continues to run
|
|---|
| 786 | * even after a user logged off.
|
|---|
| 787 | */
|
|---|
| 788 | DWORD WIN32API RegisterServiceProcess(DWORD dwProcessId,
|
|---|
| 789 | DWORD dwType)
|
|---|
| 790 | {
|
|---|
| 791 | dprintf(("KERNEL32: RegisterServiceProcess(%08xh,%08xh) not implemented.\n",
|
|---|
| 792 | dwProcessId,
|
|---|
| 793 | dwType));
|
|---|
| 794 |
|
|---|
| 795 | /* I don't think that Wine needs to do anything in that function */
|
|---|
| 796 | return 1; /* success */
|
|---|
| 797 | }
|
|---|
| 798 |
|
|---|
| 799 | //******************************************************************************
|
|---|
| 800 | //TODO:What does this do exactly??
|
|---|
| 801 | //******************************************************************************
|
|---|
| 802 | ODINFUNCTION1(BOOL,DisableThreadLibraryCalls,HMODULE,hModule)
|
|---|
| 803 | {
|
|---|
| 804 | Win32DllBase *winmod;
|
|---|
| 805 | FARPROC proc;
|
|---|
| 806 | ULONG ulAPIOrdinal;
|
|---|
| 807 |
|
|---|
| 808 | winmod = Win32DllBase::findModule((HINSTANCE)hModule);
|
|---|
| 809 | if(winmod)
|
|---|
| 810 | {
|
|---|
| 811 | // don't call ATTACH/DETACH thread functions in DLL
|
|---|
| 812 | winmod->setThreadLibraryCalls(FALSE);
|
|---|
| 813 | return TRUE;
|
|---|
| 814 | }
|
|---|
| 815 | else
|
|---|
| 816 | {
|
|---|
| 817 | // raise error condition
|
|---|
| 818 | SetLastError(ERROR_INVALID_HANDLE);
|
|---|
| 819 | return FALSE;
|
|---|
| 820 | }
|
|---|
| 821 | }
|
|---|
| 822 |
|
|---|