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