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

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

cmd line fix, mmap fix + EB's string fixes

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