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

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

changes for OpenThreadToken

File size: 32.5 KB
Line 
1/* $Id: wprocess.cpp,v 1.65 2000-01-05 19:39:57 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->getCommandLine();
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 asciicmdline = WinExe->getCommandLine();
600 }
601 if(asciicmdline == NULL) //not used for converted exes
602 asciicmdline = O32_GetCommandLine();
603
604 if(asciicmdline) {
605 UnicodeCmdLine = (WCHAR *)malloc(strlen(asciicmdline)*2 + 2);
606 AsciiToUnicode(asciicmdline, UnicodeCmdLine);
607 dprintf(("KERNEL32: OS2GetCommandLineW: %s\n", asciicmdline));
608 return(UnicodeCmdLine);
609 }
610 dprintf(("KERNEL32: OS2GetCommandLineW: asciicmdline == NULL\n"));
611 return NULL;
612}
613//******************************************************************************
614//******************************************************************************
615DWORD WIN32API GetModuleFileNameA(HMODULE hinstModule, LPTSTR lpszPath, DWORD cchPath)
616{
617 DWORD rc;
618 Win32ImageBase *module;
619 char *fpath = NULL;
620
621 dprintf(("GetModuleFileName %X", hinstModule));
622 if(hinstModule == 0 || hinstModule == -1 || (WinExe && hinstModule == WinExe->getInstanceHandle())) {
623 module = (Win32ImageBase *)WinExe;
624 }
625 else {
626 module = (Win32ImageBase *)Win32DllBase::findModule(hinstModule);
627 }
628
629 if(module) {
630 fpath = module->getFullPath();
631 }
632 if(fpath) {
633 //SvL: 13-9-98: +1
634 rc = min(strlen(fpath)+1, cchPath);
635 strncpy(lpszPath, fpath, rc);
636 }
637 else rc = O32_GetModuleFileName(hinstModule, lpszPath, cchPath);
638
639 if(rc) {
640 dprintf(("KERNEL32: GetModuleFileName %s %d\n", lpszPath, hinstModule));
641 }
642 return(rc);
643}
644//******************************************************************************
645//******************************************************************************
646DWORD WIN32API GetModuleFileNameW(HMODULE hModule, LPWSTR lpFileName, DWORD nSize)
647{
648 char *asciifilename = (char *)malloc(nSize+1);
649 DWORD rc;
650
651 dprintf(("KERNEL32: OSLibGetModuleFileNameW\n"));
652 rc = GetModuleFileNameA(hModule, asciifilename, nSize);
653 if(rc) AsciiToUnicode(asciifilename, lpFileName);
654 free(asciifilename);
655 return(rc);
656}
657//******************************************************************************
658//NOTE: GetModuleHandleA does NOT support files with multiple dots (i.e.
659// very.weird.exe)
660//******************************************************************************
661HANDLE WIN32API GetModuleHandleA(LPCTSTR lpszModule)
662{
663 HANDLE hMod;
664 Win32DllBase *windll;
665 char szModule[CCHMAXPATH];
666 BOOL fDllModule = FALSE;
667
668 if(lpszModule == NULL) {
669 if(WinExe)
670 hMod = WinExe->getInstanceHandle();
671 else hMod = -1;
672 }
673 else {
674 strcpy(szModule, OSLibStripPath((char *)lpszModule));
675 strupr(szModule);
676 if(strstr(szModule, ".DLL")) {
677 fDllModule = TRUE;
678 }
679 else {
680 if(!strstr(szModule, ".")) {
681 //if there's no extension or trainling dot, we
682 //assume it's a dll (see Win32 SDK docs)
683 fDllModule = TRUE;
684 }
685 }
686 char *dot = strstr(szModule, ".");
687 if(dot)
688 *dot = 0;
689
690 if(!fDllModule && WinExe && !strcmpi(szModule, WinExe->getModuleName())) {
691 hMod = WinExe->getInstanceHandle();
692 }
693 else {
694 windll = Win32DllBase::findModule(szModule);
695 if(windll) {
696 hMod = windll->getInstanceHandle();
697 }
698 else hMod = OSLibiGetModuleHandleA((char *)lpszModule);
699 }
700 }
701
702 dprintf(("KERNEL32: GetModuleHandle %s returned %X\n", lpszModule, hMod));
703 return(hMod);
704}
705//******************************************************************************
706//******************************************************************************
707HMODULE WIN32API GetModuleHandleW(LPCWSTR arg1)
708{
709 HMODULE rc;
710 char *astring;
711
712 astring = UnicodeToAsciiString((LPWSTR)arg1);
713 rc = GetModuleHandleA(astring);
714 dprintf(("KERNEL32: OS2GetModuleHandleW %s returned %X\n", astring, rc));
715 FreeAsciiString(astring);
716 return(rc);
717}
718//******************************************************************************
719//******************************************************************************
720BOOL WINAPI CreateProcessA( LPCSTR lpApplicationName, LPSTR lpCommandLine,
721 LPSECURITY_ATTRIBUTES lpProcessAttributes,
722 LPSECURITY_ATTRIBUTES lpThreadAttributes,
723 BOOL bInheritHandles, DWORD dwCreationFlags,
724 LPVOID lpEnvironment, LPCSTR lpCurrentDirectory,
725 LPSTARTUPINFOA lpStartupInfo,
726 LPPROCESS_INFORMATION lpProcessInfo )
727{
728 THDB *pThreadDB = (THDB*)GetThreadTHDB();
729 char *cmdline = NULL;
730 BOOL rc;
731
732 dprintf(("KERNEL32: CreateProcessA %s cline:%s inherit:%d cFlags:%x Env:%x CurDir:%s StartupFlags:%x\n",
733 lpApplicationName, lpCommandLine, bInheritHandles, dwCreationFlags,
734 lpEnvironment, lpCurrentDirectory, lpStartupInfo));
735
736 // open32 does not support DEBUG_ONLY_THIS_PROCESS
737 if(dwCreationFlags & DEBUG_ONLY_THIS_PROCESS)
738 dwCreationFlags |= DEBUG_PROCESS;
739
740 if(O32_CreateProcess(lpApplicationName, lpCommandLine, lpProcessAttributes,
741 lpThreadAttributes, bInheritHandles, dwCreationFlags,
742 lpEnvironment, lpCurrentDirectory, lpStartupInfo,
743 lpProcessInfo) == TRUE)
744 {
745 if (dwCreationFlags & DEBUG_PROCESS && pThreadDB != NULL)
746 {
747 if(pThreadDB->pidDebuggee != 0)
748 {
749 // TODO: handle this
750 dprintf(("KERNEL32: CreateProcess ERROR: This thread is already a debugger\n"));
751 }
752 else
753 {
754 pThreadDB->pidDebuggee = lpProcessInfo->dwProcessId;
755 OSLibStartDebugger((ULONG*)&pThreadDB->pidDebuggee);
756 }
757 }
758 else pThreadDB->pidDebuggee = 0;
759
760 return(TRUE);
761 }
762 //probably a win32 exe, so run it in the pe loader
763 if(lpApplicationName) {
764 if(lpCommandLine) {
765 //skip exe name in lpCommandLine
766 while(*lpCommandLine != 0 && *lpCommandLine != ' ')
767 lpCommandLine++;
768
769 if(*lpCommandLine != 0) {
770 lpCommandLine++;
771 }
772 cmdline = (char *)malloc(strlen(lpApplicationName)+strlen(lpCommandLine) + 16);
773 sprintf(cmdline, "PE.EXE %s %s", lpApplicationName, lpCommandLine);
774 }
775 else {
776 cmdline = (char *)malloc(strlen(lpApplicationName) + 16);
777 sprintf(cmdline, "PE.EXE %s", lpApplicationName);
778 }
779 }
780 else {
781 cmdline = (char *)malloc(strlen(lpCommandLine) + 16);
782 sprintf(cmdline, "PE.EXE %s", lpCommandLine);
783 }
784 dprintf(("KERNEL32: CreateProcess %s\n", cmdline));
785 rc = O32_CreateProcess("PE.EXE", (LPCSTR)cmdline,lpProcessAttributes,
786 lpThreadAttributes, bInheritHandles, dwCreationFlags,
787 lpEnvironment, lpCurrentDirectory, lpStartupInfo,
788 lpProcessInfo);
789 if(rc == TRUE) {
790 if (dwCreationFlags & DEBUG_PROCESS && pThreadDB != NULL)
791 {
792 if(pThreadDB->pidDebuggee != 0)
793 {
794 // TODO: handle this
795 dprintf(("KERNEL32: CreateProcess ERROR: This thread is already a debugger\n"));
796 }
797 else
798 {
799 pThreadDB->pidDebuggee = lpProcessInfo->dwProcessId;
800 OSLibStartDebugger((ULONG*)&pThreadDB->pidDebuggee);
801 }
802 }
803 else
804 pThreadDB->pidDebuggee = 0;
805 }
806 if(cmdline)
807 free(cmdline);
808
809 if(lpProcessInfo)
810 dprintf(("KERNEL32: CreateProcess returned %d hPro:%x hThr:%x pid:%x tid:%x\n",
811 rc, lpProcessInfo->hProcess, lpProcessInfo->hThread,
812 lpProcessInfo->dwProcessId,lpProcessInfo->dwThreadId));
813 else
814 dprintf(("KERNEL32: CreateProcess returned %d\n", rc));
815 return(rc);
816}
817//******************************************************************************
818//******************************************************************************
819BOOL WIN32API CreateProcessW(LPCWSTR lpApplicationName, LPWSTR lpCommandLine,
820 PSECURITY_ATTRIBUTES lpProcessAttributes,
821 PSECURITY_ATTRIBUTES lpThreadAttributes,
822 BOOL bInheritHandles, DWORD dwCreationFlags,
823 LPVOID lpEnvironment,
824 LPCWSTR lpCurrentDirectory, LPSTARTUPINFOW lpStartupInfo,
825 LPPROCESS_INFORMATION lpProcessInfo)
826{
827 BOOL rc;
828 char *astring1 = 0, *astring2 = 0, *astring3 = 0;
829
830 dprintf(("KERNEL32: CreateProcessW"));
831 if(lpApplicationName)
832 astring1 = UnicodeToAsciiString((LPWSTR)lpApplicationName);
833 if(lpCommandLine)
834 astring2 = UnicodeToAsciiString(lpCommandLine);
835 if(lpCurrentDirectory)
836 astring3 = UnicodeToAsciiString((LPWSTR)lpCurrentDirectory);
837 rc = CreateProcessA(astring1, astring2, lpProcessAttributes, lpThreadAttributes,
838 bInheritHandles, dwCreationFlags, lpEnvironment,
839 astring3, (LPSTARTUPINFOA)lpStartupInfo,
840 lpProcessInfo);
841 if(astring3) FreeAsciiString(astring3);
842 if(astring2) FreeAsciiString(astring2);
843 if(astring1) FreeAsciiString(astring1);
844 return(rc);
845}
846//******************************************************************************
847//******************************************************************************
848HINSTANCE WIN32API WinExec(LPCSTR lpCmdLine, UINT nCmdShow)
849{
850 STARTUPINFOA startinfo = {0};
851 PROCESS_INFORMATION procinfo;
852
853 dprintf(("KERNEL32: WinExec %s\n", lpCmdLine));
854 startinfo.dwFlags = nCmdShow;
855 if(CreateProcessA(NULL, (LPSTR)lpCmdLine, NULL, NULL, FALSE, 0, NULL, NULL,
856 &startinfo, &procinfo) == FALSE)
857 {
858 return 0;
859 }
860 return procinfo.hProcess; //correct?
861}
862//******************************************************************************
863//******************************************************************************
864FARPROC WIN32API GetProcAddress(HMODULE hModule, LPCSTR lpszProc)
865{
866 Win32ImageBase *winmod;
867 FARPROC proc;
868 ULONG ulAPIOrdinal;
869
870 if(hModule == 0 || hModule == -1 || (WinExe && hModule == WinExe->getInstanceHandle())) {
871 winmod = WinExe;
872 }
873 else winmod = (Win32ImageBase *)Win32DllBase::findModule((HINSTANCE)hModule);
874
875 if(winmod) {
876 ulAPIOrdinal = (ULONG)lpszProc;
877 if (ulAPIOrdinal <= 0x0000FFFF) {
878 proc = (FARPROC)winmod->getApi((int)ulAPIOrdinal);
879 }
880 else proc = (FARPROC)winmod->getApi((char *)lpszProc);
881 if(proc == 0) {
882 SetLastError(ERROR_PROC_NOT_FOUND);
883 }
884 return proc;
885 }
886 proc = O32_GetProcAddress(hModule, lpszProc);
887 if(HIWORD(lpszProc))
888 dprintf(("KERNEL32: GetProcAddress %s from %X returned %X\n", lpszProc, hModule, proc));
889 else dprintf(("KERNEL32: GetProcAddress %x from %X returned %X\n", lpszProc, hModule, proc));
890 return(proc);
891}
892//******************************************************************************
893//Retrieve the version
894//******************************************************************************
895BOOL SYSTEM GetVersionStruct(char *lpszModName, char *verstruct, ULONG bufLength)
896{
897 Win32ImageBase *winimage;
898 Win32PeLdrRsrcImg *rsrcimg;
899
900 dprintf(("GetVersionStruct of module %s", lpszModName));
901 if(WinExe && !stricmp(WinExe->getFullPath(), lpszModName)) {
902 winimage = (Win32ImageBase *)WinExe;
903 }
904 else {
905 winimage = (Win32ImageBase *)Win32DllBase::findModule(lpszModName);
906 if(winimage == NULL)
907 {
908 char modname[CCHMAXPATH];
909
910 strcpy(modname, lpszModName);
911 //rename dll if necessary (i.e. OLE32 -> OLE32OS2)
912 Win32DllBase::renameDll(modname);
913
914 if(Win32ImageBase::isPEImage(modname) == FALSE)
915 {
916 HINSTANCE hInstance;
917
918 //must be an LX dll, just load it (app will probably load it anyway)
919 hInstance = LoadLibraryA(modname);
920 if(hInstance == 0)
921 return 0;
922 winimage = (Win32ImageBase *)Win32DllBase::findModule(hInstance);
923 if(winimage) {
924 return winimage->getVersionStruct(verstruct, bufLength);
925 }
926 return 0;
927 }
928 //SvL: Try to load it
929 rsrcimg = new Win32PeLdrRsrcImg(modname);
930 if(rsrcimg == NULL)
931 return 0;
932
933 rsrcimg->init(0);
934 if(rsrcimg->getError() != NO_ERROR)
935 {
936 dprintf(("GetVersionStruct can't load %s\n", modname));
937 delete rsrcimg;
938 return(FALSE);
939 }
940 BOOL rc = rsrcimg->getVersionStruct(verstruct, bufLength);
941 delete rsrcimg;
942 return rc;
943 }
944 }
945 return winimage->getVersionStruct(verstruct, bufLength);
946}
947//******************************************************************************
948//******************************************************************************
949ULONG SYSTEM GetVersionSize(char *lpszModName)
950{
951 Win32ImageBase *winimage;
952 Win32PeLdrRsrcImg *rsrcimg;
953
954 dprintf(("GetVersionSize of %s\n", lpszModName));
955
956 if(WinExe && !stricmp(WinExe->getFullPath(), lpszModName)) {
957 winimage = (Win32ImageBase *)WinExe;
958 }
959 else {
960 winimage = (Win32ImageBase *)Win32DllBase::findModule(lpszModName);
961 if(winimage == NULL)
962 {
963 char modname[CCHMAXPATH];
964
965 strcpy(modname, lpszModName);
966 //rename dll if necessary (i.e. OLE32 -> OLE32OS2)
967 Win32DllBase::renameDll(modname);
968
969 if(Win32ImageBase::isPEImage(modname) == FALSE)
970 {
971 HINSTANCE hInstance;
972
973 //must be an LX dll, just load it (app will probably load it anyway)
974 hInstance = LoadLibraryA(modname);
975 if(hInstance == 0)
976 return 0;
977 winimage = (Win32ImageBase *)Win32DllBase::findModule(hInstance);
978 if(winimage) {
979 return winimage->getVersionSize();
980 }
981 return 0;
982 }
983
984 //SvL: Try to load it
985 rsrcimg = new Win32PeLdrRsrcImg(modname);
986 if(rsrcimg == NULL)
987 return 0;
988
989 rsrcimg->init(0);
990 if(rsrcimg->getError() != NO_ERROR)
991 {
992 dprintf(("GetVersionSize can't load %s\n", modname));
993 delete rsrcimg;
994 return(FALSE);
995 }
996 int size = rsrcimg->getVersionSize();
997 delete rsrcimg;
998 return size;
999 }
1000 }
1001 return winimage->getVersionSize();
1002}
1003//******************************************************************************
1004//TODO:What does this do exactly??
1005//******************************************************************************
1006ODINFUNCTION1(BOOL,DisableThreadLibraryCalls,HMODULE,hModule)
1007{
1008 Win32DllBase *winmod;
1009 FARPROC proc;
1010 ULONG ulAPIOrdinal;
1011
1012 winmod = Win32DllBase::findModule((HINSTANCE)hModule);
1013 if(winmod)
1014 {
1015 // don't call ATTACH/DETACH thread functions in DLL
1016 winmod->setThreadLibraryCalls(FALSE);
1017 return TRUE;
1018 }
1019 else
1020 {
1021 // raise error condition
1022 SetLastError(ERROR_INVALID_HANDLE);
1023 return FALSE;
1024 }
1025}
1026//******************************************************************************
1027//******************************************************************************
Note: See TracBrowser for help on using the repository browser.