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

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

WinExec: Wait for process to block on input before returning

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