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

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

remove THDB structure from list when thread ends

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