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