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

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

DosOpen (file handle error) & dll destruction bugfixes

File size: 32.9 KB
Line 
1/* $Id: wprocess.cpp,v 1.71 2000-03-04 19:52:37 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//******************************************************************************
346VOID WIN32API ExitProcess(DWORD exitcode)
347{
348 dprintf(("KERNEL32: ExitProcess %d\n", exitcode));
349 dprintf(("KERNEL32: ExitProcess FS = %x\n", GetFS()));
350
351 SetOS2ExceptionChain(-1);
352
353 if(WinExe) {
354 delete(WinExe);
355 WinExe = NULL;
356 }
357
358 //Note: Needs to be done after deleting WinExe (destruction of exe + dll objects)
359 //Flush and delete all open memory mapped files
360 Win32MemMap::deleteAll();
361
362 //Restore original OS/2 TIB selector
363 DestroyTIB();
364 SetExceptionChain((ULONG)-1);
365
366 //avoid crashes since win32 & OS/2 exception handler aren't identical
367 //(terminate process generates two exceptions)
368 /* @@@PH 1998/02/12 Added Console Support */
369 if (iConsoleIsActive())
370 iConsoleWaitClose();
371
372 O32_ExitProcess(exitcode);
373}
374//******************************************************************************
375//******************************************************************************
376BOOL WIN32API FreeLibrary(HINSTANCE hinstance)
377{
378 Win32DllBase *winmod;
379 BOOL rc;
380
381 winmod = Win32DllBase::findModule(hinstance);
382 if(winmod) {
383 dprintf(("FreeLibrary %s", winmod->getName()));
384 winmod->Release();
385 return(TRUE);
386 }
387 dprintf(("KERNEL32: FreeLibrary %s %X\n", OSLibGetDllName(hinstance), hinstance));
388
389 //TODO: Not thread safe
390 fFreeLibrary = TRUE; //ditch dll
391 rc = O32_FreeLibrary(hinstance);
392 fFreeLibrary = FALSE;
393 dprintf(("FreeLibrary returned %X\n", rc));
394 return(TRUE);
395}
396/******************************************************************************/
397/******************************************************************************/
398static HINSTANCE iLoadLibraryA(LPCTSTR lpszLibFile, DWORD dwFlags)
399{
400 char modname[CCHMAXPATH];
401 HINSTANCE hDll;
402 Win32DllBase *module;
403
404 module = Win32DllBase::findModule((LPSTR)lpszLibFile);
405 if(module) {
406 module->AddRef();
407 dprintf(("iLoadLibrary: found %s -> handle %x", lpszLibFile, module->getInstanceHandle()));
408 return module->getInstanceHandle();
409 }
410
411 strcpy(modname, lpszLibFile);
412 strupr(modname);
413 //rename dll if necessary (i.e. OLE32 -> OLE32OS2)
414 Win32DllBase::renameDll(modname);
415
416 hDll = O32_LoadLibrary(modname);
417 dprintf(("KERNEL32: iLoadLibraryA %s returned %X (%d)\n",
418 lpszLibFile,
419 hDll,
420 GetLastError()));
421 if(hDll)
422 {
423 return hDll; //converted dll or win32k took care of it
424 }
425
426 if(!strstr(modname, ".")) {
427 strcat(modname,".DLL");
428 }
429
430 if(Win32ImageBase::isPEImage((char *)modname))
431 {
432 module = Win32DllBase::findModule((char *)modname);
433 if(module) {//don't load it again
434 module->AddRef();
435 return module->getInstanceHandle();
436 }
437
438 Win32PeLdrDll *peldrDll = new Win32PeLdrDll((char *)modname);
439 if(peldrDll == NULL)
440 return(0);
441
442 peldrDll->init(0);
443 if(peldrDll->getError() != NO_ERROR) {
444 dprintf(("LoadLibary %s failed (::init)\n", lpszLibFile));
445 delete(peldrDll);
446 return(0);
447 }
448 if(dwFlags & DONT_RESOLVE_DLL_REFERENCES) {
449 peldrDll->setNoEntryCalls();
450 }
451 //Set flag so this dll doesn't get automatically deleted in ExitProcess
452 //(the first time); give application the chance to free it first
453 //If it's not freed, we delete it the 2nd time in ExitProcess
454 peldrDll->SetDynamicallyLoaded();
455
456 if(peldrDll->attachProcess() == FALSE) {
457 dprintf(("LoadLibary %s failed (::attachProcess)\n", lpszLibFile));
458 delete(peldrDll);
459 return(0);
460 }
461 peldrDll->AddRef();
462 return peldrDll->getInstanceHandle();
463 }
464 else return(0);
465}
466//******************************************************************************
467//******************************************************************************
468HINSTANCE16 WIN32API LoadLibrary16(LPCTSTR lpszLibFile)
469{
470 dprintf(("ERROR: LoadLibrary16 %s, not implemented", lpszLibFile));
471 return 0;
472}
473//******************************************************************************
474//******************************************************************************
475VOID WIN32API FreeLibrary16(HINSTANCE16 hinstance)
476{
477 dprintf(("ERROR: FreeLibrary16 %x, not implemented", hinstance));
478}
479//******************************************************************************
480//******************************************************************************
481FARPROC WIN32API GetProcAddress16(HMODULE hModule, LPCSTR lpszProc)
482{
483 dprintf(("ERROR: GetProcAddress16 %x %x, not implemented", hModule, lpszProc));
484 return 0;
485}
486//******************************************************************************
487//******************************************************************************
488HINSTANCE WIN32API LoadLibraryA(LPCTSTR lpszLibFile)
489{
490 HINSTANCE hDll;
491
492 dprintf(("KERNEL32: LoadLibraryA(%s)\n",
493 lpszLibFile));
494 dprintf(("KERNEL32: LoadLibrary %x FS = %x\n", GetCurrentThreadId(), GetFS()));
495
496 hDll = iLoadLibraryA(lpszLibFile, 0);
497 if (hDll == 0)
498 {
499 char * pszName;
500
501 // remove path from the image name
502 pszName = strrchr((char *)lpszLibFile,
503 '\\');
504 if (pszName != NULL)
505 {
506 pszName++; // skip backslash
507
508 // now try again without fully qualified path
509 hDll = iLoadLibraryA(pszName, 0);
510 }
511 }
512
513 return hDll;
514}
515//******************************************************************************
516//******************************************************************************
517HINSTANCE WIN32API LoadLibraryExA(LPCTSTR lpszLibFile, HANDLE hFile, DWORD dwFlags)
518{
519 HINSTANCE hDll;
520
521 dprintf(("KERNEL32: LoadLibraryExA %s (%X)\n", lpszLibFile, dwFlags));
522 hDll = iLoadLibraryA(lpszLibFile, dwFlags);
523 if (hDll == 0)
524 {
525 char * pszName;
526
527 // remove path from the image name
528 pszName = strrchr((char *)lpszLibFile,
529 '\\');
530 if (pszName != NULL)
531 {
532 pszName++; // skip backslash
533
534 // now try again without fully qualified path
535 hDll = iLoadLibraryA(pszName, dwFlags);
536 }
537 }
538
539 return hDll;
540}
541//******************************************************************************
542//******************************************************************************
543HINSTANCE WIN32API LoadLibraryW(LPCWSTR lpModule)
544{
545 char *asciimodule;
546 HINSTANCE rc;
547
548 asciimodule = UnicodeToAsciiString((LPWSTR)lpModule);
549 dprintf(("KERNEL32: OS2LoadLibraryW %s\n", asciimodule));
550 rc = LoadLibraryA(asciimodule);
551 free(asciimodule);
552 return(rc);
553}
554//******************************************************************************
555//******************************************************************************
556HINSTANCE WIN32API LoadLibraryExW(LPCWSTR lpModule, HANDLE hFile, DWORD dwFlags)
557{
558 char *asciimodule;
559 HINSTANCE rc;
560
561 asciimodule = UnicodeToAsciiString((LPWSTR)lpModule);
562 dprintf(("KERNEL32: OS2LoadLibraryExW %s (%d)\n", asciimodule, dwFlags));
563 rc = LoadLibraryExA(asciimodule, hFile, dwFlags);
564 free(asciimodule);
565 return(rc);
566}
567//******************************************************************************
568//******************************************************************************
569LPCSTR WIN32API GetCommandLineA()
570{
571 LPTSTR cmdline = NULL;
572
573 if(WinExe) {
574 cmdline = WinExe->getCommandLineA();
575 }
576 if(cmdline == NULL) //not used for converted exes
577 cmdline = O32_GetCommandLine();
578
579 dprintf(("KERNEL32: GetCommandLine %s\n", cmdline));
580 dprintf(("KERNEL32: FS = %x\n", GetFS()));
581 return(cmdline);
582}
583//******************************************************************************
584//******************************************************************************
585LPCWSTR WIN32API GetCommandLineW(void)
586{
587 static WCHAR *UnicodeCmdLine = NULL;
588 char *asciicmdline = NULL;
589
590 dprintf(("KERNEL32: FS = %x\n", GetFS()));
591
592 if(UnicodeCmdLine)
593 return(UnicodeCmdLine); //already called before
594
595 if(WinExe) {
596 if(WinExe->getCommandLineW())
597 return WinExe->getCommandLineW();
598 }
599 if(asciicmdline == NULL) //not used for converted exes
600 asciicmdline = O32_GetCommandLine();
601
602 if(asciicmdline) {
603 UnicodeCmdLine = (WCHAR *)malloc(strlen(asciicmdline)*2 + 2);
604 AsciiToUnicode(asciicmdline, UnicodeCmdLine);
605 dprintf(("KERNEL32: OS2GetCommandLineW: %s\n", asciicmdline));
606 return(UnicodeCmdLine);
607 }
608 dprintf(("KERNEL32: OS2GetCommandLineW: asciicmdline == NULL\n"));
609 return NULL;
610}
611//******************************************************************************
612//******************************************************************************
613DWORD WIN32API GetModuleFileNameA(HMODULE hinstModule, LPTSTR lpszPath, DWORD cchPath)
614{
615 DWORD rc;
616 Win32ImageBase *module;
617 char *fpath = NULL;
618
619 dprintf(("GetModuleFileName %X", hinstModule));
620 if(hinstModule == 0 || hinstModule == -1 || (WinExe && hinstModule == WinExe->getInstanceHandle())) {
621 module = (Win32ImageBase *)WinExe;
622 }
623 else {
624 module = (Win32ImageBase *)Win32DllBase::findModule(hinstModule);
625 }
626
627 if(module) {
628 fpath = module->getFullPath();
629 }
630 if(fpath) {
631 //SvL: 13-9-98: +1
632 rc = min(strlen(fpath)+1, cchPath);
633 strncpy(lpszPath, fpath, rc);
634 }
635 else rc = O32_GetModuleFileName(hinstModule, lpszPath, cchPath);
636
637 if(rc) {
638 dprintf(("KERNEL32: GetModuleFileName %s %d\n", lpszPath, hinstModule));
639 }
640 return(rc);
641}
642//******************************************************************************
643//******************************************************************************
644DWORD WIN32API GetModuleFileNameW(HMODULE hModule, LPWSTR lpFileName, DWORD nSize)
645{
646 char *asciifilename = (char *)malloc(nSize+1);
647 DWORD rc;
648
649 dprintf(("KERNEL32: OSLibGetModuleFileNameW\n"));
650 rc = GetModuleFileNameA(hModule, asciifilename, nSize);
651 if(rc) AsciiToUnicode(asciifilename, lpFileName);
652 free(asciifilename);
653 return(rc);
654}
655//******************************************************************************
656//NOTE: GetModuleHandleA does NOT support files with multiple dots (i.e.
657// very.weird.exe)
658//******************************************************************************
659HANDLE WIN32API GetModuleHandleA(LPCTSTR lpszModule)
660{
661 HANDLE hMod;
662 Win32DllBase *windll;
663 char szModule[CCHMAXPATH];
664 BOOL fDllModule = FALSE;
665
666 if(lpszModule == NULL) {
667 if(WinExe)
668 hMod = WinExe->getInstanceHandle();
669 else hMod = -1;
670 }
671 else {
672 strcpy(szModule, OSLibStripPath((char *)lpszModule));
673 strupr(szModule);
674 if(strstr(szModule, ".DLL")) {
675 fDllModule = TRUE;
676 }
677 else {
678 if(!strstr(szModule, ".")) {
679 //if there's no extension or trainling dot, we
680 //assume it's a dll (see Win32 SDK docs)
681 fDllModule = TRUE;
682 }
683 }
684 char *dot = strstr(szModule, ".");
685 if(dot)
686 *dot = 0;
687
688 if(!fDllModule && WinExe && !strcmpi(szModule, WinExe->getModuleName())) {
689 hMod = WinExe->getInstanceHandle();
690 }
691 else {
692 windll = Win32DllBase::findModule(szModule);
693 if(windll) {
694 hMod = windll->getInstanceHandle();
695 }
696 else hMod = OSLibiGetModuleHandleA((char *)lpszModule);
697 }
698 }
699
700 dprintf(("KERNEL32: GetModuleHandle %s returned %X\n", lpszModule, hMod));
701 return(hMod);
702}
703//******************************************************************************
704//******************************************************************************
705HMODULE WIN32API GetModuleHandleW(LPCWSTR arg1)
706{
707 HMODULE rc;
708 char *astring;
709
710 astring = UnicodeToAsciiString((LPWSTR)arg1);
711 rc = GetModuleHandleA(astring);
712 dprintf(("KERNEL32: OS2GetModuleHandleW %s returned %X\n", astring, rc));
713 FreeAsciiString(astring);
714 return(rc);
715}
716//******************************************************************************
717//******************************************************************************
718BOOL WINAPI CreateProcessA( LPCSTR lpApplicationName, LPSTR lpCommandLine,
719 LPSECURITY_ATTRIBUTES lpProcessAttributes,
720 LPSECURITY_ATTRIBUTES lpThreadAttributes,
721 BOOL bInheritHandles, DWORD dwCreationFlags,
722 LPVOID lpEnvironment, LPCSTR lpCurrentDirectory,
723 LPSTARTUPINFOA lpStartupInfo,
724 LPPROCESS_INFORMATION lpProcessInfo )
725{
726 THDB *pThreadDB = (THDB*)GetThreadTHDB();
727 char *cmdline = NULL;
728 BOOL rc;
729
730 dprintf(("KERNEL32: CreateProcessA %s cline:%s inherit:%d cFlags:%x Env:%x CurDir:%s StartupFlags:%x\n",
731 lpApplicationName, lpCommandLine, bInheritHandles, dwCreationFlags,
732 lpEnvironment, lpCurrentDirectory, lpStartupInfo));
733
734 // open32 does not support DEBUG_ONLY_THIS_PROCESS
735 if(dwCreationFlags & DEBUG_ONLY_THIS_PROCESS)
736 dwCreationFlags |= DEBUG_PROCESS;
737
738 if(O32_CreateProcess(lpApplicationName, lpCommandLine, lpProcessAttributes,
739 lpThreadAttributes, bInheritHandles, dwCreationFlags,
740 lpEnvironment, lpCurrentDirectory, lpStartupInfo,
741 lpProcessInfo) == TRUE)
742 {
743 if (dwCreationFlags & DEBUG_PROCESS && pThreadDB != NULL)
744 {
745 if(pThreadDB->pidDebuggee != 0)
746 {
747 // TODO: handle this
748 dprintf(("KERNEL32: CreateProcess ERROR: This thread is already a debugger\n"));
749 }
750 else
751 {
752 pThreadDB->pidDebuggee = lpProcessInfo->dwProcessId;
753 OSLibStartDebugger((ULONG*)&pThreadDB->pidDebuggee);
754 }
755 }
756 else pThreadDB->pidDebuggee = 0;
757
758 return(TRUE);
759 }
760 //probably a win32 exe, so run it in the pe loader
761 if(lpApplicationName) {
762 if(lpCommandLine) {
763 //skip exe name in lpCommandLine
764 while(*lpCommandLine != 0 && *lpCommandLine != ' ')
765 lpCommandLine++;
766
767 if(*lpCommandLine != 0) {
768 lpCommandLine++;
769 }
770 cmdline = (char *)malloc(strlen(lpApplicationName)+strlen(lpCommandLine) + 16);
771 sprintf(cmdline, "PE.EXE %s %s", lpApplicationName, lpCommandLine);
772 }
773 else {
774 cmdline = (char *)malloc(strlen(lpApplicationName) + 16);
775 sprintf(cmdline, "PE.EXE %s", lpApplicationName);
776 }
777 }
778 else {
779 cmdline = (char *)malloc(strlen(lpCommandLine) + 16);
780 sprintf(cmdline, "PE.EXE %s", lpCommandLine);
781 }
782 dprintf(("KERNEL32: CreateProcess %s\n", cmdline));
783 rc = O32_CreateProcess("PE.EXE", (LPCSTR)cmdline,lpProcessAttributes,
784 lpThreadAttributes, bInheritHandles, dwCreationFlags,
785 lpEnvironment, lpCurrentDirectory, lpStartupInfo,
786 lpProcessInfo);
787 if(rc == TRUE) {
788 if (dwCreationFlags & DEBUG_PROCESS && pThreadDB != NULL)
789 {
790 if(pThreadDB->pidDebuggee != 0)
791 {
792 // TODO: handle this
793 dprintf(("KERNEL32: CreateProcess ERROR: This thread is already a debugger\n"));
794 }
795 else
796 {
797 pThreadDB->pidDebuggee = lpProcessInfo->dwProcessId;
798 OSLibStartDebugger((ULONG*)&pThreadDB->pidDebuggee);
799 }
800 }
801 else
802 pThreadDB->pidDebuggee = 0;
803 }
804 if(cmdline)
805 free(cmdline);
806
807 if(lpProcessInfo)
808 dprintf(("KERNEL32: CreateProcess returned %d hPro:%x hThr:%x pid:%x tid:%x\n",
809 rc, lpProcessInfo->hProcess, lpProcessInfo->hThread,
810 lpProcessInfo->dwProcessId,lpProcessInfo->dwThreadId));
811 else
812 dprintf(("KERNEL32: CreateProcess returned %d\n", rc));
813 return(rc);
814}
815//******************************************************************************
816//******************************************************************************
817BOOL WIN32API CreateProcessW(LPCWSTR lpApplicationName, LPWSTR lpCommandLine,
818 PSECURITY_ATTRIBUTES lpProcessAttributes,
819 PSECURITY_ATTRIBUTES lpThreadAttributes,
820 BOOL bInheritHandles, DWORD dwCreationFlags,
821 LPVOID lpEnvironment,
822 LPCWSTR lpCurrentDirectory, LPSTARTUPINFOW lpStartupInfo,
823 LPPROCESS_INFORMATION lpProcessInfo)
824{
825 BOOL rc;
826 char *astring1 = 0, *astring2 = 0, *astring3 = 0;
827
828 dprintf(("KERNEL32: CreateProcessW"));
829 if(lpApplicationName)
830 astring1 = UnicodeToAsciiString((LPWSTR)lpApplicationName);
831 if(lpCommandLine)
832 astring2 = UnicodeToAsciiString(lpCommandLine);
833 if(lpCurrentDirectory)
834 astring3 = UnicodeToAsciiString((LPWSTR)lpCurrentDirectory);
835 rc = CreateProcessA(astring1, astring2, lpProcessAttributes, lpThreadAttributes,
836 bInheritHandles, dwCreationFlags, lpEnvironment,
837 astring3, (LPSTARTUPINFOA)lpStartupInfo,
838 lpProcessInfo);
839 if(astring3) FreeAsciiString(astring3);
840 if(astring2) FreeAsciiString(astring2);
841 if(astring1) FreeAsciiString(astring1);
842 return(rc);
843}
844//******************************************************************************
845//******************************************************************************
846HINSTANCE WIN32API WinExec(LPCSTR lpCmdLine, UINT nCmdShow)
847{
848 STARTUPINFOA startinfo = {0};
849 PROCESS_INFORMATION procinfo;
850 DWORD rc;
851
852 dprintf(("KERNEL32: WinExec %s\n", lpCmdLine));
853 startinfo.dwFlags = nCmdShow;
854 if(CreateProcessA(NULL, (LPSTR)lpCmdLine, NULL, NULL, FALSE, 0, NULL, NULL,
855 &startinfo, &procinfo) == FALSE)
856 {
857 return 0;
858 }
859 //block until the launched app waits for input (or a timeout of 15 seconds)
860 //TODO: Shouldn't call Open32, but the api in user32..
861 rc = O32_WaitForInputIdle(procinfo.hProcess, 15000);
862 if(rc != 0) {
863 dprintf(("WinExec: WaitForInputIdle %x returned %x", procinfo.hProcess, rc));
864 }
865 return procinfo.hProcess; //correct?
866}
867//******************************************************************************
868//******************************************************************************
869FARPROC WIN32API GetProcAddress(HMODULE hModule, LPCSTR lpszProc)
870{
871 Win32ImageBase *winmod;
872 FARPROC proc;
873 ULONG ulAPIOrdinal;
874
875 if(hModule == 0 || hModule == -1 || (WinExe && hModule == WinExe->getInstanceHandle())) {
876 winmod = WinExe;
877 }
878 else winmod = (Win32ImageBase *)Win32DllBase::findModule((HINSTANCE)hModule);
879
880 if(winmod) {
881 ulAPIOrdinal = (ULONG)lpszProc;
882 if (ulAPIOrdinal <= 0x0000FFFF) {
883 proc = (FARPROC)winmod->getApi((int)ulAPIOrdinal);
884 }
885 else proc = (FARPROC)winmod->getApi((char *)lpszProc);
886 if(proc == 0) {
887 SetLastError(ERROR_PROC_NOT_FOUND);
888 }
889 return proc;
890 }
891 proc = O32_GetProcAddress(hModule, lpszProc);
892 if(HIWORD(lpszProc))
893 dprintf(("KERNEL32: GetProcAddress %s from %X returned %X\n", lpszProc, hModule, proc));
894 else dprintf(("KERNEL32: GetProcAddress %x from %X returned %X\n", lpszProc, hModule, proc));
895 return(proc);
896}
897//******************************************************************************
898//Retrieve the version
899//******************************************************************************
900BOOL SYSTEM GetVersionStruct(char *lpszModName, char *verstruct, ULONG bufLength)
901{
902 Win32ImageBase *winimage;
903 Win32PeLdrRsrcImg *rsrcimg;
904
905 dprintf(("GetVersionStruct of module %s", lpszModName));
906 if(WinExe && !stricmp(WinExe->getFullPath(), lpszModName)) {
907 winimage = (Win32ImageBase *)WinExe;
908 }
909 else {
910 winimage = (Win32ImageBase *)Win32DllBase::findModule(lpszModName);
911 if(winimage == NULL)
912 {
913 char modname[CCHMAXPATH];
914
915 strcpy(modname, lpszModName);
916 //rename dll if necessary (i.e. OLE32 -> OLE32OS2)
917 Win32DllBase::renameDll(modname);
918
919 if(Win32ImageBase::isPEImage(modname) == FALSE)
920 {
921 HINSTANCE hInstance;
922
923 //must be an LX dll, just load it (app will probably load it anyway)
924 hInstance = LoadLibraryA(modname);
925 if(hInstance == 0)
926 return 0;
927 winimage = (Win32ImageBase *)Win32DllBase::findModule(hInstance);
928 if(winimage) {
929 return winimage->getVersionStruct(verstruct, bufLength);
930 }
931 return 0;
932 }
933 //SvL: Try to load it
934 rsrcimg = new Win32PeLdrRsrcImg(modname);
935 if(rsrcimg == NULL)
936 return 0;
937
938 rsrcimg->init(0);
939 if(rsrcimg->getError() != NO_ERROR)
940 {
941 dprintf(("GetVersionStruct can't load %s\n", modname));
942 delete rsrcimg;
943 return(FALSE);
944 }
945 BOOL rc = rsrcimg->getVersionStruct(verstruct, bufLength);
946 delete rsrcimg;
947 return rc;
948 }
949 }
950 return winimage->getVersionStruct(verstruct, bufLength);
951}
952//******************************************************************************
953//******************************************************************************
954ULONG SYSTEM GetVersionSize(char *lpszModName)
955{
956 Win32ImageBase *winimage;
957 Win32PeLdrRsrcImg *rsrcimg;
958
959 dprintf(("GetVersionSize of %s\n", lpszModName));
960
961 if(WinExe && !stricmp(WinExe->getFullPath(), lpszModName)) {
962 winimage = (Win32ImageBase *)WinExe;
963 }
964 else {
965 winimage = (Win32ImageBase *)Win32DllBase::findModule(lpszModName);
966 if(winimage == NULL)
967 {
968 char modname[CCHMAXPATH];
969
970 strcpy(modname, lpszModName);
971 //rename dll if necessary (i.e. OLE32 -> OLE32OS2)
972 Win32DllBase::renameDll(modname);
973
974 if(Win32ImageBase::isPEImage(modname) == FALSE)
975 {
976 HINSTANCE hInstance;
977
978 //must be an LX dll, just load it (app will probably load it anyway)
979 hInstance = LoadLibraryA(modname);
980 if(hInstance == 0)
981 return 0;
982 winimage = (Win32ImageBase *)Win32DllBase::findModule(hInstance);
983 if(winimage) {
984 return winimage->getVersionSize();
985 }
986 return 0;
987 }
988
989 //SvL: Try to load it
990 rsrcimg = new Win32PeLdrRsrcImg(modname);
991 if(rsrcimg == NULL)
992 return 0;
993
994 rsrcimg->init(0);
995 if(rsrcimg->getError() != NO_ERROR)
996 {
997 dprintf(("GetVersionSize can't load %s\n", modname));
998 delete rsrcimg;
999 return(FALSE);
1000 }
1001 int size = rsrcimg->getVersionSize();
1002 delete rsrcimg;
1003 return size;
1004 }
1005 }
1006 return winimage->getVersionSize();
1007}
1008//******************************************************************************
1009//TODO:What does this do exactly??
1010//******************************************************************************
1011ODINFUNCTION1(BOOL,DisableThreadLibraryCalls,HMODULE,hModule)
1012{
1013 Win32DllBase *winmod;
1014 FARPROC proc;
1015 ULONG ulAPIOrdinal;
1016
1017 winmod = Win32DllBase::findModule((HINSTANCE)hModule);
1018 if(winmod)
1019 {
1020 // don't call ATTACH/DETACH thread functions in DLL
1021 winmod->setThreadLibraryCalls(FALSE);
1022 return TRUE;
1023 }
1024 else
1025 {
1026 // raise error condition
1027 SetLastError(ERROR_INVALID_HANDLE);
1028 return FALSE;
1029 }
1030}
1031//******************************************************************************
1032//******************************************************************************
Note: See TracBrowser for help on using the repository browser.