source: trunk/src/kernel32/wprocess.cpp@ 2280

Last change on this file since 2280 was 2280, checked in by sandervl, 26 years ago

EB's debug support added

File size: 32.1 KB
Line 
1/* $Id: wprocess.cpp,v 1.63 1999-12-31 10:47:12 sandervl Exp $ */
2
3/*
4 * Win32 process functions
5 *
6 * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl)
7 *
8 * NOTE: Even though Odin32 OS/2 apps don't switch FS selectors,
9 * we still allocate a TEB to store misc information.
10 *
11 * Project Odin Software License can be found in LICENSE.TXT
12 *
13 */
14#include <odin.h>
15#include <odinwrap.h>
16#include <os2win.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20
21#include <unicode.h>
22#include <windllbase.h>
23#include <winexebase.h>
24#include <windllpeldr.h>
25#include <winfakepeldr.h>
26#include <vmutex.h>
27
28#ifdef __IBMCPP__
29#include <builtin.h>
30#endif
31
32#include "exceptutil.h"
33#include "oslibmisc.h"
34#include "oslibdebug.h"
35
36#include "console.h"
37#include "cio.h"
38#include "versionos2.h" /*PLF Wed 98-03-18 02:36:51*/
39#include <wprocess.h>
40#include "mmap.h"
41
42
43ODINDEBUGCHANNEL(KERNEL32-WPROCESS)
44
45
46//******************************************************************************
47//******************************************************************************
48BOOL fFreeLibrary = FALSE;
49BOOL fIsOS2Image = FALSE; //TRUE -> Odin32 OS/2 application (not converted!)
50 //FALSE -> otherwise
51//Process database
52PDB ProcessPDB = {0};
53USHORT ProcessTIBSel = 0;
54DWORD *TIBFlatPtr = 0;
55
56//list of thread database structures
57static THDB *threadList = 0;
58static VMutex threadListMutex;
59//******************************************************************************
60//******************************************************************************
61TEB *WIN32API GetThreadTEB()
62{
63 if(TIBFlatPtr == NULL)
64 return 0;
65
66 return (TEB *)*TIBFlatPtr;
67}
68//******************************************************************************
69//******************************************************************************
70THDB *WIN32API GetThreadTHDB()
71{
72 TEB *winteb;
73 THDB *thdb;
74
75 if(TIBFlatPtr == NULL)
76 return 0;
77
78 winteb = (TEB *)*TIBFlatPtr;
79 if(winteb == NULL) {
80 return NULL;
81 }
82 thdb = (THDB *)(winteb+1);
83
84 return thdb;
85}
86//******************************************************************************
87//******************************************************************************
88THDB *WIN32API GetTHDBFromThreadId(ULONG threadId)
89{
90 THDB *thdb = threadList;
91
92 threadListMutex.enter();
93 while(thdb) {
94 if(thdb->threadId == threadId) {
95 break;
96 }
97 thdb = thdb->next;
98 }
99 threadListMutex.leave();
100 return thdb;
101}
102//******************************************************************************
103// Set up the TIB selector and memory for the current thread
104//******************************************************************************
105TEB *InitializeTIB(BOOL fMainThread)
106{
107 TEB *winteb;
108 THDB *thdb;
109
110 USHORT tibsel;
111
112 //Allocate one dword to store the flat address of our TEB
113 if(fMainThread) {
114 TIBFlatPtr = (DWORD *)OSLibAllocThreadLocalMemory(1);
115 if(TIBFlatPtr == 0) {
116 dprintf(("InitializeTIB: local thread memory alloc failed!!"));
117 DebugInt3();
118 return NULL;
119 }
120 }
121 if(OSLibAllocSel(PAGE_SIZE, &tibsel) == FALSE)
122 {
123 dprintf(("InitializeTIB: selector alloc failed!!"));
124 DebugInt3();
125 return NULL;
126 }
127 winteb = (TEB *)OSLibSelToFlat(tibsel);
128 if(winteb == NULL)
129 {
130 dprintf(("InitializeTIB: DosSelToFlat failed!!"));
131 DebugInt3();
132 return NULL;
133 }
134 memset(winteb, 0, PAGE_SIZE);
135 thdb = (THDB *)(winteb+1);
136 *TIBFlatPtr = (DWORD)winteb;
137
138 winteb->except = (PVOID)-1; /* 00 Head of exception handling chain */
139 winteb->stack_top = (PVOID)OSLibGetTIB(TIB_STACKTOP); /* 04 Top of thread stack */
140 winteb->stack_low = (PVOID)OSLibGetTIB(TIB_STACKLOW); /* 08 Stack low-water mark */
141 winteb->htask16 = (USHORT)OSLibGetPIB(PIB_TASKHNDL); /* 0c Win16 task handle */
142 winteb->stack_sel = getSS(); /* 0e 16-bit stack selector */
143 winteb->self = winteb; /* 18 Pointer to this structure */
144 winteb->flags = TEBF_WIN32; /* 1c Flags */
145 winteb->queue = 0; /* 28 Message queue */
146 winteb->tls_ptr = &thdb->tls_array[0]; /* 2c Pointer to TLS array */
147 winteb->process = &ProcessPDB; /* 30 owning process (used by NT3.51 applets)*/
148
149 memcpy(&thdb->teb, winteb, sizeof(TEB));
150 thdb->process = &ProcessPDB;
151 thdb->exit_code = 0x103; /* STILL_ACTIVE */
152 thdb->teb_sel = tibsel;
153 thdb->OrgTIBSel = GetFS();
154 thdb->pWsockData = NULL;
155 thdb->threadId = GetCurrentThreadId();
156
157 threadListMutex.enter();
158 THDB *thdblast = threadList;
159 if(!thdblast) {
160 threadList = thdb;
161 }
162 else {
163 while(thdblast->next) {
164 thdblast = thdblast->next;
165 }
166 thdblast->next = thdb;
167 }
168 thdb->next = NULL;
169 threadListMutex.leave();
170
171 if(OSLibGetPIB(PIB_TASKTYPE) == TASKTYPE_PM)
172 {
173 thdb->flags = 0; //todo gui
174 }
175 else thdb->flags = 0; //todo textmode
176
177 if(fMainThread)
178 {
179 //todo initialize PDB during process creation
180 //todo: initialize TLS array if required
181 //TLS in executable always TLS index 0?
182 ProcessTIBSel = tibsel;
183 ProcessPDB.exit_code = 0x103; /* STILL_ACTIVE */
184 ProcessPDB.threads = 1;
185 ProcessPDB.running_threads = 1;
186 ProcessPDB.ring0_threads = 1;
187 ProcessPDB.system_heap = GetProcessHeap();
188 ProcessPDB.parent = 0;
189 ProcessPDB.group = &ProcessPDB;
190 ProcessPDB.priority = 8; /* Normal */
191 ProcessPDB.heap = ProcessPDB.system_heap; /* will be changed later on */
192 ProcessPDB.next = NULL;
193 ProcessPDB.winver = 0xffff; /* to be determined */
194 ProcessPDB.server_pid = (void *)GetCurrentProcessId();
195
196 GetSystemTime(&ProcessPDB.creationTime);
197
198 /* Initialize the critical section */
199 InitializeCriticalSection( &ProcessPDB.crit_section );
200 }
201 dprintf(("InitializeTIB setup TEB with selector %x", tibsel));
202 dprintf(("InitializeTIB: FS(%x):[0] = %x", GetFS(), QueryExceptionChain()));
203 return winteb;
204}
205//******************************************************************************
206// Destroy the TIB selector and memory for the current thread
207//******************************************************************************
208void DestroyTIB()
209{
210 SHORT orgtibsel;
211 TEB *winteb;
212 THDB *thdb;
213
214 dprintf(("DestroyTIB: FS = %x", GetFS()));
215 dprintf(("DestroyTIB: FS:[0] = %x", QueryExceptionChain()));
216
217 winteb = (TEB *)*TIBFlatPtr;
218 if(winteb) {
219 thdb = (THDB *)(winteb+1);
220 orgtibsel = thdb->OrgTIBSel;
221
222 threadListMutex.enter();
223 THDB *curthdb = threadList;
224 if(curthdb == thdb) {
225 threadList = thdb->next;
226 }
227 else {
228 while(curthdb->next != thdb) {
229 curthdb = curthdb->next;
230 if(curthdb == NULL) {
231 dprintf(("DestroyTIB: couldn't find thdb %x", thdb));
232 DebugInt3();
233 break;
234 }
235 }
236 if(curthdb) {
237 curthdb->next = thdb->next;
238 }
239 }
240 threadListMutex.leave();
241
242 //Restore our original FS selector
243 SetFS(orgtibsel);
244
245 //And free our own
246 OSLibFreeSel(thdb->teb_sel);
247 }
248 else dprintf(("Already destroyed TIB"));
249
250 dprintf(("DestroyTIB: FS(%x):[0] = %x", GetFS(), QueryExceptionChain()));
251 *TIBFlatPtr = 0;
252 return;
253}
254/******************************************************************************/
255/******************************************************************************/
256void SetPDBInstance(HINSTANCE hInstance)
257{
258 ProcessPDB.hInstance = hInstance;
259}
260/******************************************************************************/
261/******************************************************************************/
262void WIN32API RestoreOS2TIB()
263{
264 SHORT orgtibsel;
265 TEB *winteb;
266 THDB *thdb;
267
268 //If we're running an Odin32 OS/2 application (not converted!), then we
269 //we don't switch FS selectors
270 if(fIsOS2Image) {
271 return;
272 }
273
274 winteb = (TEB *)*TIBFlatPtr;
275 if(winteb) {
276 thdb = (THDB *)(winteb+1);
277 orgtibsel = thdb->OrgTIBSel;
278
279 //Restore our original FS selector
280 SetFS(orgtibsel);
281 }
282}
283/******************************************************************************/
284/******************************************************************************/
285USHORT WIN32API SetWin32TIB()
286{
287 SHORT win32tibsel;
288 TEB *winteb;
289 THDB *thdb;
290
291 //If we're running an Odin32 OS/2 application (not converted!), then we
292 //we don't switch FS selectors
293 if(fIsOS2Image) {
294 return GetFS();
295 }
296
297 winteb = (TEB *)*TIBFlatPtr;
298 if(winteb) {
299 thdb = (THDB *)(winteb+1);
300 win32tibsel = thdb->teb_sel;
301
302 //Restore our win32 FS selector
303 return SetReturnFS(win32tibsel);
304 }
305 else {
306 //we didn't create this thread, so allocate a selector now
307 //NOTE: Possible memory leak (i.e. DART threads in WINMM)
308 winteb = InitializeTIB();
309 if(winteb == NULL) {
310 DebugInt3();
311 return GetFS();
312 }
313 thdb = (THDB *)(winteb+1);
314 win32tibsel = thdb->teb_sel;
315
316 //Restore our win32 FS selector
317 return SetReturnFS(win32tibsel);
318 }
319 // nested calls are OK, OS2ToWinCallback for instance
320 //else DebugInt3();
321
322 return GetFS();
323}
324//******************************************************************************
325//******************************************************************************
326void _System Win32DllExitList(ULONG reason)
327{
328 dprintf(("Win32DllExitList %d\n", reason));
329
330 if(WinExe) {
331 delete(WinExe);
332 WinExe = NULL;
333 }
334 return;
335}
336//******************************************************************************
337//******************************************************************************
338VOID WIN32API ExitProcess(DWORD exitcode)
339{
340 dprintf(("KERNEL32: ExitProcess %d\n", exitcode));
341 dprintf(("KERNEL32: ExitProcess FS = %x\n", GetFS()));
342
343 SetOS2ExceptionChain(-1);
344
345 Win32DllExitList(0);
346
347 //Note: Needs to be done after Win32DllExitList (destruction of exe + dll objects)
348 //Flush and delete all open memory mapped files
349 Win32MemMap::deleteAll();
350
351 //Restore original OS/2 TIB selector
352 DestroyTIB();
353 SetExceptionChain((ULONG)-1);
354
355 //avoid crashes since win32 & OS/2 exception handler aren't identical
356 //(terminate process generates two exceptions)
357 /* @@@PH 1998/02/12 Added Console Support */
358 if (iConsoleIsActive())
359 iConsoleWaitClose();
360
361 O32_ExitProcess(exitcode);
362}
363//******************************************************************************
364//******************************************************************************
365BOOL WIN32API FreeLibrary(HINSTANCE hinstance)
366{
367 Win32DllBase *winmod;
368 BOOL rc;
369
370 dprintf(("FreeLibrary"));
371 winmod = Win32DllBase::findModule(hinstance);
372 if(winmod) {
373 winmod->Release();
374 return(TRUE);
375 }
376 dprintf(("KERNEL32: FreeLibrary %s %X\n", OSLibGetDllName(hinstance), hinstance));
377
378 //TODO: Not thread safe
379 fFreeLibrary = TRUE; //ditch dll
380 rc = O32_FreeLibrary(hinstance);
381 fFreeLibrary = FALSE;
382 dprintf(("FreeLibrary returned %X\n", rc));
383 return(TRUE);
384}
385/******************************************************************************/
386/******************************************************************************/
387static HINSTANCE iLoadLibraryA(LPCTSTR lpszLibFile, DWORD dwFlags)
388{
389 char modname[CCHMAXPATH];
390 HINSTANCE hDll;
391 Win32DllBase *module;
392
393 module = Win32DllBase::findModule((LPSTR)lpszLibFile);
394 if(module) {
395 module->AddRef();
396 dprintf(("iLoadLibrary: found %s -> handle %x", lpszLibFile, module->getInstanceHandle()));
397 return module->getInstanceHandle();
398 }
399
400 strcpy(modname, lpszLibFile);
401 strupr(modname);
402 //rename dll if necessary (i.e. OLE32 -> OLE32OS2)
403 Win32DllBase::renameDll(modname);
404
405 hDll = O32_LoadLibrary(modname);
406 dprintf(("KERNEL32: iLoadLibraryA %s returned %X (%d)\n",
407 lpszLibFile,
408 hDll,
409 GetLastError()));
410 if(hDll)
411 {
412 return hDll; //converted dll or win32k took care of it
413 }
414
415 if(!strstr(modname, ".")) {
416 strcat(modname,".DLL");
417 }
418
419 if(Win32ImageBase::isPEImage((char *)modname))
420 {
421 module = Win32DllBase::findModule((char *)modname);
422 if(module) {//don't load it again
423 module->AddRef();
424 return module->getInstanceHandle();
425 }
426
427 Win32PeLdrDll *peldrDll = new Win32PeLdrDll((char *)modname);
428 if(peldrDll == NULL)
429 return(0);
430
431 peldrDll->init(0);
432 if(peldrDll->getError() != NO_ERROR) {
433 dprintf(("LoadLibary %s failed (::init)\n", lpszLibFile));
434 delete(peldrDll);
435 return(0);
436 }
437 if(dwFlags & DONT_RESOLVE_DLL_REFERENCES) {
438 peldrDll->setNoEntryCalls();
439 }
440
441 if(peldrDll->attachProcess() == FALSE) {
442 dprintf(("LoadLibary %s failed (::attachProcess)\n", lpszLibFile));
443 delete(peldrDll);
444 return(0);
445 }
446 peldrDll->AddRef();
447 return peldrDll->getInstanceHandle();
448 }
449 else return(0);
450}
451//******************************************************************************
452//******************************************************************************
453HINSTANCE16 WIN32API LoadLibrary16(LPCTSTR lpszLibFile)
454{
455 dprintf(("ERROR: LoadLibrary16 %s, not implemented", lpszLibFile));
456 return 0;
457}
458//******************************************************************************
459//******************************************************************************
460VOID WIN32API FreeLibrary16(HINSTANCE16 hinstance)
461{
462 dprintf(("ERROR: FreeLibrary16 %x, not implemented", hinstance));
463}
464//******************************************************************************
465//******************************************************************************
466FARPROC WIN32API GetProcAddress16(HMODULE hModule, LPCSTR lpszProc)
467{
468 dprintf(("ERROR: GetProcAddress16 %x %x, not implemented", hModule, lpszProc));
469 return 0;
470}
471//******************************************************************************
472//******************************************************************************
473HINSTANCE WIN32API LoadLibraryA(LPCTSTR lpszLibFile)
474{
475 HINSTANCE hDll;
476
477 dprintf(("KERNEL32: LoadLibraryA(%s)\n",
478 lpszLibFile));
479 dprintf(("KERNEL32: LoadLibrary %x FS = %x\n", GetCurrentThreadId(), GetFS()));
480
481 hDll = iLoadLibraryA(lpszLibFile, 0);
482 if (hDll == 0)
483 {
484 char * pszName;
485
486 // remove path from the image name
487 pszName = strrchr((char *)lpszLibFile,
488 '\\');
489 if (pszName != NULL)
490 {
491 pszName++; // skip backslash
492
493 // now try again without fully qualified path
494 hDll = iLoadLibraryA(pszName, 0);
495 }
496 }
497
498 return hDll;
499}
500//******************************************************************************
501//******************************************************************************
502HINSTANCE WIN32API LoadLibraryExA(LPCTSTR lpszLibFile, HANDLE hFile, DWORD dwFlags)
503{
504 HINSTANCE hDll;
505
506 dprintf(("KERNEL32: LoadLibraryExA %s (%X)\n", lpszLibFile, dwFlags));
507 hDll = iLoadLibraryA(lpszLibFile, dwFlags);
508 if (hDll == 0)
509 {
510 char * pszName;
511
512 // remove path from the image name
513 pszName = strrchr((char *)lpszLibFile,
514 '\\');
515 if (pszName != NULL)
516 {
517 pszName++; // skip backslash
518
519 // now try again without fully qualified path
520 hDll = iLoadLibraryA(pszName, dwFlags);
521 }
522 }
523
524 return hDll;
525}
526//******************************************************************************
527//******************************************************************************
528HINSTANCE WIN32API LoadLibraryW(LPCWSTR lpModule)
529{
530 char *asciimodule;
531 HINSTANCE rc;
532
533 asciimodule = UnicodeToAsciiString((LPWSTR)lpModule);
534 dprintf(("KERNEL32: OS2LoadLibraryW %s\n", asciimodule));
535 rc = LoadLibraryA(asciimodule);
536 free(asciimodule);
537 return(rc);
538}
539//******************************************************************************
540//******************************************************************************
541HINSTANCE WIN32API LoadLibraryExW(LPCWSTR lpModule, HANDLE hFile, DWORD dwFlags)
542{
543 char *asciimodule;
544 HINSTANCE rc;
545
546 asciimodule = UnicodeToAsciiString((LPWSTR)lpModule);
547 dprintf(("KERNEL32: OS2LoadLibraryExW %s (%d)\n", asciimodule, dwFlags));
548 rc = LoadLibraryExA(asciimodule, hFile, dwFlags);
549 free(asciimodule);
550 return(rc);
551}
552//******************************************************************************
553//******************************************************************************
554LPCSTR WIN32API GetCommandLineA()
555{
556 LPTSTR cmdline = NULL;
557
558 if(WinExe) {
559 cmdline = WinExe->getCommandLine();
560 }
561 if(cmdline == NULL) //not used for converted exes
562 cmdline = O32_GetCommandLine();
563
564 dprintf(("KERNEL32: GetCommandLine %s\n", cmdline));
565 dprintf(("KERNEL32: FS = %x\n", GetFS()));
566 return(cmdline);
567}
568//******************************************************************************
569//******************************************************************************
570LPCWSTR WIN32API GetCommandLineW(void)
571{
572 static WCHAR *UnicodeCmdLine = NULL;
573 char *asciicmdline = NULL;
574
575 dprintf(("KERNEL32: FS = %x\n", GetFS()));
576
577 if(UnicodeCmdLine)
578 return(UnicodeCmdLine); //already called before
579
580 if(WinExe) {
581 asciicmdline = WinExe->getCommandLine();
582 }
583 if(asciicmdline == NULL) //not used for converted exes
584 asciicmdline = O32_GetCommandLine();
585
586 if(asciicmdline) {
587 UnicodeCmdLine = (WCHAR *)malloc(strlen(asciicmdline)*2 + 2);
588 AsciiToUnicode(asciicmdline, UnicodeCmdLine);
589 dprintf(("KERNEL32: OS2GetCommandLineW: %s\n", asciicmdline));
590 return(UnicodeCmdLine);
591 }
592 dprintf(("KERNEL32: OS2GetCommandLineW: asciicmdline == NULL\n"));
593 return NULL;
594}
595//******************************************************************************
596//******************************************************************************
597DWORD WIN32API GetModuleFileNameA(HMODULE hinstModule, LPTSTR lpszPath, DWORD cchPath)
598{
599 DWORD rc;
600 Win32ImageBase *module;
601 char *fpath = NULL;
602
603 dprintf(("GetModuleFileName %X", hinstModule));
604 if(hinstModule == 0 || hinstModule == -1 || (WinExe && hinstModule == WinExe->getInstanceHandle())) {
605 module = (Win32ImageBase *)WinExe;
606 }
607 else {
608 module = (Win32ImageBase *)Win32DllBase::findModule(hinstModule);
609 }
610
611 if(module) {
612 fpath = module->getFullPath();
613 }
614 if(fpath) {
615 //SvL: 13-9-98: +1
616 rc = min(strlen(fpath)+1, cchPath);
617 strncpy(lpszPath, fpath, rc);
618 }
619 else rc = O32_GetModuleFileName(hinstModule, lpszPath, cchPath);
620
621 if(rc) {
622 dprintf(("KERNEL32: GetModuleFileName %s %d\n", lpszPath, hinstModule));
623 }
624 return(rc);
625}
626//******************************************************************************
627//******************************************************************************
628DWORD WIN32API GetModuleFileNameW(HMODULE hModule, LPWSTR lpFileName, DWORD nSize)
629{
630 char *asciifilename = (char *)malloc(nSize+1);
631 DWORD rc;
632
633 dprintf(("KERNEL32: OSLibGetModuleFileNameW\n"));
634 rc = GetModuleFileNameA(hModule, asciifilename, nSize);
635 if(rc) AsciiToUnicode(asciifilename, lpFileName);
636 free(asciifilename);
637 return(rc);
638}
639//******************************************************************************
640//NOTE: GetModuleHandleA does NOT support files with multiple dots (i.e.
641// very.weird.exe)
642//******************************************************************************
643HANDLE WIN32API GetModuleHandleA(LPCTSTR lpszModule)
644{
645 HANDLE hMod;
646 Win32DllBase *windll;
647 char szModule[CCHMAXPATH];
648 BOOL fDllModule = FALSE;
649
650 if(lpszModule == NULL) {
651 if(WinExe)
652 hMod = WinExe->getInstanceHandle();
653 else hMod = -1;
654 }
655 else {
656 strcpy(szModule, OSLibStripPath((char *)lpszModule));
657 strupr(szModule);
658 if(strstr(szModule, ".DLL")) {
659 fDllModule = TRUE;
660 }
661 else {
662 if(!strstr(szModule, ".")) {
663 //if there's no extension or trainling dot, we
664 //assume it's a dll (see Win32 SDK docs)
665 fDllModule = TRUE;
666 }
667 }
668 char *dot = strstr(szModule, ".");
669 if(dot)
670 *dot = 0;
671
672 if(!fDllModule && WinExe && !strcmpi(szModule, WinExe->getModuleName())) {
673 hMod = WinExe->getInstanceHandle();
674 }
675 else {
676 windll = Win32DllBase::findModule(szModule);
677 if(windll) {
678 hMod = windll->getInstanceHandle();
679 }
680 else hMod = OSLibiGetModuleHandleA((char *)lpszModule);
681 }
682 }
683
684 dprintf(("KERNEL32: GetModuleHandle %s returned %X\n", lpszModule, hMod));
685 return(hMod);
686}
687//******************************************************************************
688//******************************************************************************
689HMODULE WIN32API GetModuleHandleW(LPCWSTR arg1)
690{
691 HMODULE rc;
692 char *astring;
693
694 astring = UnicodeToAsciiString((LPWSTR)arg1);
695 rc = GetModuleHandleA(astring);
696 dprintf(("KERNEL32: OS2GetModuleHandleW %s returned %X\n", astring, rc));
697 FreeAsciiString(astring);
698 return(rc);
699}
700//******************************************************************************
701//******************************************************************************
702BOOL WINAPI CreateProcessA( LPCSTR lpApplicationName, LPSTR lpCommandLine,
703 LPSECURITY_ATTRIBUTES lpProcessAttributes,
704 LPSECURITY_ATTRIBUTES lpThreadAttributes,
705 BOOL bInheritHandles, DWORD dwCreationFlags,
706 LPVOID lpEnvironment, LPCSTR lpCurrentDirectory,
707 LPSTARTUPINFOA lpStartupInfo,
708 LPPROCESS_INFORMATION lpProcessInfo )
709{
710 THDB *pThreadDB = (THDB*)GetThreadTHDB();
711 char *cmdline = NULL;
712 BOOL rc;
713
714 dprintf(("KERNEL32: CreateProcessA %s cline:%s inherit:%d cFlags:%x Env:%x CurDir:%s StartupFlags:%x\n",
715 lpApplicationName, lpCommandLine, bInheritHandles, dwCreationFlags,
716 lpEnvironment, lpCurrentDirectory, lpStartupInfo));
717
718 // open32 does not support DEBUG_ONLY_THIS_PROCESS
719 if(dwCreationFlags & DEBUG_ONLY_THIS_PROCESS)
720 dwCreationFlags |= DEBUG_PROCESS;
721
722 if(O32_CreateProcess(lpApplicationName, lpCommandLine, lpProcessAttributes,
723 lpThreadAttributes, bInheritHandles, dwCreationFlags,
724 lpEnvironment, lpCurrentDirectory, lpStartupInfo,
725 lpProcessInfo) == TRUE)
726 {
727 if (dwCreationFlags & DEBUG_PROCESS && pThreadDB != NULL)
728 {
729 if(pThreadDB->pidDebuggee != 0)
730 {
731 // TODO: handle this
732 dprintf(("KERNEL32: CreateProcess ERROR: This thread is already a debugger\n"));
733 }
734 else
735 {
736 pThreadDB->pidDebuggee = lpProcessInfo->dwProcessId;
737 OSLibStartDebugger((ULONG*)&pThreadDB->pidDebuggee);
738 }
739 }
740 else pThreadDB->pidDebuggee = 0;
741
742 return(TRUE);
743 }
744 //probably a win32 exe, so run it in the pe loader
745 if(lpApplicationName) {
746 if(lpCommandLine) {
747 //skip exe name in lpCommandLine
748 while(*lpCommandLine != 0 && *lpCommandLine != ' ')
749 lpCommandLine++;
750
751 if(*lpCommandLine != 0) {
752 lpCommandLine++;
753 }
754 cmdline = (char *)malloc(strlen(lpApplicationName)+strlen(lpCommandLine) + 16);
755 sprintf(cmdline, "PE.EXE %s %s", lpApplicationName, lpCommandLine);
756 }
757 else {
758 cmdline = (char *)malloc(strlen(lpApplicationName) + 16);
759 sprintf(cmdline, "PE.EXE %s", lpApplicationName);
760 }
761 }
762 else {
763 cmdline = (char *)malloc(strlen(lpCommandLine) + 16);
764 sprintf(cmdline, "PE.EXE %s", lpCommandLine);
765 }
766 dprintf(("KERNEL32: CreateProcess %s\n", cmdline));
767 rc = O32_CreateProcess("PE.EXE", (LPCSTR)cmdline,lpProcessAttributes,
768 lpThreadAttributes, bInheritHandles, dwCreationFlags,
769 lpEnvironment, lpCurrentDirectory, lpStartupInfo,
770 lpProcessInfo);
771 if(rc == TRUE) {
772 if (dwCreationFlags & DEBUG_PROCESS && pThreadDB != NULL)
773 {
774 if(pThreadDB->pidDebuggee != 0)
775 {
776 // TODO: handle this
777 dprintf(("KERNEL32: CreateProcess ERROR: This thread is already a debugger\n"));
778 }
779 else
780 {
781 pThreadDB->pidDebuggee = lpProcessInfo->dwProcessId;
782 OSLibStartDebugger((ULONG*)&pThreadDB->pidDebuggee);
783 }
784 }
785 else
786 pThreadDB->pidDebuggee = 0;
787 }
788 if(cmdline)
789 free(cmdline);
790
791 if(lpProcessInfo)
792 dprintf(("KERNEL32: CreateProcess returned %d hPro:%x hThr:%x pid:%x tid:%x\n",
793 rc, lpProcessInfo->hProcess, lpProcessInfo->hThread,
794 lpProcessInfo->dwProcessId,lpProcessInfo->dwThreadId));
795 else
796 dprintf(("KERNEL32: CreateProcess returned %d\n", rc));
797 return(rc);
798}
799//******************************************************************************
800//******************************************************************************
801BOOL WIN32API CreateProcessW(LPCWSTR lpApplicationName, LPWSTR lpCommandLine,
802 PSECURITY_ATTRIBUTES lpProcessAttributes,
803 PSECURITY_ATTRIBUTES lpThreadAttributes,
804 BOOL bInheritHandles, DWORD dwCreationFlags,
805 LPVOID lpEnvironment,
806 LPCWSTR lpCurrentDirectory, LPSTARTUPINFOW lpStartupInfo,
807 LPPROCESS_INFORMATION lpProcessInfo)
808{
809 BOOL rc;
810 char *astring1 = 0, *astring2 = 0, *astring3 = 0;
811
812 dprintf(("KERNEL32: CreateProcessW"));
813 if(lpApplicationName)
814 astring1 = UnicodeToAsciiString((LPWSTR)lpApplicationName);
815 if(lpCommandLine)
816 astring2 = UnicodeToAsciiString(lpCommandLine);
817 if(lpCurrentDirectory)
818 astring3 = UnicodeToAsciiString((LPWSTR)lpCurrentDirectory);
819 rc = CreateProcessA(astring1, astring2, lpProcessAttributes, lpThreadAttributes,
820 bInheritHandles, dwCreationFlags, lpEnvironment,
821 astring3, (LPSTARTUPINFOA)lpStartupInfo,
822 lpProcessInfo);
823 if(astring3) FreeAsciiString(astring3);
824 if(astring2) FreeAsciiString(astring2);
825 if(astring1) FreeAsciiString(astring1);
826 return(rc);
827}
828//******************************************************************************
829//******************************************************************************
830HINSTANCE WIN32API WinExec(LPCSTR lpCmdLine, UINT nCmdShow)
831{
832 STARTUPINFOA startinfo = {0};
833 PROCESS_INFORMATION procinfo;
834
835 dprintf(("KERNEL32: WinExec %s\n", lpCmdLine));
836 startinfo.dwFlags = nCmdShow;
837 if(CreateProcessA(NULL, (LPSTR)lpCmdLine, NULL, NULL, FALSE, 0, NULL, NULL,
838 &startinfo, &procinfo) == FALSE)
839 {
840 return 0;
841 }
842 return procinfo.hProcess; //correct?
843}
844//******************************************************************************
845//******************************************************************************
846FARPROC WIN32API GetProcAddress(HMODULE hModule, LPCSTR lpszProc)
847{
848 Win32ImageBase *winmod;
849 FARPROC proc;
850 ULONG ulAPIOrdinal;
851
852 if(hModule == 0 || hModule == -1 || (WinExe && hModule == WinExe->getInstanceHandle())) {
853 winmod = WinExe;
854 }
855 else winmod = (Win32ImageBase *)Win32DllBase::findModule((HINSTANCE)hModule);
856
857 if(winmod) {
858 ulAPIOrdinal = (ULONG)lpszProc;
859 if (ulAPIOrdinal <= 0x0000FFFF) {
860 proc = (FARPROC)winmod->getApi((int)ulAPIOrdinal);
861 }
862 else proc = (FARPROC)winmod->getApi((char *)lpszProc);
863 if(proc == 0) {
864 SetLastError(ERROR_PROC_NOT_FOUND);
865 }
866 return proc;
867 }
868 proc = O32_GetProcAddress(hModule, lpszProc);
869 if(HIWORD(lpszProc))
870 dprintf(("KERNEL32: GetProcAddress %s from %X returned %X\n", lpszProc, hModule, proc));
871 else dprintf(("KERNEL32: GetProcAddress %x from %X returned %X\n", lpszProc, hModule, proc));
872 return(proc);
873}
874//******************************************************************************
875//Retrieve the version
876//******************************************************************************
877BOOL SYSTEM GetVersionStruct(char *lpszModName, char *verstruct, ULONG bufLength)
878{
879 Win32ImageBase *winimage;
880 Win32PeLdrRsrcImg *rsrcimg;
881
882 dprintf(("GetVersionStruct of module %s", lpszModName));
883 if(WinExe && !stricmp(WinExe->getFullPath(), lpszModName)) {
884 winimage = (Win32ImageBase *)WinExe;
885 }
886 else {
887 winimage = (Win32ImageBase *)Win32DllBase::findModule(lpszModName);
888 if(winimage == NULL)
889 {
890 char modname[CCHMAXPATH];
891
892 strcpy(modname, lpszModName);
893 //rename dll if necessary (i.e. OLE32 -> OLE32OS2)
894 Win32DllBase::renameDll(modname);
895
896 if(Win32ImageBase::isPEImage(modname) == FALSE)
897 {
898 HINSTANCE hInstance;
899
900 //must be an LX dll, just load it (app will probably load it anyway)
901 hInstance = LoadLibraryA(modname);
902 if(hInstance == 0)
903 return 0;
904 winimage = (Win32ImageBase *)Win32DllBase::findModule(hInstance);
905 if(winimage) {
906 return winimage->getVersionStruct(verstruct, bufLength);
907 }
908 return 0;
909 }
910 //SvL: Try to load it
911 rsrcimg = new Win32PeLdrRsrcImg(modname);
912 if(rsrcimg == NULL)
913 return 0;
914
915 rsrcimg->init(0);
916 if(rsrcimg->getError() != NO_ERROR)
917 {
918 dprintf(("GetVersionStruct can't load %s\n", modname));
919 delete rsrcimg;
920 return(FALSE);
921 }
922 BOOL rc = rsrcimg->getVersionStruct(verstruct, bufLength);
923 delete rsrcimg;
924 return rc;
925 }
926 }
927 return winimage->getVersionStruct(verstruct, bufLength);
928}
929//******************************************************************************
930//******************************************************************************
931ULONG SYSTEM GetVersionSize(char *lpszModName)
932{
933 Win32ImageBase *winimage;
934 Win32PeLdrRsrcImg *rsrcimg;
935
936 dprintf(("GetVersionSize of %s\n", lpszModName));
937
938 if(WinExe && !stricmp(WinExe->getFullPath(), lpszModName)) {
939 winimage = (Win32ImageBase *)WinExe;
940 }
941 else {
942 winimage = (Win32ImageBase *)Win32DllBase::findModule(lpszModName);
943 if(winimage == NULL)
944 {
945 char modname[CCHMAXPATH];
946
947 strcpy(modname, lpszModName);
948 //rename dll if necessary (i.e. OLE32 -> OLE32OS2)
949 Win32DllBase::renameDll(modname);
950
951 if(Win32ImageBase::isPEImage(modname) == FALSE)
952 {
953 HINSTANCE hInstance;
954
955 //must be an LX dll, just load it (app will probably load it anyway)
956 hInstance = LoadLibraryA(modname);
957 if(hInstance == 0)
958 return 0;
959 winimage = (Win32ImageBase *)Win32DllBase::findModule(hInstance);
960 if(winimage) {
961 return winimage->getVersionSize();
962 }
963 return 0;
964 }
965
966 //SvL: Try to load it
967 rsrcimg = new Win32PeLdrRsrcImg(modname);
968 if(rsrcimg == NULL)
969 return 0;
970
971 rsrcimg->init(0);
972 if(rsrcimg->getError() != NO_ERROR)
973 {
974 dprintf(("GetVersionSize can't load %s\n", modname));
975 delete rsrcimg;
976 return(FALSE);
977 }
978 int size = rsrcimg->getVersionSize();
979 delete rsrcimg;
980 return size;
981 }
982 }
983 return winimage->getVersionSize();
984}
985//******************************************************************************
986//TODO:What does this do exactly??
987//******************************************************************************
988ODINFUNCTION1(BOOL,DisableThreadLibraryCalls,HMODULE,hModule)
989{
990 Win32DllBase *winmod;
991 FARPROC proc;
992 ULONG ulAPIOrdinal;
993
994 winmod = Win32DllBase::findModule((HINSTANCE)hModule);
995 if(winmod)
996 {
997 // don't call ATTACH/DETACH thread functions in DLL
998 winmod->setThreadLibraryCalls(FALSE);
999 return TRUE;
1000 }
1001 else
1002 {
1003 // raise error condition
1004 SetLastError(ERROR_INVALID_HANDLE);
1005 return FALSE;
1006 }
1007}
1008//******************************************************************************
1009//******************************************************************************
Note: See TracBrowser for help on using the repository browser.