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

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

Implemented SEC_COMMIT flag for memory maps

File size: 31.0 KB
Line 
1/* $Id: wprocess.cpp,v 1.62 1999-12-30 11:19:54 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//******************************************************************************
452HINSTANCE16 WIN32API LoadLibrary16(LPCTSTR lpszLibFile)
453{
454 dprintf(("ERROR: LoadLibrary16 %s, not implemented", lpszLibFile));
455 return 0;
456}
457//******************************************************************************
458//******************************************************************************
459VOID WIN32API FreeLibrary16(HINSTANCE16 hinstance)
460{
461 dprintf(("ERROR: FreeLibrary16 %x, not implemented", hinstance));
462}
463//******************************************************************************
464//******************************************************************************
465FARPROC WIN32API GetProcAddress16(HMODULE hModule, LPCSTR lpszProc)
466{
467 dprintf(("ERROR: GetProcAddress16 %x %x, not implemented", hModule, lpszProc));
468 return 0;
469}
470//******************************************************************************
471//******************************************************************************
472HINSTANCE WIN32API LoadLibraryA(LPCTSTR lpszLibFile)
473{
474 HINSTANCE hDll;
475
476 dprintf(("KERNEL32: LoadLibraryA(%s)\n",
477 lpszLibFile));
478 dprintf(("KERNEL32: LoadLibrary %x FS = %x\n", GetCurrentThreadId(), GetFS()));
479
480 hDll = iLoadLibraryA(lpszLibFile, 0);
481 if (hDll == 0)
482 {
483 char * pszName;
484
485 // remove path from the image name
486 pszName = strrchr((char *)lpszLibFile,
487 '\\');
488 if (pszName != NULL)
489 {
490 pszName++; // skip backslash
491
492 // now try again without fully qualified path
493 hDll = iLoadLibraryA(pszName, 0);
494 }
495 }
496
497 return hDll;
498}
499//******************************************************************************
500//******************************************************************************
501HINSTANCE WIN32API LoadLibraryExA(LPCTSTR lpszLibFile, HANDLE hFile, DWORD dwFlags)
502{
503 HINSTANCE hDll;
504
505 dprintf(("KERNEL32: LoadLibraryExA %s (%X)\n", lpszLibFile, dwFlags));
506 hDll = iLoadLibraryA(lpszLibFile, dwFlags);
507 if (hDll == 0)
508 {
509 char * pszName;
510
511 // remove path from the image name
512 pszName = strrchr((char *)lpszLibFile,
513 '\\');
514 if (pszName != NULL)
515 {
516 pszName++; // skip backslash
517
518 // now try again without fully qualified path
519 hDll = iLoadLibraryA(pszName, dwFlags);
520 }
521 }
522
523 return hDll;
524}
525//******************************************************************************
526//******************************************************************************
527HINSTANCE WIN32API LoadLibraryW(LPCWSTR lpModule)
528{
529 char *asciimodule;
530 HINSTANCE rc;
531
532 asciimodule = UnicodeToAsciiString((LPWSTR)lpModule);
533 dprintf(("KERNEL32: OS2LoadLibraryW %s\n", asciimodule));
534 rc = LoadLibraryA(asciimodule);
535 free(asciimodule);
536 return(rc);
537}
538//******************************************************************************
539//******************************************************************************
540HINSTANCE WIN32API LoadLibraryExW(LPCWSTR lpModule, HANDLE hFile, DWORD dwFlags)
541{
542 char *asciimodule;
543 HINSTANCE rc;
544
545 asciimodule = UnicodeToAsciiString((LPWSTR)lpModule);
546 dprintf(("KERNEL32: OS2LoadLibraryExW %s (%d)\n", asciimodule, dwFlags));
547 rc = LoadLibraryExA(asciimodule, hFile, dwFlags);
548 free(asciimodule);
549 return(rc);
550}
551//******************************************************************************
552//******************************************************************************
553LPCSTR WIN32API GetCommandLineA()
554{
555 LPTSTR cmdline = NULL;
556
557 if(WinExe) {
558 cmdline = WinExe->getCommandLine();
559 }
560 if(cmdline == NULL) //not used for converted exes
561 cmdline = O32_GetCommandLine();
562
563 dprintf(("KERNEL32: GetCommandLine %s\n", cmdline));
564 dprintf(("KERNEL32: FS = %x\n", GetFS()));
565 return(cmdline);
566}
567//******************************************************************************
568//******************************************************************************
569LPCWSTR WIN32API GetCommandLineW(void)
570{
571 static WCHAR *UnicodeCmdLine = NULL;
572 char *asciicmdline = NULL;
573
574 dprintf(("KERNEL32: FS = %x\n", GetFS()));
575
576 if(UnicodeCmdLine)
577 return(UnicodeCmdLine); //already called before
578
579 if(WinExe) {
580 asciicmdline = WinExe->getCommandLine();
581 }
582 if(asciicmdline == NULL) //not used for converted exes
583 asciicmdline = O32_GetCommandLine();
584
585 if(asciicmdline) {
586 UnicodeCmdLine = (WCHAR *)malloc(strlen(asciicmdline)*2 + 2);
587 AsciiToUnicode(asciicmdline, UnicodeCmdLine);
588 dprintf(("KERNEL32: OS2GetCommandLineW: %s\n", asciicmdline));
589 return(UnicodeCmdLine);
590 }
591 dprintf(("KERNEL32: OS2GetCommandLineW: asciicmdline == NULL\n"));
592 return NULL;
593}
594//******************************************************************************
595//******************************************************************************
596DWORD WIN32API GetModuleFileNameA(HMODULE hinstModule, LPTSTR lpszPath, DWORD cchPath)
597{
598 DWORD rc;
599 Win32ImageBase *module;
600 char *fpath = NULL;
601
602 dprintf(("GetModuleFileName %X", hinstModule));
603 if(hinstModule == 0 || hinstModule == -1 || (WinExe && hinstModule == WinExe->getInstanceHandle())) {
604 module = (Win32ImageBase *)WinExe;
605 }
606 else {
607 module = (Win32ImageBase *)Win32DllBase::findModule(hinstModule);
608 }
609
610 if(module) {
611 fpath = module->getFullPath();
612 }
613 if(fpath) {
614 //SvL: 13-9-98: +1
615 rc = min(strlen(fpath)+1, cchPath);
616 strncpy(lpszPath, fpath, rc);
617 }
618 else rc = O32_GetModuleFileName(hinstModule, lpszPath, cchPath);
619
620 if(rc) {
621 dprintf(("KERNEL32: GetModuleFileName %s %d\n", lpszPath, hinstModule));
622 }
623 return(rc);
624}
625//******************************************************************************
626//******************************************************************************
627DWORD WIN32API GetModuleFileNameW(HMODULE hModule, LPWSTR lpFileName, DWORD nSize)
628{
629 char *asciifilename = (char *)malloc(nSize+1);
630 DWORD rc;
631
632 dprintf(("KERNEL32: OSLibGetModuleFileNameW\n"));
633 rc = GetModuleFileNameA(hModule, asciifilename, nSize);
634 if(rc) AsciiToUnicode(asciifilename, lpFileName);
635 free(asciifilename);
636 return(rc);
637}
638//******************************************************************************
639//NOTE: GetModuleHandleA does NOT support files with multiple dots (i.e.
640// very.weird.exe)
641//******************************************************************************
642HANDLE WIN32API GetModuleHandleA(LPCTSTR lpszModule)
643{
644 HANDLE hMod;
645 Win32DllBase *windll;
646 char szModule[CCHMAXPATH];
647 BOOL fDllModule = FALSE;
648
649 if(lpszModule == NULL) {
650 if(WinExe)
651 hMod = WinExe->getInstanceHandle();
652 else hMod = -1;
653 }
654 else {
655 strcpy(szModule, OSLibStripPath((char *)lpszModule));
656 strupr(szModule);
657 if(strstr(szModule, ".DLL")) {
658 fDllModule = TRUE;
659 }
660 else {
661 if(!strstr(szModule, ".")) {
662 //if there's no extension or trainling dot, we
663 //assume it's a dll (see Win32 SDK docs)
664 fDllModule = TRUE;
665 }
666 }
667 char *dot = strstr(szModule, ".");
668 if(dot)
669 *dot = 0;
670
671 if(!fDllModule && WinExe && !strcmpi(szModule, WinExe->getModuleName())) {
672 hMod = WinExe->getInstanceHandle();
673 }
674 else {
675 windll = Win32DllBase::findModule(szModule);
676 if(windll) {
677 hMod = windll->getInstanceHandle();
678 }
679 else hMod = OSLibiGetModuleHandleA((char *)lpszModule);
680 }
681 }
682
683 dprintf(("KERNEL32: GetModuleHandle %s returned %X\n", lpszModule, hMod));
684 return(hMod);
685}
686//******************************************************************************
687//******************************************************************************
688HMODULE WIN32API GetModuleHandleW(LPCWSTR arg1)
689{
690 HMODULE rc;
691 char *astring;
692
693 astring = UnicodeToAsciiString((LPWSTR)arg1);
694 rc = GetModuleHandleA(astring);
695 dprintf(("KERNEL32: OS2GetModuleHandleW %s returned %X\n", astring, rc));
696 FreeAsciiString(astring);
697 return(rc);
698}
699//******************************************************************************
700//******************************************************************************
701BOOL WINAPI CreateProcessA( LPCSTR lpApplicationName, LPSTR lpCommandLine,
702 LPSECURITY_ATTRIBUTES lpProcessAttributes,
703 LPSECURITY_ATTRIBUTES lpThreadAttributes,
704 BOOL bInheritHandles, DWORD dwCreationFlags,
705 LPVOID lpEnvironment, LPCSTR lpCurrentDirectory,
706 LPSTARTUPINFOA lpStartupInfo,
707 LPPROCESS_INFORMATION lpProcessInfo )
708{
709 BOOL rc;
710 char *cmdline = NULL;
711
712 dprintf(("KERNEL32: CreateProcessA %s cline:%s inherit:%d cFlags:%x Env:%x CurDir:%s StartupFlags:%x\n",
713 lpApplicationName, lpCommandLine, bInheritHandles, dwCreationFlags,
714 lpEnvironment, lpCurrentDirectory, lpStartupInfo));
715
716 // open32 does not support DEBUG_ONLY_THIS_PROCESS
717 if(dwCreationFlags & DEBUG_ONLY_THIS_PROCESS)
718 dwCreationFlags |= DEBUG_PROCESS;
719
720 if(O32_CreateProcess(lpApplicationName, lpCommandLine, lpProcessAttributes,
721 lpThreadAttributes, bInheritHandles, dwCreationFlags,
722 lpEnvironment, lpCurrentDirectory, lpStartupInfo,
723 lpProcessInfo) == TRUE)
724 return(TRUE);
725
726 //probably a win32 exe, so run it in the pe loader
727 if(lpApplicationName) {
728 if(lpCommandLine) {
729 //skip exe name in lpCommandLine
730 while(*lpCommandLine != 0 && *lpCommandLine != ' ')
731 lpCommandLine++;
732
733 if(*lpCommandLine != 0) {
734 lpCommandLine++;
735 }
736 cmdline = (char *)malloc(strlen(lpApplicationName)+strlen(lpCommandLine) + 16);
737 sprintf(cmdline, "PE.EXE %s %s", lpApplicationName, lpCommandLine);
738 }
739 else {
740 cmdline = (char *)malloc(strlen(lpApplicationName) + 16);
741 sprintf(cmdline, "PE.EXE %s", lpApplicationName);
742 }
743 }
744 else {
745 cmdline = (char *)malloc(strlen(lpCommandLine) + 16);
746 sprintf(cmdline, "PE.EXE %s", lpCommandLine);
747 }
748 dprintf(("KERNEL32: CreateProcess %s\n", cmdline));
749 rc = O32_CreateProcess("PE.EXE", (LPCSTR)cmdline,lpProcessAttributes,
750 lpThreadAttributes, bInheritHandles, dwCreationFlags,
751 lpEnvironment, lpCurrentDirectory, lpStartupInfo,
752 lpProcessInfo);
753 if(cmdline)
754 free(cmdline);
755
756 if(lpProcessInfo)
757 dprintf(("KERNEL32: CreateProcess returned %d hPro:%x hThr:%x pid:%x tid:%x\n",
758 rc, lpProcessInfo->hProcess, lpProcessInfo->hThread,
759 lpProcessInfo->dwProcessId,lpProcessInfo->dwThreadId));
760 else
761 dprintf(("KERNEL32: CreateProcess returned %d\n", rc));
762 return(rc);
763}
764//******************************************************************************
765//******************************************************************************
766BOOL WIN32API CreateProcessW(LPCWSTR lpApplicationName, LPWSTR lpCommandLine,
767 PSECURITY_ATTRIBUTES lpProcessAttributes,
768 PSECURITY_ATTRIBUTES lpThreadAttributes,
769 BOOL bInheritHandles, DWORD dwCreationFlags,
770 LPVOID lpEnvironment,
771 LPCWSTR lpCurrentDirectory, LPSTARTUPINFOW lpStartupInfo,
772 LPPROCESS_INFORMATION lpProcessInfo)
773{
774 BOOL rc;
775 char *astring1 = 0, *astring2 = 0, *astring3 = 0;
776
777 dprintf(("KERNEL32: CreateProcessW"));
778 if(lpApplicationName)
779 astring1 = UnicodeToAsciiString((LPWSTR)lpApplicationName);
780 if(lpCommandLine)
781 astring2 = UnicodeToAsciiString(lpCommandLine);
782 if(lpCurrentDirectory)
783 astring3 = UnicodeToAsciiString((LPWSTR)lpCurrentDirectory);
784 rc = CreateProcessA(astring1, astring2, lpProcessAttributes, lpThreadAttributes,
785 bInheritHandles, dwCreationFlags, lpEnvironment,
786 astring3, (LPSTARTUPINFOA)lpStartupInfo,
787 lpProcessInfo);
788 if(astring3) FreeAsciiString(astring3);
789 if(astring2) FreeAsciiString(astring2);
790 if(astring1) FreeAsciiString(astring1);
791 return(rc);
792}
793//******************************************************************************
794//******************************************************************************
795HINSTANCE WIN32API WinExec(LPCSTR lpCmdLine, UINT nCmdShow)
796{
797 STARTUPINFOA startinfo = {0};
798 PROCESS_INFORMATION procinfo;
799
800 dprintf(("KERNEL32: WinExec %s\n", lpCmdLine));
801 startinfo.dwFlags = nCmdShow;
802 if(CreateProcessA(NULL, (LPSTR)lpCmdLine, NULL, NULL, FALSE, 0, NULL, NULL,
803 &startinfo, &procinfo) == FALSE)
804 {
805 return 0;
806 }
807 return procinfo.hProcess; //correct?
808}
809//******************************************************************************
810//******************************************************************************
811FARPROC WIN32API GetProcAddress(HMODULE hModule, LPCSTR lpszProc)
812{
813 Win32ImageBase *winmod;
814 FARPROC proc;
815 ULONG ulAPIOrdinal;
816
817 if(hModule == 0 || hModule == -1 || (WinExe && hModule == WinExe->getInstanceHandle())) {
818 winmod = WinExe;
819 }
820 else winmod = (Win32ImageBase *)Win32DllBase::findModule((HINSTANCE)hModule);
821
822 if(winmod) {
823 ulAPIOrdinal = (ULONG)lpszProc;
824 if (ulAPIOrdinal <= 0x0000FFFF) {
825 proc = (FARPROC)winmod->getApi((int)ulAPIOrdinal);
826 }
827 else proc = (FARPROC)winmod->getApi((char *)lpszProc);
828 if(proc == 0) {
829 SetLastError(ERROR_PROC_NOT_FOUND);
830 }
831 return proc;
832 }
833 proc = O32_GetProcAddress(hModule, lpszProc);
834 if(HIWORD(lpszProc))
835 dprintf(("KERNEL32: GetProcAddress %s from %X returned %X\n", lpszProc, hModule, proc));
836 else dprintf(("KERNEL32: GetProcAddress %x from %X returned %X\n", lpszProc, hModule, proc));
837 return(proc);
838}
839//******************************************************************************
840//Retrieve the version
841//******************************************************************************
842BOOL SYSTEM GetVersionStruct(char *lpszModName, char *verstruct, ULONG bufLength)
843{
844 Win32ImageBase *winimage;
845 Win32PeLdrRsrcImg *rsrcimg;
846
847 dprintf(("GetVersionStruct of module %s", lpszModName));
848 if(WinExe && !stricmp(WinExe->getFullPath(), lpszModName)) {
849 winimage = (Win32ImageBase *)WinExe;
850 }
851 else {
852 winimage = (Win32ImageBase *)Win32DllBase::findModule(lpszModName);
853 if(winimage == NULL)
854 {
855 char modname[CCHMAXPATH];
856
857 strcpy(modname, lpszModName);
858 //rename dll if necessary (i.e. OLE32 -> OLE32OS2)
859 Win32DllBase::renameDll(modname);
860
861 if(Win32ImageBase::isPEImage(modname) == FALSE)
862 {
863 HINSTANCE hInstance;
864
865 //must be an LX dll, just load it (app will probably load it anyway)
866 hInstance = LoadLibraryA(modname);
867 if(hInstance == 0)
868 return 0;
869 winimage = (Win32ImageBase *)Win32DllBase::findModule(hInstance);
870 if(winimage) {
871 return winimage->getVersionStruct(verstruct, bufLength);
872 }
873 return 0;
874 }
875 //SvL: Try to load it
876 rsrcimg = new Win32PeLdrRsrcImg(modname);
877 if(rsrcimg == NULL)
878 return 0;
879
880 rsrcimg->init(0);
881 if(rsrcimg->getError() != NO_ERROR)
882 {
883 dprintf(("GetVersionStruct can't load %s\n", modname));
884 delete rsrcimg;
885 return(FALSE);
886 }
887 BOOL rc = rsrcimg->getVersionStruct(verstruct, bufLength);
888 delete rsrcimg;
889 return rc;
890 }
891 }
892 return winimage->getVersionStruct(verstruct, bufLength);
893}
894//******************************************************************************
895//******************************************************************************
896ULONG SYSTEM GetVersionSize(char *lpszModName)
897{
898 Win32ImageBase *winimage;
899 Win32PeLdrRsrcImg *rsrcimg;
900
901 dprintf(("GetVersionSize of %s\n", lpszModName));
902
903 if(WinExe && !stricmp(WinExe->getFullPath(), lpszModName)) {
904 winimage = (Win32ImageBase *)WinExe;
905 }
906 else {
907 winimage = (Win32ImageBase *)Win32DllBase::findModule(lpszModName);
908 if(winimage == NULL)
909 {
910 char modname[CCHMAXPATH];
911
912 strcpy(modname, lpszModName);
913 //rename dll if necessary (i.e. OLE32 -> OLE32OS2)
914 Win32DllBase::renameDll(modname);
915
916 if(Win32ImageBase::isPEImage(modname) == FALSE)
917 {
918 HINSTANCE hInstance;
919
920 //must be an LX dll, just load it (app will probably load it anyway)
921 hInstance = LoadLibraryA(modname);
922 if(hInstance == 0)
923 return 0;
924 winimage = (Win32ImageBase *)Win32DllBase::findModule(hInstance);
925 if(winimage) {
926 return winimage->getVersionSize();
927 }
928 return 0;
929 }
930
931 //SvL: Try to load it
932 rsrcimg = new Win32PeLdrRsrcImg(modname);
933 if(rsrcimg == NULL)
934 return 0;
935
936 rsrcimg->init(0);
937 if(rsrcimg->getError() != NO_ERROR)
938 {
939 dprintf(("GetVersionSize can't load %s\n", modname));
940 delete rsrcimg;
941 return(FALSE);
942 }
943 int size = rsrcimg->getVersionSize();
944 delete rsrcimg;
945 return size;
946 }
947 }
948 return winimage->getVersionSize();
949}
950//******************************************************************************
951//TODO:What does this do exactly??
952//******************************************************************************
953ODINFUNCTION1(BOOL,DisableThreadLibraryCalls,HMODULE,hModule)
954{
955 Win32DllBase *winmod;
956 FARPROC proc;
957 ULONG ulAPIOrdinal;
958
959 winmod = Win32DllBase::findModule((HINSTANCE)hModule);
960 if(winmod)
961 {
962 // don't call ATTACH/DETACH thread functions in DLL
963 winmod->setThreadLibraryCalls(FALSE);
964 return TRUE;
965 }
966 else
967 {
968 // raise error condition
969 SetLastError(ERROR_INVALID_HANDLE);
970 return FALSE;
971 }
972}
973//******************************************************************************
974//******************************************************************************
Note: See TracBrowser for help on using the repository browser.