1 | /* $Id: wprocess.cpp,v 1.18 1999-08-11 22:27:56 phaller Exp $ */
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * Win32 process functions
|
---|
5 | *
|
---|
6 | * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl)
|
---|
7 | *
|
---|
8 | *
|
---|
9 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
10 | *
|
---|
11 | */
|
---|
12 | #include <os2win.h>
|
---|
13 | #include <stdio.h>
|
---|
14 | #include <stdlib.h>
|
---|
15 | #include <string.h>
|
---|
16 | #include <odincrt.h>
|
---|
17 | #include <odinwrap.h>
|
---|
18 |
|
---|
19 | #include "unicode.h"
|
---|
20 | #include "windll.h"
|
---|
21 | #include "winexe.h"
|
---|
22 |
|
---|
23 | #ifdef __IBMCPP__
|
---|
24 | #include <builtin.h>
|
---|
25 | #endif
|
---|
26 |
|
---|
27 | #include "except.h"
|
---|
28 | #include "os2util.h"
|
---|
29 |
|
---|
30 | #include "console.h"
|
---|
31 | #include "cio.h"
|
---|
32 | #include "versionos2.h" /*PLF Wed 98-03-18 02:36:51*/
|
---|
33 | #include <wprocess.h>
|
---|
34 |
|
---|
35 | BOOL fExeStarted = FALSE;
|
---|
36 | BOOL fFreeLibrary = FALSE;
|
---|
37 |
|
---|
38 | //Process database
|
---|
39 | PDB ProcessPDB = {0};
|
---|
40 | USHORT ProcessTIBSel = 0;
|
---|
41 | DWORD *TIBFlatPtr = 0;
|
---|
42 |
|
---|
43 | extern "C" ULONG QueryExceptionChain();
|
---|
44 |
|
---|
45 |
|
---|
46 | ODINDEBUGCHANNEL(KERNEL32-WPROCESS)
|
---|
47 |
|
---|
48 | //******************************************************************************
|
---|
49 | //******************************************************************************
|
---|
50 | TEB *WIN32API GetThreadTEB()
|
---|
51 | {
|
---|
52 | return (TEB *)*TIBFlatPtr;
|
---|
53 | }
|
---|
54 | //******************************************************************************
|
---|
55 | //******************************************************************************
|
---|
56 | THDB *WIN32API GetThreadTHDB()
|
---|
57 | {
|
---|
58 | TEB *winteb;
|
---|
59 | THDB *thdb;
|
---|
60 |
|
---|
61 | winteb = (TEB *)*TIBFlatPtr;
|
---|
62 | if(winteb == NULL) {
|
---|
63 | return NULL;
|
---|
64 | }
|
---|
65 | thdb = (THDB *)(winteb+1);
|
---|
66 |
|
---|
67 | return thdb;
|
---|
68 | }
|
---|
69 | //******************************************************************************
|
---|
70 | // Set up the TIB selector and memory for the current thread
|
---|
71 | //******************************************************************************
|
---|
72 | TEB *InitializeTIB(BOOL fMainThread)
|
---|
73 | {
|
---|
74 | TEB *winteb;
|
---|
75 | THDB *thdb;
|
---|
76 |
|
---|
77 | USHORT tibsel;
|
---|
78 |
|
---|
79 | //Allocate one dword to store the flat address of our TEB
|
---|
80 | TIBFlatPtr = (DWORD *)OS2AllocThreadLocalMemory(1);
|
---|
81 | if(TIBFlatPtr == 0) {
|
---|
82 | dprintf(("InitializeTIB: local thread memory alloc failed!!"));
|
---|
83 | DebugInt3();
|
---|
84 | return NULL;
|
---|
85 | }
|
---|
86 | if(OS2AllocSel(PAGE_SIZE, &tibsel) == FALSE)
|
---|
87 | {
|
---|
88 | dprintf(("InitializeTIB: selector alloc failed!!"));
|
---|
89 | DebugInt3();
|
---|
90 | return NULL;
|
---|
91 | }
|
---|
92 | winteb = (TEB *)OS2SelToFlat(tibsel);
|
---|
93 | if(winteb == NULL)
|
---|
94 | {
|
---|
95 | dprintf(("InitializeTIB: DosSelToFlat failed!!"));
|
---|
96 | DebugInt3();
|
---|
97 | return NULL;
|
---|
98 | }
|
---|
99 | memset(winteb, 0, PAGE_SIZE);
|
---|
100 | thdb = (THDB *)(winteb+1);
|
---|
101 | *TIBFlatPtr = (DWORD)winteb;
|
---|
102 |
|
---|
103 | winteb->except = (PVOID)-1; /* 00 Head of exception handling chain */
|
---|
104 | winteb->stack_top = (PVOID)OS2GetTIB(TIB_STACKTOP); /* 04 Top of thread stack */
|
---|
105 | winteb->stack_low = (PVOID)OS2GetTIB(TIB_STACKLOW); /* 08 Stack low-water mark */
|
---|
106 | winteb->htask16 = (USHORT)OS2GetPIB(PIB_TASKHNDL); /* 0c Win16 task handle */
|
---|
107 | winteb->stack_sel = getSS(); /* 0e 16-bit stack selector */
|
---|
108 | winteb->self = winteb; /* 18 Pointer to this structure */
|
---|
109 | winteb->flags = TEBF_WIN32; /* 1c Flags */
|
---|
110 | winteb->queue = 0; /* 28 Message queue */
|
---|
111 | winteb->tls_ptr = &thdb->tls_array[0]; /* 2c Pointer to TLS array */
|
---|
112 | winteb->process = &ProcessPDB; /* 30 owning process (used by NT3.51 applets)*/
|
---|
113 |
|
---|
114 | memcpy(&thdb->teb, winteb, sizeof(TEB));
|
---|
115 | thdb->process = &ProcessPDB;
|
---|
116 | thdb->exit_code = 0x103; /* STILL_ACTIVE */
|
---|
117 | thdb->teb_sel = tibsel;
|
---|
118 | thdb->OrgTIBSel = GetFS();
|
---|
119 |
|
---|
120 | if(OS2GetPIB(PIB_TASKTYPE) == TASKTYPE_PM)
|
---|
121 | {
|
---|
122 | thdb->flags = 0; //todo gui
|
---|
123 | }
|
---|
124 | else thdb->flags = 0; //todo textmode
|
---|
125 |
|
---|
126 | if(fMainThread)
|
---|
127 | {
|
---|
128 | //todo initialize PDB during process creation
|
---|
129 | //todo: initialize TLS array if required
|
---|
130 | //TLS in executable always TLS index 0?
|
---|
131 | ProcessTIBSel = tibsel;
|
---|
132 | }
|
---|
133 | dprintf(("InitializeTIB setup TEB with selector %x", tibsel));
|
---|
134 | dprintf(("InitializeTIB: FS(%x):[0] = %x", GetFS(), QueryExceptionChain()));
|
---|
135 | return winteb;
|
---|
136 | }
|
---|
137 | //******************************************************************************
|
---|
138 | // Destroy the TIB selector and memory for the current thread
|
---|
139 | //******************************************************************************
|
---|
140 | void DestroyTIB()
|
---|
141 | {
|
---|
142 | SHORT orgtibsel;
|
---|
143 | TEB *winteb;
|
---|
144 | THDB *thdb;
|
---|
145 |
|
---|
146 | dprintf(("DestroyTIB: FS = %x", GetFS()));
|
---|
147 | dprintf(("DestroyTIB: FS:[0] = %x", QueryExceptionChain()));
|
---|
148 |
|
---|
149 | winteb = (TEB *)*TIBFlatPtr;
|
---|
150 | if(winteb) {
|
---|
151 | thdb = (THDB *)(winteb+1);
|
---|
152 | orgtibsel = thdb->OrgTIBSel;
|
---|
153 |
|
---|
154 | //Restore our original FS selector
|
---|
155 | SetFS(orgtibsel);
|
---|
156 |
|
---|
157 | //And free our own
|
---|
158 | OS2FreeSel(thdb->teb_sel);
|
---|
159 | }
|
---|
160 | else dprintf(("Already destroyed TIB"));
|
---|
161 |
|
---|
162 | dprintf(("DestroyTIB: FS(%x):[0] = %x", GetFS(), QueryExceptionChain()));
|
---|
163 | *TIBFlatPtr = 0;
|
---|
164 | return;
|
---|
165 | }
|
---|
166 | /******************************************************************************/
|
---|
167 | /******************************************************************************/
|
---|
168 | void WIN32API RestoreOS2TIB()
|
---|
169 | {
|
---|
170 | SHORT orgtibsel;
|
---|
171 | TEB *winteb;
|
---|
172 | THDB *thdb;
|
---|
173 |
|
---|
174 | winteb = (TEB *)*TIBFlatPtr;
|
---|
175 | if(winteb) {
|
---|
176 | thdb = (THDB *)(winteb+1);
|
---|
177 | orgtibsel = thdb->OrgTIBSel;
|
---|
178 |
|
---|
179 | //Restore our original FS selector
|
---|
180 | SetFS(orgtibsel);
|
---|
181 | }
|
---|
182 | }
|
---|
183 | /******************************************************************************/
|
---|
184 | /******************************************************************************/
|
---|
185 | USHORT WIN32API SetWin32TIB()
|
---|
186 | {
|
---|
187 | SHORT win32tibsel;
|
---|
188 | TEB *winteb;
|
---|
189 | THDB *thdb;
|
---|
190 |
|
---|
191 | winteb = (TEB *)*TIBFlatPtr;
|
---|
192 | if(winteb) {
|
---|
193 | thdb = (THDB *)(winteb+1);
|
---|
194 | win32tibsel = thdb->teb_sel;
|
---|
195 |
|
---|
196 | //Restore our win32 FS selector
|
---|
197 | return SetReturnFS(win32tibsel);
|
---|
198 | }
|
---|
199 | // nested calls are OK, OS2ToWinCallback for instance
|
---|
200 | //else DebugInt3();
|
---|
201 |
|
---|
202 | return GetFS();
|
---|
203 | }
|
---|
204 | /******************************************************************************/
|
---|
205 | //******************************************************************************
|
---|
206 | void WIN32API RegisterExe(WIN32EXEENTRY EntryPoint, PIMAGE_TLS_CALLBACK *TlsCallbackAddr,
|
---|
207 | LPDWORD TlsIndexAddr, ULONG TlsInitSize,
|
---|
208 | ULONG TlsTotalSize, LPVOID TlsAddress,
|
---|
209 | LONG Win32TableId, LONG NameTableId, LONG VersionResId,
|
---|
210 | LONG Pe2lxVersion, HINSTANCE hinstance, ULONG dwReserved)
|
---|
211 | {
|
---|
212 | if(WinExe != NULL) //should never happen
|
---|
213 | ODIN_delete(WinExe);
|
---|
214 |
|
---|
215 |
|
---|
216 | CheckVersion(Pe2lxVersion, OS2GetDllName(hinstance));
|
---|
217 |
|
---|
218 | if(getenv("WIN32_IOPL2")) {
|
---|
219 | io_init1();
|
---|
220 | }
|
---|
221 |
|
---|
222 | //SvL: Use 0 instead of the real instance handle (for resource lookup)
|
---|
223 | Win32Exe *winexe;
|
---|
224 |
|
---|
225 | ODIN_FS_BEGIN
|
---|
226 | winexe = new Win32Exe(0, NameTableId, Win32TableId);
|
---|
227 | ODIN_FS_END
|
---|
228 |
|
---|
229 | if(winexe) {
|
---|
230 | dprintf(("RegisterExe Win32TableId = %x", Win32TableId));
|
---|
231 | dprintf(("RegisterExe NameTableId = %x", NameTableId));
|
---|
232 | dprintf(("RegisterExe VersionResId = %x", VersionResId));
|
---|
233 | dprintf(("RegisterExe Pe2lxVersion = %x", Pe2lxVersion));
|
---|
234 |
|
---|
235 | winexe->setVersionId(VersionResId);
|
---|
236 | winexe->setOS2InstanceHandle(hinstance);
|
---|
237 | winexe->setEntryPoint((ULONG)EntryPoint);
|
---|
238 | winexe->setTLSAddress(TlsAddress);
|
---|
239 | winexe->setTLSInitSize(TlsInitSize);
|
---|
240 | winexe->setTLSTotalSize(TlsTotalSize);
|
---|
241 | winexe->setTLSIndexAddr(TlsIndexAddr);
|
---|
242 | winexe->setTLSCallBackAddr(TlsCallbackAddr);
|
---|
243 |
|
---|
244 | char *modname = getenv("WIN32MODULE");
|
---|
245 |
|
---|
246 | if(modname != NULL)
|
---|
247 | {
|
---|
248 | dprintf(("Set full path for exe to %s", modname));
|
---|
249 | winexe->setFullPath(modname);
|
---|
250 | }
|
---|
251 | winexe->start();
|
---|
252 | }
|
---|
253 | else {
|
---|
254 | eprintf(("Win32Exe creation failed!\n"));
|
---|
255 | DebugInt3();
|
---|
256 | return;
|
---|
257 | }
|
---|
258 | }
|
---|
259 | //******************************************************************************
|
---|
260 | //******************************************************************************
|
---|
261 | ULONG WIN32API RegisterDll(WIN32DLLENTRY pfnDllEntry, PIMAGE_TLS_CALLBACK *TlsCallbackAddr,
|
---|
262 | LPDWORD TlsIndexAddr, ULONG TlsInitSize,
|
---|
263 | ULONG TlsTotalSize, LPVOID TlsAddress,
|
---|
264 | LONG Win32TableId, LONG NameTableId, LONG VersionResId,
|
---|
265 | LONG Pe2lxVersion, HINSTANCE hinstance, ULONG dwAttachType)
|
---|
266 | {
|
---|
267 | char *name;
|
---|
268 |
|
---|
269 | Win32Dll *winmod = Win32Dll::findModule(hinstance);
|
---|
270 | if(dwAttachType == 0)
|
---|
271 | { //Process attach
|
---|
272 | if(getenv("WIN32_IOPL2")) {
|
---|
273 | io_init1();
|
---|
274 | }
|
---|
275 | name = OS2GetDllName(hinstance);
|
---|
276 | CheckVersion(Pe2lxVersion, name);
|
---|
277 |
|
---|
278 | dprintf(("RegisterDll %X %s reason %d\n", hinstance, name, dwAttachType));
|
---|
279 | dprintf(("RegisterDll Win32TableId = %x", Win32TableId));
|
---|
280 | dprintf(("RegisterDll NameTableId = %x", NameTableId));
|
---|
281 | dprintf(("RegisterDll VersionResId = %x", VersionResId));
|
---|
282 | dprintf(("RegisterDll Pe2lxVersion = %x", Pe2lxVersion));
|
---|
283 |
|
---|
284 | if(winmod != NULL) {
|
---|
285 | //dll manually loaded by PE loader (Win32Dll::init)
|
---|
286 | winmod->OS2DllInit(hinstance, NameTableId, Win32TableId, pfnDllEntry);
|
---|
287 | }
|
---|
288 | else {
|
---|
289 | //converted win32 dll loaded by OS/2 loader
|
---|
290 | ODIN_FS_BEGIN
|
---|
291 | winmod = new Win32Dll(hinstance, NameTableId, Win32TableId, pfnDllEntry);
|
---|
292 | ODIN_FS_END
|
---|
293 | if(winmod == NULL) {
|
---|
294 | eprintf(("Failed to allocate module object!\n"));
|
---|
295 | DebugInt3();
|
---|
296 | return 0; //fail dll load
|
---|
297 | }
|
---|
298 | }
|
---|
299 | winmod->setTLSAddress(TlsAddress);
|
---|
300 | winmod->setTLSInitSize(TlsInitSize);
|
---|
301 | winmod->setTLSTotalSize(TlsTotalSize);
|
---|
302 | winmod->setTLSIndexAddr(TlsIndexAddr);
|
---|
303 | winmod->setTLSCallBackAddr(TlsCallbackAddr);
|
---|
304 |
|
---|
305 | /* @@@PH 1998/03/17 console devices initialization */
|
---|
306 | iConsoleDevicesRegister();
|
---|
307 |
|
---|
308 | //SvL: 19-8-'98
|
---|
309 | winmod->AddRef();
|
---|
310 | winmod->setVersionId(VersionResId);
|
---|
311 |
|
---|
312 | winmod->attachProcess();
|
---|
313 | }
|
---|
314 | else {//process detach
|
---|
315 | if(winmod != NULL && !fFreeLibrary) {
|
---|
316 | return 0; //don't unload (OS/2 dll unload bug)
|
---|
317 | }
|
---|
318 | //Runtime environment could already be gone, so don't do this
|
---|
319 | // dprintf(("KERNEL32: Dll Removed by FreeLibrary or ExitProcess\n"));
|
---|
320 | }
|
---|
321 | return 1; //success
|
---|
322 | }
|
---|
323 | //******************************************************************************
|
---|
324 | //******************************************************************************
|
---|
325 | void _System Win32DllExitList(ULONG reason)
|
---|
326 | {
|
---|
327 | dprintf(("Win32DllExitList %d\n", reason));
|
---|
328 |
|
---|
329 | if(WinExe) {
|
---|
330 | ODIN_delete(WinExe);
|
---|
331 | WinExe = NULL;
|
---|
332 | }
|
---|
333 | return;
|
---|
334 | }
|
---|
335 | //******************************************************************************
|
---|
336 | //******************************************************************************
|
---|
337 | VOID WIN32API ExitProcess(DWORD exitcode)
|
---|
338 | {
|
---|
339 | dprintf(("KERNEL32: ExitProcess %d\n", exitcode));
|
---|
340 | dprintf(("KERNEL32: ExitProcess FS = %x\n", GetFS()));
|
---|
341 |
|
---|
342 | //avoid crashes since win32 & OS/2 exception handler aren't identical
|
---|
343 | //(terminate process generates two exceptions)
|
---|
344 | /* @@@PH 1998/02/12 Added Console Support */
|
---|
345 | if (iConsoleIsActive())
|
---|
346 | iConsoleWaitClose();
|
---|
347 |
|
---|
348 | Win32DllExitList(0);
|
---|
349 |
|
---|
350 | //Restore original OS/2 TIB selector
|
---|
351 | DestroyTIB();
|
---|
352 | SetExceptionChain((ULONG)-1);
|
---|
353 |
|
---|
354 | O32_ExitProcess(exitcode);
|
---|
355 | }
|
---|
356 | //******************************************************************************
|
---|
357 | //******************************************************************************
|
---|
358 | BOOL WIN32API FreeLibrary(HINSTANCE hinstance)
|
---|
359 | {
|
---|
360 | Win32Dll *winmod;
|
---|
361 | BOOL rc;
|
---|
362 |
|
---|
363 | dprintf(("FreeLibrary"));
|
---|
364 | winmod = Win32Dll::findModule(hinstance);
|
---|
365 | if(winmod) {
|
---|
366 | winmod->Release();
|
---|
367 | return(TRUE);
|
---|
368 | }
|
---|
369 | dprintf(("KERNEL32: FreeLibrary %s %X\n", OS2GetDllName(hinstance), hinstance));
|
---|
370 |
|
---|
371 | //TODO: Not thread safe
|
---|
372 | fFreeLibrary = TRUE; //ditch dll
|
---|
373 | rc = O32_FreeLibrary(hinstance);
|
---|
374 | fFreeLibrary = FALSE;
|
---|
375 | dprintf(("FreeLibrary returned %X\n", rc));
|
---|
376 | return(TRUE);
|
---|
377 | }
|
---|
378 | /******************************************************************************/
|
---|
379 | /******************************************************************************/
|
---|
380 | static HINSTANCE iLoadLibraryA(LPCTSTR lpszLibFile)
|
---|
381 | {
|
---|
382 | HINSTANCE hDll;
|
---|
383 | Win32Dll *module;
|
---|
384 |
|
---|
385 | hDll = O32_LoadLibrary(lpszLibFile);
|
---|
386 | dprintf(("KERNEL32: iLoadLibraryA %s returned %X (%d)\n",
|
---|
387 | lpszLibFile,
|
---|
388 | hDll,
|
---|
389 | GetLastError()));
|
---|
390 | if(hDll)
|
---|
391 | {
|
---|
392 | return hDll; //converted dll or win32k took care of it
|
---|
393 | }
|
---|
394 |
|
---|
395 | if(Win32Image::isPEImage((char *)lpszLibFile)) {
|
---|
396 | module = Win32Dll::findModule((char *)lpszLibFile);
|
---|
397 | if(module) {//don't load it again
|
---|
398 | module->AddRef();
|
---|
399 | return module->getInstanceHandle();
|
---|
400 | }
|
---|
401 |
|
---|
402 | ODIN_FS_BEGIN
|
---|
403 | module = new Win32Dll((char *)lpszLibFile);
|
---|
404 | ODIN_FS_END
|
---|
405 | if(module == NULL)
|
---|
406 | return(0);
|
---|
407 |
|
---|
408 | module->init();
|
---|
409 | if(module->getError() != NO_ERROR) {
|
---|
410 | dprintf(("LoadLibary %s failed (::init)\n", lpszLibFile));
|
---|
411 | ODIN_delete(module);
|
---|
412 | return(0);
|
---|
413 | }
|
---|
414 | if(module->attachProcess() == FALSE) {
|
---|
415 | dprintf(("LoadLibary %s failed (::attachProcess)\n", lpszLibFile));
|
---|
416 | ODIN_delete(module);
|
---|
417 | return(0);
|
---|
418 | }
|
---|
419 | module->AddRef();
|
---|
420 | return module->getInstanceHandle();
|
---|
421 | }
|
---|
422 | else
|
---|
423 | return(0);
|
---|
424 | }
|
---|
425 |
|
---|
426 |
|
---|
427 | ODINFUNCTION1(HINSTANCE,LoadLibraryA,LPCTSTR,lpszLibFile)
|
---|
428 | //HINSTANCE WIN32API LoadLibraryA(LPCTSTR lpszLibFile)
|
---|
429 | {
|
---|
430 | HINSTANCE hDll;
|
---|
431 |
|
---|
432 | dprintf(("KERNEL32: LoadLibraryA(%s)\n",
|
---|
433 | lpszLibFile));
|
---|
434 |
|
---|
435 | hDll = iLoadLibraryA(lpszLibFile);
|
---|
436 | if (hDll == 0)
|
---|
437 | {
|
---|
438 | PSZ pszName;
|
---|
439 |
|
---|
440 | // remove path from the image name
|
---|
441 | pszName = strrchr((PSZ)lpszLibFile,
|
---|
442 | '\\');
|
---|
443 | if (pszName != NULL)
|
---|
444 | {
|
---|
445 | pszName++; // skip backslash
|
---|
446 |
|
---|
447 | // now try again without fully qualified path
|
---|
448 | hDll = iLoadLibraryA(pszName);
|
---|
449 | }
|
---|
450 | }
|
---|
451 |
|
---|
452 | return hDll;
|
---|
453 | }
|
---|
454 |
|
---|
455 |
|
---|
456 | //******************************************************************************
|
---|
457 | //******************************************************************************
|
---|
458 | HINSTANCE WIN32API LoadLibraryExA(LPCTSTR lpszLibFile, HANDLE hFile, DWORD dwFlags)
|
---|
459 | {
|
---|
460 | Win32Dll *module;
|
---|
461 | HINSTANCE hDll;
|
---|
462 |
|
---|
463 | dprintf(("KERNEL32: LoadLibraryExA %s (%X)\n", lpszLibFile, dwFlags));
|
---|
464 | hDll = O32_LoadLibrary(lpszLibFile);
|
---|
465 | if(hDll) {
|
---|
466 | return hDll; //converted dll or win32k took care of it
|
---|
467 | }
|
---|
468 |
|
---|
469 | if(Win32Image::isPEImage((char *)lpszLibFile)) {
|
---|
470 | module = Win32Dll::findModule((char *)lpszLibFile);
|
---|
471 | if(module) {//don't load it again
|
---|
472 | module->AddRef();
|
---|
473 | return module->getInstanceHandle();
|
---|
474 | }
|
---|
475 |
|
---|
476 | ODIN_FS_BEGIN
|
---|
477 | module = new Win32Dll((char *)lpszLibFile);
|
---|
478 | ODIN_FS_END
|
---|
479 |
|
---|
480 | if(module == NULL)
|
---|
481 | return(0);
|
---|
482 |
|
---|
483 | module->init();
|
---|
484 | if(module->getError() != NO_ERROR) {
|
---|
485 | dprintf(("LoadLibary %s failed (::init)\n", lpszLibFile));
|
---|
486 | ODIN_delete(module);
|
---|
487 | return(0);
|
---|
488 | }
|
---|
489 | if(dwFlags & DONT_RESOLVE_DLL_REFERENCES) {
|
---|
490 | module->setNoEntryCalls();
|
---|
491 | }
|
---|
492 | if(module->attachProcess() == FALSE) {
|
---|
493 | dprintf(("LoadLibary %s failed (::attachProcess)\n", lpszLibFile));
|
---|
494 | ODIN_delete(module);
|
---|
495 | return(0);
|
---|
496 | }
|
---|
497 | module->AddRef();
|
---|
498 | return module->getInstanceHandle();
|
---|
499 | }
|
---|
500 | return(0);
|
---|
501 | }
|
---|
502 | //******************************************************************************
|
---|
503 | //******************************************************************************
|
---|
504 | HINSTANCE WIN32API LoadLibraryW(LPCWSTR lpModule)
|
---|
505 | {
|
---|
506 | char *asciimodule;
|
---|
507 | HINSTANCE rc;
|
---|
508 |
|
---|
509 | asciimodule = UnicodeToAsciiString((LPWSTR)lpModule);
|
---|
510 | dprintf(("KERNEL32: OS2LoadLibraryW %s\n", asciimodule));
|
---|
511 | rc = LoadLibraryA(asciimodule);
|
---|
512 | free(asciimodule);
|
---|
513 | return(rc);
|
---|
514 | }
|
---|
515 | //******************************************************************************
|
---|
516 | //******************************************************************************
|
---|
517 | HINSTANCE WIN32API LoadLibraryExW(LPCWSTR lpModule, HANDLE hFile, DWORD dwFlags)
|
---|
518 | {
|
---|
519 | char *asciimodule;
|
---|
520 | HINSTANCE rc;
|
---|
521 |
|
---|
522 | asciimodule = UnicodeToAsciiString((LPWSTR)lpModule);
|
---|
523 | dprintf(("KERNEL32: OS2LoadLibraryExW %s (%d)\n", asciimodule, dwFlags));
|
---|
524 | rc = LoadLibraryExA(asciimodule, hFile, dwFlags);
|
---|
525 | free(asciimodule);
|
---|
526 | return(rc);
|
---|
527 | }
|
---|
528 | //******************************************************************************
|
---|
529 | //******************************************************************************
|
---|
530 | LPCSTR WIN32API GetCommandLineA()
|
---|
531 | {
|
---|
532 | LPTSTR cmdline = NULL;
|
---|
533 |
|
---|
534 | if(WinExe) {
|
---|
535 | cmdline = WinExe->getCommandLine();
|
---|
536 | }
|
---|
537 | if(cmdline == NULL) //not used for converted exes
|
---|
538 | cmdline = O32_GetCommandLine();
|
---|
539 |
|
---|
540 | dprintf(("KERNEL32: GetCommandLine %s\n", cmdline));
|
---|
541 | dprintf(("KERNEL32: FS = %x\n", GetFS()));
|
---|
542 | return(cmdline);
|
---|
543 | }
|
---|
544 | //******************************************************************************
|
---|
545 | //******************************************************************************
|
---|
546 | LPCWSTR WIN32API GetCommandLineW(void)
|
---|
547 | {
|
---|
548 | static WCHAR *UnicodeCmdLine = NULL;
|
---|
549 | char *asciicmdline = NULL;
|
---|
550 |
|
---|
551 | dprintf(("KERNEL32: FS = %x\n", GetFS()));
|
---|
552 |
|
---|
553 | if(UnicodeCmdLine)
|
---|
554 | return(UnicodeCmdLine); //already called before
|
---|
555 |
|
---|
556 | if(WinExe) {
|
---|
557 | asciicmdline = WinExe->getCommandLine();
|
---|
558 | }
|
---|
559 | if(asciicmdline == NULL) //not used for converted exes
|
---|
560 | asciicmdline = O32_GetCommandLine();
|
---|
561 |
|
---|
562 | if(asciicmdline) {
|
---|
563 | UnicodeCmdLine = (WCHAR *)malloc(strlen(asciicmdline)*2 + 2);
|
---|
564 | AsciiToUnicode(asciicmdline, UnicodeCmdLine);
|
---|
565 | dprintf(("KERNEL32: OS2GetCommandLineW: %s\n", asciicmdline));
|
---|
566 | return(UnicodeCmdLine);
|
---|
567 | }
|
---|
568 | dprintf(("KERNEL32: OS2GetCommandLineW: asciicmdline == NULL\n"));
|
---|
569 | return NULL;
|
---|
570 | }
|
---|
571 | //******************************************************************************
|
---|
572 | //******************************************************************************
|
---|
573 | DWORD WIN32API GetModuleFileNameA(HMODULE hinstModule, LPTSTR lpszPath, DWORD cchPath)
|
---|
574 | {
|
---|
575 | DWORD rc;
|
---|
576 | Win32Image *module;
|
---|
577 | char *fpath = NULL;
|
---|
578 |
|
---|
579 | dprintf(("GetModuleFileName %X", hinstModule));
|
---|
580 | if(hinstModule == 0 || hinstModule == -1 || (WinExe && hinstModule == WinExe->getOS2InstanceHandle())) {
|
---|
581 | module = (Win32Image *)WinExe;
|
---|
582 | }
|
---|
583 | else {
|
---|
584 | module = (Win32Image *)Win32Dll::findModule(hinstModule);
|
---|
585 | }
|
---|
586 |
|
---|
587 | if(module) {
|
---|
588 | fpath = module->getFullPath();
|
---|
589 | }
|
---|
590 | if(fpath) {
|
---|
591 | //SvL: 13-9-98: +1
|
---|
592 | rc = min(strlen(fpath)+1, cchPath);
|
---|
593 | strncpy(lpszPath, fpath, rc);
|
---|
594 | }
|
---|
595 | else rc = O32_GetModuleFileName(hinstModule, lpszPath, cchPath);
|
---|
596 |
|
---|
597 | if(rc) {
|
---|
598 | dprintf(("KERNEL32: GetModuleFileName %s %d\n", lpszPath, hinstModule));
|
---|
599 | }
|
---|
600 | return(rc);
|
---|
601 | }
|
---|
602 | //******************************************************************************
|
---|
603 | //******************************************************************************
|
---|
604 | DWORD WIN32API GetModuleFileNameW(HMODULE hModule, LPWSTR lpFileName, DWORD nSize)
|
---|
605 | {
|
---|
606 | char *asciifilename = (char *)malloc(nSize+1);
|
---|
607 | DWORD rc;
|
---|
608 |
|
---|
609 | dprintf(("KERNEL32: OS2GetModuleFileNameW\n"));
|
---|
610 | rc = GetModuleFileNameA(hModule, asciifilename, nSize);
|
---|
611 | if(rc) AsciiToUnicode(asciifilename, lpFileName);
|
---|
612 | free(asciifilename);
|
---|
613 | return(rc);
|
---|
614 | }
|
---|
615 | //******************************************************************************
|
---|
616 | //******************************************************************************
|
---|
617 | BOOL WIN32API CreateProcessA(LPCSTR lpszImageName, LPSTR lpszCommandLine,
|
---|
618 | PSECURITY_ATTRIBUTES arg3,
|
---|
619 | PSECURITY_ATTRIBUTES arg4, BOOL arg5, DWORD arg6,
|
---|
620 | PVOID arg7, LPCSTR arg8, LPSTARTUPINFOA arg9,
|
---|
621 | LPPROCESS_INFORMATION arg10)
|
---|
622 | {
|
---|
623 | BOOL rc;
|
---|
624 | char *cmdline;
|
---|
625 | BOOL fAllocStr = FALSE;
|
---|
626 |
|
---|
627 | if(O32_CreateProcess(lpszImageName, lpszCommandLine, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10) == TRUE)
|
---|
628 | return(TRUE);
|
---|
629 |
|
---|
630 | //probably a win32 exe, so run it in the pe loader
|
---|
631 | if(lpszImageName) {
|
---|
632 | if(lpszCommandLine) {
|
---|
633 | cmdline = (char *)malloc(strlen(lpszImageName)+strlen(lpszCommandLine) + 16);
|
---|
634 | sprintf(cmdline, "%s %s", lpszImageName, lpszCommandLine);
|
---|
635 | fAllocStr = TRUE;
|
---|
636 | }
|
---|
637 | else cmdline = (char *)lpszImageName;
|
---|
638 | }
|
---|
639 | else cmdline = (char *)lpszCommandLine;
|
---|
640 |
|
---|
641 | dprintf(("KERNEL32: CreateProcess %s\n", cmdline));
|
---|
642 | rc = O32_CreateProcess("PE.EXE", (LPCSTR)cmdline, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10);
|
---|
643 | if(fAllocStr)
|
---|
644 | free(cmdline);
|
---|
645 |
|
---|
646 | dprintf(("KERNEL32: CreateProcess returned %d\n", rc));
|
---|
647 | return(rc);
|
---|
648 | }
|
---|
649 | //******************************************************************************
|
---|
650 | //******************************************************************************
|
---|
651 | BOOL WIN32API CreateProcessW(LPCWSTR arg1, LPWSTR arg2,
|
---|
652 | PSECURITY_ATTRIBUTES arg3,
|
---|
653 | PSECURITY_ATTRIBUTES arg4,
|
---|
654 | BOOL arg5, DWORD arg6, PVOID arg7,
|
---|
655 | LPCWSTR arg8, LPSTARTUPINFOW arg9,
|
---|
656 | LPPROCESS_INFORMATION arg10)
|
---|
657 | {
|
---|
658 | BOOL rc;
|
---|
659 | char *astring1, *astring2, *astring3;
|
---|
660 |
|
---|
661 | dprintf(("KERNEL32: OS2CreateProcessW DOESN't WORK"));
|
---|
662 | astring1 = UnicodeToAsciiString((LPWSTR)arg1);
|
---|
663 | astring2 = UnicodeToAsciiString(arg2);
|
---|
664 | astring3 = UnicodeToAsciiString((LPWSTR)arg8);
|
---|
665 | // NOTE: This will not work as is (needs UNICODE support)
|
---|
666 | rc = CreateProcessA(astring1, astring2, arg3, arg4, arg5, arg6, arg7,
|
---|
667 | astring3, (LPSTARTUPINFOA)arg9, arg10);
|
---|
668 | FreeAsciiString(astring3);
|
---|
669 | FreeAsciiString(astring2);
|
---|
670 | FreeAsciiString(astring1);
|
---|
671 | return(rc);
|
---|
672 | }
|
---|
673 | //******************************************************************************
|
---|
674 | //******************************************************************************
|
---|
675 | FARPROC WIN32API GetProcAddress(HMODULE hModule, LPCSTR lpszProc)
|
---|
676 | {
|
---|
677 | Win32Dll *winmod;
|
---|
678 | FARPROC proc;
|
---|
679 | ULONG ulAPIOrdinal;
|
---|
680 |
|
---|
681 | winmod = Win32Dll::findModule((HINSTANCE)hModule);
|
---|
682 | if(winmod) {
|
---|
683 | ulAPIOrdinal = (ULONG)lpszProc;
|
---|
684 | if (ulAPIOrdinal <= 0x0000FFFF) {
|
---|
685 | return (FARPROC)winmod->getApi((int)ulAPIOrdinal);
|
---|
686 | }
|
---|
687 | else return (FARPROC)winmod->getApi((char *)lpszProc);
|
---|
688 | }
|
---|
689 | proc = O32_GetProcAddress(hModule, lpszProc);
|
---|
690 | dprintf(("KERNEL32: GetProcAddress %s from %X returned %X\n", lpszProc, hModule, proc));
|
---|
691 | return(proc);
|
---|
692 | }
|
---|
693 | //******************************************************************************
|
---|
694 | //Retrieve the version
|
---|
695 | //******************************************************************************
|
---|
696 | BOOL SYSTEM GetVersionStruct(char *modname, char *verstruct, ULONG bufLength)
|
---|
697 | {
|
---|
698 | HINSTANCE hinstance;
|
---|
699 | Win32Image *winimage;
|
---|
700 |
|
---|
701 | dprintf(("GetVersionStruct"));
|
---|
702 | hinstance = OS2QueryModuleHandle(modname);
|
---|
703 | if(hinstance == 0) {
|
---|
704 | dprintf(("GetVersionStruct can't find handle for %s\n", modname));
|
---|
705 | return(FALSE);
|
---|
706 | }
|
---|
707 | if(WinExe && WinExe->getOS2InstanceHandle() == hinstance) {
|
---|
708 | winimage = (Win32Image *)WinExe;
|
---|
709 | }
|
---|
710 | else {
|
---|
711 | winimage = (Win32Image *)Win32Dll::findModule(hinstance);
|
---|
712 | if(winimage == NULL) {
|
---|
713 | dprintf(("GetVersionStruct can't find Win32Image for %s\n", modname));
|
---|
714 | return(FALSE);
|
---|
715 | }
|
---|
716 | }
|
---|
717 | if(winimage->getVersionId() == -1) {
|
---|
718 | dprintf(("GetVersionStruct: %s has no version resource!\n", modname));
|
---|
719 | return(FALSE);
|
---|
720 | }
|
---|
721 | return OS2GetResource(hinstance, winimage->getVersionId(), verstruct, bufLength);
|
---|
722 | }
|
---|
723 | //******************************************************************************
|
---|
724 | //******************************************************************************
|
---|
725 | ULONG SYSTEM GetVersionSize(char *modname)
|
---|
726 | {
|
---|
727 | HINSTANCE hinstance;
|
---|
728 | Win32Image *winimage;
|
---|
729 |
|
---|
730 | dprintf(("GetVersionSize of %s\n", modname));
|
---|
731 | hinstance = OS2QueryModuleHandle(modname);
|
---|
732 | if(hinstance == 0) {
|
---|
733 | dprintf(("GetVersionSize can't find handle for %s\n", modname));
|
---|
734 | return(FALSE);
|
---|
735 | }
|
---|
736 |
|
---|
737 | if(WinExe && WinExe->getOS2InstanceHandle() == hinstance) {
|
---|
738 | winimage = (Win32Image *)WinExe;
|
---|
739 | }
|
---|
740 | else {
|
---|
741 | winimage = (Win32Image *)Win32Dll::findModule(hinstance);
|
---|
742 | if(winimage == NULL) {
|
---|
743 | dprintf(("GetVersionSize can't find Win32Image for %s\n", modname));
|
---|
744 | return(FALSE);
|
---|
745 | }
|
---|
746 | }
|
---|
747 | if(winimage->getVersionId() == -1) {
|
---|
748 | dprintf(("GetVersionSize: %s has no version resource!\n", modname));
|
---|
749 | return(FALSE);
|
---|
750 | }
|
---|
751 | ULONG size = OS2GetResourceSize(hinstance, winimage->getVersionId());
|
---|
752 |
|
---|
753 | dprintf(("Version resource size = %d, id %d\n", size, winimage->getVersionId()));
|
---|
754 | return(size);
|
---|
755 | }
|
---|
756 | //******************************************************************************
|
---|
757 | //******************************************************************************
|
---|
758 |
|
---|
759 |
|
---|
760 | /***********************************************************************
|
---|
761 | * RegisterServiceProcess (KERNEL, KERNEL32)
|
---|
762 | *
|
---|
763 | * A service process calls this function to ensure that it continues to run
|
---|
764 | * even after a user logged off.
|
---|
765 | */
|
---|
766 | DWORD WIN32API RegisterServiceProcess(DWORD dwProcessId,
|
---|
767 | DWORD dwType)
|
---|
768 | {
|
---|
769 | dprintf(("KERNEL32: RegisterServiceProcess(%08xh,%08xh) not implemented.\n",
|
---|
770 | dwProcessId,
|
---|
771 | dwType));
|
---|
772 |
|
---|
773 | /* I don't think that Wine needs to do anything in that function */
|
---|
774 | return 1; /* success */
|
---|
775 | }
|
---|
776 |
|
---|