source: trunk/src/kernel32/HandleManager.cpp@ 2129

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

Added token handlemanager class + HMHandleGetUserData export

File size: 111.4 KB
Line 
1/* $Id: HandleManager.cpp,v 1.31 1999-12-18 21:45:53 sandervl Exp $ */
2
3/*
4 * Win32 Unified Handle Manager for OS/2
5 *
6 * 1998/02/11 PH Patrick Haller (haller@zebra.fh-weingarten.de)
7 *
8 * @(#) HandleManager.Cpp 1.0.0 1998/02/11 PH start
9 *
10 * Project Odin Software License can be found in LICENSE.TXT
11 *
12 */
13
14#undef DEBUG_LOCAL
15//#define DEBUG_LOCAL
16
17
18/*****************************************************************************
19 * Remark *
20 *****************************************************************************
21
22 1998/02/11 PH Even overlapped I/O could be simulated by another subsystem
23 thread with a request queue. We'll see if required ...
24
25
26 Flush (flush handle buffer)
27 WaitForSingleObject
28 WaitForMultipleObjects (?)
29
30 1998/02/12 PH IBM and Microsoft disagree about the definition of FILE_TYPE_xxx
31 Interesting, Open32 returns Microsoft's values ...
32
33 1998/02/12 PH Handles should be equipped with a locking mechanism, in particular
34 as we publish a pointer into the handle table via HMHandleQueryHandleData
35
36 */
37
38
39/*****************************************************************************
40 * Includes *
41 *****************************************************************************/
42
43#include <os2win.h>
44#include <stdlib.h>
45#include <string.h>
46
47#include "unicode.h"
48#include "misc.h"
49
50#include "HandleManager.H"
51#include "HMDevice.h"
52#include "HMOpen32.h"
53#include "HMEvent.h"
54#include "HMMutex.h"
55#include "HMSemaphore.h"
56#include "HMMMap.h"
57#include "HMComm.h"
58#include "HMToken.h"
59#include <winconst.h>
60
61/*****************************************************************************
62 * Defines *
63 *****************************************************************************/
64
65 /* this is the size of our currently static handle table */
66#define MAX_OS2_HMHANDLES 2048
67
68
69/*****************************************************************************
70 * Structures *
71 *****************************************************************************/
72
73
74typedef struct _HMHANDLE
75{
76 HMDeviceHandler *pDeviceHandler; /* handler for this pseudo-device */
77 HMHANDLEDATA hmHandleData; /* attributes of the handle */
78} HMHANDLE, *PHMHANDLE;
79
80
81typedef struct _HMDEVICE
82{
83 struct _HMDEVICE *pNext; /* pointer to next device in chain */
84
85 LPSTR pszDeviceName; /* name or alias of the pseudo-device */
86 HMDeviceHandler *pDeviceHandler; /* handler for this pseudo-device */
87} HMDEVICE, *PHMDEVICE;
88
89
90/*****************************************************************************
91 * This pseudo-device logs all device requests to the logfile and returns *
92 * ERROR_INVALID_FUNCTION to virtually all requests -> debugging *
93 *****************************************************************************/
94class HMDeviceDebugClass : public HMDeviceHandler
95{
96 public:
97 HMDeviceDebugClass(LPCSTR lpDeviceName) : HMDeviceHandler(lpDeviceName) {}
98};
99
100
101/*****************************************************************************
102 * Process Global Structures *
103 *****************************************************************************/
104
105
106 /* the device name is repeated here to enable device alias names */
107static PHMDEVICE TabWin32Devices = NULL;
108static HMHANDLE TabWin32Handles[MAX_OS2_HMHANDLES]; /* static handle table */
109
110
111struct _HMGlobals
112{
113 HANDLE hStandardIn; /* stdin handle to CONIN$ */
114 HANDLE hStandardOut; /* stdout handle to CONOUT$ */
115 HANDLE hStandardError; /* stderr handle to CONOUT$ */
116
117 BOOL fIsInitialized; /* if HM is initialized already ? */
118 /* this MUST !!! be false initially */
119
120 HMDeviceHandler *pHMOpen32; /* default handle manager instance */
121 HMDeviceHandler *pHMEvent; /* static instances of subsystems */
122 HMDeviceHandler *pHMMutex;
123 HMDeviceHandler *pHMSemaphore;
124 HMDeviceHandler *pHMFileMapping; /* static instances of subsystems */
125 HMDeviceHandler *pHMComm; /* serial communication */
126 HMDeviceHandler *pHMToken; /* security tokens */
127
128 ULONG ulHandleLast; /* index of last used handle */
129} HMGlobals;
130
131
132/*****************************************************************************
133 * Local Prototypes *
134 *****************************************************************************/
135
136 /* get appropriate device handler by the device name */
137static HMDeviceHandler* _Optlink _HMDeviceFind(LPSTR pszDeviceName);
138
139 /* get next free handle from the handle table */
140static ULONG _Optlink _HMHandleGetFree(void);
141
142 /* get handle table entry from handle */
143static ULONG _Optlink _HMHandleQuery(HANDLE hHandle);
144
145
146
147/*****************************************************************************
148 * Name : static HMDeviceHandler * _HMDeviceFind
149 * Purpose : obtain appropriate device handler from the table by searching
150 * for a device name or alias
151 * Parameters: PSZ pszDeviceName
152 * Variables :
153 * Result : HMDeviceHandler * - pointer to the handler object
154 * Remark :
155 * Status :
156 *
157 * Author : Patrick Haller [Wed, 1998/02/11 20:42]
158 *****************************************************************************/
159
160static HMDeviceHandler *_HMDeviceFind (LPSTR pszDeviceName)
161{
162 PHMDEVICE pHMDevice; /* iterator over the device table */
163
164 if (pszDeviceName != NULL)
165 for (pHMDevice = TabWin32Devices; /* loop over all devices in the table */
166 pHMDevice != NULL;
167 pHMDevice = pHMDevice->pNext)
168 {
169 if (stricmp(pHMDevice->pszDeviceName, /* case-insensitive search */
170 pszDeviceName) == 0)
171 return (pHMDevice->pDeviceHandler); /* OK, we've found our device */
172 }
173
174 return (HMGlobals.pHMOpen32); /* haven't found anything, return default */
175}
176
177
178/*****************************************************************************
179 * Name : static int _HMHandleGetFree
180 * Purpose : get index to first free handle in the handle table
181 * Parameters:
182 * Variables :
183 * Result : int iIndex - index to the table or -1 in case of error
184 * Remark :
185 * Status :
186 *
187 * Author : Patrick Haller [Wed, 1998/02/11 20:43]
188 *****************************************************************************/
189
190static ULONG _HMHandleGetFree(void)
191{
192 register ULONG ulLoop;
193
194 for (ulLoop = 1; // @@@PH Note, this is an experimental change
195 // 0L as hHandle is sometimes considered invalid!
196 // this will never return 0l as free handle now.
197 ulLoop < MAX_OS2_HMHANDLES;
198 ulLoop++)
199 {
200 /* free handle found ? */
201 if (INVALID_HANDLE_VALUE == TabWin32Handles[ulLoop].hmHandleData.hHMHandle) {
202 TabWin32Handles[ulLoop].hmHandleData.dwUserData = 0;
203 TabWin32Handles[ulLoop].hmHandleData.dwInternalType = HMTYPE_UNKNOWN;
204 return (ulLoop); /* OK, then return it to the caller */
205 }
206 }
207
208 return (INVALID_HANDLE_VALUE); /* haven't found any free handle */
209}
210
211
212/*****************************************************************************
213 * Name : HMHandleGetUserData
214 * Purpose : Get the dwUserData dword for a specific handle
215 * Parameters: HANDLE hHandle
216 * Variables :
217 * Result : -1 or dwUserData
218 * Remark :
219 * Status :
220 *
221 * Author : SvL
222 *****************************************************************************/
223DWORD HMHandleGetUserData(ULONG hHandle)
224{
225 if (hHandle > MAX_OS2_HMHANDLES) /* check the table range */
226 return (-1);
227 /* Oops, invalid handle ! */
228 if (INVALID_HANDLE_VALUE == TabWin32Handles[hHandle].hmHandleData.hHMHandle)
229 return (-1); /* nope, ERROR_INVALID_HANDLE */
230
231 return TabWin32Handles[hHandle].hmHandleData.dwUserData;
232}
233
234
235/*****************************************************************************
236 * Name : static int _HMHandleQuery
237 * Purpose : gets the index of handle table entry as fast as possible from
238 * the specified handle
239 * Parameters: HANDLE hHandle
240 * Variables :
241 * Result : index or -1 in case of error
242 * Remark :
243 * Status :
244 *
245 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
246 *****************************************************************************/
247
248static ULONG _HMHandleQuery(HANDLE hHandle)
249{
250 if (hHandle > MAX_OS2_HMHANDLES) /* check the table range */
251 return (INVALID_HANDLE_VALUE); /* nope, ERROR_INVALID_HANDLE */
252
253 /* Oops, invalid handle ! */
254 if (INVALID_HANDLE_VALUE == TabWin32Handles[hHandle].hmHandleData.hHMHandle)
255 return (INVALID_HANDLE_VALUE); /* nope, ERROR_INVALID_HANDLE */
256
257 return ( hHandle); /* OK, we've got our handle index */
258}
259
260
261/*****************************************************************************
262 * Name : DWORD HMDeviceRegister
263 * Purpose : register a device with the handle manager
264 * Parameters: PSZ pszDeviceName
265 * HMDeviceHandler *pDeviceHandler
266 * Variables :
267 * Result : API returncode
268 * Remark :
269 * Status :
270 *
271 * Author : Patrick Haller [Wed, 1998/02/12 20:44]
272 *****************************************************************************/
273
274DWORD HMDeviceRegister(LPSTR pszDeviceName,
275 HMDeviceHandler *pDeviceHandler)
276{
277 PHMDEVICE pHMDevice; /* our new device to be allocated */
278
279 if ( (pszDeviceName == NULL) || /* check parameters */
280 (pDeviceHandler == NULL) )
281 return (ERROR_INVALID_PARAMETER); /* raise error conditon */
282
283
284 pHMDevice = (PHMDEVICE) malloc (sizeof (HMDEVICE) ); /* allocate memory */
285 if (pHMDevice == NULL) /* check proper allocation */
286 return (ERROR_NOT_ENOUGH_MEMORY); /* signal error */
287
288 pHMDevice->pszDeviceName = strdup(pszDeviceName); /* copy name */
289 if (pHMDevice->pszDeviceName == NULL) /* check proper allocation */
290 {
291 free (pHMDevice); /* free previously allocated memory */
292 return (ERROR_NOT_ENOUGH_MEMORY); /* signal error */
293 }
294
295 pHMDevice->pDeviceHandler = pDeviceHandler; /* store pointer to device */
296 pHMDevice->pNext = TabWin32Devices; /* establish linkage */
297
298 TabWin32Devices = pHMDevice; /* insert new node as root in the list */
299
300 return (NO_ERROR);
301}
302
303
304/*****************************************************************************
305 * Name : DWORD HMInitialize
306 * Purpose : Initialize the handlemanager
307 * Parameters: -
308 * Variables : -
309 * Result : always NO_ERROR
310 * Remark : this routine just stores the standard handles in the
311 * internal table within the HandleManager
312 * Status :
313 *
314 * Author : Patrick Haller [Wed, 1998/02/12 20:44]
315 *****************************************************************************/
316
317DWORD HMInitialize(void)
318{
319 ULONG ulIndex;
320
321 if (HMGlobals.fIsInitialized != TRUE)
322 {
323 HMGlobals.fIsInitialized = TRUE; /* OK, done */
324
325 // fill handle table
326 for (ulIndex = 0;
327 ulIndex < MAX_OS2_HMHANDLES;
328 ulIndex++)
329 TabWin32Handles[ulIndex].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
330
331 dprintf(("KERNEL32:HandleManager:HMInitialize() storing handles.\n"));
332
333 memset(&HMGlobals, /* zero out the structure first */
334 0,
335 sizeof(HMGlobals));
336
337 /* copy standard handles from OS/2's Open32 Subsystem */
338 HMSetStdHandle(STD_INPUT_HANDLE, GetStdHandle(STD_INPUT_HANDLE));
339 HMSetStdHandle(STD_OUTPUT_HANDLE, GetStdHandle(STD_OUTPUT_HANDLE));
340 HMSetStdHandle(STD_ERROR_HANDLE, GetStdHandle(STD_ERROR_HANDLE));
341
342 /* create handle manager instance for Open32 handles */
343 HMGlobals.pHMOpen32 = new HMDeviceOpen32Class("\\\\.\\");
344 HMGlobals.pHMEvent = new HMDeviceEventClass("\\\\EVENT\\");
345 HMGlobals.pHMMutex = new HMDeviceMutexClass("\\\\MUTEX\\");
346 HMGlobals.pHMSemaphore = new HMDeviceSemaphoreClass("\\\\SEM\\");
347 HMGlobals.pHMFileMapping= new HMDeviceMemMapClass("\\\\MEMMAP\\");
348 HMGlobals.pHMComm = new HMDeviceCommClass("\\\\COM\\");
349 HMGlobals.pHMToken = new HMDeviceTokenClass("\\\\TOKEN\\");
350 }
351 return (NO_ERROR);
352}
353
354
355/*****************************************************************************
356 * Name : DWORD HMTerminate
357 * Purpose : Terminate the handlemanager
358 * Parameters:
359 * Variables :
360 * Result :
361 * Remark :
362 * Status :
363 *
364 * Author : Patrick Haller [Wed, 1998/02/12 20:44]
365 *****************************************************************************/
366
367DWORD HMTerminate(void)
368{
369 /* @@@PH we could deallocate the device list here */
370
371 delete HMGlobals.pHMOpen32;
372 delete HMGlobals.pHMEvent;
373 delete HMGlobals.pHMMutex;
374 delete HMGlobals.pHMSemaphore;
375 delete HMGlobals.pHMFileMapping;
376 delete HMGlobals.pHMComm;
377
378 return (NO_ERROR);
379}
380
381
382/*****************************************************************************/
383/* handle translation buffer management */
384/* */
385/* Since some Win32 applications rely (!) on 16-bit handles, we've got to do */
386/* 32-bit to 16-bit and vs vsa translation here. */
387/* Filehandle-based functions should be routed via the handlemanager instead */
388/* of going to Open32 directly. */
389/*****************************************************************************/
390
391
392/*****************************************************************************
393 * Name : DWORD HMHandleAllocate
394 * Purpose : allocate a handle in the translation table
395 * Parameters: PULONG pHandle16 - to return the allocated handle
396 * ULONG hHandle32 - the associated OS/2 handle
397 * Variables :
398 * Result : API returncode
399 * Remark : no parameter checking is done, phHandle may not be invalid
400 * hHandle32 shouldn't be 0
401 * Should be protected with a HM-Mutex !
402 * Status :
403 *
404 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
405 *****************************************************************************/
406
407DWORD HMHandleAllocate (PULONG phHandle16,
408 ULONG hHandleOS2)
409{
410 register ULONG ulHandle;
411
412#ifdef DEBUG_LOCAL
413 dprintf(("KERNEL32: HMHandleAllocate (%08xh,%08xh)\n",
414 phHandle16,
415 hHandleOS2));
416#endif
417
418 ulHandle = HMGlobals.ulHandleLast; /* get free handle */
419
420 if(ulHandle == 0) {
421 ulHandle = 1; //SvL: Start searching from index 1
422 }
423 do
424 {
425 /* check if handle is free */
426 if (TabWin32Handles[ulHandle].hmHandleData.hHMHandle == INVALID_HANDLE_VALUE)
427 {
428 *phHandle16 = ulHandle;
429 TabWin32Handles[ulHandle].hmHandleData.hHMHandle = hHandleOS2;
430 HMGlobals.ulHandleLast = ulHandle; /* to shorten search times */
431
432 return (NO_ERROR); /* OK */
433 }
434
435 ulHandle++; /* skip to next entry */
436
437 if (ulHandle >= MAX_OS2_HMHANDLES) /* check boundary */
438 ulHandle = 1;
439 }
440 while (ulHandle != HMGlobals.ulHandleLast);
441
442 return (ERROR_TOO_MANY_OPEN_FILES); /* OK, we're done */
443}
444
445
446/*****************************************************************************
447 * Name : DWORD HMHandleFree
448 * Purpose : free a handle from the translation table
449 * Parameters: ULONG hHandle16 - the handle to be freed
450 * Variables :
451 * Result : API returncode
452 * Remark : no parameter checking is done, hHandle16 MAY NEVER exceed
453 * the MAX_TRANSLATION_HANDLES boundary
454 * Status :
455 *
456 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
457 *****************************************************************************/
458
459DWORD HMHandleFree (ULONG hHandle16)
460{
461 ULONG rc; /* API returncode */
462
463#ifdef DEBUG_LOCAL
464 dprintf(("KERNEL32: HMHandleFree (%08xh)\n",
465 hHandle16));
466#endif
467
468 rc = HMHandleValidate(hHandle16); /* verify handle */
469 if (rc != NO_ERROR) /* check errors */
470 return (rc); /* raise error condition */
471
472 TabWin32Handles[hHandle16].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
473 /* OK, done */
474
475 return (NO_ERROR);
476}
477
478
479/*****************************************************************************
480 * Name : DWORD HMHandleValidate
481 * Purpose : validate a handle through the translation table
482 * Parameters: ULONG hHandle16 - the handle to be verified
483 * Variables :
484 * Result : API returncode
485 * Remark :
486 * Status :
487 *
488 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
489 *****************************************************************************/
490
491DWORD HMHandleValidate (ULONG hHandle16)
492{
493#ifdef DEBUG_LOCAL
494 dprintf(("KERNEL32: HMHandleValidate (%08xh)\n",
495 hHandle16));
496#endif
497
498 if (hHandle16 >= MAX_OS2_HMHANDLES) /* check boundary */
499 return (ERROR_INVALID_HANDLE); /* raise error condition */
500
501 if (TabWin32Handles[hHandle16].hmHandleData.hHMHandle == INVALID_HANDLE_VALUE)
502 /* valid ? */
503 return (ERROR_INVALID_HANDLE); /* raise error condition */
504
505 return (NO_ERROR);
506}
507
508
509/*****************************************************************************
510 * Name : DWORD HMHandleTranslateToWin
511 * Purpose : translate a 32-bit OS/2 handle to the associated windows handle
512 * Parameters: ULONG hHandle32 - the OS/2 handle
513 * PULONG phHandle16 - the associated windows handle
514 * Variables :
515 * Result : API returncode
516 * Remark :
517 * Status :
518 *
519 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
520 *****************************************************************************/
521
522DWORD HMHandleTranslateToWin (ULONG hHandleOS2,
523 PULONG phHandle16)
524{
525 ULONG rc; /* API returncode */
526 register ULONG ulIndex; /* index counter over the table */
527
528#ifdef DEBUG_LOCAL
529 dprintf(("KERNEL32: HMHandleTranslateToWin (%08xh, %08xh)\n",
530 hHandleOS2,
531 phHandle16));
532#endif
533
534 for (ulIndex = 1;
535 ulIndex < MAX_OS2_HMHANDLES;
536 ulIndex++)
537 {
538 /* look for the handle */
539 if (TabWin32Handles[ulIndex].hmHandleData.hHMHandle == hHandleOS2)
540 {
541 *phHandle16 = ulIndex; /* deliver result */
542 return (NO_ERROR); /* OK */
543 }
544 }
545
546 return (ERROR_INVALID_HANDLE); /* raise error condition */
547}
548
549
550/*****************************************************************************
551 * Name : DWORD HMHandleTranslateToOS2
552 * Purpose : translate a 16-bit Win32 handle to the associated OS/2 handle
553 * Parameters: ULONG hHandle16 - the windows handle
554 * PULONG phHandle32 - the associated OS/2 handle
555 * Variables :
556 * Result : API returncode
557 * Remark :
558 * Status :
559 *
560 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
561 *****************************************************************************/
562
563DWORD HMHandleTranslateToOS2 (ULONG hHandle16,
564 PULONG phHandleOS2)
565{
566#ifdef DEBUG_LOCAL
567 dprintf(("KERNEL32: HMHandleTranslateToOS2 (%08xh, %08xh)\n",
568 hHandle16,
569 phHandleOS2));
570#endif
571
572 if (HMHandleValidate(hHandle16) == NO_ERROR) /* verify handle */
573 {
574 *phHandleOS2 = TabWin32Handles[hHandle16].hmHandleData.hHMHandle;
575 return (NO_ERROR);
576 }
577
578 return (ERROR_INVALID_HANDLE); /* raise error condition */
579}
580
581
582/*****************************************************************************
583 * Name : DWORD HMHandleTranslateToOS2i
584 * Purpose : translate a 16-bit Win32 handle to the associated OS/2 handle
585 * Parameters: ULONG hHandle16 - the windows handle
586 * Variables :
587 * Result : OS/2 handle
588 * Remark : no checkinf
589 * Status :
590 *
591 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
592 *****************************************************************************/
593
594DWORD HMHandleTranslateToOS2i (ULONG hHandle16)
595{
596#ifdef DEBUG_LOCAL
597 dprintf(("KERNEL32: HMHandleTranslateToOS2i (%08xh)\n",
598 hHandle16));
599#endif
600
601 return(TabWin32Handles[hHandle16].hmHandleData.hHMHandle);
602}
603
604
605
606/*****************************************************************************
607 * Name : HANDLE _HMGetStdHandle
608 * Purpose : replacement for Open32's GetStdHandle function
609 * Parameters: DWORD nStdHandle
610 * Variables :
611 * Result : HANDLE to standard device
612 * Remark :
613 * Status :
614 *
615 * Author : Patrick Haller [Wed, 1998/02/12 20:44]
616 *****************************************************************************/
617
618HANDLE HMGetStdHandle(DWORD nStdHandle)
619{
620 switch (nStdHandle)
621 {
622 case STD_INPUT_HANDLE: return (HMGlobals.hStandardIn);
623 case STD_OUTPUT_HANDLE: return (HMGlobals.hStandardOut);
624 case STD_ERROR_HANDLE: return (HMGlobals.hStandardError);
625
626 default:
627 {
628 SetLastError(ERROR_INVALID_PARAMETER); /* set error information */
629 return (INVALID_HANDLE_VALUE); /* raise error condition */
630 }
631 }
632}
633
634
635/*****************************************************************************
636 * Name : HANDLE _HMSetStdHandle
637 * Purpose : replacement for Open32's SetStdHandle function
638 * Parameters: DWORD nStdHandle
639 * HANDLE hHandle
640 * Variables :
641 * Result : BOOL fSuccess
642 * Remark :
643 * Status :
644 *
645 * Author : Patrick Haller [Wed, 1998/02/12 20:44]
646 *****************************************************************************/
647
648BOOL HMSetStdHandle(DWORD nStdHandle,
649 HANDLE hHandle)
650{
651 switch (nStdHandle)
652 {
653 case STD_INPUT_HANDLE: HMGlobals.hStandardIn = hHandle; return TRUE;
654 case STD_OUTPUT_HANDLE: HMGlobals.hStandardOut = hHandle; return TRUE;
655 case STD_ERROR_HANDLE: HMGlobals.hStandardError = hHandle; return TRUE;
656
657 default:
658 {
659 SetLastError(ERROR_INVALID_PARAMETER); /* set error information */
660 return (FALSE); /* raise error condition */
661 }
662 }
663}
664
665
666/*****************************************************************************
667 * Name : HANDLE HMDuplicateHandle
668 * Purpose : replacement for Open32's HMDuplicateHandle function
669 * Parameters:
670 *
671 * Variables :
672 * Result : BOOL fSuccess
673 * Remark :
674 * Status :
675 *
676 * Author : Sander van Leeuwen [Wed, 1999/08/25 15:44]
677 *****************************************************************************/
678
679BOOL HMDuplicateHandle(HANDLE srcprocess,
680 HANDLE srchandle,
681 HANDLE destprocess,
682 PHANDLE desthandle,
683 DWORD fdwAccess,
684 BOOL fInherit,
685 DWORD fdwOptions)
686{
687 int iIndex; /* index into the handle table */
688 int iIndexNew; /* index into the handle table */
689 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
690 PHMHANDLEDATA pHMHandleData;
691 DWORD rc; /* API return code */
692
693 if(HMHandleValidate(srchandle) != NO_ERROR)
694 {
695 dprintf(("KERNEL32: HMDuplicateHandle: invalid handle %x", srchandle));
696 SetLastError(ERROR_INVALID_HANDLE); /* use this as error message */
697 return FALSE;
698 }
699
700 pDeviceHandler = TabWin32Handles[srchandle].pDeviceHandler; /* device is predefined */
701 iIndexNew = _HMHandleGetFree(); /* get free handle */
702 if (-1 == iIndexNew) /* oops, no free handles ! */
703 {
704 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
705 return FALSE; /* signal error */
706 }
707
708 /* initialize the complete HMHANDLEDATA structure */
709 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
710 pHMHandleData->dwType = TabWin32Handles[srchandle].hmHandleData.dwType;
711 if (fdwOptions & DUPLICATE_SAME_ACCESS)
712 pHMHandleData->dwAccess = TabWin32Handles[srchandle].hmHandleData.dwAccess;
713 else
714 pHMHandleData->dwAccess = fdwAccess;
715
716 pHMHandleData->dwShare = TabWin32Handles[srchandle].hmHandleData.dwShare;
717 pHMHandleData->dwCreation = TabWin32Handles[srchandle].hmHandleData.dwCreation;
718 pHMHandleData->dwFlags = TabWin32Handles[srchandle].hmHandleData.dwFlags;
719 pHMHandleData->lpHandlerData = TabWin32Handles[srchandle].hmHandleData.lpHandlerData;
720
721
722 /* we've got to mark the handle as occupied here, since another device */
723 /* could be created within the device handler -> deadlock */
724
725 /* write appropriate entry into the handle table if open succeeded */
726 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
727 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
728
729 /* call the device handler */
730 rc = pDeviceHandler->DuplicateHandle(&TabWin32Handles[iIndexNew].hmHandleData,
731 srcprocess,
732 &TabWin32Handles[srchandle].hmHandleData,
733 destprocess,
734 desthandle,
735 fdwAccess,
736 fInherit,
737 fdwOptions & ~DUPLICATE_CLOSE_SOURCE);
738
739 //Don't let Open32 close it for us, but do it manually (regardless of error; see SDK docs))
740 if (fdwOptions & DUPLICATE_CLOSE_SOURCE)
741 HMCloseHandle(srchandle);
742
743 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
744 {
745 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
746 SetLastError(rc); /* Hehe, OS/2 and NT are pretty compatible :) */
747 return FALSE; /* signal error */
748 }
749 else
750 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
751
752 *desthandle = iIndexNew;
753 return TRUE; /* return valid handle */
754}
755
756/*****************************************************************************
757 * Name : HANDLE HMCreateFile
758 * Purpose : Wrapper for the CreateFile() API
759 * Parameters:
760 * Variables :
761 * Result :
762 * Remark : Fix parameters passed to the HMDeviceManager::CreateFile
763 * Supply access mode and share mode validation routines
764 * Status :
765 *
766 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
767 *****************************************************************************/
768
769HFILE HMCreateFile(LPCSTR lpFileName,
770 DWORD dwDesiredAccess,
771 DWORD dwShareMode,
772 LPSECURITY_ATTRIBUTES lpSecurityAttributes,
773 DWORD dwCreationDisposition,
774 DWORD dwFlagsAndAttributes,
775 HANDLE hTemplateFile)
776{
777 int iIndex; /* index into the handle table */
778 int iIndexNew; /* index into the handle table */
779 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
780 HANDLE hResult;
781 DWORD rc; /* API return code */
782 PHMHANDLEDATA pHMHandleData;
783 HMHANDLEDATA HMHandleTemp; /* temporary buffer for handle data */
784
785 /* create new handle by either lpFileName or hTemplateFile */
786 if (lpFileName == NULL) /* this indicates creation from template */
787 {
788 iIndex = _HMHandleQuery(hTemplateFile); /* query table for template */
789 if (-1 == iIndex) /* this device is unknown to us */
790 {
791 SetLastError (ERROR_INVALID_HANDLE);
792 return INVALID_HANDLE_VALUE;
793 }
794 else
795 {
796 /* to pass to handler */
797 pHMHandleData = &TabWin32Handles[iIndex].hmHandleData;
798 pDeviceHandler = TabWin32Handles[iIndex].pDeviceHandler;
799 }
800 }
801 else
802 {
803 pDeviceHandler = _HMDeviceFind((LPSTR)lpFileName); /* find device */
804
805 if (NULL == pDeviceHandler) /* this name is unknown to us */
806 {
807 SetLastError(ERROR_FILE_NOT_FOUND);
808 return (INVALID_HANDLE_VALUE); /* signal error */
809 }
810 else
811 pHMHandleData = NULL;
812 }
813
814
815 iIndexNew = _HMHandleGetFree(); /* get free handle */
816 if (-1 == iIndexNew) /* oops, no free handles ! */
817 {
818 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
819 return (INVALID_HANDLE_VALUE); /* signal error */
820 }
821
822
823 /* initialize the complete HMHANDLEDATA structure */
824 if (lpFileName == NULL) /* create from template */
825 memcpy (&HMHandleTemp,
826 &TabWin32Handles[iIndex].hmHandleData,
827 sizeof(HMHANDLEDATA));
828 else
829 {
830 HMHandleTemp.dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
831 HMHandleTemp.dwAccess = dwDesiredAccess;
832 HMHandleTemp.dwShare = dwShareMode;
833 HMHandleTemp.dwCreation = dwCreationDisposition;
834 HMHandleTemp.dwFlags = dwFlagsAndAttributes;
835 HMHandleTemp.lpHandlerData = NULL;
836 }
837
838 /* we've got to mark the handle as occupied here, since another device */
839 /* could be created within the device handler -> deadlock */
840
841 /* write appropriate entry into the handle table if open succeeded */
842 HMHandleTemp.hHMHandle = iIndexNew;
843 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
844
845 /* now copy back our temporary handle data */
846 memcpy(&TabWin32Handles[iIndexNew].hmHandleData,
847 &HMHandleTemp,
848 sizeof(HMHANDLEDATA));
849
850 rc = pDeviceHandler->CreateFile(lpFileName, /* call the device handler */
851 &HMHandleTemp,
852 lpSecurityAttributes,
853 pHMHandleData);
854
855#ifdef DEBUG_LOCAL
856 dprintf(("KERNEL32/HandleManager:CheckPoint2: %s lpHandlerData=%08xh\n",
857 lpFileName,
858 HMHandleTemp.lpHandlerData));
859#endif
860
861 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
862 {
863 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
864 SetLastError(rc); /* Hehe, OS/2 and NT are pretty compatible :) */
865 return (INVALID_HANDLE_VALUE); /* signal error */
866 }
867 else
868 {
869 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
870
871 /* copy data fields that might have been modified by CreateFile */
872 memcpy(&TabWin32Handles[iIndexNew].hmHandleData,
873 &HMHandleTemp,
874 sizeof(HMHANDLEDATA));
875 }
876
877
878#ifdef DEBUG_LOCAL
879 dprintf(("KERNEL32/HandleManager: CreateFile(%s)=%08xh\n",
880 lpFileName,
881 iIndexNew));
882#endif
883
884 return (HFILE)iIndexNew; /* return valid handle */
885}
886
887
888/*****************************************************************************
889 * Name : HANDLE HMOpenFile
890 * Purpose : Wrapper for the OpenFile() API
891 * Parameters:
892 * Variables :
893 * Result :
894 * Remark : Fix parameters passed to the HMDeviceManager::OpenFile
895 * Supply access mode and share mode validation routines
896 * Status :
897 *
898 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
899 *****************************************************************************/
900
901
902/***********************************************************************
903 * FILE_ConvertOFMode
904 *
905 * Convert OF_* mode into flags for CreateFile.
906 */
907static void FILE_ConvertOFMode( INT mode, DWORD *access, DWORD *sharing )
908{
909 switch(mode & 0x03)
910 {
911 case OF_READ: *access = GENERIC_READ; break;
912 case OF_WRITE: *access = GENERIC_WRITE; break;
913 case OF_READWRITE: *access = GENERIC_READ | GENERIC_WRITE; break;
914 default: *access = 0; break;
915 }
916 switch(mode & 0x70)
917 {
918 case OF_SHARE_EXCLUSIVE: *sharing = 0; break;
919 case OF_SHARE_DENY_WRITE: *sharing = FILE_SHARE_READ; break;
920 case OF_SHARE_DENY_READ: *sharing = FILE_SHARE_WRITE; break;
921 case OF_SHARE_DENY_NONE:
922 case OF_SHARE_COMPAT:
923 default: *sharing = FILE_SHARE_READ | FILE_SHARE_WRITE; break;
924 }
925}
926
927HANDLE HMOpenFile(LPCSTR lpFileName,
928 OFSTRUCT* pOFStruct,
929 UINT fuMode)
930{
931 int iIndex; /* index into the handle table */
932 int iIndexNew; /* index into the handle table */
933 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
934 PHMHANDLEDATA pHMHandleData;
935 DWORD rc; /* API return code */
936
937
938 pDeviceHandler = _HMDeviceFind((LPSTR)lpFileName); /* find device */
939 if (NULL == pDeviceHandler) /* this name is unknown to us */
940 {
941 SetLastError(ERROR_FILE_NOT_FOUND);
942 return (INVALID_HANDLE_VALUE); /* signal error */
943 }
944 else
945 pHMHandleData = NULL;
946
947
948 iIndexNew = _HMHandleGetFree(); /* get free handle */
949 if (-1 == iIndexNew) /* oops, no free handles ! */
950 {
951 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
952 return (INVALID_HANDLE_VALUE); /* signal error */
953 }
954
955
956 /* initialize the complete HMHANDLEDATA structure */
957 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
958 pHMHandleData->dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
959
960 FILE_ConvertOFMode(fuMode, /* map OF_flags */
961 &pHMHandleData->dwAccess,
962 &pHMHandleData->dwShare);
963
964 pHMHandleData->dwCreation = 0;
965 pHMHandleData->dwFlags = 0;
966 pHMHandleData->lpHandlerData = NULL;
967
968
969 /* we've got to mark the handle as occupied here, since another device */
970 /* could be created within the device handler -> deadlock */
971
972 /* write appropriate entry into the handle table if open succeeded */
973 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
974 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
975
976 rc = pDeviceHandler->OpenFile (lpFileName, /* call the device handler */
977 &TabWin32Handles[iIndexNew].hmHandleData,
978 pOFStruct,
979 fuMode);
980
981#ifdef DEBUG_LOCAL
982 dprintf(("KERNEL32/HandleManager:CheckPoint3: %s lpHandlerData=%08xh rc=%08xh\n",
983 lpFileName,
984 &TabWin32Handles[iIndexNew].hmHandleData.lpHandlerData,
985 rc));
986#endif
987
988 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
989 {
990 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
991 SetLastError(rc); /* Hehe, OS/2 and NT are pretty compatible :) */
992 return (INVALID_HANDLE_VALUE); /* signal error */
993 }
994 else
995 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
996
997#ifdef DEBUG_LOCAL
998 dprintf(("KERNEL32/HandleManager: OpenFile(%s)=%08xh\n",
999 lpFileName,
1000 hResult));
1001#endif
1002
1003 return iIndexNew; /* return valid handle */
1004}
1005
1006
1007
1008/*****************************************************************************
1009 * Name : HANDLE HMCloseFile
1010 * Purpose : Wrapper for the CloseHandle() API
1011 * Parameters:
1012 * Variables :
1013 * Result :
1014 * Remark :
1015 * Status :
1016 *
1017 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
1018 *****************************************************************************/
1019
1020BOOL HMCloseHandle(HANDLE hObject)
1021{
1022 int iIndex; /* index into the handle table */
1023 BOOL fResult; /* result from the device handler's CloseHandle() */
1024 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1025
1026 /* validate handle */
1027 iIndex = _HMHandleQuery(hObject); /* get the index */
1028 if (-1 == iIndex) /* error ? */
1029 {
1030 //@@@PH it may occur someone closes e.g. a semaphore handle
1031 // which is not registered through the HandleManager yet.
1032 // so we try to pass on to Open32 instead.
1033 dprintf(("KERNEL32: HandleManager:HMCloseHandle(%08xh) passed on to Open32.\n",
1034 hObject));
1035
1036 fResult = O32_CloseHandle(hObject);
1037 return (fResult);
1038
1039 //SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1040 //return (FALSE); /* signal failure */
1041 }
1042
1043 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1044 //SvL: Check if pDeviceHandler is set
1045 if (pHMHandle->pDeviceHandler)
1046 {
1047 fResult = pHMHandle->pDeviceHandler->CloseHandle(&pHMHandle->hmHandleData);
1048 }
1049 else
1050 {
1051 dprintf(("HMCloseHAndle(%08xh): pDeviceHandler not set", hObject));
1052 fResult = TRUE;
1053 }
1054
1055 if (fResult == TRUE) /* remove handle if close succeeded */
1056 {
1057 pHMHandle->hmHandleData.hHMHandle = INVALID_HANDLE_VALUE; /* mark handle as free */
1058 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
1059 }
1060
1061 return (fResult); /* deliver return code */
1062}
1063
1064
1065/*****************************************************************************
1066 * Name : HANDLE HMReadFile
1067 * Purpose : Wrapper for the ReadHandle() API
1068 * Parameters:
1069 * Variables :
1070 * Result :
1071 * Remark :
1072 * Status :
1073 *
1074 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
1075 *****************************************************************************/
1076
1077BOOL HMReadFile(HANDLE hFile,
1078 LPVOID lpBuffer,
1079 DWORD nNumberOfBytesToRead,
1080 LPDWORD lpNumberOfBytesRead,
1081 LPOVERLAPPED lpOverlapped)
1082{
1083 int iIndex; /* index into the handle table */
1084 BOOL fResult; /* result from the device handler's CloseHandle() */
1085 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1086
1087 /* validate handle */
1088 iIndex = _HMHandleQuery(hFile); /* get the index */
1089 if (-1 == iIndex) /* error ? */
1090 {
1091 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1092 return (FALSE); /* signal failure */
1093 }
1094
1095 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1096 fResult = pHMHandle->pDeviceHandler->ReadFile(&pHMHandle->hmHandleData,
1097 lpBuffer,
1098 nNumberOfBytesToRead,
1099 lpNumberOfBytesRead,
1100 lpOverlapped);
1101
1102 return (fResult); /* deliver return code */
1103}
1104
1105
1106/*****************************************************************************
1107 * Name : HANDLE HMWriteFile
1108 * Purpose : Wrapper for the WriteHandle() API
1109 * Parameters:
1110 * Variables :
1111 * Result :
1112 * Remark :
1113 * Status :
1114 *
1115 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
1116 *****************************************************************************/
1117
1118BOOL HMWriteFile(HANDLE hFile,
1119 LPCVOID lpBuffer,
1120 DWORD nNumberOfBytesToWrite,
1121 LPDWORD lpNumberOfBytesWritten,
1122 LPOVERLAPPED lpOverlapped)
1123{
1124 int iIndex; /* index into the handle table */
1125 BOOL fResult; /* result from the device handler's CloseHandle() */
1126 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1127
1128 /* validate handle */
1129 iIndex = _HMHandleQuery(hFile); /* get the index */
1130 if (-1 == iIndex) /* error ? */
1131 {
1132 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1133 return (FALSE); /* signal failure */
1134 }
1135
1136 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1137 fResult = pHMHandle->pDeviceHandler->WriteFile(&pHMHandle->hmHandleData,
1138 lpBuffer,
1139 nNumberOfBytesToWrite,
1140 lpNumberOfBytesWritten,
1141 lpOverlapped);
1142
1143 return (fResult); /* deliver return code */
1144}
1145
1146
1147/*****************************************************************************
1148 * Name : HANDLE HMGetFileType
1149 * Purpose : Wrapper for the GetFileType() API
1150 * Parameters:
1151 * Variables :
1152 * Result :
1153 * Remark :
1154 * Status :
1155 *
1156 * Author : Patrick Haller [Wed, 1998/02/12 13:37]
1157 *****************************************************************************/
1158
1159DWORD HMGetFileType(HANDLE hFile)
1160{
1161 int iIndex; /* index into the handle table */
1162 DWORD dwResult; /* result from the device handler's API */
1163 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1164
1165 /* validate handle */
1166 iIndex = _HMHandleQuery(hFile); /* get the index */
1167 if (-1 == iIndex) /* error ? */
1168 {
1169 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1170 return FILE_TYPE_UNKNOWN; /* signal failure */
1171 }
1172
1173 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1174 dwResult = pHMHandle->pDeviceHandler->GetFileType(&pHMHandle->hmHandleData);
1175
1176 return (dwResult); /* deliver return code */
1177}
1178
1179
1180/*****************************************************************************
1181 * Name : HMDeviceHandler::_DeviceReuqest
1182 * Purpose : entry method for special request functions
1183 * Parameters: ULONG ulRequestCode
1184 * various parameters as required
1185 * Variables :
1186 * Result :
1187 * Remark : the standard behaviour is to return an error code for non-
1188 * existant request codes
1189 * Status :
1190 *
1191 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
1192 *****************************************************************************/
1193
1194DWORD HMDeviceRequest (HANDLE hFile,
1195 ULONG ulRequestCode,
1196 ULONG arg1,
1197 ULONG arg2,
1198 ULONG arg3,
1199 ULONG arg4)
1200{
1201 int iIndex; /* index into the handle table */
1202 DWORD dwResult; /* result from the device handler's API */
1203 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1204
1205 /* validate handle */
1206 iIndex = _HMHandleQuery(hFile); /* get the index */
1207 if (-1 == iIndex) /* error ? */
1208 {
1209 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1210 return (INVALID_HANDLE_ERROR); /* signal failure */
1211 }
1212
1213 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1214 dwResult = pHMHandle->pDeviceHandler->_DeviceRequest(&pHMHandle->hmHandleData,
1215 ulRequestCode,
1216 arg1,
1217 arg2,
1218 arg3,
1219 arg4);
1220
1221 return (dwResult); /* deliver return code */
1222}
1223
1224
1225/*****************************************************************************
1226 * Name : HMDeviceHandler::GetFileInformationByHandle
1227 * Purpose : router function for GetFileInformationByHandle
1228 * Parameters:
1229 * Variables :
1230 * Result :
1231 * Remark :
1232 * Status :
1233 *
1234 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1235 *****************************************************************************/
1236
1237BOOL HMGetFileInformationByHandle (HANDLE hFile,
1238 BY_HANDLE_FILE_INFORMATION *pHFI)
1239{
1240 int iIndex; /* index into the handle table */
1241 DWORD dwResult; /* result from the device handler's API */
1242 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1243
1244 /* validate handle */
1245 iIndex = _HMHandleQuery(hFile); /* get the index */
1246 if (-1 == iIndex) /* error ? */
1247 {
1248 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1249 return FALSE; /* signal failure */
1250 }
1251
1252 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1253 dwResult = pHMHandle->pDeviceHandler->GetFileInformationByHandle(&pHMHandle->hmHandleData,
1254 pHFI);
1255
1256 return (dwResult); /* deliver return code */
1257}
1258
1259
1260/*****************************************************************************
1261 * Name : HMDeviceHandler::SetEndOfFile
1262 * Purpose : router function for SetEndOfFile
1263 * Parameters:
1264 * Variables :
1265 * Result :
1266 * Remark :
1267 * Status :
1268 *
1269 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1270 *****************************************************************************/
1271
1272BOOL HMSetEndOfFile (HANDLE hFile)
1273{
1274 int iIndex; /* index into the handle table */
1275 BOOL bResult; /* result from the device handler's API */
1276 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1277
1278 /* validate handle */
1279 iIndex = _HMHandleQuery(hFile); /* get the index */
1280 if (-1 == iIndex) /* error ? */
1281 {
1282 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1283 return FALSE; /* signal failure */
1284 }
1285
1286 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1287 bResult = pHMHandle->pDeviceHandler->SetEndOfFile(&pHMHandle->hmHandleData);
1288
1289 return (bResult); /* deliver return code */
1290}
1291
1292
1293/*****************************************************************************
1294 * Name : HMDeviceHandler::SetFileTime
1295 * Purpose : router function for SetFileTime
1296 * Parameters:
1297 * Variables :
1298 * Result :
1299 * Remark :
1300 * Status :
1301 *
1302 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1303 *****************************************************************************/
1304
1305BOOL HMSetFileTime (HANDLE hFile,
1306 const FILETIME *pFT1,
1307 const FILETIME *pFT2,
1308 const FILETIME *pFT3)
1309{
1310 int iIndex; /* index into the handle table */
1311 BOOL bResult; /* result from the device handler's API */
1312 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1313
1314 /* validate handle */
1315 iIndex = _HMHandleQuery(hFile); /* get the index */
1316 if (-1 == iIndex) /* error ? */
1317 {
1318 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1319 return FALSE; /* signal failure */
1320 }
1321
1322 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1323 bResult = pHMHandle->pDeviceHandler->SetFileTime(&pHMHandle->hmHandleData,
1324 (LPFILETIME)pFT1,
1325 (LPFILETIME)pFT2,
1326 (LPFILETIME)pFT3);
1327
1328 return (bResult); /* deliver return code */
1329}
1330
1331
1332/*****************************************************************************
1333 * Name : HMDeviceHandler::GetFileSize
1334 * Purpose : router function for GetFileSize
1335 * Parameters:
1336 * Variables :
1337 * Result :
1338 * Remark :
1339 * Status :
1340 *
1341 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1342 *****************************************************************************/
1343
1344DWORD HMGetFileSize (HANDLE hFile,
1345 PDWORD pSize)
1346{
1347 int iIndex; /* index into the handle table */
1348 DWORD dwResult; /* result from the device handler's API */
1349 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1350
1351 /* validate handle */
1352 iIndex = _HMHandleQuery(hFile); /* get the index */
1353 if (-1 == iIndex) /* error ? */
1354 {
1355 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1356 return (INVALID_HANDLE_ERROR); /* signal failure */
1357 }
1358
1359 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1360 dwResult = pHMHandle->pDeviceHandler->GetFileSize(&pHMHandle->hmHandleData,
1361 pSize);
1362
1363 return (dwResult); /* deliver return code */
1364}
1365
1366
1367/*****************************************************************************
1368 * Name : HMDeviceHandler::SetFilePointer
1369 * Purpose : router function for SetFilePointer
1370 * Parameters:
1371 * Variables :
1372 * Result :
1373 * Remark :
1374 * Status :
1375 *
1376 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1377 *****************************************************************************/
1378
1379DWORD HMSetFilePointer (HANDLE hFile,
1380 LONG lDistanceToMove,
1381 PLONG lpDistanceToMoveHigh,
1382 DWORD dwMoveMethod)
1383{
1384 int iIndex; /* index into the handle table */
1385 DWORD dwResult; /* result from the device handler's API */
1386 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1387
1388 /* validate handle */
1389 iIndex = _HMHandleQuery(hFile); /* get the index */
1390 if (-1 == iIndex) /* error ? */
1391 {
1392 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1393 return (INVALID_HANDLE_ERROR); /* signal failure */
1394 }
1395
1396 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1397 dwResult = pHMHandle->pDeviceHandler->SetFilePointer(&pHMHandle->hmHandleData,
1398 lDistanceToMove,
1399 lpDistanceToMoveHigh,
1400 dwMoveMethod);
1401
1402 return (dwResult); /* deliver return code */
1403}
1404
1405
1406/*****************************************************************************
1407 * Name : HMDeviceHandler::LockFile
1408 * Purpose : router function for LockFile
1409 * Parameters:
1410 * Variables :
1411 * Result :
1412 * Remark :
1413 * Status :
1414 *
1415 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1416 *****************************************************************************/
1417
1418BOOL HMLockFile (HFILE hFile,
1419 DWORD arg2,
1420 DWORD arg3,
1421 DWORD arg4,
1422 DWORD arg5)
1423{
1424 int iIndex; /* index into the handle table */
1425 DWORD dwResult; /* result from the device handler's API */
1426 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1427
1428 /* validate handle */
1429 iIndex = _HMHandleQuery(hFile); /* get the index */
1430 if (-1 == iIndex) /* error ? */
1431 {
1432 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1433 return FALSE; /* signal failure */
1434 }
1435
1436 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1437 dwResult = pHMHandle->pDeviceHandler->LockFile(&pHMHandle->hmHandleData,
1438 arg2,
1439 arg3,
1440 arg4,
1441 arg5);
1442
1443 return (dwResult); /* deliver return code */
1444}
1445
1446
1447/*****************************************************************************
1448 * Name : HMDeviceHandler::LockFileEx
1449 * Purpose : router function for LockFileEx
1450 * Parameters:
1451 * Variables :
1452 * Result :
1453 * Remark :
1454 * Status :
1455 *
1456 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1457 *****************************************************************************/
1458
1459BOOL HMLockFileEx(HANDLE hFile,
1460 DWORD dwFlags,
1461 DWORD dwReserved,
1462 DWORD nNumberOfBytesToLockLow,
1463 DWORD nNumberOfBytesToLockHigh,
1464 LPOVERLAPPED lpOverlapped)
1465{
1466 int iIndex; /* index into the handle table */
1467 DWORD dwResult; /* result from the device handler's API */
1468 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1469
1470 /* validate handle */
1471 iIndex = _HMHandleQuery(hFile); /* get the index */
1472 if (-1 == iIndex) /* error ? */
1473 {
1474 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1475 return FALSE; /* signal failure */
1476 }
1477
1478 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1479 dwResult = pHMHandle->pDeviceHandler->LockFileEx(&pHMHandle->hmHandleData,
1480 dwFlags,
1481 dwReserved,
1482 nNumberOfBytesToLockLow,
1483 nNumberOfBytesToLockHigh,
1484 lpOverlapped);
1485
1486 return (dwResult); /* deliver return code */
1487}
1488
1489
1490
1491/*****************************************************************************
1492 * Name : HMDeviceHandler::UnlockFile
1493 * Purpose : router function for UnlockFile
1494 * Parameters:
1495 * Variables :
1496 * Result :
1497 * Remark :
1498 * Status :
1499 *
1500 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1501 *****************************************************************************/
1502
1503BOOL HMUnlockFile (HFILE hFile,
1504 DWORD arg2,
1505 DWORD arg3,
1506 DWORD arg4,
1507 DWORD arg5)
1508{
1509 int iIndex; /* index into the handle table */
1510 DWORD dwResult; /* result from the device handler's API */
1511 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1512
1513 /* validate handle */
1514 iIndex = _HMHandleQuery(hFile); /* get the index */
1515 if (-1 == iIndex) /* error ? */
1516 {
1517 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1518 return FALSE; /* signal failure */
1519 }
1520
1521 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1522 dwResult = pHMHandle->pDeviceHandler->UnlockFile(&pHMHandle->hmHandleData,
1523 arg2,
1524 arg3,
1525 arg4,
1526 arg5);
1527
1528 return (dwResult); /* deliver return code */
1529}
1530
1531
1532/*****************************************************************************
1533 * Name : HMDeviceHandler::UnlockFileEx
1534 * Purpose : router function for UnlockFileEx
1535 * Parameters:
1536 * Variables :
1537 * Result :
1538 * Remark :
1539 * Status :
1540 *
1541 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1542 *****************************************************************************/
1543
1544BOOL HMUnlockFileEx(HANDLE hFile,
1545 DWORD dwFlags,
1546 DWORD dwReserved,
1547 DWORD nNumberOfBytesToLockLow,
1548 DWORD nNumberOfBytesToLockHigh,
1549 LPOVERLAPPED lpOverlapped)
1550{
1551 int iIndex; /* index into the handle table */
1552 DWORD dwResult; /* result from the device handler's API */
1553 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1554
1555 /* validate handle */
1556 iIndex = _HMHandleQuery(hFile); /* get the index */
1557 if (-1 == iIndex) /* error ? */
1558 {
1559 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1560 return FALSE; /* signal failure */
1561 }
1562
1563 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1564 dwResult = pHMHandle->pDeviceHandler->UnlockFileEx(&pHMHandle->hmHandleData,
1565 dwFlags,
1566 dwReserved,
1567 nNumberOfBytesToLockLow,
1568 nNumberOfBytesToLockHigh,
1569 lpOverlapped);
1570
1571 return (dwResult); /* deliver return code */
1572}
1573
1574
1575/*****************************************************************************
1576 * Name : HMDeviceHandler::WaitForSingleObject
1577 * Purpose : router function for WaitForSingleObject
1578 * Parameters:
1579 * Variables :
1580 * Result :
1581 * Remark :
1582 * Status :
1583 *
1584 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1585 *****************************************************************************/
1586
1587DWORD HMWaitForSingleObject(HANDLE hObject,
1588 DWORD dwTimeout)
1589{
1590 int iIndex; /* index into the handle table */
1591 DWORD dwResult; /* result from the device handler's API */
1592 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1593
1594 /* validate handle */
1595 iIndex = _HMHandleQuery(hObject); /* get the index */
1596 if (-1 == iIndex) /* error ? */
1597 {
1598 dprintf(("KERNEL32: HandleManager:HMWaitForSingleObject(%08xh) passed on to Open32.\n",
1599 hObject));
1600
1601 // maybe handles from CreateProcess() ...
1602 dwResult = O32_WaitForSingleObject(hObject, dwTimeout);
1603 return (dwResult);
1604 }
1605
1606 // @@@PH Problem: wrong class (base class) is called instead of
1607 // open32 class ?! Why ?!
1608 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1609 dwResult = pHMHandle->pDeviceHandler->WaitForSingleObject(&pHMHandle->hmHandleData,
1610 dwTimeout);
1611
1612 return (dwResult); /* deliver return code */
1613}
1614
1615
1616/*****************************************************************************
1617 * Name : HMDeviceHandler::WaitForSingleObjectEx
1618 * Purpose : router function for WaitForSingleObjectEx
1619 * Parameters:
1620 * Variables :
1621 * Result :
1622 * Remark :
1623 * Status :
1624 *
1625 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1626 *****************************************************************************/
1627
1628DWORD HMWaitForSingleObjectEx(HANDLE hObject,
1629 DWORD dwTimeout,
1630 BOOL fAlertable)
1631{
1632 int iIndex; /* index into the handle table */
1633 DWORD dwResult; /* result from the device handler's API */
1634 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1635
1636 /* validate handle */
1637 iIndex = _HMHandleQuery(hObject); /* get the index */
1638 if (-1 == iIndex) /* error ? */
1639 {
1640 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1641 return WAIT_FAILED; /* signal failure */
1642 }
1643
1644 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1645 dwResult = pHMHandle->pDeviceHandler->WaitForSingleObjectEx(&pHMHandle->hmHandleData,
1646 dwTimeout,
1647 fAlertable);
1648
1649 return (dwResult); /* deliver return code */
1650}
1651
1652
1653/*****************************************************************************
1654 * Name : HMDeviceHandler::FlushFileBuffers
1655 * Purpose : router function for FlushFileBuffers
1656 * Parameters:
1657 * Variables :
1658 * Result :
1659 * Remark :
1660 * Status :
1661 *
1662 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1663 *****************************************************************************/
1664
1665BOOL HMFlushFileBuffers(HANDLE hFile)
1666{
1667 int iIndex; /* index into the handle table */
1668 DWORD dwResult; /* result from the device handler's API */
1669 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1670
1671 /* validate handle */
1672 iIndex = _HMHandleQuery(hFile); /* get the index */
1673 if (-1 == iIndex) /* error ? */
1674 {
1675 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1676 return FALSE; /* signal failure */
1677 }
1678
1679 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1680 dwResult = pHMHandle->pDeviceHandler->FlushFileBuffers(&pHMHandle->hmHandleData);
1681
1682 return (dwResult); /* deliver return code */
1683}
1684
1685
1686/*****************************************************************************
1687 * Name : HMDeviceHandler::GetOverlappedResult
1688 * Purpose : router function for GetOverlappedResult
1689 * Parameters:
1690 * Variables :
1691 * Result :
1692 * Remark :
1693 * Status :
1694 *
1695 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1696 *****************************************************************************/
1697
1698BOOL HMGetOverlappedResult(HANDLE hObject,
1699 LPOVERLAPPED lpOverlapped,
1700 LPDWORD arg3,
1701 BOOL arg4)
1702{
1703 int iIndex; /* index into the handle table */
1704 DWORD dwResult; /* result from the device handler's API */
1705 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1706
1707 /* validate handle */
1708 iIndex = _HMHandleQuery(hObject); /* get the index */
1709 if (-1 == iIndex) /* error ? */
1710 {
1711 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1712 return FALSE; /* signal failure */
1713 }
1714
1715 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1716 dwResult = pHMHandle->pDeviceHandler->GetOverlappedResult(&pHMHandle->hmHandleData,
1717 lpOverlapped,
1718 arg3,
1719 arg4);
1720
1721 return (dwResult); /* deliver return code */
1722}
1723
1724
1725/*****************************************************************************
1726 * Name : HMReleaseMutex
1727 * Purpose : router function for ReleaseMutex
1728 * Parameters:
1729 * Variables :
1730 * Result :
1731 * Remark :
1732 * Status :
1733 *
1734 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1735 *****************************************************************************/
1736
1737BOOL HMReleaseMutex(HANDLE hObject)
1738{
1739 int iIndex; /* index into the handle table */
1740 DWORD dwResult; /* result from the device handler's API */
1741 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1742
1743 /* validate handle */
1744 iIndex = _HMHandleQuery(hObject); /* get the index */
1745 if (-1 == iIndex) /* error ? */
1746 {
1747 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1748 return FALSE; /* signal failure */
1749 }
1750
1751 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1752 dwResult = pHMHandle->pDeviceHandler->ReleaseMutex(&pHMHandle->hmHandleData);
1753
1754 return (dwResult); /* deliver return code */
1755}
1756
1757
1758/*****************************************************************************
1759 * Name : HMSetEvent
1760 * Purpose : router function for SetEvent
1761 * Parameters:
1762 * Variables :
1763 * Result :
1764 * Remark :
1765 * Status :
1766 *
1767 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1768 *****************************************************************************/
1769
1770BOOL HMSetEvent(HANDLE hEvent)
1771{
1772 int iIndex; /* index into the handle table */
1773 DWORD dwResult; /* result from the device handler's API */
1774 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1775
1776 /* validate handle */
1777 iIndex = _HMHandleQuery(hEvent); /* get the index */
1778 if (-1 == iIndex) /* error ? */
1779 {
1780 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1781 return FALSE; /* signal failure */
1782 }
1783
1784 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1785 dwResult = pHMHandle->pDeviceHandler->SetEvent(&pHMHandle->hmHandleData);
1786
1787 return (dwResult); /* deliver return code */
1788}
1789
1790
1791/*****************************************************************************
1792 * Name : HMPulseEvent
1793 * Purpose : router function for PulseEvent
1794 * Parameters:
1795 * Variables :
1796 * Result :
1797 * Remark :
1798 * Status :
1799 *
1800 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1801 *****************************************************************************/
1802
1803BOOL HMPulseEvent(HANDLE hEvent)
1804{
1805 int iIndex; /* index into the handle table */
1806 DWORD dwResult; /* result from the device handler's API */
1807 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1808
1809 /* validate handle */
1810 iIndex = _HMHandleQuery(hEvent); /* get the index */
1811 if (-1 == iIndex) /* error ? */
1812 {
1813 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1814 return FALSE; /* signal failure */
1815 }
1816
1817 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1818 dwResult = pHMHandle->pDeviceHandler->PulseEvent(&pHMHandle->hmHandleData);
1819
1820 return (dwResult); /* deliver return code */
1821}
1822
1823
1824/*****************************************************************************
1825 * Name : HMResetEvent
1826 * Purpose : router function for ResetEvent
1827 * Parameters:
1828 * Variables :
1829 * Result :
1830 * Remark :
1831 * Status :
1832 *
1833 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1834 *****************************************************************************/
1835
1836BOOL HMResetEvent(HANDLE hEvent)
1837{
1838 int iIndex; /* index into the handle table */
1839 DWORD dwResult; /* result from the device handler's API */
1840 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1841
1842 /* validate handle */
1843 iIndex = _HMHandleQuery(hEvent); /* get the index */
1844 if (-1 == iIndex) /* error ? */
1845 {
1846 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1847 return FALSE; /* signal failure */
1848 }
1849
1850 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1851 dwResult = pHMHandle->pDeviceHandler->ResetEvent(&pHMHandle->hmHandleData);
1852
1853 return (dwResult); /* deliver return code */
1854}
1855
1856
1857/*****************************************************************************
1858 * Name : HANDLE HMCreateEvent
1859 * Purpose : Wrapper for the CreateEvent() API
1860 * Parameters:
1861 * Variables :
1862 * Result :
1863 * Remark :
1864 * Status :
1865 *
1866 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
1867 *****************************************************************************/
1868
1869HANDLE HMCreateEvent(LPSECURITY_ATTRIBUTES lpsa,
1870 BOOL bManualReset,
1871 BOOL bInitialState,
1872 LPCTSTR lpName)
1873{
1874 int iIndex; /* index into the handle table */
1875 int iIndexNew; /* index into the handle table */
1876 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
1877 PHMHANDLEDATA pHMHandleData;
1878 DWORD rc; /* API return code */
1879
1880
1881 pDeviceHandler = HMGlobals.pHMEvent; /* device is predefined */
1882
1883 iIndexNew = _HMHandleGetFree(); /* get free handle */
1884 if (-1 == iIndexNew) /* oops, no free handles ! */
1885 {
1886 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
1887 return 0; /* signal error */
1888 }
1889
1890
1891 /* initialize the complete HMHANDLEDATA structure */
1892 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
1893 pHMHandleData->dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
1894 pHMHandleData->dwAccess = 0;
1895 pHMHandleData->dwShare = 0;
1896 pHMHandleData->dwCreation = 0;
1897 pHMHandleData->dwFlags = 0;
1898 pHMHandleData->lpHandlerData = NULL;
1899
1900
1901 /* we've got to mark the handle as occupied here, since another device */
1902 /* could be created within the device handler -> deadlock */
1903
1904 /* write appropriate entry into the handle table if open succeeded */
1905 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
1906 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
1907
1908 /* call the device handler */
1909 rc = pDeviceHandler->CreateEvent(&TabWin32Handles[iIndexNew].hmHandleData,
1910 lpsa,
1911 bManualReset,
1912 bInitialState,
1913 lpName);
1914 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
1915 {
1916 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
1917 SetLastError(rc); /* Hehe, OS/2 and NT are pretty compatible :) */
1918 return 0; /* signal error */
1919 }
1920 else
1921 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
1922
1923 return iIndexNew; /* return valid handle */
1924}
1925
1926
1927/*****************************************************************************
1928 * Name : HANDLE HMCreateMutex
1929 * Purpose : Wrapper for the CreateMutex() API
1930 * Parameters:
1931 * Variables :
1932 * Result :
1933 * Remark :
1934 * Status :
1935 *
1936 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
1937 *****************************************************************************/
1938
1939HANDLE HMCreateMutex(LPSECURITY_ATTRIBUTES lpsa,
1940 BOOL bInitialOwner,
1941 LPCTSTR lpName)
1942{
1943 int iIndex; /* index into the handle table */
1944 int iIndexNew; /* index into the handle table */
1945 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
1946 PHMHANDLEDATA pHMHandleData;
1947 DWORD rc; /* API return code */
1948
1949
1950 pDeviceHandler = HMGlobals.pHMMutex; /* device is predefined */
1951
1952 iIndexNew = _HMHandleGetFree(); /* get free handle */
1953 if (-1 == iIndexNew) /* oops, no free handles ! */
1954 {
1955 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
1956 return 0; /* signal error */
1957 }
1958
1959
1960 /* initialize the complete HMHANDLEDATA structure */
1961 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
1962 pHMHandleData->dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
1963 pHMHandleData->dwAccess = 0;
1964 pHMHandleData->dwShare = 0;
1965 pHMHandleData->dwCreation = 0;
1966 pHMHandleData->dwFlags = 0;
1967 pHMHandleData->lpHandlerData = NULL;
1968
1969
1970 /* we've got to mark the handle as occupied here, since another device */
1971 /* could be created within the device handler -> deadlock */
1972
1973 /* write appropriate entry into the handle table if open succeeded */
1974 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
1975 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
1976
1977 /* call the device handler */
1978 rc = pDeviceHandler->CreateMutex(&TabWin32Handles[iIndexNew].hmHandleData,
1979 lpsa,
1980 bInitialOwner,
1981 lpName);
1982 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
1983 {
1984 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
1985 SetLastError(rc); /* Hehe, OS/2 and NT are pretty compatible :) */
1986 return 0; /* signal error */
1987 }
1988 else
1989 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
1990
1991 return iIndexNew; /* return valid handle */
1992}
1993
1994
1995/*****************************************************************************
1996 * Name : HANDLE HMOpenEvent
1997 * Purpose : Wrapper for the OpenEvent() API
1998 * Parameters:
1999 * Variables :
2000 * Result :
2001 * Remark :
2002 * Status :
2003 *
2004 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
2005 *****************************************************************************/
2006
2007HANDLE HMOpenEvent(DWORD fdwAccess,
2008 BOOL fInherit,
2009 LPCTSTR lpName)
2010{
2011 int iIndex; /* index into the handle table */
2012 int iIndexNew; /* index into the handle table */
2013 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
2014 PHMHANDLEDATA pHMHandleData;
2015 DWORD rc; /* API return code */
2016
2017
2018 pDeviceHandler = HMGlobals.pHMEvent; /* device is predefined */
2019
2020 iIndexNew = _HMHandleGetFree(); /* get free handle */
2021 if (-1 == iIndexNew) /* oops, no free handles ! */
2022 {
2023 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
2024 return 0; /* signal error */
2025 }
2026
2027
2028 /* initialize the complete HMHANDLEDATA structure */
2029 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
2030 pHMHandleData->dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
2031 pHMHandleData->dwAccess = fdwAccess;
2032 pHMHandleData->dwShare = 0;
2033 pHMHandleData->dwCreation = 0;
2034 pHMHandleData->dwFlags = 0;
2035 pHMHandleData->lpHandlerData = NULL;
2036
2037
2038 /* we've got to mark the handle as occupied here, since another device */
2039 /* could be created within the device handler -> deadlock */
2040
2041 /* write appropriate entry into the handle table if open succeeded */
2042 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
2043 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
2044
2045 /* call the device handler */
2046 rc = pDeviceHandler->OpenEvent(&TabWin32Handles[iIndexNew].hmHandleData,
2047 fInherit,
2048 lpName);
2049 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
2050 {
2051 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
2052 SetLastError(rc); /* Hehe, OS/2 and NT are pretty compatible :) */
2053 return 0; /* signal error */
2054 }
2055 else
2056 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
2057
2058 return iIndexNew; /* return valid handle */
2059}
2060
2061
2062/*****************************************************************************
2063 * Name : HANDLE HMOpenMutex
2064 * Purpose : Wrapper for the OpenMutex() API
2065 * Parameters:
2066 * Variables :
2067 * Result :
2068 * Remark :
2069 * Status :
2070 *
2071 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
2072 *****************************************************************************/
2073
2074HANDLE HMOpenMutex(DWORD fdwAccess,
2075 BOOL fInherit,
2076 LPCTSTR lpName)
2077{
2078 int iIndex; /* index into the handle table */
2079 int iIndexNew; /* index into the handle table */
2080 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
2081 PHMHANDLEDATA pHMHandleData;
2082 DWORD rc; /* API return code */
2083
2084
2085 pDeviceHandler = HMGlobals.pHMMutex; /* device is predefined */
2086
2087 iIndexNew = _HMHandleGetFree(); /* get free handle */
2088 if (-1 == iIndexNew) /* oops, no free handles ! */
2089 {
2090 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
2091 return 0; /* signal error */
2092 }
2093
2094
2095 /* initialize the complete HMHANDLEDATA structure */
2096 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
2097 pHMHandleData->dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
2098 pHMHandleData->dwAccess = fdwAccess;
2099 pHMHandleData->dwShare = 0;
2100 pHMHandleData->dwCreation = 0;
2101 pHMHandleData->dwFlags = 0;
2102 pHMHandleData->lpHandlerData = NULL;
2103
2104
2105 /* we've got to mark the handle as occupied here, since another device */
2106 /* could be created within the device handler -> deadlock */
2107
2108 /* write appropriate entry into the handle table if open succeeded */
2109 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
2110 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
2111
2112 /* call the device handler */
2113 rc = pDeviceHandler->OpenMutex(&TabWin32Handles[iIndexNew].hmHandleData,
2114 fInherit,
2115 lpName);
2116 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
2117 {
2118 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
2119 SetLastError(rc); /* Hehe, OS/2 and NT are pretty compatible :) */
2120 return 0; /* signal error */
2121 }
2122 else
2123 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
2124
2125 return iIndexNew; /* return valid handle */
2126}
2127
2128
2129/*****************************************************************************
2130 * Name : HANDLE HMCreateSemaphore
2131 * Purpose : Wrapper for the CreateSemaphore() API
2132 * Parameters:
2133 * Variables :
2134 * Result :
2135 * Remark :
2136 * Status :
2137 *
2138 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
2139 *****************************************************************************/
2140
2141HANDLE HMCreateSemaphore(LPSECURITY_ATTRIBUTES lpsa,
2142 LONG lInitialCount,
2143 LONG lMaximumCount,
2144 LPCTSTR lpName)
2145{
2146 int iIndex; /* index into the handle table */
2147 int iIndexNew; /* index into the handle table */
2148 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
2149 PHMHANDLEDATA pHMHandleData;
2150 DWORD rc; /* API return code */
2151
2152
2153 pDeviceHandler = HMGlobals.pHMSemaphore; /* device is predefined */
2154
2155 iIndexNew = _HMHandleGetFree(); /* get free handle */
2156 if (-1 == iIndexNew) /* oops, no free handles ! */
2157 {
2158 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
2159 return 0; /* signal error */
2160 }
2161
2162
2163 /* initialize the complete HMHANDLEDATA structure */
2164 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
2165 pHMHandleData->dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
2166 pHMHandleData->dwAccess = 0;
2167 pHMHandleData->dwShare = 0;
2168 pHMHandleData->dwCreation = 0;
2169 pHMHandleData->dwFlags = 0;
2170 pHMHandleData->lpHandlerData = NULL;
2171
2172
2173 /* we've got to mark the handle as occupied here, since another device */
2174 /* could be created within the device handler -> deadlock */
2175
2176 /* write appropriate entry into the handle table if open succeeded */
2177 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
2178 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
2179
2180 /* call the device handler */
2181 rc = pDeviceHandler->CreateSemaphore(&TabWin32Handles[iIndexNew].hmHandleData,
2182 lpsa,
2183 lInitialCount,
2184 lMaximumCount,
2185 lpName);
2186 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
2187 {
2188 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
2189 SetLastError(rc); /* Hehe, OS/2 and NT are pretty compatible :) */
2190 return 0; /* signal failure */
2191 }
2192 else
2193 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
2194
2195 return iIndexNew; /* return valid handle */
2196}
2197
2198
2199/*****************************************************************************
2200 * Name : HANDLE HMOpenSemaphore
2201 * Purpose : Wrapper for the OpenSemaphore() API
2202 * Parameters:
2203 * Variables :
2204 * Result :
2205 * Remark :
2206 * Status :
2207 *
2208 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
2209 *****************************************************************************/
2210
2211HANDLE HMOpenSemaphore(DWORD fdwAccess,
2212 BOOL fInherit,
2213 LPCTSTR lpName)
2214{
2215 int iIndex; /* index into the handle table */
2216 int iIndexNew; /* index into the handle table */
2217 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
2218 PHMHANDLEDATA pHMHandleData;
2219 DWORD rc; /* API return code */
2220
2221
2222 pDeviceHandler = HMGlobals.pHMSemaphore; /* device is predefined */
2223
2224 iIndexNew = _HMHandleGetFree(); /* get free handle */
2225 if (-1 == iIndexNew) /* oops, no free handles ! */
2226 {
2227 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
2228 return 0; /* signal error */
2229 }
2230
2231
2232 /* initialize the complete HMHANDLEDATA structure */
2233 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
2234 pHMHandleData->dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
2235 pHMHandleData->dwAccess = fdwAccess;
2236 pHMHandleData->dwShare = 0;
2237 pHMHandleData->dwCreation = 0;
2238 pHMHandleData->dwFlags = 0;
2239 pHMHandleData->lpHandlerData = NULL;
2240
2241
2242 /* we've got to mark the handle as occupied here, since another device */
2243 /* could be created within the device handler -> deadlock */
2244
2245 /* write appropriate entry into the handle table if open succeeded */
2246 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
2247 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
2248
2249 /* call the device handler */
2250 rc = pDeviceHandler->OpenSemaphore(&TabWin32Handles[iIndexNew].hmHandleData,
2251 fInherit,
2252 lpName);
2253 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
2254 {
2255 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
2256 SetLastError(rc); /* Hehe, OS/2 and NT are pretty compatible :) */
2257 return 0; /* signal failure */
2258 }
2259 else
2260 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
2261
2262 return iIndexNew; /* return valid handle */
2263}
2264
2265
2266/*****************************************************************************
2267 * Name : HMReleaseSemaphore
2268 * Purpose : router function for ReleaseSemaphore
2269 * Parameters:
2270 * Variables :
2271 * Result :
2272 * Remark :
2273 * Status :
2274 *
2275 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
2276 *****************************************************************************/
2277
2278BOOL HMReleaseSemaphore(HANDLE hEvent,
2279 LONG cReleaseCount,
2280 LPLONG lpPreviousCount)
2281{
2282 int iIndex; /* index into the handle table */
2283 DWORD dwResult; /* result from the device handler's API */
2284 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
2285
2286 /* validate handle */
2287 iIndex = _HMHandleQuery(hEvent); /* get the index */
2288 if (-1 == iIndex) /* error ? */
2289 {
2290 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
2291 return 0; /* signal failure */
2292 }
2293 else
2294 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
2295
2296 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
2297 dwResult = pHMHandle->pDeviceHandler->ReleaseSemaphore(&pHMHandle->hmHandleData,
2298 cReleaseCount,
2299 lpPreviousCount);
2300
2301 return (dwResult); /* deliver return code */
2302}
2303
2304
2305/*****************************************************************************
2306 * Name : HANDLE HMCreateFileMapping
2307 * Purpose : Wrapper for the CreateFileMapping() API
2308 * Parameters:
2309 * Variables :
2310 * Result :
2311 * Remark :
2312 * Status :
2313 *
2314 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
2315 *****************************************************************************/
2316
2317HANDLE HMCreateFileMapping(HANDLE hFile,
2318 LPSECURITY_ATTRIBUTES lpFileMappingAttributes,
2319 DWORD flProtect,
2320 DWORD dwMaximumSizeHigh,
2321 DWORD dwMaximumSizeLow,
2322 LPCTSTR lpName)
2323{
2324 int iIndex; /* index into the handle table */
2325 int iIndexNew; /* index into the handle table */
2326 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
2327 PHMHANDLEDATA pHMHandleData;
2328 DWORD rc; /* API return code */
2329 HANDLE hOldMemMap = -1;
2330
2331 pDeviceHandler = HMGlobals.pHMFileMapping; /* device is predefined */
2332
2333 iIndexNew = _HMHandleGetFree(); /* get free handle */
2334 if (-1 == iIndexNew) /* oops, no free handles ! */
2335 {
2336 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
2337 return 0; /* signal error */
2338 }
2339
2340
2341 /* initialize the complete HMHANDLEDATA structure */
2342 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
2343 pHMHandleData->dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
2344 pHMHandleData->dwAccess = 0;
2345 pHMHandleData->dwShare = 0;
2346 pHMHandleData->dwCreation = 0;
2347 pHMHandleData->dwFlags = 0;
2348 pHMHandleData->lpHandlerData = NULL;
2349
2350
2351 /* we've got to mark the handle as occupied here, since another device */
2352 /* could be created within the device handler -> deadlock */
2353
2354 /* write appropriate entry into the handle table if open succeeded */
2355 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
2356 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
2357
2358 /* call the device handler */
2359
2360 // @@@PH: Note: hFile is a Win32-style handle, it's not (yet) converted to
2361 // a valid HandleManager-internal handle!
2362 rc = pDeviceHandler->CreateFileMapping(&TabWin32Handles[iIndexNew].hmHandleData,
2363 hFile,
2364 lpFileMappingAttributes,
2365 flProtect,
2366 dwMaximumSizeHigh,
2367 dwMaximumSizeLow,
2368 lpName, &hOldMemMap);
2369
2370 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
2371 {
2372 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
2373 SetLastError(rc); /* Hehe, OS/2 and NT are pretty compatible :) */
2374 if(rc == ERROR_ALREADY_EXISTS) {
2375 return hOldMemMap; //return handle of existing file mapping
2376 }
2377 else return (NULL); /* signal error */
2378 }
2379 else
2380 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
2381
2382 return iIndexNew; /* return valid handle */
2383}
2384
2385
2386/*****************************************************************************
2387 * Name : HANDLE HMOpenFileMapping
2388 * Purpose : Wrapper for the OpenFileMapping() API
2389 * Parameters:
2390 * Variables :
2391 * Result : HANDLE if succeeded,
2392 * NULL if failed.
2393 * Remark :
2394 * Status :
2395 *
2396 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
2397 *****************************************************************************/
2398
2399HANDLE HMOpenFileMapping(DWORD fdwAccess,
2400 BOOL fInherit,
2401 LPCTSTR lpName)
2402{
2403 int iIndex; /* index into the handle table */
2404 int iIndexNew; /* index into the handle table */
2405 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
2406 PHMHANDLEDATA pHMHandleData;
2407 DWORD rc; /* API return code */
2408
2409
2410 pDeviceHandler = HMGlobals.pHMFileMapping; /* device is predefined */
2411
2412 iIndexNew = _HMHandleGetFree(); /* get free handle */
2413 if (-1 == iIndexNew) /* oops, no free handles ! */
2414 {
2415 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
2416 return (NULL); /* signal error */
2417 }
2418
2419 /* initialize the complete HMHANDLEDATA structure */
2420 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
2421 pHMHandleData->dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
2422 pHMHandleData->dwAccess = fdwAccess;
2423 pHMHandleData->dwShare = 0;
2424 pHMHandleData->dwCreation = 0;
2425 pHMHandleData->dwFlags = 0;
2426 pHMHandleData->lpHandlerData = NULL;
2427
2428
2429 /* we've got to mark the handle as occupied here, since another device */
2430 /* could be created within the device handler -> deadlock */
2431
2432 /* write appropriate entry into the handle table if open succeeded */
2433 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
2434 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
2435
2436 /* call the device handler */
2437 rc = pDeviceHandler->OpenFileMapping(&TabWin32Handles[iIndexNew].hmHandleData,
2438 fdwAccess,
2439 fInherit,
2440 lpName);
2441 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
2442 {
2443 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
2444 SetLastError(rc); /* Hehe, OS/2 and NT are pretty compatible :) */
2445 return (NULL); /* signal error */
2446 }
2447 else
2448 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
2449
2450 return iIndexNew; /* return valid handle */
2451}
2452
2453
2454/*****************************************************************************
2455 * Name : HMMapViewOfFileEx
2456 * Purpose : router function for MapViewOfFileEx
2457 * Parameters:
2458 * Variables :
2459 * Result :
2460 * Remark :
2461 * Status :
2462 *
2463 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
2464 *****************************************************************************/
2465
2466LPVOID HMMapViewOfFileEx(HANDLE hFileMappingObject,
2467 DWORD dwDesiredAccess,
2468 DWORD dwFileOffsetHigh,
2469 DWORD dwFileOffsetLow,
2470 DWORD dwNumberOfBytesToMap,
2471 LPVOID lpBaseAddress)
2472{
2473 int iIndex; /* index into the handle table */
2474 LPVOID lpResult; /* result from the device handler's API */
2475 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
2476
2477 /* validate handle */
2478 iIndex = _HMHandleQuery(hFileMappingObject); /* get the index */
2479 if (-1 == iIndex) /* error ? */
2480 {
2481 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
2482 return (NULL); /* signal failure */
2483 }
2484
2485 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
2486 lpResult = pHMHandle->pDeviceHandler->MapViewOfFileEx(&pHMHandle->hmHandleData,
2487 dwDesiredAccess,
2488 dwFileOffsetHigh,
2489 dwFileOffsetLow,
2490 dwNumberOfBytesToMap,
2491 lpBaseAddress);
2492
2493 return (lpResult); /* deliver return code */
2494}
2495
2496/*****************************************************************************
2497 * Name : HMWaitForMultipleObjects
2498 * Purpose : router function for WaitForMultipleObjects
2499 * Parameters:
2500 * Variables :
2501 * Result :
2502 * Remark :
2503 * Status :
2504 *
2505 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
2506 *****************************************************************************/
2507
2508DWORD HMWaitForMultipleObjects (DWORD cObjects,
2509 PHANDLE lphObjects,
2510 BOOL fWaitAll,
2511 DWORD dwTimeout)
2512{
2513 ULONG ulIndex;
2514 PHANDLE pArrayOfHandles;
2515 PHANDLE pLoop1 = lphObjects;
2516 PHANDLE pLoop2;
2517 DWORD rc;
2518
2519 // allocate array for handle table
2520 pArrayOfHandles = (PHANDLE)alloca(cObjects * sizeof(HANDLE));
2521 if (pArrayOfHandles == NULL)
2522 {
2523 dprintf(("ERROR: HMWaitForMultipleObjects: alloca failed to allocate %d handles", cObjects));
2524 O32_SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2525 return WAIT_FAILED;
2526 }
2527 else pLoop2 = pArrayOfHandles;
2528
2529 // convert array to odin handles
2530 for (ulIndex = 0;
2531
2532 ulIndex < cObjects;
2533
2534 ulIndex++,
2535 pLoop1++,
2536 pLoop2++)
2537 {
2538 rc = HMHandleTranslateToOS2 (*pLoop1, // translate handle
2539 pLoop2);
2540
2541 if (rc != NO_ERROR)
2542 {
2543 O32_SetLastError(ERROR_INVALID_HANDLE);
2544 return (WAIT_FAILED);
2545 }
2546 }
2547
2548 // OK, now forward to Open32.
2549 // @@@PH: Note this will fail on handles that do NOT belong to Open32
2550 // but to i.e. the console subsystem!
2551 rc = O32_WaitForMultipleObjects(cObjects,
2552 pArrayOfHandles,
2553 fWaitAll,
2554 dwTimeout);
2555
2556 return (rc); // OK, done
2557}
2558
2559
2560/*****************************************************************************
2561 * Name : HMWaitForMultipleObjectsEx
2562 * Purpose : router function for WaitForMultipleObjectsEx
2563 * Parameters:
2564 * Variables :
2565 * Result :
2566 * Remark :
2567 * Status :
2568 *
2569 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
2570 *****************************************************************************/
2571
2572DWORD HMWaitForMultipleObjectsEx (DWORD cObjects,
2573 PHANDLE lphObjects,
2574 BOOL fWaitAll,
2575 DWORD dwTimeout,
2576 BOOL fAlertable)
2577{
2578 // @@@PH: Note: fAlertable is ignored !
2579 return (HMWaitForMultipleObjects(cObjects,
2580 lphObjects,
2581 fWaitAll,
2582 dwTimeout));
2583}
2584
2585/*****************************************************************************
2586 * Name : HMMsgWaitForMultipleObjects
2587 * Purpose : router function for MsgWaitForMultipleObjects
2588 * Parameters:
2589 * Variables :
2590 * Result :
2591 * Remark : Open32 doesn't implement this properly! (doesn't check dwWakeMask)
2592 * Status :
2593 *
2594 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
2595 *****************************************************************************/
2596
2597DWORD HMMsgWaitForMultipleObjects (DWORD nCount,
2598 LPHANDLE pHandles,
2599 BOOL fWaitAll,
2600 DWORD dwMilliseconds,
2601 DWORD dwWakeMask)
2602{
2603 ULONG ulIndex;
2604 PHANDLE pArrayOfHandles;
2605 PHANDLE pLoop1 = pHandles;
2606 PHANDLE pLoop2;
2607 DWORD rc;
2608
2609 // allocate array for handle table
2610 pArrayOfHandles = (PHANDLE)alloca(nCount * sizeof(HANDLE));
2611 if (pArrayOfHandles == NULL)
2612 {
2613 dprintf(("ERROR: HMMsgWaitForMultipleObjects: alloca failed to allocate %d handles", nCount));
2614 O32_SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2615 return WAIT_FAILED;
2616 }
2617 else
2618 pLoop2 = pArrayOfHandles;
2619
2620 // convert array to odin handles
2621 for (ulIndex = 0;
2622
2623 ulIndex < nCount;
2624
2625 ulIndex++,
2626 pLoop1++,
2627 pLoop2++)
2628 {
2629 rc = HMHandleTranslateToOS2 (*pLoop1, // translate handle
2630 pLoop2);
2631
2632 if (rc != NO_ERROR)
2633 {
2634 O32_SetLastError(ERROR_INVALID_HANDLE);
2635 return (WAIT_FAILED);
2636 }
2637 }
2638
2639 // OK, now forward to Open32.
2640 // @@@PH: Note this will fail on handles that do NOT belong to Open32
2641 // but to i.e. the console subsystem!
2642 rc = O32_MsgWaitForMultipleObjects(nCount,
2643 pArrayOfHandles,
2644 fWaitAll, dwMilliseconds,
2645 dwWakeMask);
2646
2647 return (rc); // OK, done
2648}
2649/*****************************************************************************
2650 * Name : HMDeviceIoControl
2651 * Purpose : router function for DeviceIoControl
2652 * Parameters:
2653 * Variables :
2654 * Result :
2655 * Remark :
2656 * Status :
2657 *
2658 * Author : Sander van Leeuwen
2659 *****************************************************************************/
2660
2661BOOL WIN32API DeviceIoControl(HANDLE hDevice, DWORD dwIoControlCode,
2662 LPVOID lpInBuffer, DWORD nInBufferSize,
2663 LPVOID lpOutBuffer, DWORD nOutBufferSize,
2664 LPDWORD lpBytesReturned, LPOVERLAPPED lpOverlapped)
2665{
2666 int iIndex; /* index into the handle table */
2667 BOOL fResult; /* result from the device handler's CloseHandle() */
2668 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
2669
2670 /* validate handle */
2671 iIndex = _HMHandleQuery(hDevice); /* get the index */
2672 if (-1 == iIndex) /* error ? */
2673 {
2674 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
2675 return (FALSE); /* signal failure */
2676 }
2677
2678 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
2679 fResult = pHMHandle->pDeviceHandler->DeviceIoControl(&pHMHandle->hmHandleData,
2680 dwIoControlCode,
2681 lpInBuffer, nInBufferSize,
2682 lpOutBuffer, nOutBufferSize,
2683 lpBytesReturned, lpOverlapped);
2684
2685 return (fResult); /* deliver return code */
2686}
2687
2688
2689/*****************************************************************************
2690 * Name : HMSetupComm
2691 * Purpose : router function for SetupComm
2692 * Parameters:
2693 * Variables :
2694 * Result :
2695 * Remark :
2696 * Status :
2697 *
2698 * Author : Achim Hasenmueller [Sat, 1999/11/27 13:13]
2699 *****************************************************************************/
2700
2701BOOL HMSetupComm(HANDLE hFile, DWORD dwInQueue, DWORD dwOutQueue)
2702{
2703 int iIndex; /* index into the handle table */
2704 BOOL bResult; /* result from the device handler's API */
2705 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
2706
2707 /* validate handle */
2708 iIndex = _HMHandleQuery(hFile); /* get the index */
2709 if (-1 == iIndex) /* error ? */
2710 {
2711 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
2712 return (NULL); /* signal failure */
2713 }
2714
2715 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
2716 bResult = pHMHandle->pDeviceHandler->SetupComm(&pHMHandle->hmHandleData,
2717 dwInQueue, dwOutQueue);
2718
2719 return (bResult); /* deliver return code */
2720}
2721
2722
2723/*****************************************************************************
2724 * Name : HMGetCommState
2725 * Purpose : router function for GetCommState
2726 * Parameters:
2727 * Variables :
2728 * Result :
2729 * Remark :
2730 * Status :
2731 *
2732 * Author : Achim Hasenmueller [Sat, 1999/11/27 13:40]
2733 *****************************************************************************/
2734
2735BOOL HMGetCommState(INT hCommDev, LPDCB lpdcb)
2736{
2737 int iIndex; /* index into the handle table */
2738 BOOL bResult; /* result from the device handler's API */
2739 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
2740
2741 /* validate handle */
2742 iIndex = _HMHandleQuery(hCommDev); /* get the index */
2743 if (-1 == iIndex) /* error ? */
2744 {
2745 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
2746 return (NULL); /* signal failure */
2747 }
2748
2749 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
2750 bResult = pHMHandle->pDeviceHandler->GetCommState(&pHMHandle->hmHandleData,
2751 lpdcb);
2752
2753 return (bResult); /* deliver return code */
2754}
2755
2756/*****************************************************************************
2757 * Name : HMOpenThreadToken
2758 * Purpose : router function for NtOpenThreadToken
2759 * Parameters:
2760 * Variables :
2761 * Result :
2762 * Remark :
2763 * Status :
2764 *
2765 * Author : SvL
2766 *****************************************************************************/
2767
2768DWORD HMOpenThreadToken(HANDLE ThreadHandle,
2769 DWORD DesiredAccess,
2770 BOOLEAN OpenAsSelf,
2771 DWORD dwUserData,
2772 HANDLE *TokenHandle)
2773{
2774 int iIndex; /* index into the handle table */
2775 int iIndexNew; /* index into the handle table */
2776 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
2777 PHMHANDLEDATA pHMHandleData;
2778 DWORD rc; /* API return code */
2779
2780 pDeviceHandler = HMGlobals.pHMToken; /* device is predefined */
2781
2782 iIndexNew = _HMHandleGetFree(); /* get free handle */
2783 if (-1 == iIndexNew) /* oops, no free handles ! */
2784 {
2785 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
2786 return ERROR_NOT_ENOUGH_MEMORY;
2787 }
2788
2789 /* initialize the complete HMHANDLEDATA structure */
2790 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
2791 pHMHandleData->dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
2792 pHMHandleData->dwAccess = DesiredAccess;
2793 pHMHandleData->dwShare = 0;
2794 pHMHandleData->dwCreation = 0;
2795 pHMHandleData->dwFlags = 0;
2796 pHMHandleData->lpHandlerData = NULL;
2797
2798
2799 /* we've got to mark the handle as occupied here, since another device */
2800 /* could be created within the device handler -> deadlock */
2801
2802 /* write appropriate entry into the handle table if open succeeded */
2803 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
2804 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
2805
2806 /* call the device handler */
2807
2808 // @@@PH: Note: hFile is a Win32-style handle, it's not (yet) converted to
2809 // a valid HandleManager-internal handle!
2810 rc = pDeviceHandler->OpenThreadToken(&TabWin32Handles[iIndexNew].hmHandleData,
2811 dwUserData,
2812 ThreadHandle,
2813 OpenAsSelf);
2814
2815 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
2816 {
2817 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
2818 return (rc); /* signal error */
2819 }
2820 else
2821 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
2822
2823 *TokenHandle = iIndexNew; /* return valid handle */
2824 return STATUS_SUCCESS;
2825}
2826
2827/*****************************************************************************
2828 * Name : HMOpenProcessToken
2829 * Purpose : router function for NtOpenProcessToken
2830 * Parameters:
2831 * Variables :
2832 * Result :
2833 * Remark :
2834 * Status :
2835 *
2836 * Author : SvL
2837 *****************************************************************************/
2838DWORD HMOpenProcessToken(HANDLE ProcessHandle,
2839 DWORD DesiredAccess,
2840 DWORD dwUserData,
2841 HANDLE *TokenHandle)
2842{
2843 int iIndex; /* index into the handle table */
2844 int iIndexNew; /* index into the handle table */
2845 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
2846 PHMHANDLEDATA pHMHandleData;
2847 DWORD rc; /* API return code */
2848
2849 pDeviceHandler = HMGlobals.pHMToken; /* device is predefined */
2850
2851 iIndexNew = _HMHandleGetFree(); /* get free handle */
2852 if (-1 == iIndexNew) /* oops, no free handles ! */
2853 {
2854 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
2855 return ERROR_NOT_ENOUGH_MEMORY;
2856 }
2857
2858 /* initialize the complete HMHANDLEDATA structure */
2859 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
2860 pHMHandleData->dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
2861 pHMHandleData->dwAccess = DesiredAccess;
2862 pHMHandleData->dwShare = 0;
2863 pHMHandleData->dwCreation = 0;
2864 pHMHandleData->dwFlags = 0;
2865 pHMHandleData->lpHandlerData = NULL;
2866
2867
2868 /* we've got to mark the handle as occupied here, since another device */
2869 /* could be created within the device handler -> deadlock */
2870
2871 /* write appropriate entry into the handle table if open succeeded */
2872 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
2873 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
2874
2875 /* call the device handler */
2876
2877 // @@@PH: Note: hFile is a Win32-style handle, it's not (yet) converted to
2878 // a valid HandleManager-internal handle!
2879 rc = pDeviceHandler->OpenProcessToken(&TabWin32Handles[iIndexNew].hmHandleData,
2880 dwUserData,
2881 ProcessHandle);
2882
2883 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
2884 {
2885 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
2886 return (rc); /* signal error */
2887 }
2888 else
2889 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
2890
2891 *TokenHandle = iIndexNew; /* return valid handle */
2892 return STATUS_SUCCESS;
2893}
2894
Note: See TracBrowser for help on using the repository browser.