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

Last change on this file since 2984 was 2984, checked in by sandervl, 25 years ago

moved registry apis into kernel32 + cleanup

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