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

Last change on this file since 9802 was 9802, checked in by sandervl, 23 years ago

Forward RtlZero/Move/FillMemory to ntdll

File size: 91.9 KB
Line 
1/* $Id: wprocess.cpp,v 1.180 2003-02-13 17:16:50 sandervl Exp $ */
2
3/*
4 * Win32 process functions
5 *
6 * Copyright 1998-2000 Sander van Leeuwen (sandervl@xs4all.nl)
7 * Copyright 2000 knut st. osmundsen (knut.stange.osmundsen@mynd.no)
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 * TODO: What happens when a dll is first loaded as LOAD_LIBRARY_AS_DATAFILE
13 * and then for real? (first one not freed of course)
14 *
15 * Project Odin Software License can be found in LICENSE.TXT
16 *
17 */
18#include <odin.h>
19#include <odinwrap.h>
20#include <os2win.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24
25#include <unicode.h>
26#include "windllbase.h"
27#include "winexebase.h"
28#include "windllpeldr.h"
29#include "winexepeldr.h"
30#include "windlllx.h"
31#include <vmutex.h>
32#include <handlemanager.h>
33#include <odinpe.h>
34
35#include "odin32validate.h"
36#include "exceptutil.h"
37#include "asmutil.h"
38#include "oslibdos.h"
39#include "oslibmisc.h"
40#include "oslibdebug.h"
41#include "hmcomm.h"
42
43#include "console.h"
44#include "wincon.h"
45#include "versionos2.h" /*PLF Wed 98-03-18 02:36:51*/
46#include <wprocess.h>
47#include "mmap.h"
48#include "initterm.h"
49#include "directory.h"
50
51#include <win\ntddk.h>
52
53#define DBG_LOCALLOG DBG_wprocess
54#include "dbglocal.h"
55
56#ifdef PROFILE
57#include <perfview.h>
58#include <profiler.h>
59#endif /* PROFILE */
60
61
62ODINDEBUGCHANNEL(KERNEL32-WPROCESS)
63
64
65/*******************************************************************************
66* Global Variables *
67*******************************************************************************/
68BOOL fIsOS2Image = FALSE; /* TRUE -> Odin32 OS/2 application (not converted!) */
69 /* FALSE -> otherwise */
70BOOL fSwitchTIBSel = TRUE; // TRUE -> switch TIB selectors
71 // FALSE -> don't
72BOOL fExitProcess = FALSE;
73
74//Commandlines
75PCSTR pszCmdLineA; /* ASCII/ANSII commandline. */
76PCWSTR pszCmdLineW; /* Unicode commandline. */
77
78//Process database
79PDB ProcessPDB = {0};
80ENVDB ProcessENVDB = {0};
81CONCTRLDATA ProcessConCtrlData = {0};
82STARTUPINFOA StartupInfo = {0};
83CHAR unknownPDBData[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 ,0 ,0};
84USHORT ProcessTIBSel = 0;
85DWORD *TIBFlatPtr = 0;
86
87//list of thread database structures
88static TEB *threadList = 0;
89static VMutex threadListMutex;
90
91
92//******************************************************************************
93//******************************************************************************
94TEB *WIN32API GetThreadTEB()
95{
96 if(TIBFlatPtr == NULL) {
97 return 0;
98 }
99 return (TEB *)*TIBFlatPtr;
100}
101//******************************************************************************
102//******************************************************************************
103TEB *WIN32API GetTEBFromThreadId(ULONG threadId)
104{
105 TEB *teb = threadList;
106
107 threadListMutex.enter();
108 while(teb) {
109 if(teb->o.odin.threadId == threadId) {
110 break;
111 }
112 teb = teb->o.odin.next;
113 }
114 threadListMutex.leave();
115 return teb;
116}
117//******************************************************************************
118//******************************************************************************
119TEB *WIN32API GetTEBFromThreadHandle(HANDLE hThread)
120{
121 TEB *teb = threadList;
122
123 threadListMutex.enter();
124 while(teb) {
125 if(teb->o.odin.hThread == hThread) {
126 break;
127 }
128 teb = teb->o.odin.next;
129 }
130 threadListMutex.leave();
131 return teb;
132}
133//******************************************************************************
134//Allocate TEB structure for new thread
135//******************************************************************************
136TEB *WIN32API CreateTEB(HANDLE hThread, DWORD dwThreadId)
137{
138 USHORT tibsel;
139 TEB *winteb;
140
141 if(OSLibAllocSel(sizeof(TEB), &tibsel) == FALSE)
142 {
143 dprintf(("InitializeTIB: selector alloc failed!!"));
144 DebugInt3();
145 return NULL;
146 }
147 winteb = (TEB *)OSLibSelToFlat(tibsel);
148 if(winteb == NULL)
149 {
150 dprintf(("InitializeTIB: DosSelToFlat failed!!"));
151 DebugInt3();
152 return NULL;
153 }
154 memset(winteb, 0, sizeof(TEB));
155 dprintf(("TIB selector %x; linaddr 0x%x", tibsel, winteb));
156
157 threadListMutex.enter();
158 TEB *teblast = threadList;
159 if(!teblast) {
160 threadList = winteb;
161 winteb->o.odin.next = NULL;
162 }
163 else {
164 while(teblast->o.odin.next) {
165 teblast = teblast->o.odin.next;
166 }
167 teblast->o.odin.next = winteb;
168 }
169 threadListMutex.leave();
170
171 winteb->except = (PVOID)-1; /* 00 Head of exception handling chain */
172 winteb->htask16 = (USHORT)OSLibGetPIB(PIB_TASKHNDL); /* 0c Win16 task handle */
173 winteb->stack_sel = getSS(); /* 0e 16-bit stack selector */
174 winteb->self = winteb; /* 18 Pointer to this structure */
175 winteb->flags = TEBF_WIN32; /* 1c Flags */
176 winteb->queue = 0; /* 28 Message queue */
177 winteb->tls_ptr = &winteb->tls_array[0]; /* 2c Pointer to TLS array */
178 winteb->process = &ProcessPDB; /* 30 owning process (used by NT3.51 applets)*/
179 winteb->delta_priority = THREAD_PRIORITY_NORMAL;
180 winteb->process = &ProcessPDB;
181
182 //store selector of new TEB
183 winteb->teb_sel = tibsel;
184
185 winteb->o.odin.hThread = hThread;
186 winteb->o.odin.threadId = dwThreadId;
187
188 // Event semaphore (auto-reset) to signal message post to MsgWaitForMultipleObjects
189 winteb->o.odin.hPostMsgEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
190
191 return winteb;
192}
193//******************************************************************************
194// Set up the TIB selector and memory for the main thread
195//******************************************************************************
196TEB *WIN32API InitializeMainThread()
197{
198 HANDLE hThreadMain;
199 TEB *teb;
200
201 //Allocate one dword to store the flat address of our TEB
202 dprintf(("InitializeMainThread Process handle %x, id %x", GetCurrentProcess(), GetCurrentProcessId()));
203
204 TIBFlatPtr = (DWORD *)OSLibAllocThreadLocalMemory(1);
205 if(TIBFlatPtr == 0) {
206 dprintf(("InitializeTIB: local thread memory alloc failed!!"));
207 DebugInt3();
208 return NULL;
209 }
210 //SvL: This doesn't really create a thread, but only sets up the
211 // handle of thread 0
212 hThreadMain = HMCreateThread(NULL, 0, 0, 0, 0, 0, TRUE);
213
214 //create and initialize TEB
215 teb = CreateTEB(hThreadMain, GetCurrentThreadId());
216 if(teb == NULL || InitializeThread(teb, TRUE) == FALSE) {
217 DebugInt3();
218 return NULL;
219 }
220
221 ProcessTIBSel = teb->teb_sel;
222
223 //todo initialize PDB during process creation
224 //todo: initialize TLS array if required
225 //TLS in executable always TLS index 0?
226//// ProcessPDB.exit_code = 0x103; /* STILL_ACTIVE */
227 ProcessPDB.threads = 1;
228 ProcessPDB.running_threads = 1;
229 ProcessPDB.ring0_threads = 1;
230 ProcessPDB.system_heap = GetProcessHeap();
231 ProcessPDB.parent = 0;
232 ProcessPDB.group = &ProcessPDB;
233 ProcessPDB.priority = 8; /* Normal */
234 ProcessPDB.heap = ProcessPDB.system_heap; /* will be changed later on */
235 ProcessPDB.next = NULL;
236 ProcessPDB.winver = 0xffff; /* to be determined */
237 ProcessPDB.server_pid = (void *)GetCurrentProcessId();
238 ProcessPDB.tls_bits[0] = 0; //all tls slots are free
239 ProcessPDB.tls_bits[1] = 0;
240
241 GetSystemTime(&ProcessPDB.creationTime);
242
243 /* Initialize the critical section */
244 InitializeCriticalSection(&ProcessPDB.crit_section );
245
246 //initialize the environment db entry.
247 ProcessPDB.env_db = &ProcessENVDB;
248 ProcessENVDB.startup_info = &StartupInfo;
249 ProcessENVDB.environ = GetEnvironmentStringsA();
250 ProcessENVDB.cmd_line = (CHAR*)(void*)pszCmdLineA;
251 ProcessENVDB.cmd_lineW = (WCHAR*)(void*)pszCmdLineW;
252 ProcessENVDB.break_handlers = &ProcessConCtrlData;
253 ProcessConCtrlData.fIgnoreCtrlC = FALSE; /* TODO! Should be inherited from parent. */
254 ProcessConCtrlData.pHead = ProcessConCtrlData.pTail = (PCONCTRL)malloc(sizeof(CONCTRL));
255 ProcessConCtrlData.pHead->pfnHandler = (void*)DefaultConsoleCtrlHandler;
256 ProcessConCtrlData.pHead->pNext = ProcessConCtrlData.pHead->pPrev = NULL;
257 ProcessConCtrlData.pHead->flFlags = ODIN32_CONCTRL_FLAGS_INIT;
258 InitializeCriticalSection(&ProcessENVDB.section);
259
260// ProcessPDB.startup_info = &StartupInfo;
261 ProcessPDB.unknown10 = (PVOID)&unknownPDBData[0];
262 StartupInfo.cb = sizeof(StartupInfo);
263 ProcessENVDB.hStdin = StartupInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
264 ProcessENVDB.hStdout = StartupInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
265 ProcessENVDB.hStderr = StartupInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE);
266
267 return teb;
268}
269//******************************************************************************
270// Set up the TEB structure of the CURRENT (!) thread
271//******************************************************************************
272BOOL WIN32API InitializeThread(TEB *winteb, BOOL fMainThread)
273{
274 //store TEB address in thread locale memory for easy retrieval
275 *TIBFlatPtr = (DWORD)winteb;
276
277//// winteb->exit_code = 0x103; /* STILL_ACTIVE */
278
279 winteb->stack_top = (PVOID)OSLibGetTIB(TIB_STACKTOP); /* 04 Top of thread stack */
280 winteb->stack_top = (PVOID)(((ULONG)winteb->stack_top + 0xFFF) & ~0xFFF);
281 //round to next page (OS/2 doesn't return a nice rounded value)
282 winteb->stack_low = (PVOID)OSLibGetTIB(TIB_STACKLOW); /* 08 Stack low-water mark */
283 //round to page boundary (OS/2 doesn't return a nice rounded value)
284 winteb->stack_low = (PVOID)((ULONG)winteb->stack_low & ~0xFFF);
285
286 winteb->o.odin.OrgTIBSel = GetFS();
287 winteb->o.odin.pWsockData = NULL;
288#ifdef DEBUG
289 winteb->o.odin.dbgCallDepth = 0;
290#endif
291 winteb->o.odin.pMessageBuffer = NULL;
292 winteb->o.odin.lcid = GetUserDefaultLCID();
293
294 if(OSLibGetPIB(PIB_TASKTYPE) == TASKTYPE_PM)
295 {
296 winteb->flags = 0; //todo gui
297 }
298 else winteb->flags = 0; //todo textmode
299
300 //Initialize thread security objects (TODO: Not complete)
301 SID_IDENTIFIER_AUTHORITY sidIdAuth = {0};
302 winteb->o.odin.threadinfo.dwType = SECTYPE_PROCESS | SECTYPE_INITIALIZED;
303
304 RtlAllocateAndInitializeSid(&sidIdAuth, 1, 0, 0, 0, 0, 0, 0, 0, 0, &winteb->o.odin.threadinfo.SidUser.User.Sid);
305
306 winteb->o.odin.threadinfo.SidUser.User.Attributes = 0; //?????????
307
308 winteb->o.odin.threadinfo.pTokenGroups = (TOKEN_GROUPS*)malloc(sizeof(TOKEN_GROUPS));
309 winteb->o.odin.threadinfo.pTokenGroups->GroupCount = 1;
310
311 RtlAllocateAndInitializeSid(&sidIdAuth, 1, 0, 0, 0, 0, 0, 0, 0, 0, &winteb->o.odin.threadinfo.PrimaryGroup.PrimaryGroup);
312
313 winteb->o.odin.threadinfo.pTokenGroups->Groups[0].Sid = winteb->o.odin.threadinfo.PrimaryGroup.PrimaryGroup;
314 winteb->o.odin.threadinfo.pTokenGroups->Groups[0].Attributes = 0; //????
315// pPrivilegeSet = NULL;
316// pTokenPrivileges= NULL;
317// TokenOwner = {0};
318// DefaultDACL = {0};
319// TokenSource = {0};
320 winteb->o.odin.threadinfo.TokenType = TokenPrimary;
321
322 dprintf(("InitializeTIB setup TEB with selector %x", winteb->teb_sel));
323 dprintf(("InitializeTIB: FS(%x):[0] = %x", GetFS(), QueryExceptionChain()));
324 return TRUE;
325}
326//******************************************************************************
327// Destroy the TIB selector and memory for the current thread
328//******************************************************************************
329void WIN32API DestroyTEB(TEB *winteb)
330{
331 SHORT orgtibsel;
332
333 dprintf(("DestroyTIB: FS = %x", GetFS()));
334 dprintf(("DestroyTIB: FS:[0] = %x", QueryExceptionChain()));
335
336 orgtibsel = winteb->o.odin.OrgTIBSel;
337
338 dprintf(("DestroyTIB: OSLibFreeSel %x", winteb->teb_sel));
339
340 threadListMutex.enter();
341 TEB *curteb = threadList;
342 if(curteb == winteb) {
343 threadList = winteb->o.odin.next;
344 }
345 else {
346 while(curteb->o.odin.next != winteb) {
347 curteb = curteb->o.odin.next;
348 if(curteb == NULL) {
349 dprintf(("DestroyTIB: couldn't find teb %x", winteb));
350 DebugInt3();
351 break;
352 }
353 }
354 if(curteb) {
355 curteb->o.odin.next = winteb->o.odin.next;
356 }
357 }
358 threadListMutex.leave();
359
360 // free allocated memory for security structures
361 free( winteb->o.odin.threadinfo.pTokenGroups );
362
363 // free PostMessage event semaphore
364 if(winteb->o.odin.hPostMsgEvent) {
365 CloseHandle(winteb->o.odin.hPostMsgEvent);
366 }
367
368#ifdef DEBUG
369 if (winteb->o.odin.arrstrCallStack != NULL)
370 free( winteb->o.odin.arrstrCallStack );
371#endif
372
373 //Restore our original FS selector
374 SetFS(orgtibsel);
375
376 //And free our own
377 OSLibFreeSel(winteb->teb_sel);
378
379 *TIBFlatPtr = 0;
380
381 dprintf(("DestroyTIB: FS(%x):[0] = %x", GetFS(), QueryExceptionChain()));
382 return;
383}
384//******************************************************************************
385//******************************************************************************
386ULONG WIN32API GetProcessTIBSel()
387{
388 if(fExitProcess) {
389 return 0;
390 }
391 return ProcessTIBSel;
392}
393/******************************************************************************/
394/******************************************************************************/
395void SetPDBInstance(HINSTANCE hInstance)
396{
397 ProcessPDB.hInstance = hInstance;
398}
399/******************************************************************************/
400/******************************************************************************/
401void WIN32API RestoreOS2TIB()
402{
403 SHORT orgtibsel;
404 TEB *winteb;
405
406 //If we're running an Odin32 OS/2 application (not converted!), then we
407 //we don't switch FS selectors
408 if(!fSwitchTIBSel) {
409 return;
410 }
411
412 winteb = (TEB *)*TIBFlatPtr;
413 if(winteb) {
414 orgtibsel = winteb->o.odin.OrgTIBSel;
415
416 //Restore our original FS selector
417 SetFS(orgtibsel);
418 }
419}
420/******************************************************************************/
421//Switch to WIN32 TIB (FS selector)
422//NOTE: This is not done for Odin32 applications (LX), unless
423// fForceSwitch is TRUE)
424/******************************************************************************/
425USHORT WIN32API SetWin32TIB(BOOL fForceSwitch)
426{
427 SHORT win32tibsel;
428 TEB *winteb;
429
430 //If we're running an Odin32 OS/2 application (not converted!), then we
431 //we don't switch FS selectors
432 if(!fSwitchTIBSel && !fForceSwitch) {
433 return GetFS();
434 }
435
436 winteb = (TEB *)*TIBFlatPtr;
437 if(winteb) {
438 win32tibsel = winteb->teb_sel;
439
440 //Restore our win32 FS selector
441 return SetReturnFS(win32tibsel);
442 }
443 else {
444 return GetFS();
445 }
446 // nested calls are OK, OS2ToWinCallback for instance
447 //else DebugInt3();
448
449 return GetFS();
450}
451//******************************************************************************
452// ODIN_SetTIBSwitch: override TIB switching
453//
454// Parameters:
455// BOOL fSwitchTIB
456// FALSE -> no TIB selector switching
457// TRUE -> force TIB selector switching
458//
459//******************************************************************************
460void WIN32API ODIN_SetTIBSwitch(BOOL fSwitchTIB)
461{
462 dprintf(("ODIN_SetTIBSwitch %d", fSwitchTIB));
463 fSwitchTIBSel = fSwitchTIB;
464 if(fSwitchTIBSel) {
465 SetWin32TIB();
466 }
467 else RestoreOS2TIB();
468}
469//******************************************************************************
470//******************************************************************************
471//#define DEBUG_HEAPSTATE
472#ifdef DEBUG_HEAPSTATE
473char *pszHeapDump = NULL;
474char *pszHeapDumpStart = NULL;
475
476int _LNK_CONV callback_function(const void *pentry, size_t sz, int useflag, int status,
477 const char *filename, size_t line)
478{
479 if (_HEAPOK != status) {
480// dprintf(("status is not _HEAPOK."));
481 return 1;
482 }
483 if (_USEDENTRY == useflag && sz && filename && line && pszHeapDump) {
484 sprintf(pszHeapDump, "allocated %08x %u at %s %d\n", pentry, sz, filename, line);
485 pszHeapDump += strlen(pszHeapDump);
486 }
487
488 return 0;
489}
490//******************************************************************************
491//******************************************************************************
492#endif
493VOID WIN32API ExitProcess(DWORD exitcode)
494{
495 HANDLE hThread = GetCurrentThread();
496 TEB *teb;
497
498 dprintf(("KERNEL32: ExitProcess %d (time %x)", exitcode, GetCurrentTime()));
499 dprintf(("KERNEL32: ExitProcess FS = %x\n", GetFS()));
500
501 fExitProcess = TRUE;
502
503 SetOS2ExceptionChain(-1);
504
505 HMDeviceCommClass::CloseOverlappedIOHandlers();
506
507 //detach all dlls (LIFO order) before really unloading them; this
508 //should take care of circular dependencies (crash while accessing
509 //memory of a dll that has just been freed)
510 dprintf(("********************************************"));
511 dprintf(("**** Detach process from all dlls -- START"));
512 Win32DllBase::detachProcessFromAllDlls();
513 dprintf(("**** Detach process from all dlls -- END"));
514 dprintf(("********************************************"));
515
516 if(WinExe) {
517 delete(WinExe);
518 WinExe = NULL;
519 }
520
521 //Note: Needs to be done after deleting WinExe (destruction of exe + dll objects)
522 //Flush and delete all open memory mapped files
523 Win32MemMap::deleteAll();
524
525 //SvL: We must make sure no threads are still suspended (with SuspendThread)
526 // OS/2 seems to be unable to terminate the process otherwise (exitlist hang)
527 threadListMutex.enter();
528 teb = threadList;
529 while(teb) {
530 dprintf(("Active thread id %d, handle %x", LOWORD(teb->o.odin.threadId), teb->o.odin.hThread));
531 if(teb->o.odin.hThread != hThread && teb->o.odin.dwSuspend > 0) {
532 //kill any threads that are suspended; dangerous, but so is calling
533 //SuspendThread; we assume the app knew what it was doing
534 TerminateThread(teb->o.odin.hThread, 0);
535 ResumeThread(teb->o.odin.hThread);
536 }
537 teb = teb->o.odin.next;
538 }
539 threadListMutex.leave();
540
541#ifdef DEBUG_HEAPSTATE
542 pszHeapDumpStart = pszHeapDump = (char *)malloc(10*1024*1024);
543 _heap_walk(callback_function);
544 dprintf((pszHeapDumpStart));
545 free(pszHeapDumpStart);
546#endif
547
548#ifdef PROFILE
549 // Note: after this point we do not expect any more Win32-API calls,
550 // so this is probably the best time to dump the gathered profiling
551 // information
552 PerfView_Write();
553 ProfilerWrite();
554 ProfilerTerminate();
555#endif /* PROFILE */
556
557 //Restore original OS/2 TIB selector
558 teb = GetThreadTEB();
559 if(teb) DestroyTEB(teb);
560 SetExceptionChain((ULONG)-1);
561
562 //avoid crashes since win32 & OS/2 exception handler aren't identical
563 //(terminate process generates two exceptions)
564 /* @@@PH 1998/02/12 Added Console Support */
565 if (iConsoleIsActive())
566 iConsoleWaitClose();
567
568 dprintf(("KERNEL32: ExitProcess done (time %x)", GetCurrentTime()));
569 O32_ExitProcess(exitcode);
570}
571//******************************************************************************
572//******************************************************************************
573BOOL WIN32API FreeLibrary(HINSTANCE hinstance)
574{
575 Win32DllBase *winmod;
576 BOOL rc;
577
578 SetLastError(ERROR_SUCCESS);
579 //SvL: Ignore FreeLibary for executable
580 if(WinExe && hinstance == WinExe->getInstanceHandle()) {
581 return TRUE;
582 }
583
584 winmod = Win32DllBase::findModule(hinstance);
585 if(winmod) {
586 dprintf(("FreeLibrary %s", winmod->getName()));
587 //Only free it when the nrDynamicLibRef != 0
588 //This prevent problems after ExitProcess:
589 //i.e. dll A is referenced by our exe and loaded with LoadLibrary by dll B
590 // During ExitProcess it's unloaded once (before dll B), dll B calls
591 // FreeLibrary, but our exe also has a reference -> unloaded too many times
592 if(winmod->isDynamicLib()) {
593 winmod->decDynamicLib();
594 winmod->Release();
595 }
596 else {
597 dprintf(("Skipping dynamic unload as nrDynamicLibRef == 0"));
598 }
599 return(TRUE);
600 }
601 dprintf(("WARNING: KERNEL32: FreeLibrary %s %x NOT FOUND!", OSLibGetDllName(hinstance), hinstance));
602 return(TRUE);
603}
604/*****************************************************************************
605 * Name : VOID WIN32API FreeLibraryAndExitThread
606 * Purpose : The FreeLibraryAndExitThread function decrements the reference
607 * count of a loaded dynamic-link library (DLL) by one, and then
608 * calls ExitThread to terminate the calling thread.
609 * The function does not return.
610 *
611 * The FreeLibraryAndExitThread function gives threads that are
612 * created and executed within a dynamic-link library an opportunity
613 * to safely unload the DLL and terminate themselves.
614 * Parameters:
615 * Variables :
616 * Result :
617 * Remark :
618 *****************************************************************************/
619VOID WIN32API FreeLibraryAndExitThread( HMODULE hLibModule, DWORD dwExitCode)
620{
621
622 dprintf(("KERNEL32: FreeLibraryAndExitThread(%08x,%08x)", hLibModule, dwExitCode));
623 FreeLibrary(hLibModule);
624 ExitThread(dwExitCode);
625}
626/******************************************************************************/
627/******************************************************************************/
628/**
629 * LoadLibraryA can be used to map a DLL module into the calling process's
630 * addressspace. It returns a handle that can be used with GetProcAddress to
631 * get addresses of exported entry points (functions and variables).
632 *
633 * LoadLibraryA can also be used to map executable (.exe) modules into the
634 * address to access resources in the module. However, LoadLibrary can't be
635 * used to run an executable (.exe) module.
636 *
637 * @returns Handle to the library which was loaded.
638 * @param lpszLibFile Pointer to zero ASCII string giving the name of the
639 * executable image (either a Dll or an Exe) which is to be
640 * loaded.
641 *
642 * If no extention is specified the default .DLL extention is
643 * appended to the name. End the filename with an '.' if the
644 * file does not have an extention (and don't want the .DLL
645 * appended).
646 *
647 * If no path is specified, this API will use the Odin32
648 * standard search strategy to find the file. This strategy
649 * is described in the method Win32ImageBase::findDLL.
650 *
651 * This API likes to have backslashes (\), but will probably
652 * accept forward slashes too. Win32 SDK docs says that it
653 * should not contain forward slashes.
654 *
655 * Win32 SDK docs adds:
656 * "The name specified is the file name of the module and
657 * is not related to the name stored in the library module
658 * itself, as specified by the LIBRARY keyword in the
659 * module-definition (.def) file."
660 *
661 * @sketch Call LoadLibraryExA with flags set to 0.
662 * @status Odin32 Completely Implemented.
663 * @author Sander van Leeuwen (sandervl@xs4all.nl)
664 * knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
665 * @remark Forwards to LoadLibraryExA.
666 */
667HINSTANCE WIN32API LoadLibraryA(LPCTSTR lpszLibFile)
668{
669 HINSTANCE hDll;
670
671 dprintf(("KERNEL32: LoadLibraryA(%s) --> LoadLibraryExA(lpszLibFile, 0, 0)",
672 lpszLibFile));
673 hDll = LoadLibraryExA(lpszLibFile, 0, 0);
674 dprintf(("KERNEL32: LoadLibraryA(%s) returns 0x%x",
675 lpszLibFile, hDll));
676 return hDll;
677}
678
679
680/**
681 * LoadLibraryW can be used to map a DLL module into the calling process's
682 * addressspace. It returns a handle that can be used with GetProcAddress to
683 * get addresses of exported entry points (functions and variables).
684 *
685 * LoadLibraryW can also be used to map executable (.exe) modules into the
686 * address to access resources in the module. However, LoadLibrary can't be
687 * used to run an executable (.exe) module.
688 *
689 * @returns Handle to the library which was loaded.
690 * @param lpszLibFile Pointer to Unicode string giving the name of
691 * the executable image (either a Dll or an Exe) which is to
692 * be loaded.
693 *
694 * If no extention is specified the default .DLL extention is
695 * appended to the name. End the filename with an '.' if the
696 * file does not have an extention (and don't want the .DLL
697 * appended).
698 *
699 * If no path is specified, this API will use the Odin32
700 * standard search strategy to find the file. This strategy
701 * is described in the method Win32ImageBase::findDLL.
702 *
703 * This API likes to have backslashes (\), but will probably
704 * accept forward slashes too. Win32 SDK docs says that it
705 * should not contain forward slashes.
706 *
707 * Win32 SDK docs adds:
708 * "The name specified is the file name of the module and
709 * is not related to the name stored in the library module
710 * itself, as specified by the LIBRARY keyword in the
711 * module-definition (.def) file."
712 *
713 * @sketch Convert Unicode name to ascii.
714 * Call LoadLibraryExA with flags set to 0.
715 * free ascii string.
716 * @status Odin32 Completely Implemented.
717 * @author Sander van Leeuwen (sandervl@xs4all.nl)
718 * knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
719 * @remark Forwards to LoadLibraryExA.
720 */
721HINSTANCE WIN32API LoadLibraryW(LPCWSTR lpszLibFile)
722{
723 char * pszAsciiLibFile;
724 HINSTANCE hDll;
725
726 pszAsciiLibFile = UnicodeToAsciiString(lpszLibFile);
727 dprintf(("KERNEL32: LoadLibraryW(%s) --> LoadLibraryExA(lpszLibFile, 0, 0)",
728 pszAsciiLibFile));
729 hDll = LoadLibraryExA(pszAsciiLibFile, NULL, 0);
730 dprintf(("KERNEL32: LoadLibraryW(%s) returns 0x%x",
731 pszAsciiLibFile, hDll));
732 FreeAsciiString(pszAsciiLibFile);
733
734 return hDll;
735}
736
737//******************************************************************************
738//Custom build function to disable loading of LX dlls
739static BOOL fDisableLXDllLoading = FALSE;
740//******************************************************************************
741void WIN32API ODIN_DisableLXDllLoading()
742{
743 fDisableLXDllLoading = TRUE;
744}
745
746/**
747 * LoadLibraryExA can be used to map a DLL module into the calling process's
748 * addressspace. It returns a handle that can be used with GetProcAddress to
749 * get addresses of exported entry points (functions and variables).
750 *
751 * LoadLibraryExA can also be used to map executable (.exe) modules into the
752 * address to access resources in the module. However, LoadLibrary can't be
753 * used to run an executable (.exe) module.
754 *
755 * @returns Handle to the library which was loaded.
756 * @param lpszLibFile Pointer to Unicode string giving the name of
757 * the executable image (either a Dll or an Exe) which is to
758 * be loaded.
759 *
760 * If no extention is specified the default .DLL extention is
761 * appended to the name. End the filename with an '.' if the
762 * file does not have an extention (and don't want the .DLL
763 * appended).
764 *
765 * If no path is specified, this API will use the Odin32
766 * standard search strategy to find the file. This strategy
767 * is described in the method Win32ImageBase::findDLL.
768 * This may be alterned by the LOAD_WITH_ALTERED_SEARCH_PATH
769 * flag, see below.
770 *
771 * This API likes to have backslashes (\), but will probably
772 * accept forward slashes too. Win32 SDK docs says that it
773 * should not contain forward slashes.
774 *
775 * Win32 SDK docs adds:
776 * "The name specified is the file name of the module and
777 * is not related to the name stored in the library module
778 * itself, as specified by the LIBRARY keyword in the
779 * module-definition (.def) file."
780 *
781 * @param hFile Reserved. Must be 0.
782 *
783 * @param dwFlags Flags which specifies the taken when loading the module.
784 * The value 0 makes it identical to LoadLibraryA/W.
785 *
786 * Flags:
787 *
788 * DONT_RESOLVE_DLL_REFERENCES
789 * (WinNT/2K feature): Don't load imported modules and
790 * hence don't resolve imported symbols.
791 * DllMain isn't called either. (Which is obvious since
792 * it may use one of the importe symbols.)
793 *
794 * On the other hand, if this flag is NOT set, the system
795 * load imported modules, resolves imported symbols, calls
796 * DllMain for process and thread init and term (if wished
797 * by the module).
798 *
799 *
800 * LOAD_LIBRARY_AS_DATAFILE
801 * If this flag is set, the module is mapped into the
802 * address space but is not prepared for execution. Though
803 * it's preparted for resource API. Hence, you'll use this
804 * flag when you want to load a DLL for extracting
805 * messages or resources from it.
806 *
807 * The resulting handle can be used with any Odin32 API
808 * which operates on resources.
809 * (WinNt/2k supports all resource APIs while Win9x don't
810 * support the specialized resource APIs: LoadBitmap,
811 * LoadCursor, LoadIcon, LoadImage, LoadMenu.)
812 *
813 *
814 * LOAD_WITH_ALTERED_SEARCH_PATH
815 * If this flag is set and lpszLibFile specifies a path
816 * we'll use an alternative file search strategy to find
817 * imported modules. This stratgy is simply to use the
818 * path of the module being loaded instead of the path
819 * of the executable module as the first location
820 * to search for imported modules.
821 *
822 * If this flag is clear, the standard Odin32 standard
823 * search strategy. See Win32ImageBase::findDll for
824 * further information.
825 *
826 * not implemented yet.
827 *
828 * @status Open32 Partially Implemented.
829 * @author Sander van Leeuwen (sandervl@xs4all.nl)
830 * knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
831 * @remark Forwards to LoadLibraryExA.
832 */
833HINSTANCE WIN32API LoadLibraryExA(LPCTSTR lpszLibFile, HANDLE hFile, DWORD dwFlags)
834{
835 HINSTANCE hDll;
836 Win32DllBase * pModule;
837 char szModname[CCHMAXPATH];
838 BOOL fPath; /* Flags which is set if the */
839 /* lpszLibFile contains a path. */
840 ULONG fPE; /* isPEImage return value. */
841 DWORD Characteristics; //file header's Characteristics
842 char *dot;
843
844 /** @sketch
845 * Some parameter validations is probably useful.
846 */
847 if (!VALID_PSZ(lpszLibFile))
848 {
849 dprintf(("KERNEL32: LoadLibraryExA(0x%x, 0x%x, 0x%x): invalid pointer lpszLibFile = 0x%x\n",
850 lpszLibFile, hFile, dwFlags, lpszLibFile));
851 SetLastError(ERROR_INVALID_PARAMETER); //or maybe ERROR_ACCESS_DENIED is more appropriate?
852 return NULL;
853 }
854 if (!VALID_PSZMAXSIZE(lpszLibFile, CCHMAXPATH))
855 {
856 dprintf(("KERNEL32: LoadLibraryExA(%s, 0x%x, 0x%x): lpszLibFile string too long, %d\n",
857 lpszLibFile, hFile, dwFlags, strlen(lpszLibFile)));
858 SetLastError(ERROR_INVALID_PARAMETER);
859 return NULL;
860 }
861 if ((dwFlags & ~(DONT_RESOLVE_DLL_REFERENCES | LOAD_WITH_ALTERED_SEARCH_PATH | LOAD_LIBRARY_AS_DATAFILE)) != 0)
862 {
863 dprintf(("KERNEL32: LoadLibraryExA(%s, 0x%x, 0x%x): dwFlags have invalid or unsupported flags\n",
864 lpszLibFile, hFile, dwFlags));
865 SetLastError(ERROR_INVALID_PARAMETER);
866 return NULL;
867 }
868
869 /** @sketch
870 * First we'll see if the module is allready loaded - either as the EXE or as DLL.
871 * IF Executable present AND libfile matches the modname of the executable THEN
872 * RETURN instance handle of executable.
873 * Endif
874 * IF allready loaded THEN
875 * IF it's a LX dll which isn't loaded and we're using the PeLoader THEN
876 * Set Load library.
877 * Endif
878 * Inc dynamic reference count.
879 * Inc reference count.
880 * RETURN instance handle.
881 * Endif
882 */
883 strcpy(szModname, lpszLibFile);
884 strupr(szModname);
885 dot = strchr(szModname, '.');
886 if(dot == NULL) {
887 //if there's no extension or trainling dot, we
888 //assume it's a dll (see Win32 SDK docs)
889 strcat(szModname, DLL_EXTENSION);
890 }
891 else {
892 if(dot[1] == 0) {
893 //a trailing dot means the module has no extension (SDK docs)
894 *dot = 0;
895 }
896 }
897 if (WinExe != NULL && WinExe->matchModName(szModname))
898 return WinExe->getInstanceHandle();
899
900 pModule = Win32DllBase::findModule((LPSTR)szModname);
901 if (pModule)
902 {
903 pModule->incDynamicLib();
904 pModule->AddRef();
905 dprintf(("KERNEL32: LoadLibraryExA(%s, 0x%x, 0x%x): returns 0x%x. Dll found %s",
906 szModname, hFile, dwFlags, pModule->getInstanceHandle(), pModule->getFullPath()));
907 return pModule->getInstanceHandle();
908 }
909
910
911 /** @sketch
912 * Test if lpszLibFile has a path or not.
913 * Copy the lpszLibFile to szModname, rename the dll and uppercase the name.
914 * IF it hasn't a path THEN
915 * Issue a findDll to find the dll/executable to be loaded.
916 * IF the Dll isn't found THEN
917 * Set last error and RETURN.
918 * Endif.
919 * Endif
920 */
921 fPath = strchr(szModname, '\\') || strchr(szModname, '/');
922 Win32DllBase::renameDll(szModname);
923
924 if (!fPath)
925 {
926 char szModName2[CCHMAXPATH];
927 strcpy(szModName2, szModname);
928 if (!Win32ImageBase::findDll(szModName2, szModname, sizeof(szModname)))
929 {
930 dprintf(("KERNEL32: LoadLibraryExA(%s, 0x%x, 0x%x): module wasn't found. returns NULL",
931 lpszLibFile, hFile, dwFlags));
932 SetLastError(ERROR_FILE_NOT_FOUND);
933 return NULL;
934 }
935 }
936
937 //test if dll is in PE or LX format
938 fPE = Win32ImageBase::isPEImage(szModname, &Characteristics, NULL);
939
940 /** @sketch
941 * IF (fDisableLXDllLoading && (!fPeLoader || fPE == failure)) THEN
942 * Try load the executable using LoadLibrary
943 * IF successfully loaded THEN
944 * IF LX dll and is using the PE Loader THEN
945 * Set Load library.
946 * Inc reference count.
947 * Endif
948 * Inc dynamic reference count.
949 * RETURN successfully.
950 * Endif
951 * Endif
952 */
953 //only call OS/2 if LX binary or win32k process
954 if (!fDisableLXDllLoading && (!fPeLoader || fPE != ERROR_SUCCESS))
955 {
956 hDll = OSLibDosLoadModule(szModname);
957 if (hDll)
958 {
959 /* OS/2 dll, system dll, converted dll or win32k took care of it. */
960 pModule = Win32DllBase::findModuleByOS2Handle(hDll);
961 if (pModule)
962 {
963 if (pModule->isLxDll())
964 {
965 ((Win32LxDll *)pModule)->setDllHandleOS2(hDll);
966 if (fPeLoader && pModule->AddRef() == -1)
967 { //-1 -> load failed (attachProcess)
968 delete pModule;
969 SetLastError(ERROR_INVALID_EXE_SIGNATURE);
970 dprintf(("Dll %s refused to be loaded; aborting", szModname));
971 return 0;
972 }
973
974 }
975 pModule->incDynamicLib();
976 }
977 else if (fExeStarted && !fIsOS2Image) {
978 OSLibDosFreeModule(hDll);
979 SetLastError(ERROR_INVALID_EXE_SIGNATURE);
980 dprintf(("Dll %s is not an Odin dll; unload & return failure", szModname));
981 return 0;
982 }
983 else {
984 /* bird 2001-07-10:
985 * let's fail right away instead of hitting DebugInt3s and fail other places.
986 * This is very annoying when running Opera on a debug build with netscape/2
987 * plugins present. We'll make this conditional for the time being.
988 */
989 static BOOL fFailIfUnregisteredLX = -1;
990 if (fFailIfUnregisteredLX == -1)
991 fFailIfUnregisteredLX = getenv("ODIN32.FAIL_IF_UNREGISTEREDLX") != NULL;
992 if (fExeStarted && fFailIfUnregisteredLX)
993 {
994 dprintf(("KERNEL32: LoadLibraryExA(%s, 0x%x, 0x%x): returns 0x%x. Loaded OS/2 dll %s using DosLoadModule. returns NULL.",
995 lpszLibFile, hFile, dwFlags, hDll, szModname));
996 SetLastError(ERROR_INVALID_EXE_SIGNATURE);
997 return NULL;
998 }
999 dprintf(("KERNEL32: LoadLibraryExA(%s, 0x%x, 0x%x): returns 0x%x. Loaded OS/2 dll %s using DosLoadModule.",
1000 lpszLibFile, hFile, dwFlags, hDll, szModname));
1001 return hDll; //happens when LoadLibrary is called in kernel32's initterm (nor harmful)
1002 }
1003 dprintf(("KERNEL32: LoadLibraryExA(%s, 0x%x, 0x%x): returns 0x%x. Loaded %s using DosLoadModule.",
1004 lpszLibFile, hFile, dwFlags, hDll, szModname));
1005 return pModule->getInstanceHandle();
1006 }
1007 dprintf(("KERNEL32: LoadLibraryExA(%s, 0x%x, 0x%x): DosLoadModule (%s) failed. LastError=%d",
1008 lpszLibFile, hFile, dwFlags, szModname, GetLastError()));
1009 }
1010 else
1011 hDll = NULL;
1012
1013
1014 /** @sketch
1015 * If PE image THEN
1016 * IF LOAD_LIBRARY_AS_DATAFILE or Executable THEN
1017 *
1018 *
1019 * Try load the file using the Win32PeLdrDll class.
1020 * <sketch continued further down>
1021 * Else
1022 * Set last error.
1023 * (hDll is NULL)
1024 * Endif
1025 * return hDll.
1026 */
1027 if(fPE == ERROR_SUCCESS)
1028 {
1029 Win32PeLdrDll *peldrDll;
1030
1031 //SvL: If executable -> load as data file (only resources)
1032 if(!(Characteristics & IMAGE_FILE_DLL))
1033 {
1034 dwFlags |= (LOAD_LIBRARY_AS_DATAFILE | DONT_RESOLVE_DLL_REFERENCES);
1035 }
1036
1037 peldrDll = new Win32PeLdrDll(szModname);
1038 if (peldrDll == NULL)
1039 {
1040 dprintf(("KERNEL32: LoadLibraryExA(%s, 0x%x, 0x%x): Failed to created instance of Win32PeLdrDll. returns NULL.",
1041 lpszLibFile, hFile, dwFlags));
1042 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1043 return NULL;
1044 }
1045
1046 /** @sketch
1047 * Process dwFlags
1048 */
1049 if (dwFlags & LOAD_LIBRARY_AS_DATAFILE)
1050 {
1051 dprintf(("KERNEL32: LoadLibraryExA(%s, 0x%x, 0x%x): LOAD_LIBRARY_AS_DATAFILE",
1052 lpszLibFile, hFile, dwFlags));
1053 peldrDll->setLoadAsDataFile();
1054 peldrDll->disableLibraryCalls();
1055 }
1056 if (dwFlags & DONT_RESOLVE_DLL_REFERENCES)
1057 {
1058 dprintf(("KERNEL32: LoadLibraryExA(%s, 0x%x, 0x%x): DONT_RESOLVE_DLL_REFERENCES",
1059 lpszLibFile, hFile, dwFlags));
1060 peldrDll->disableLibraryCalls();
1061 peldrDll->disableImportHandling();
1062 }
1063 if (dwFlags & LOAD_WITH_ALTERED_SEARCH_PATH)
1064 {
1065 dprintf(("KERNEL32: LoadLibraryExA(%s, 0x%x, 0x%x): Warning dwFlags LOAD_WITH_ALTERED_SEARCH_PATH is not implemented.",
1066 lpszLibFile, hFile, dwFlags));
1067 //peldrDll->setLoadWithAlteredSearchPath();
1068 }
1069
1070 /** @sketch
1071 * Initiate the peldr DLL.
1072 * IF successful init THEN
1073 * Inc dynamic ref count.
1074 * Inc ref count.
1075 * Attach to process
1076 * IF successful THEN
1077 * hDLL <- instance handle.
1078 * ELSE
1079 * set last error
1080 * delete Win32PeLdrDll instance.
1081 * Endif
1082 * ELSE
1083 * set last error
1084 * delete Win32PeLdrDll instance.
1085 * Endif.
1086 */
1087 if(peldrDll->init(0) == LDRERROR_SUCCESS)
1088 {
1089 peldrDll->AddRef();
1090 if (peldrDll->attachProcess())
1091 {
1092 hDll = peldrDll->getInstanceHandle();
1093 //Must be called *after* attachprocess, since attachprocess may also
1094 //trigger LoadLibrary calls
1095 //Those dlls must not be put in front of this dll in the dynamic
1096 //dll list; or else the unload order is wrong:
1097 //i.e. RPAP3260 loads PNRS3260 in DLL_PROCESS_ATTACH
1098 // this means that in ExitProcess, PNRS3260 needs to be removed
1099 // first since RPAP3260 depends on it
1100 peldrDll->incDynamicLib();
1101 }
1102 else
1103 {
1104 dprintf(("KERNEL32: LoadLibraryExA(%s, 0x%x, 0x%x): attachProcess call to Win32PeLdrDll instance failed. returns NULL.",
1105 lpszLibFile, hFile, dwFlags));
1106 SetLastError(ERROR_DLL_INIT_FAILED);
1107 delete peldrDll;
1108 return NULL;
1109 }
1110 }
1111 else
1112 {
1113 dprintf(("KERNEL32: LoadLibraryExA(%s, 0x%x, 0x%x): Failed to init Win32PeLdrDll instance. error=%d returns NULL.",
1114 lpszLibFile, hFile, dwFlags, peldrDll->getError()));
1115 SetLastError(ERROR_INVALID_EXE_SIGNATURE);
1116 delete peldrDll;
1117 return NULL;
1118 }
1119 }
1120 else
1121 {
1122 dprintf(("KERNEL32: LoadLibraryExA(%s, 0x%x, 0x%x) library wasn't found (%s) or isn't loadable; err %x",
1123 lpszLibFile, hFile, dwFlags, szModname, fPE));
1124 SetLastError(fPE);
1125 return NULL;
1126 }
1127
1128 return hDll;
1129}
1130
1131
1132/**
1133 * LoadLibraryExW can be used to map a DLL module into the calling process's
1134 * addressspace. It returns a handle that can be used with GetProcAddress to
1135 * get addresses of exported entry points (functions and variables).
1136 *
1137 * LoadLibraryExW can also be used to map executable (.exe) modules into the
1138 * address to access resources in the module. However, LoadLibrary can't be
1139 * used to run an executable (.exe) module.
1140 *
1141 * @returns Handle to the library which was loaded.
1142 * @param lpszLibFile Pointer to Unicode string giving the name of
1143 * the executable image (either a Dll or an Exe) which is to
1144 * be loaded.
1145 *
1146 * If no extention is specified the default .DLL extention is
1147 * appended to the name. End the filename with an '.' if the
1148 * file does not have an extention (and don't want the .DLL
1149 * appended).
1150 *
1151 * If no path is specified, this API will use the Odin32
1152 * standard search strategy to find the file. This strategy
1153 * is described in the method Win32ImageBase::findDLL.
1154 * This may be alterned by the LOAD_WITH_ALTERED_SEARCH_PATH
1155 * flag, see below.
1156 *
1157 * This API likes to have backslashes (\), but will probably
1158 * accept forward slashes too. Win32 SDK docs says that it
1159 * should not contain forward slashes.
1160 *
1161 * Win32 SDK docs adds:
1162 * "The name specified is the file name of the module and
1163 * is not related to the name stored in the library module
1164 * itself, as specified by the LIBRARY keyword in the
1165 * module-definition (.def) file."
1166 *
1167 * @param hFile Reserved. Must be 0.
1168 *
1169 * @param dwFlags Flags which specifies the taken when loading the module.
1170 * The value 0 makes it identical to LoadLibraryA/W.
1171 *
1172 * Flags:
1173 *
1174 * DONT_RESOLVE_DLL_REFERENCES
1175 * (WinNT/2K feature): Don't load imported modules and
1176 * hence don't resolve imported symbols.
1177 * DllMain isn't called either. (Which is obvious since
1178 * it may use one of the importe symbols.)
1179 *
1180 * On the other hand, if this flag is NOT set, the system
1181 * load imported modules, resolves imported symbols, calls
1182 * DllMain for process and thread init and term (if wished
1183 * by the module).
1184 *
1185 * LOAD_LIBRARY_AS_DATAFILE
1186 * If this flag is set, the module is mapped into the
1187 * address space but is not prepared for execution. Though
1188 * it's preparted for resource API. Hence, you'll use this
1189 * flag when you want to load a DLL for extracting
1190 * messages or resources from it.
1191 *
1192 * The resulting handle can be used with any Odin32 API
1193 * which operates on resources.
1194 * (WinNt/2k supports all resource APIs while Win9x don't
1195 * support the specialized resource APIs: LoadBitmap,
1196 * LoadCursor, LoadIcon, LoadImage, LoadMenu.)
1197 *
1198 * LOAD_WITH_ALTERED_SEARCH_PATH
1199 * If this flag is set and lpszLibFile specifies a path
1200 * we'll use an alternative file search strategy to find
1201 * imported modules. This stratgy is simply to use the
1202 * path of the module being loaded instead of the path
1203 * of the executable module as the first location
1204 * to search for imported modules.
1205 *
1206 * If this flag is clear, the standard Odin32 standard
1207 * search strategy. See Win32ImageBase::findDll for
1208 * further information.
1209 *
1210 * @sketch Convert Unicode name to ascii.
1211 * Call LoadLibraryExA.
1212 * Free ascii string.
1213 * return handle from LoadLibraryExA.
1214 * @status Open32 Partially Implemented.
1215 * @author Sander van Leeuwen (sandervl@xs4all.nl)
1216 * knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
1217 * @remark Forwards to LoadLibraryExA.
1218 */
1219HINSTANCE WIN32API LoadLibraryExW(LPCWSTR lpszLibFile, HANDLE hFile, DWORD dwFlags)
1220{
1221 char * pszAsciiLibFile;
1222 HINSTANCE hDll;
1223
1224 pszAsciiLibFile = UnicodeToAsciiString(lpszLibFile);
1225 dprintf(("KERNEL32: LoadLibraryExW(%s, 0x%x, 0x%x) --> LoadLibraryExA",
1226 pszAsciiLibFile, hFile, dwFlags));
1227 hDll = LoadLibraryExA(pszAsciiLibFile, hFile, dwFlags);
1228 dprintf(("KERNEL32: LoadLibraryExW(%s, 0x%x, 0x%x) returns 0x%x",
1229 pszAsciiLibFile, hFile, dwFlags, hDll));
1230 FreeAsciiString(pszAsciiLibFile);
1231
1232 return hDll;
1233}
1234//******************************************************************************
1235//******************************************************************************
1236HINSTANCE16 WIN32API LoadLibrary16(LPCTSTR lpszLibFile)
1237{
1238 dprintf(("ERROR: LoadLibrary16 %s, not implemented", lpszLibFile));
1239 return 0;
1240}
1241//******************************************************************************
1242//******************************************************************************
1243VOID WIN32API FreeLibrary16(HINSTANCE16 hinstance)
1244{
1245 dprintf(("ERROR: FreeLibrary16 %x, not implemented", hinstance));
1246}
1247//******************************************************************************
1248//******************************************************************************
1249FARPROC WIN32API GetProcAddress16(HMODULE hModule, LPCSTR lpszProc)
1250{
1251 dprintf(("ERROR: GetProcAddress16 %x %x, not implemented", hModule, lpszProc));
1252 return 0;
1253}
1254
1255
1256/**
1257 * Internal function which gets the commandline (string) used to start the current process.
1258 * @returns OS/2 / Windows return code
1259 * On successful return (NO_ERROR) the global variables
1260 * pszCmdLineA and pszCmdLineW are set.
1261 *
1262 * @param pszPeExe Pass in the name of the PE exe of this process. We'll
1263 * us this as exename and skip the first argument (ie. argv[1]).
1264 * If NULL we'll use the commandline from OS/2 as it is.
1265 * @status Completely implemented and tested.
1266 * @author knut st. osmundsen (knut.stange.osmundsen@mynd.no)
1267 */
1268ULONG InitCommandLine(const char *pszPeExe)
1269{
1270 PCHAR pib_pchcmd; /* PIB pointer to commandline. */
1271 CHAR szFilename[CCHMAXPATH]; /* Filename buffer used to get the exe filename in. */
1272 ULONG cch; /* Commandline string length. (including terminator) */
1273 PSZ psz; /* Temporary string pointer. */
1274 PSZ psz2; /* Temporary string pointer. */
1275 APIRET rc; /* OS/2 return code. */
1276 BOOL fQuotes; /* Flag used to remember if the exe filename should be in quotes. */
1277
1278 /** @sketch
1279 * Get commandline from the PIB.
1280 */
1281 pib_pchcmd = (PCHAR)OSLibGetPIB(PIB_PCHCMD);
1282
1283 /** @sketch
1284 * Two methods of making the commandline:
1285 * (1) The first argument is skipped and the second is used as exe filname.
1286 * This applies to PE.EXE launched processes only.
1287 * (2) No skipping. First argument is the exe filename.
1288 * This applies to all but PE.EXE launched processes.
1289 *
1290 * Note: We could do some code size optimization here. Much of the code for
1291 * the two methods are nearly identical.
1292 *
1293 */
1294 if(pszPeExe)
1295 {
1296 /** @sketch
1297 * Allocate memory for the commandline.
1298 * Build commandline:
1299 * Copy exe filename.
1300 * Add arguments.
1301 */
1302 cch = strlen(pszPeExe)+1;
1303
1304 // PH 2002-04-11
1305 // Note: intentional memory leak, pszCmdLineW will not be freed
1306 // or allocated after process startup
1307 pszCmdLineA = psz = (PSZ)malloc(cch);
1308 if (psz == NULL)
1309 {
1310 dprintf(("KERNEL32: InitCommandLine(%p): malloc(%d) failed\n", pszPeExe, cch));
1311 return ERROR_NOT_ENOUGH_MEMORY;
1312 }
1313 strcpy((char *)pszCmdLineA, pszPeExe);
1314
1315 rc = NO_ERROR;
1316 }
1317 else
1318 {
1319 /** @sketch Method (2):
1320 * First we'll have to determin the size of the commandline.
1321 *
1322 * As we don't assume that OS/2 allways puts a fully qualified EXE name
1323 * as the first string, we'll check if it's empty - and get the modulename
1324 * in that case - and allways get the fully qualified filename.
1325 */
1326 if (pib_pchcmd == NULL || pib_pchcmd[0] == '\0')
1327 {
1328 rc = OSLibDosQueryModuleName(OSLibGetPIB(PIB_HMTE), sizeof(szFilename), szFilename);
1329 if (rc != NO_ERROR)
1330 {
1331 dprintf(("KERNEL32: InitCommandLine(%p): OSLibQueryModuleName(0x%x,...) failed with rc=%d\n",
1332 pszPeExe, OSLibGetPIB(PIB_HMTE), rc));
1333 return rc;
1334 }
1335 }
1336 else
1337 {
1338 rc = OSLibDosQueryPathInfo(pib_pchcmd, FIL_QUERYFULLNAME, szFilename, sizeof(szFilename));
1339 if (rc != NO_ERROR)
1340 {
1341 dprintf(("KERNEL32: InitCommandLine(%p): (info) OSLibDosQueryPathInfo failed with rc=%d\n", pszPeExe, rc));
1342 strcpy(szFilename, pib_pchcmd);
1343 rc = NO_ERROR;
1344 }
1345 }
1346
1347 /** @sketch
1348 * We're still measuring the size of the commandline:
1349 * Check if we have to quote the exe filename.
1350 * Determin the length of the executable name including quotes and '\0'-terminator.
1351 * Count the length of the arguments. (We here count's all argument strings.)
1352 */
1353 fQuotes = strchr(szFilename, ' ') != NULL;
1354 cch = strlen(szFilename) + fQuotes*2 + 1;
1355 if (pib_pchcmd != NULL)
1356 {
1357 psz2 = pib_pchcmd + strlen(pib_pchcmd) + 1;
1358 while (*psz2 != '\0')
1359 {
1360 register int cchTmp = strlen(psz2) + 1; /* + 1 is for terminator (psz2) and space (cch). */
1361 psz2 += cchTmp;
1362 cch += cchTmp;
1363 }
1364 }
1365
1366 /** @sketch
1367 * Allocate memory for the commandline.
1368 * Build commandline:
1369 * Copy exe filename.
1370 * Add arguments.
1371 */
1372 pszCmdLineA = psz = (PSZ)malloc(cch);
1373 if (psz == NULL)
1374 {
1375 dprintf(("KERNEL32: InitCommandLine(%p): malloc(%d) failed\n", pszPeExe, cch));
1376 return ERROR_NOT_ENOUGH_MEMORY;
1377 }
1378
1379 if (fQuotes)
1380 *psz++ = '"';
1381 strcpy(psz, szFilename);
1382 psz += strlen(psz);
1383 if (fQuotes)
1384 {
1385 *psz++ = '"';
1386 *psz = '\0';
1387 }
1388
1389 if (pib_pchcmd != NULL)
1390 {
1391 psz2 = pib_pchcmd + strlen(pib_pchcmd) + 1;
1392 while (*psz2 != '\0')
1393 {
1394 register int cchTmp = strlen(psz2) + 1; /* + 1 is for terminator (psz). */
1395 *psz++ = ' '; /* add space */
1396 memcpy(psz, psz2, cchTmp);
1397 psz2 += cchTmp;
1398 psz += cchTmp - 1;
1399 }
1400 }
1401 }
1402
1403 /** @sketch
1404 * If successfully build ASCII commandline then convert it to UniCode.
1405 */
1406 if (rc == NO_ERROR)
1407 {
1408 // PH 2002-04-11
1409 // Note: intentional memory leak, pszCmdLineW will not be freed
1410 // or allocated after process startup
1411 cch = strlen(pszCmdLineA) + 1;
1412
1413 pszCmdLineW = (WCHAR*)malloc(cch * 2);
1414 if (pszCmdLineW != NULL) {
1415 //Translate from OS/2 to Windows codepage & ascii to unicode
1416 MultiByteToWideChar(CP_OEMCP, 0, pszCmdLineA, -1, (LPWSTR)pszCmdLineW, cch-1);
1417 ((LPWSTR)pszCmdLineW)[cch-1] = 0;
1418
1419 //ascii command line is still in OS/2 codepage, so convert it
1420 WideCharToMultiByte(CP_ACP, 0, pszCmdLineW, -1, (LPSTR)pszCmdLineA, cch-1, 0, NULL);
1421 ((LPSTR)pszCmdLineA)[cch-1] = 0;
1422 }
1423 else
1424 {
1425 DebugInt3();
1426 dprintf(("KERNEL32: InitCommandLine(%p): malloc(%d) failed (2)\n", pszPeExe, cch));
1427 rc = ERROR_NOT_ENOUGH_MEMORY;
1428 }
1429 }
1430
1431 return rc;
1432}
1433
1434/**
1435 * Gets the command line of the current process.
1436 * @returns On success:
1437 * Command line of the current process. One single string.
1438 * The first part of the command line string is the executable filename
1439 * of the current process. It might be in quotes if it contains spaces.
1440 * The rest of the string is arguments.
1441 *
1442 * On error:
1443 * NULL. Last error set. (does Win32 set last error this?)
1444 * @sketch IF not inited THEN
1445 * Init commandline assuming !PE.EXE
1446 * IF init failes THEN set last error.
1447 * ENDIF
1448 * return ASCII/ANSI commandline.
1449 * @status Completely implemented and tested.
1450 * @author knut st. osmundsen (knut.stange.osmundsen@mynd.no)
1451 * @remark The Ring-3 PeLdr is resposible for calling InitCommandLine before anyone
1452 * is able to call this function.
1453 */
1454LPCSTR WIN32API GetCommandLineA(VOID)
1455{
1456 /*
1457 * Check if the commandline is initiated.
1458 * If not we'll have to do it.
1459 * ASSUMES that if not inited this isn't a PE.EXE lauched process.
1460 */
1461 if (pszCmdLineA == NULL)
1462 {
1463 APIRET rc;
1464 rc = InitCommandLine(NULL);
1465 if (rc != NULL)
1466 SetLastError(rc);
1467 }
1468
1469 dprintf(("KERNEL32: GetCommandLineA: %s\n", pszCmdLineA));
1470 return pszCmdLineA;
1471}
1472
1473
1474/**
1475 * Gets the command line of the current process.
1476 * @returns On success:
1477 * Command line of the current process. One single string.
1478 * The first part of the command line string is the executable filename
1479 * of the current process. It might be in quotes if it contains spaces.
1480 * The rest of the string is arguments.
1481 *
1482 * On error:
1483 * NULL. Last error set. (does Win32 set last error this?)
1484 * @sketch IF not inited THEN
1485 * Init commandline assuming !PE.EXE
1486 * IF init failes THEN set last error.
1487 * ENDIF
1488 * return Unicode commandline.
1489 * @status Completely implemented and tested.
1490 * @author knut st. osmundsen (knut.stange.osmundsen@mynd.no)
1491 * @remark The Ring-3 PeLdr is resposible for calling InitCommandLine before anyone
1492 * is able to call this function.
1493 */
1494LPCWSTR WIN32API GetCommandLineW(void)
1495{
1496 /*
1497 * Check if the commandline is initiated.
1498 * If not we'll have to do it.
1499 * ASSUMES that if not inited this isn't a PE.EXE lauched process.
1500 */
1501 if (pszCmdLineW == NULL)
1502 {
1503 APIRET rc;
1504 rc = InitCommandLine(NULL);
1505 if (rc != NULL)
1506 SetLastError(rc);
1507 }
1508
1509 dprintf(("KERNEL32: GetCommandLineW: %s\n", pszCmdLineA));
1510 return pszCmdLineW;
1511}
1512
1513
1514/**
1515 * GetModuleFileName gets the full path and file name for the specified module.
1516 * @returns Bytes written to the buffer (lpszPath). This count includes the
1517 * terminating '\0'.
1518 * On error 0 is returned. Last error is set.
1519 *
1520 * 2002-04-25 PH
1521 * Q - Do we set ERROR_BUFFER_OVERFLOW when cch > cchPath?
1522 * Q - Does NT really set the last error?
1523 * A > Win2k does not set LastError here, remains OK
1524 *
1525 * While GetModuleFileName does add a trailing termination zero
1526 * if there is enough room, the returned number of characters
1527 * *MUST NOT* include the zero character!
1528 * (Notes R6 Installer on Win2kSP6, verified Testcase)
1529 *
1530 * @param hModule Handle to the module you like to get the file name to.
1531 * @param lpszPath Output buffer for full path and file name.
1532 * @param cchPath Size of the lpszPath buffer.
1533 * @sketch Validate lpszPath.
1534 * Find the module object using handle.
1535 * If found Then
1536 * Get full path from module object.
1537 * If found path Then
1538 * Copy path to buffer and set the number of bytes written.
1539 * Else
1540 * IPE!
1541 * Else
1542 * Call Open32 GetModuleFileName. (kernel32 initterm needs/needed this)
1543 * Log result.
1544 * Return number of bytes written to the buffer.
1545 *
1546 * @status Completely implemented, Open32.
1547 * @author knut st. osmundsen (knut.stange.osmundsen@mynd.no)
1548 * Sander van Leeuwen (sandervl@xs4all.nl)
1549 * Patrick Haller (patrick.haller@innotek.de)
1550 * @remark - Do we still have to call Open32?
1551 */
1552DWORD WIN32API GetModuleFileNameA(HMODULE hModule, LPTSTR lpszPath, DWORD cchPath)
1553{
1554 Win32ImageBase * pMod; /* Pointer to the module object. */
1555 DWORD cch = 0; /* Length of the */
1556
1557 // PH 2002-04-24 Note:
1558 // WIN2k just crashes in NTDLL if lpszPath is invalid!
1559 if (!VALID_PSZ(lpszPath))
1560 {
1561 dprintf(("KERNEL32: GetModuleFileNameA(0x%x, 0x%x, 0x%x): invalid pointer lpszLibFile = 0x%x\n",
1562 hModule, lpszPath, cchPath, lpszPath));
1563 SetLastError(ERROR_INVALID_PARAMETER); //or maybe ERROR_ACCESS_DENIED is more appropriate?
1564 return 0;
1565 }
1566
1567 pMod = Win32ImageBase::findModule(hModule);
1568 if (pMod != NULL)
1569 {
1570 const char *pszFn = pMod->getFullPath();
1571 if (pszFn)
1572 {
1573 cch = strlen(pszFn);
1574 if (cch >= cchPath)
1575 cch = cchPath;
1576 else
1577 // if there is sufficient room for the zero termination,
1578 // write it additionally, uncounted
1579 lpszPath[cch] = '\0';
1580
1581 memcpy(lpszPath, pszFn, cch);
1582 }
1583 else
1584 {
1585 dprintf(("KERNEL32: GetModuleFileNameA(%x,...): IPE - getFullPath returned NULL or empty string\n"));
1586 DebugInt3();
1587 SetLastError(ERROR_INVALID_HANDLE);
1588 }
1589 }
1590 else
1591 {
1592 SetLastError(ERROR_INVALID_HANDLE);
1593 //only needed for call inside kernel32's initterm (profile init)
1594 //(console init only it seems...)
1595 cch = OSLibDosGetModuleFileName(hModule, lpszPath, cchPath);
1596 }
1597
1598 if (cch > 0)
1599 dprintf(("KERNEL32: GetModuleFileNameA(%x %x): %s %d\n", hModule, lpszPath, lpszPath, cch));
1600 else
1601 dprintf(("KERNEL32: WARNING: GetModuleFileNameA(%x,...) - not found!", hModule));
1602
1603 return cch;
1604}
1605
1606
1607/**
1608 * GetModuleFileName gets the full path and file name for the specified module.
1609 * @returns Bytes written to the buffer (lpszPath). This count includes the
1610 * terminating '\0'.
1611 * On error 0 is returned. Last error is set.
1612 * @param hModule Handle to the module you like to get the file name to.
1613 * @param lpszPath Output buffer for full path and file name.
1614 * @param cchPath Size of the lpszPath buffer.
1615 * @sketch Find the module object using handle.
1616 * If found Then
1617 * get full path from module object.
1618 * If found path Then
1619 * Determin path length.
1620 * Translate the path to into the buffer.
1621 * Else
1622 * IPE.
1623 * else
1624 * SetLastError to invalid handle.
1625 * Log result.
1626 * return number of bytes written to the buffer.
1627 *
1628 * @status Completely implemented.
1629 * @author knut st. osmundsen (knut.stange.osmundsen@mynd.no)
1630 * @remark - We do _NOT_ call Open32.
1631 * - Do we set ERROR_BUFFER_OVERFLOW when cch > cchPath?
1632 * - Does NT really set the last error?
1633 */
1634DWORD WIN32API GetModuleFileNameW(HMODULE hModule, LPWSTR lpszPath, DWORD cchPath)
1635{
1636 Win32ImageBase * pMod;
1637 DWORD cch = 0;
1638
1639 if (!VALID_PSZ(lpszPath))
1640 {
1641 dprintf(("KERNEL32: GetModuleFileNameW(0x%x, 0x%x, 0x%x): invalid pointer lpszLibFile = 0x%x\n",
1642 hModule, lpszPath, cchPath, lpszPath));
1643 SetLastError(ERROR_INVALID_PARAMETER); //or maybe ERROR_ACCESS_DENIED is more appropriate?
1644 return 0;
1645 }
1646
1647 pMod = Win32ImageBase::findModule(hModule);
1648 if (pMod != NULL)
1649 {
1650 const char *pszFn = pMod->getFullPath();
1651 if (pszFn || *pszFn != '\0')
1652 {
1653 cch = strlen(pszFn) + 1;
1654 if (cch > cchPath)
1655 cch = cchPath;
1656 AsciiToUnicodeN(pszFn, lpszPath, cch);
1657 }
1658 else
1659 {
1660 dprintf(("KERNEL32: GetModuleFileNameW(%x,...): IPE - getFullPath returned NULL or empty string\n"));
1661 DebugInt3();
1662 SetLastError(ERROR_INVALID_HANDLE);
1663 }
1664 }
1665 else
1666 SetLastError(ERROR_INVALID_HANDLE);
1667
1668 if (cch > 0)
1669 dprintf(("KERNEL32: GetModuleFileNameW(%x,...): %s %d\n", hModule, lpszPath, cch));
1670 else
1671 dprintf(("KERNEL32: WARNING: GetModuleFileNameW(%x,...) - not found!", hModule));
1672
1673 return cch;
1674}
1675
1676
1677//******************************************************************************
1678//NOTE: GetModuleHandleA does NOT support files with multiple dots (i.e.
1679// very.weird.exe)
1680//
1681// hinst = LoadLibrary("WINSPOOL.DRV"); -> succeeds
1682// hinst2 = GetModuleHandle("WINSPOOL.DRV"); -> succeeds
1683// hinst3 = GetModuleHandle("WINSPOOL."); -> fails
1684// hinst4 = GetModuleHandle("WINSPOOL"); -> fails
1685// hinst = LoadLibrary("KERNEL32.DLL"); -> succeeds
1686// hinst2 = GetModuleHandle("KERNEL32.DLL"); -> succeeds
1687// hinst3 = GetModuleHandle("KERNEL32."); -> fails
1688// hinst4 = GetModuleHandle("KERNEL32"); -> succeeds
1689// Same behaviour as observed in NT4, SP6
1690//******************************************************************************
1691HANDLE WIN32API GetModuleHandleA(LPCTSTR lpszModule)
1692{
1693 HANDLE hMod = 0;
1694 Win32DllBase *windll;
1695 char szModule[CCHMAXPATH];
1696 char *dot;
1697
1698 if(lpszModule == NULL)
1699 {
1700 if(WinExe)
1701 hMod = WinExe->getInstanceHandle();
1702 else
1703 {
1704 // // Just fail this API
1705 // hMod = 0;
1706 // SetLastError(ERROR_INVALID_HANDLE);
1707 // Wrong: in an ODIN32-LX environment, just
1708 // assume a fake handle
1709 hMod = -1;
1710 }
1711 }
1712 else
1713 {
1714 strcpy(szModule, OSLibStripPath((char *)lpszModule));
1715 strupr(szModule);
1716 dot = strchr(szModule, '.');
1717 if(dot == NULL) {
1718 //if no extension -> add .DLL (see SDK docs)
1719 strcat(szModule, DLL_EXTENSION);
1720 }
1721 else {
1722 if(dot[1] == 0) {
1723 //a trailing dot means the module has no extension (SDK docs)
1724 *dot = 0;
1725 }
1726 }
1727 if(WinExe && WinExe->matchModName(szModule)) {
1728 hMod = WinExe->getInstanceHandle();
1729 }
1730 else {
1731 windll = Win32DllBase::findModule(szModule);
1732 if(windll) {
1733 hMod = windll->getInstanceHandle();
1734 }
1735 }
1736 }
1737 dprintf(("KERNEL32: GetModuleHandle %s returned %X\n", lpszModule, hMod));
1738 return(hMod);
1739}
1740//******************************************************************************
1741//******************************************************************************
1742HMODULE WIN32API GetModuleHandleW(LPCWSTR lpwszModuleName)
1743{
1744 HMODULE rc;
1745 char *astring = NULL;
1746
1747 if (NULL != lpwszModuleName)
1748 astring = UnicodeToAsciiString((LPWSTR)lpwszModuleName);
1749
1750 rc = GetModuleHandleA(astring);
1751 dprintf(("KERNEL32: OS2GetModuleHandleW %s returned %X\n", astring, rc));
1752
1753 if (NULL != astring)
1754 FreeAsciiString(astring);
1755
1756 return(rc);
1757}
1758//******************************************************************************
1759//******************************************************************************
1760static char szPECmdLoader[260] = "";
1761static char szPEGUILoader[260] = "";
1762static char szNELoader[260] = "";
1763//******************************************************************************
1764//Set default paths for PE & NE loaders
1765//******************************************************************************
1766BOOL InitLoaders()
1767{
1768 sprintf(szPECmdLoader, "%s\\PEC.EXE", InternalGetSystemDirectoryA());
1769 sprintf(szPEGUILoader, "%s\\PE.EXE", InternalGetSystemDirectoryA());
1770 sprintf(szNELoader, "%s\\W16ODIN.EXE", InternalGetSystemDirectoryA());
1771
1772 return TRUE;
1773}
1774//******************************************************************************
1775//Override loader names (PEC, PE, W16ODIN)
1776//******************************************************************************
1777BOOL WIN32API ODIN_SetLoaders(LPCSTR pszPECmdLoader, LPCSTR pszPEGUILoader,
1778 LPCSTR pszNELoader)
1779{
1780 if(pszPECmdLoader) strcpy(szPECmdLoader, pszPECmdLoader);
1781 if(pszPEGUILoader) strcpy(szPEGUILoader, pszPEGUILoader);
1782 if(pszNELoader) strcpy(szNELoader, pszNELoader);
1783
1784 return TRUE;
1785}
1786//******************************************************************************
1787//******************************************************************************
1788BOOL WIN32API ODIN_QueryLoaders(LPSTR pszPECmdLoader, INT cchPECmdLoader,
1789 LPSTR pszPEGUILoader, INT cchPEGUILoader,
1790 LPSTR pszNELoader, INT cchNELoader)
1791{
1792 if(pszPECmdLoader) strncpy(pszPECmdLoader, szPECmdLoader, cchPECmdLoader);
1793 if(pszPEGUILoader) strncpy(pszPEGUILoader, szPEGUILoader, cchPEGUILoader);
1794 if(pszNELoader) strncpy(pszNELoader, szNELoader, cchNELoader);
1795
1796 return TRUE;
1797}
1798//******************************************************************************
1799//******************************************************************************
1800BOOL WINAPI CreateProcessA( LPCSTR lpApplicationName, LPSTR lpCommandLine,
1801 LPSECURITY_ATTRIBUTES lpProcessAttributes,
1802 LPSECURITY_ATTRIBUTES lpThreadAttributes,
1803 BOOL bInheritHandles, DWORD dwCreationFlags,
1804 LPVOID lpEnvironment, LPCSTR lpCurrentDirectory,
1805 LPSTARTUPINFOA lpStartupInfo,
1806 LPPROCESS_INFORMATION lpProcessInfo )
1807{
1808 STARTUPINFOA startinfo;
1809 TEB *pThreadDB = (TEB*)GetThreadTEB();
1810 char *cmdline = NULL;
1811 BOOL rc;
1812
1813 dprintf(("KERNEL32: CreateProcessA %s cline:%s inherit:%d cFlags:%x Env:%x CurDir:%s StartupFlags:%x\n",
1814 lpApplicationName, lpCommandLine, bInheritHandles, dwCreationFlags,
1815 lpEnvironment, lpCurrentDirectory, lpStartupInfo));
1816
1817#ifdef DEBUG
1818 if(lpStartupInfo) {
1819 dprintf(("lpStartupInfo->lpReserved %x", lpStartupInfo->lpReserved));
1820 dprintf(("lpStartupInfo->lpDesktop %x", lpStartupInfo->lpDesktop));
1821 dprintf(("lpStartupInfo->lpTitle %s", lpStartupInfo->lpTitle));
1822 dprintf(("lpStartupInfo->dwX %x", lpStartupInfo->dwX));
1823 dprintf(("lpStartupInfo->dwY %x", lpStartupInfo->dwY));
1824 dprintf(("lpStartupInfo->dwXSize %x", lpStartupInfo->dwXSize));
1825 dprintf(("lpStartupInfo->dwYSize %x", lpStartupInfo->dwYSize));
1826 dprintf(("lpStartupInfo->dwXCountChars %x", lpStartupInfo->dwXCountChars));
1827 dprintf(("lpStartupInfo->dwYCountChars %x", lpStartupInfo->dwYCountChars));
1828 dprintf(("lpStartupInfo->dwFillAttribute %x", lpStartupInfo->dwFillAttribute));
1829 dprintf(("lpStartupInfo->dwFlags %x", lpStartupInfo->dwFlags));
1830 dprintf(("lpStartupInfo->wShowWindow %x", lpStartupInfo->wShowWindow));
1831 dprintf(("lpStartupInfo->hStdInput %x", lpStartupInfo->hStdInput));
1832 dprintf(("lpStartupInfo->hStdOutput %x", lpStartupInfo->hStdOutput));
1833 dprintf(("lpStartupInfo->hStdError %x", lpStartupInfo->hStdError));
1834 }
1835#endif
1836
1837 if(bInheritHandles && lpStartupInfo->dwFlags & STARTF_USESTDHANDLES)
1838 {
1839 //Translate standard handles if the child needs to inherit them
1840 int retcode = 0;
1841
1842 memcpy(&startinfo, lpStartupInfo, sizeof(startinfo));
1843 retcode |= HMHandleTranslateToOS2(lpStartupInfo->hStdInput, &startinfo.hStdInput);
1844 retcode |= HMHandleTranslateToOS2(lpStartupInfo->hStdOutput, &startinfo.hStdOutput);
1845 retcode |= HMHandleTranslateToOS2(lpStartupInfo->hStdError, &startinfo.hStdError);
1846
1847 if(retcode) {
1848 SetLastError(ERROR_INVALID_HANDLE);
1849 return FALSE;
1850 }
1851
1852 lpStartupInfo = &startinfo;
1853 }
1854
1855 if(lpApplicationName) {
1856 if(lpCommandLine) {
1857 //skip exe name in lpCommandLine
1858 //TODO: doesn't work for directories with spaces!
1859 while(*lpCommandLine != 0 && *lpCommandLine != ' ')
1860 lpCommandLine++;
1861
1862 if(*lpCommandLine != 0) {
1863 lpCommandLine++;
1864 }
1865 cmdline = (char *)malloc(strlen(lpApplicationName)+strlen(lpCommandLine) + 16);
1866 sprintf(cmdline, "%s %s", lpApplicationName, lpCommandLine);
1867 }
1868 else {
1869 cmdline = (char *)malloc(strlen(lpApplicationName) + 16);
1870 sprintf(cmdline, "%s", lpApplicationName);
1871 }
1872 }
1873 else {
1874 cmdline = (char *)malloc(strlen(lpCommandLine) + 16);
1875 sprintf(cmdline, "%s", lpCommandLine);
1876 }
1877
1878 char szAppName[MAX_PATH];
1879 char buffer[MAX_PATH];
1880 DWORD fileAttr;
1881 char *exename;
1882
1883 exename = buffer;
1884 strncpy(buffer, cmdline, sizeof(buffer));
1885 buffer[MAX_PATH-1] = 0;
1886 if(*exename == '"') {
1887 exename++;
1888 while(*exename != 0 && *exename != '"')
1889 exename++;
1890
1891 if(*exename != 0) {
1892 *exename = 0;
1893 }
1894 exename++;
1895 if (SearchPathA( NULL, &buffer[1], ".exe", sizeof(szAppName), szAppName, NULL ) ||
1896 SearchPathA( NULL, &buffer[1], NULL, sizeof(szAppName), szAppName, NULL ))
1897 {
1898 //
1899 }
1900 }
1901 else {
1902 BOOL fTerminate = FALSE;
1903 DWORD fileAttr;
1904
1905 while(*exename != 0) {
1906 while(*exename != 0 && *exename != ' ')
1907 exename++;
1908
1909 if(*exename != 0) {
1910 *exename = 0;
1911 fTerminate = TRUE;
1912 }
1913 dprintf(("Trying '%s'", buffer ));
1914 if (SearchPathA( NULL, buffer, ".exe", sizeof(szAppName), szAppName, NULL ) ||
1915 SearchPathA( NULL, buffer, NULL, sizeof(szAppName), szAppName, NULL ))
1916 {
1917 if(fTerminate) exename++;
1918 break;
1919 }
1920
1921 if(fTerminate) {
1922 *exename = ' ';
1923 exename++;
1924 fTerminate = FALSE;
1925 }
1926 }
1927 }
1928 lpCommandLine = cmdline + (exename - buffer); //start of command line parameters
1929
1930 fileAttr = GetFileAttributesA(szAppName);
1931 if(fileAttr == -1 || (fileAttr & FILE_ATTRIBUTE_DIRECTORY)) {
1932 dprintf(("CreateProcess: can't find executable!"));
1933
1934 if(cmdline)
1935 free(cmdline);
1936
1937 SetLastError(ERROR_FILE_NOT_FOUND);
1938 return FALSE;
1939 }
1940
1941 // open32 does not support DEBUG_ONLY_THIS_PROCESS
1942 if(dwCreationFlags & DEBUG_ONLY_THIS_PROCESS)
1943 dwCreationFlags |= DEBUG_PROCESS;
1944
1945 if(O32_CreateProcess(szAppName, lpCommandLine, lpProcessAttributes,
1946 lpThreadAttributes, bInheritHandles, dwCreationFlags,
1947 lpEnvironment, lpCurrentDirectory, lpStartupInfo,
1948 lpProcessInfo) == TRUE)
1949 {
1950 if (dwCreationFlags & DEBUG_PROCESS && pThreadDB != NULL)
1951 {
1952 if(pThreadDB->o.odin.pidDebuggee != 0)
1953 {
1954 // TODO: handle this
1955 dprintf(("KERNEL32: CreateProcess ERROR: This thread is already a debugger\n"));
1956 }
1957 else
1958 {
1959 pThreadDB->o.odin.pidDebuggee = lpProcessInfo->dwProcessId;
1960 OSLibStartDebugger((ULONG*)&pThreadDB->o.odin.pidDebuggee);
1961 }
1962 }
1963 else pThreadDB->o.odin.pidDebuggee = 0;
1964
1965 if(lpProcessInfo)
1966 {
1967 lpProcessInfo->dwThreadId = MAKE_THREADID(lpProcessInfo->dwProcessId, lpProcessInfo->dwThreadId);
1968 }
1969
1970 if(cmdline)
1971 free(cmdline);
1972 return(TRUE);
1973 }
1974
1975 // PH 2001-05-07
1976 // verify why O32_CreateProcess actually failed.
1977 // If GetLastError() == 191 (ERROR_INVALID_EXE_SIGNATURE)
1978 // we can continue to call "PE.EXE".
1979 // Note: Open32 does not translate ERROR_INVALID_EXE_SIGNATURE,
1980 // it is also valid in Win32.
1981 DWORD dwError = GetLastError();
1982 if (ERROR_INVALID_EXE_SIGNATURE != dwError && ERROR_FILE_NOT_FOUND != dwError && ERROR_ACCESS_DENIED != dwError)
1983 {
1984 dprintf(("CreateProcess: O32_CreateProcess failed with rc=%d, not PE-executable !",
1985 dwError));
1986
1987 // the current value of GetLastError() is still valid.
1988
1989 if(cmdline)
1990 free(cmdline);
1991
1992 return FALSE;
1993 }
1994
1995 // else ...
1996
1997 //probably a win32 exe, so run it in the pe loader
1998 dprintf(("KERNEL32: CreateProcess %s %s", szAppName, lpCommandLine));
1999
2000 DWORD Characteristics, SubSystem, fNEExe;
2001 if(Win32ImageBase::isPEImage(szAppName, &Characteristics, &SubSystem, &fNEExe) == 0)
2002 {
2003 char *lpszPE;
2004 char *lpszExecutable;
2005 int iNewCommandLineLength;
2006
2007 // calculate base length for the new command line
2008 iNewCommandLineLength = strlen(szAppName) + strlen(lpCommandLine);
2009
2010 if(SubSystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
2011 lpszExecutable = (LPSTR)szPECmdLoader;
2012 else
2013 lpszExecutable = (LPSTR)szPEGUILoader;
2014
2015 lpszPE = lpszExecutable;
2016
2017 // 2002-04-24 PH
2018 // set the ODIN32.DEBUG_CHILD environment variable to start new PE processes
2019 // under a new instance of the (IPMD) debugger.
2020#ifdef DEBUG
2021 CHAR debug_szPE[ 512 ];
2022 PSZ debug_pszOS2Debugger = getenv("ODIN32.DEBUG_CHILD");
2023 if (NULL != debug_pszOS2Debugger)
2024 {
2025 // build new start command
2026 strcpy(debug_szPE, debug_pszOS2Debugger);
2027 strcat(debug_szPE, " ");
2028 strcat(debug_szPE, lpszExecutable);
2029
2030 // we require more space in the new command line
2031 iNewCommandLineLength += strlen( debug_szPE );
2032
2033 // only launch the specified executable (ICSDEBUG.EXE)
2034 lpszPE = debug_szPE;
2035 lpszExecutable = debug_pszOS2Debugger;
2036 }
2037#endif
2038
2039 //SvL: Allright. Before we call O32_CreateProcess, we must take care of
2040 // lpCurrentDirectory ourselves. (Open32 ignores it!)
2041 if(lpCurrentDirectory) {
2042 char *newcmdline;
2043
2044 newcmdline = (char *)malloc(strlen(lpCurrentDirectory) + iNewCommandLineLength + strlen(lpszPE) + 64);
2045 sprintf(newcmdline, "%s /OPT:[CURDIR=%s] %s %s", lpszPE, lpCurrentDirectory, szAppName, lpCommandLine);
2046 free(cmdline);
2047 cmdline = newcmdline;
2048 }
2049 else {
2050 char *newcmdline;
2051
2052 newcmdline = (char *)malloc(iNewCommandLineLength + strlen(lpszPE) + 16);
2053 sprintf(newcmdline, "%s %s %s", lpszPE, szAppName, lpCommandLine);
2054 free(cmdline);
2055 cmdline = newcmdline;
2056 }
2057
2058 dprintf(("KERNEL32: CreateProcess starting [%s],[%s]",
2059 lpszExecutable,
2060 cmdline));
2061
2062 rc = O32_CreateProcess(lpszExecutable, (LPCSTR)cmdline,lpProcessAttributes,
2063 lpThreadAttributes, bInheritHandles, dwCreationFlags,
2064 lpEnvironment, lpCurrentDirectory, lpStartupInfo,
2065 lpProcessInfo);
2066 }
2067 else
2068 if(fNEExe) {//16 bits windows app
2069 char *newcmdline;
2070
2071 newcmdline = (char *)malloc(strlen(szAppName) + strlen(cmdline) + strlen(szNELoader) + strlen(szPEGUILoader) + strlen(lpCommandLine) + 32);
2072
2073 sprintf(newcmdline, "%s /PELDR=[%s] %s", szNELoader, szPEGUILoader, szAppName, lpCommandLine);
2074 free(cmdline);
2075 cmdline = newcmdline;
2076 //Force Open32 to use DosStartSession (DosExecPgm won't do)
2077 dwCreationFlags |= CREATE_NEW_PROCESS_GROUP;
2078
2079 dprintf(("KERNEL32: CreateProcess starting [%s],[%s]",
2080 szNELoader,
2081 cmdline));
2082 rc = O32_CreateProcess(szNELoader, (LPCSTR)cmdline, lpProcessAttributes,
2083 lpThreadAttributes, bInheritHandles, dwCreationFlags,
2084 lpEnvironment, lpCurrentDirectory, lpStartupInfo,
2085 lpProcessInfo);
2086 }
2087 else {//os/2 app??
2088 rc = O32_CreateProcess(szAppName, (LPCSTR)lpCommandLine, lpProcessAttributes,
2089 lpThreadAttributes, bInheritHandles, dwCreationFlags,
2090 lpEnvironment, lpCurrentDirectory, lpStartupInfo,
2091 lpProcessInfo);
2092 }
2093 if(rc == TRUE)
2094 {
2095 if (dwCreationFlags & DEBUG_PROCESS && pThreadDB != NULL)
2096 {
2097 if(pThreadDB->o.odin.pidDebuggee != 0)
2098 {
2099 // TODO: handle this
2100 dprintf(("KERNEL32: CreateProcess ERROR: This thread is already a debugger\n"));
2101 }
2102 else
2103 {
2104 pThreadDB->o.odin.pidDebuggee = lpProcessInfo->dwProcessId;
2105 OSLibStartDebugger((ULONG*)&pThreadDB->o.odin.pidDebuggee);
2106 }
2107 }
2108 else
2109 pThreadDB->o.odin.pidDebuggee = 0;
2110 }
2111 if(cmdline)
2112 free(cmdline);
2113
2114 if(lpProcessInfo)
2115 {
2116 lpProcessInfo->dwThreadId = MAKE_THREADID(lpProcessInfo->dwProcessId, lpProcessInfo->dwThreadId);
2117 dprintf(("KERNEL32: CreateProcess returned %d hPro:%x hThr:%x pid:%x tid:%x\n",
2118 rc, lpProcessInfo->hProcess, lpProcessInfo->hThread,
2119 lpProcessInfo->dwProcessId,lpProcessInfo->dwThreadId));
2120 }
2121 else
2122 dprintf(("KERNEL32: CreateProcess returned %d\n", rc));
2123 return(rc);
2124}
2125//******************************************************************************
2126//******************************************************************************
2127BOOL WIN32API CreateProcessW(LPCWSTR lpApplicationName, LPWSTR lpCommandLine,
2128 PSECURITY_ATTRIBUTES lpProcessAttributes,
2129 PSECURITY_ATTRIBUTES lpThreadAttributes,
2130 BOOL bInheritHandles, DWORD dwCreationFlags,
2131 LPVOID lpEnvironment,
2132 LPCWSTR lpCurrentDirectory, LPSTARTUPINFOW lpStartupInfo,
2133 LPPROCESS_INFORMATION lpProcessInfo)
2134{
2135 BOOL rc;
2136 char *astring1 = 0, *astring2 = 0, *astring3 = 0;
2137
2138 dprintf(("KERNEL32: CreateProcessW"));
2139 if(lpApplicationName)
2140 astring1 = UnicodeToAsciiString((LPWSTR)lpApplicationName);
2141 if(lpCommandLine)
2142 astring2 = UnicodeToAsciiString(lpCommandLine);
2143 if(lpCurrentDirectory)
2144 astring3 = UnicodeToAsciiString((LPWSTR)lpCurrentDirectory);
2145 rc = CreateProcessA(astring1, astring2, lpProcessAttributes, lpThreadAttributes,
2146 bInheritHandles, dwCreationFlags, lpEnvironment,
2147 astring3, (LPSTARTUPINFOA)lpStartupInfo,
2148 lpProcessInfo);
2149 if(astring3) FreeAsciiString(astring3);
2150 if(astring2) FreeAsciiString(astring2);
2151 if(astring1) FreeAsciiString(astring1);
2152 return(rc);
2153}
2154//******************************************************************************
2155//******************************************************************************
2156HINSTANCE WIN32API WinExec(LPCSTR lpCmdLine, UINT nCmdShow)
2157{
2158 STARTUPINFOA startinfo = {0};
2159 PROCESS_INFORMATION procinfo;
2160 DWORD rc;
2161 HINSTANCE hInstance;
2162
2163 dprintf(("KERNEL32: WinExec %s\n", lpCmdLine));
2164 startinfo.dwFlags = nCmdShow;
2165 if(CreateProcessA(NULL, (LPSTR)lpCmdLine, NULL, NULL, FALSE, 0, NULL, NULL,
2166 &startinfo, &procinfo) == FALSE)
2167 {
2168 hInstance = (HINSTANCE)GetLastError();
2169 if(hInstance >= 32) {
2170 hInstance = 11;
2171 }
2172 dprintf(("KERNEL32: WinExec failed with rc %d", hInstance));
2173 return hInstance;
2174 }
2175 //block until the launched app waits for input (or a timeout of 15 seconds)
2176 //TODO: Shouldn't call Open32, but the api in user32..
2177 if(fVersionWarp3) {
2178 Sleep(1000); //WaitForInputIdle not available in Warp 3
2179 }
2180 else {
2181 dprintf(("Calling WaitForInputIdle %x %d", procinfo.hProcess, 15000));
2182 rc = WaitForInputIdle(procinfo.hProcess, 15000);
2183#ifdef DEBUG
2184 if(rc != 0) {
2185 dprintf(("WinExec: WaitForInputIdle %x returned %x", procinfo.hProcess, rc));
2186 }
2187 else dprintf(("WinExec: WaitForInputIdle successfull"));
2188#endif
2189 }
2190 CloseHandle(procinfo.hThread);
2191 CloseHandle(procinfo.hProcess);
2192 return 33;
2193}
2194//******************************************************************************
2195//DWORD idAttach; /* thread to attach */
2196//DWORD idAttachTo; /* thread to attach to */
2197//BOOL fAttach; /* attach or detach */
2198//******************************************************************************
2199BOOL WIN32API AttachThreadInput(DWORD idAttach, DWORD idAttachTo, BOOL fAttach)
2200{
2201 dprintf(("USER32: AttachThreadInput, not implemented\n"));
2202 return(TRUE);
2203}
2204//******************************************************************************
2205//******************************************************************************
2206DWORD WIN32API WaitForInputIdle(HANDLE hProcess, DWORD dwTimeOut)
2207{
2208 dprintf(("USER32: WaitForInputIdle %x %d\n", hProcess, dwTimeOut));
2209
2210 if(fVersionWarp3) {
2211 Sleep(1000);
2212 return 0;
2213 }
2214 else return O32_WaitForInputIdle(hProcess, dwTimeOut);
2215}
2216/**********************************************************************
2217 * LoadModule (KERNEL32.499)
2218 *
2219 * Wine: 20000909
2220 *
2221 * Copyright 1995 Alexandre Julliard
2222 */
2223HINSTANCE WINAPI LoadModule( LPCSTR name, LPVOID paramBlock )
2224{
2225 LOADPARAMS *params = (LOADPARAMS *)paramBlock;
2226 PROCESS_INFORMATION info;
2227 STARTUPINFOA startup;
2228 HINSTANCE hInstance;
2229 LPSTR cmdline, p;
2230 char filename[MAX_PATH];
2231 BYTE len;
2232
2233 dprintf(("LoadModule %s %x", name, paramBlock));
2234
2235 if (!name) return ERROR_FILE_NOT_FOUND;
2236
2237 if (!SearchPathA( NULL, name, ".exe", sizeof(filename), filename, NULL ) &&
2238 !SearchPathA( NULL, name, NULL, sizeof(filename), filename, NULL ))
2239 return GetLastError();
2240
2241 len = (BYTE)params->lpCmdLine[0];
2242 if (!(cmdline = (LPSTR)HeapAlloc( GetProcessHeap(), 0, strlen(filename) + len + 2 )))
2243 return ERROR_NOT_ENOUGH_MEMORY;
2244
2245 strcpy( cmdline, filename );
2246 p = cmdline + strlen(cmdline);
2247 *p++ = ' ';
2248 memcpy( p, params->lpCmdLine + 1, len );
2249 p[len] = 0;
2250
2251 memset( &startup, 0, sizeof(startup) );
2252 startup.cb = sizeof(startup);
2253 if (params->lpCmdShow)
2254 {
2255 startup.dwFlags = STARTF_USESHOWWINDOW;
2256 startup.wShowWindow = params->lpCmdShow[1];
2257 }
2258
2259 if (CreateProcessA( filename, cmdline, NULL, NULL, FALSE, 0,
2260 params->lpEnvAddress, NULL, &startup, &info ))
2261 {
2262 /* Give 15 seconds to the app to come up */
2263 if ( WaitForInputIdle ( info.hProcess, 15000 ) == 0xFFFFFFFF )
2264 dprintf(("ERROR: WaitForInputIdle failed: Error %ld\n", GetLastError() ));
2265 hInstance = 33;
2266 /* Close off the handles */
2267 CloseHandle( info.hThread );
2268 CloseHandle( info.hProcess );
2269 }
2270 else if ((hInstance = GetLastError()) >= 32)
2271 {
2272 dprintf(("ERROR: Strange error set by CreateProcess: %d\n", hInstance ));
2273 hInstance = 11;
2274 }
2275
2276 HeapFree( GetProcessHeap(), 0, cmdline );
2277 return hInstance;
2278}
2279//******************************************************************************
2280//******************************************************************************
2281FARPROC WIN32API GetProcAddress(HMODULE hModule, LPCSTR lpszProc)
2282{
2283 Win32ImageBase *winmod;
2284 FARPROC proc;
2285 ULONG ulAPIOrdinal;
2286
2287 if(hModule == 0 || hModule == -1 || (WinExe && hModule == WinExe->getInstanceHandle())) {
2288 winmod = WinExe;
2289 }
2290 else winmod = (Win32ImageBase *)Win32DllBase::findModule((HINSTANCE)hModule);
2291
2292 if(winmod) {
2293 ulAPIOrdinal = (ULONG)lpszProc;
2294 if (ulAPIOrdinal <= 0x0000FFFF) {
2295 proc = (FARPROC)winmod->getApi((int)ulAPIOrdinal);
2296 }
2297 else proc = (FARPROC)winmod->getApi((char *)lpszProc);
2298 if(proc == 0) {
2299#ifdef DEBUG
2300 if(ulAPIOrdinal <= 0x0000FFFF) {
2301 dprintf(("GetProcAddress %x %x not found!", hModule, ulAPIOrdinal));
2302 }
2303 else dprintf(("GetProcAddress %x %s not found!", hModule, lpszProc));
2304#endif
2305 SetLastError(ERROR_PROC_NOT_FOUND);
2306 }
2307 if(HIWORD(lpszProc))
2308 dprintf(("KERNEL32: GetProcAddress %s from %X returned %X\n", lpszProc, hModule, proc));
2309 else dprintf(("KERNEL32: GetProcAddress %x from %X returned %X\n", lpszProc, hModule, proc));
2310
2311 SetLastError(ERROR_SUCCESS);
2312 return proc;
2313 }
2314 proc = (FARPROC)OSLibDosGetProcAddress(hModule, lpszProc);
2315 if(HIWORD(lpszProc))
2316 dprintf(("KERNEL32: GetProcAddress %s from %X returned %X\n", lpszProc, hModule, proc));
2317 else dprintf(("KERNEL32: GetProcAddress %x from %X returned %X\n", lpszProc, hModule, proc));
2318 SetLastError(ERROR_SUCCESS);
2319 return(proc);
2320}
2321//******************************************************************************
2322// ODIN_SetProcAddress: Override a dll export
2323//
2324// Parameters:
2325// HMODULE hModule Module handle
2326// LPCSTR lpszProc Export name or ordinal
2327// FARPROC pfnNewProc New export function address
2328//
2329// Returns: Success -> old address of export
2330// Failure -> -1
2331//
2332//******************************************************************************
2333FARPROC WIN32API ODIN_SetProcAddress(HMODULE hModule, LPCSTR lpszProc,
2334 FARPROC pfnNewProc)
2335{
2336 Win32ImageBase *winmod;
2337 FARPROC proc;
2338 ULONG ulAPIOrdinal;
2339
2340 if(hModule == 0 || hModule == -1 || (WinExe && hModule == WinExe->getInstanceHandle())) {
2341 winmod = WinExe;
2342 }
2343 else winmod = (Win32ImageBase *)Win32DllBase::findModule((HINSTANCE)hModule);
2344
2345 if(winmod) {
2346 ulAPIOrdinal = (ULONG)lpszProc;
2347 if (ulAPIOrdinal <= 0x0000FFFF) {
2348 proc = (FARPROC)winmod->setApi((int)ulAPIOrdinal, (ULONG)pfnNewProc);
2349 }
2350 else proc = (FARPROC)winmod->setApi((char *)lpszProc, (ULONG)pfnNewProc);
2351 if(proc == 0) {
2352#ifdef DEBUG
2353 if(ulAPIOrdinal <= 0x0000FFFF) {
2354 dprintf(("ODIN_SetProcAddress %x %x not found!", hModule, ulAPIOrdinal));
2355 }
2356 else dprintf(("ODIN_SetProcAddress %x %s not found!", hModule, lpszProc));
2357#endif
2358 SetLastError(ERROR_PROC_NOT_FOUND);
2359 }
2360 if(HIWORD(lpszProc))
2361 dprintf(("KERNEL32: ODIN_SetProcAddress %s from %X returned %X\n", lpszProc, hModule, proc));
2362 else dprintf(("KERNEL32: ODIN_SetProcAddress %x from %X returned %X\n", lpszProc, hModule, proc));
2363
2364 SetLastError(ERROR_SUCCESS);
2365 return proc;
2366 }
2367 SetLastError(ERROR_INVALID_HANDLE);
2368 return (FARPROC)-1;
2369}
2370//******************************************************************************
2371//Retrieve the version
2372//******************************************************************************
2373BOOL SYSTEM GetVersionStruct(char *lpszModName, char *verstruct, ULONG bufLength)
2374{
2375 Win32ImageBase *winimage;
2376 HINSTANCE hDll;
2377 BOOL rc = FALSE;
2378
2379 dprintf(("GetVersionStruct of module %s %x %d", lpszModName, verstruct, bufLength));
2380 if(verstruct == NULL) {
2381 SetLastError(ERROR_INVALID_PARAMETER);
2382 return FALSE;
2383 }
2384 if (WinExe != NULL && WinExe->matchModName(lpszModName)) {
2385 return WinExe->getVersionStruct(verstruct, bufLength);
2386 }
2387 hDll = LoadLibraryExA(lpszModName, 0, LOAD_LIBRARY_AS_DATAFILE);
2388 if(hDll == 0) {
2389 dprintf(("ERROR: GetVersionStruct: Unable to load module!!"));
2390 return 0;
2391 }
2392 winimage = (Win32ImageBase *)Win32DllBase::findModule(hDll);
2393 if(winimage != NULL) {
2394 rc = winimage->getVersionStruct(verstruct, bufLength);
2395 }
2396 else {
2397 dprintf(("GetVersionStruct; just loaded dll %s, but can't find it now!", lpszModName));
2398//// DebugInt3();
2399 }
2400 FreeLibrary(hDll);
2401 return rc;
2402}
2403//******************************************************************************
2404//******************************************************************************
2405ULONG SYSTEM GetVersionSize(char *lpszModName)
2406{
2407 Win32ImageBase *winimage;
2408 HINSTANCE hDll;
2409 ULONG size = 0;
2410
2411 dprintf(("GetVersionSize of %s", lpszModName));
2412 if (WinExe != NULL && WinExe->matchModName(lpszModName)) {
2413 return WinExe->getVersionSize();
2414 }
2415
2416 hDll = LoadLibraryExA(lpszModName, 0, LOAD_LIBRARY_AS_DATAFILE);
2417 if(hDll == 0) {
2418 dprintf(("ERROR: GetVersionStruct: Unable to load module!!"));
2419 return 0;
2420 }
2421 winimage = (Win32ImageBase *)Win32DllBase::findModule(hDll);
2422 if(winimage != NULL) {
2423 size = winimage->getVersionSize();
2424 }
2425 else {
2426 dprintf(("GetVersionSize; just loaded dll %s, but can't find it now!", lpszModName));
2427//// DebugInt3();
2428 }
2429 FreeLibrary(hDll);
2430 return size;
2431}
2432//******************************************************************************
2433//******************************************************************************
2434BOOL WIN32API DisableThreadLibraryCalls(HMODULE hModule)
2435{
2436 Win32DllBase *winmod;
2437 FARPROC proc;
2438 ULONG ulAPIOrdinal;
2439
2440 winmod = Win32DllBase::findModule((HINSTANCE)hModule);
2441 if(winmod)
2442 {
2443 // don't call ATTACH/DETACH thread functions in DLL
2444 winmod->disableThreadLibraryCalls();
2445 return TRUE;
2446 }
2447 else
2448 {
2449 // raise error condition
2450 SetLastError(ERROR_INVALID_HANDLE);
2451 return FALSE;
2452 }
2453}
2454//******************************************************************************
2455//******************************************************************************
Note: See TracBrowser for help on using the repository browser.