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

Last change on this file since 461 was 461, checked in by phaller, 26 years ago

Fix: fixes and updates for ODINCRT support

File size: 85.1 KB
Line 
1/* $Id: HandleManager.cpp,v 1.8 1999-08-09 22:10:07 phaller Exp $ */
2
3/*
4 *
5 * Project Odin Software License can be found in LICENSE.TXT
6 *
7 */
8/*
9 * Win32 Unified Handle Manager for OS/2
10 *
11 * 1998/02/11 PH Patrick Haller (haller@zebra.fh-weingarten.de)
12 *
13 * @(#) HandleManager.Cpp 1.0.0 1998/02/11 PH start
14 */
15
16#undef DEBUG_LOCAL
17//#define DEBUG_LOCAL
18
19
20/*****************************************************************************
21 * Remark *
22 *****************************************************************************
23
24 1998/02/11 PH Even overlapped I/O could be simulated by another subsystem
25 thread with a request queue. We'll see if required ...
26
27
28 Flush (flush handle buffer)
29 WaitForSingleObject
30 WaitForMultipleObjects (?)
31
32 1998/02/12 PH IBM and Microsoft disagree about the definition of FILE_TYPE_xxx
33 Interesting, Open32 returns Microsoft's values ...
34
35 1998/02/12 PH Handles should be equipped with a locking mechanism, in particular
36 as we publish a pointer into the handle table via HMHandleQueryHandleData
37
38 */
39
40
41/*****************************************************************************
42 * Includes *
43 *****************************************************************************/
44
45#include <os2win.h>
46#include <stdlib.h>
47#include <string.h>
48#include <odincrt.h>
49
50#include "unicode.h"
51#include "misc.h"
52
53#include "HandleManager.H"
54#include "HMDevice.h"
55#include "HMOpen32.h"
56#include "HMEvent.h"
57#include "HMMutex.h"
58#include "HMSemaphore.h"
59
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 PSZ 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
125 ULONG ulHandleLast; /* index of last used handle */
126} HMGlobals;
127
128
129/*****************************************************************************
130 * Local Prototypes *
131 *****************************************************************************/
132
133 /* get appropriate device handler by the device name */
134static HMDeviceHandler *_HMDeviceFind(PSZ pszDeviceName);
135
136 /* get next free handle from the handle table */
137static ULONG _HMHandleGetFree(void);
138
139 /* get handle table entry from handle */
140static ULONG _HMHandleQuery(HANDLE hHandle);
141
142
143
144/*****************************************************************************
145 * Name : static HMDeviceHandler * _HMDeviceFind
146 * Purpose : obtain appropriate device handler from the table by searching
147 * for a device name or alias
148 * Parameters: PSZ pszDeviceName
149 * Variables :
150 * Result : HMDeviceHandler * - pointer to the handler object
151 * Remark :
152 * Status :
153 *
154 * Author : Patrick Haller [Wed, 1998/02/11 20:42]
155 *****************************************************************************/
156
157static HMDeviceHandler *_HMDeviceFind (PSZ pszDeviceName)
158{
159 PHMDEVICE pHMDevice; /* iterator over the device table */
160
161 if (pszDeviceName != NULL)
162 for (pHMDevice = TabWin32Devices; /* loop over all devices in the table */
163 pHMDevice != NULL;
164 pHMDevice = pHMDevice->pNext)
165 {
166 if (stricmp(pHMDevice->pszDeviceName, /* case-insensitive search */
167 pszDeviceName) == 0)
168 return (pHMDevice->pDeviceHandler); /* OK, we've found our device */
169 }
170
171 return (HMGlobals.pHMOpen32); /* haven't found anything, return default */
172}
173
174
175/*****************************************************************************
176 * Name : static int _HMHandleGetFree
177 * Purpose : get index to first free handle in the handle table
178 * Parameters:
179 * Variables :
180 * Result : int iIndex - index to the table or -1 in case of error
181 * Remark :
182 * Status :
183 *
184 * Author : Patrick Haller [Wed, 1998/02/11 20:43]
185 *****************************************************************************/
186
187static ULONG _HMHandleGetFree(void)
188{
189 register ULONG ulLoop;
190
191 for (ulLoop = 0;
192 ulLoop < MAX_OS2_HMHANDLES;
193 ulLoop++)
194 {
195 /* free handle found ? */
196 if (INVALID_HANDLE_VALUE == TabWin32Handles[ulLoop].hmHandleData.hHMHandle)
197 return (ulLoop); /* OK, then return it to the caller */
198 }
199
200 return (INVALID_HANDLE_VALUE); /* haven't found any free handle */
201}
202
203
204/*****************************************************************************
205 * Name : static int _HMHandleQuery
206 * Purpose : gets the index of handle table entry as fast as possible from
207 * the specified handle
208 * Parameters: HANDLE hHandle
209 * Variables :
210 * Result : index or -1 in case of error
211 * Remark :
212 * Status :
213 *
214 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
215 *****************************************************************************/
216
217static ULONG _HMHandleQuery(HANDLE hHandle)
218{
219 if (hHandle > MAX_OS2_HMHANDLES) /* check the table range */
220 return (INVALID_HANDLE_VALUE); /* nope, ERROR_INVALID_HANDLE */
221
222 /* Oops, invalid handle ! */
223 if (INVALID_HANDLE_VALUE == TabWin32Handles[hHandle].hmHandleData.hHMHandle)
224 return (INVALID_HANDLE_VALUE); /* nope, ERROR_INVALID_HANDLE */
225
226 return ( hHandle); /* OK, we've got our handle index */
227}
228
229
230/*****************************************************************************
231 * Name : DWORD HMDeviceRegister
232 * Purpose : register a device with the handle manager
233 * Parameters: PSZ pszDeviceName
234 * HMDeviceHandler *pDeviceHandler
235 * Variables :
236 * Result : API returncode
237 * Remark :
238 * Status :
239 *
240 * Author : Patrick Haller [Wed, 1998/02/12 20:44]
241 *****************************************************************************/
242
243DWORD HMDeviceRegister(PSZ pszDeviceName,
244 HMDeviceHandler *pDeviceHandler)
245{
246 PHMDEVICE pHMDevice; /* our new device to be allocated */
247
248 if ( (pszDeviceName == NULL) || /* check parameters */
249 (pDeviceHandler == NULL) )
250 return (ERROR_INVALID_PARAMETER); /* raise error conditon */
251
252
253 pHMDevice = (PHMDEVICE) malloc (sizeof (HMDEVICE) ); /* allocate memory */
254 if (pHMDevice == NULL) /* check proper allocation */
255 return (ERROR_NOT_ENOUGH_MEMORY); /* signal error */
256
257 pHMDevice->pszDeviceName = strdup(pszDeviceName); /* copy name */
258 if (pHMDevice->pszDeviceName == NULL) /* check proper allocation */
259 {
260 free (pHMDevice); /* free previously allocated memory */
261 return (ERROR_NOT_ENOUGH_MEMORY); /* signal error */
262 }
263
264 pHMDevice->pDeviceHandler = pDeviceHandler; /* store pointer to device */
265 pHMDevice->pNext = TabWin32Devices; /* establish linkage */
266
267 TabWin32Devices = pHMDevice; /* insert new node as root in the list */
268
269 return (NO_ERROR);
270}
271
272
273/*****************************************************************************
274 * Name : DWORD HMInitialize
275 * Purpose : Initialize the handlemanager
276 * Parameters: -
277 * Variables : -
278 * Result : always NO_ERROR
279 * Remark : this routine just stores the standard handles in the
280 * internal table within the HandleManager
281 * Status :
282 *
283 * Author : Patrick Haller [Wed, 1998/02/12 20:44]
284 *****************************************************************************/
285
286DWORD HMInitialize(void)
287{
288 ULONG ulIndex;
289
290 if (HMGlobals.fIsInitialized != TRUE)
291 {
292 HMGlobals.fIsInitialized = TRUE; /* OK, done */
293
294 // fill handle table
295 for (ulIndex = 0;
296 ulIndex < MAX_OS2_HMHANDLES;
297 ulIndex++)
298 TabWin32Handles[ulIndex].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
299
300 dprintf(("KERNEL32:HandleManager:HMInitialize() storing handles.\n"));
301
302 memset(&HMGlobals, /* zero out the structure first */
303 0,
304 sizeof(HMGlobals));
305
306 /* copy standard handles from OS/2's Open32 Subsystem */
307 HMSetStdHandle(STD_INPUT_HANDLE, GetStdHandle(STD_INPUT_HANDLE));
308 HMSetStdHandle(STD_OUTPUT_HANDLE, GetStdHandle(STD_OUTPUT_HANDLE));
309 HMSetStdHandle(STD_ERROR_HANDLE, GetStdHandle(STD_ERROR_HANDLE));
310
311 /* create handle manager instance for Open32 handles */
312 ODIN_FS_BEGIN
313 HMGlobals.pHMOpen32 = new HMDeviceOpen32Class("\\\\.\\");
314 HMGlobals.pHMEvent = new HMDeviceEventClass("\\\\EVENT\\");
315 HMGlobals.pHMMutex = new HMDeviceMutexClass("\\\\MUTEX\\");
316 HMGlobals.pHMSemaphore = new HMDeviceSemaphoreClass("\\\\SEM\\");
317 ODIN_FS_END
318 }
319 return (NO_ERROR);
320}
321
322
323/*****************************************************************************
324 * Name : DWORD HMTerminate
325 * Purpose : Terminate the handlemanager
326 * Parameters:
327 * Variables :
328 * Result :
329 * Remark :
330 * Status :
331 *
332 * Author : Patrick Haller [Wed, 1998/02/12 20:44]
333 *****************************************************************************/
334
335DWORD HMTerminate(void)
336{
337 /* @@@PH we could deallocate the device list here */
338
339 ODIN_FS_BEGIN
340 delete HMGlobals.pHMOpen32;
341 delete HMGlobals.pHMEvent;
342 delete HMGlobals.pHMMutex;
343 delete HMGlobals.pHMSemaphore;
344 ODIN_FS_END
345
346 return (NO_ERROR);
347}
348
349
350/*****************************************************************************/
351/* handle translation buffer management */
352/* */
353/* Since some Win32 applications rely (!) on 16-bit handles, we've got to do */
354/* 32-bit to 16-bit and vs vsa translation here. */
355/* Filehandle-based functions should be routed via the handlemanager instead */
356/* of going to Open32 directly. */
357/*****************************************************************************/
358
359
360/*****************************************************************************
361 * Name : DWORD HMHandleAllocate
362 * Purpose : allocate a handle in the translation table
363 * Parameters: PULONG pHandle16 - to return the allocated handle
364 * ULONG hHandle32 - the associated OS/2 handle
365 * Variables :
366 * Result : API returncode
367 * Remark : no parameter checking is done, phHandle may not be invalid
368 * hHandle32 shouldn't be 0
369 * Should be protected with a HM-Mutex !
370 * Status :
371 *
372 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
373 *****************************************************************************/
374
375DWORD HMHandleAllocate (PULONG phHandle16,
376 ULONG hHandleOS2)
377{
378 register ULONG ulHandle;
379
380#ifdef DEBUG_LOCAL
381 dprintf(("KERNEL32: HMHandleAllocate (%08xh,%08xh)\n",
382 phHandle16,
383 hHandleOS2));
384#endif
385
386 ulHandle = HMGlobals.ulHandleLast; /* get free handle */
387
388 do
389 {
390 /* check if handle is free */
391 if (TabWin32Handles[ulHandle].hmHandleData.hHMHandle == INVALID_HANDLE_VALUE)
392 {
393 *phHandle16 = ulHandle;
394 TabWin32Handles[ulHandle].hmHandleData.hHMHandle = hHandleOS2;
395 HMGlobals.ulHandleLast = ulHandle; /* to shorten search times */
396
397 return (NO_ERROR); /* OK */
398 }
399
400 ulHandle++; /* skip to next entry */
401
402 if (ulHandle >= MAX_OS2_HMHANDLES) /* check boundary */
403 ulHandle = 0;
404 }
405 while (ulHandle != HMGlobals.ulHandleLast);
406
407 return (ERROR_TOO_MANY_OPEN_FILES); /* OK, we're done */
408}
409
410
411/*****************************************************************************
412 * Name : DWORD HMHandleFree
413 * Purpose : free a handle from the translation table
414 * Parameters: ULONG hHandle16 - the handle to be freed
415 * Variables :
416 * Result : API returncode
417 * Remark : no parameter checking is done, hHandle16 MAY NEVER exceed
418 * the MAX_TRANSLATION_HANDLES boundary
419 * Status :
420 *
421 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
422 *****************************************************************************/
423
424DWORD HMHandleFree (ULONG hHandle16)
425{
426 ULONG rc; /* API returncode */
427
428#ifdef DEBUG_LOCAL
429 dprintf(("KERNEL32: HMHandleFree (%08xh)\n",
430 hHandle16));
431#endif
432
433 rc = HMHandleValidate(hHandle16); /* verify handle */
434 if (rc != NO_ERROR) /* check errors */
435 return (rc); /* raise error condition */
436
437 TabWin32Handles[hHandle16].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
438 /* OK, done */
439
440 return (NO_ERROR);
441}
442
443
444/*****************************************************************************
445 * Name : DWORD HMHandleValidate
446 * Purpose : validate a handle through the translation table
447 * Parameters: ULONG hHandle16 - the handle to be verified
448 * Variables :
449 * Result : API returncode
450 * Remark :
451 * Status :
452 *
453 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
454 *****************************************************************************/
455
456DWORD HMHandleValidate (ULONG hHandle16)
457{
458#ifdef DEBUG_LOCAL
459 dprintf(("KERNEL32: HMHandleValidate (%08xh)\n",
460 hHandle16));
461#endif
462
463 if (hHandle16 >= MAX_OS2_HMHANDLES) /* check boundary */
464 return (ERROR_INVALID_HANDLE); /* raise error condition */
465
466 if (TabWin32Handles[hHandle16].hmHandleData.hHMHandle == INVALID_HANDLE_VALUE)
467 /* valid ? */
468 return (ERROR_INVALID_HANDLE); /* raise error condition */
469
470 return (NO_ERROR);
471}
472
473
474/*****************************************************************************
475 * Name : DWORD HMHandleTranslateToWin
476 * Purpose : translate a 32-bit OS/2 handle to the associated windows handle
477 * Parameters: ULONG hHandle32 - the OS/2 handle
478 * PULONG phHandle16 - the associated windows handle
479 * Variables :
480 * Result : API returncode
481 * Remark :
482 * Status :
483 *
484 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
485 *****************************************************************************/
486
487DWORD HMHandleTranslateToWin (ULONG hHandleOS2,
488 PULONG phHandle16)
489{
490 ULONG rc; /* API returncode */
491 register ULONG ulIndex; /* index counter over the table */
492
493#ifdef DEBUG_LOCAL
494 dprintf(("KERNEL32: HMHandleTranslateToWin (%08xh, %08xh)\n",
495 hHandleOS2,
496 phHandle16));
497#endif
498
499 for (ulIndex = 0;
500 ulIndex < MAX_OS2_HMHANDLES;
501 ulIndex++)
502 {
503 /* look for the handle */
504 if (TabWin32Handles[ulIndex].hmHandleData.hHMHandle == hHandleOS2)
505 {
506 *phHandle16 = ulIndex; /* deliver result */
507 return (NO_ERROR); /* OK */
508 }
509 }
510
511 return (ERROR_INVALID_HANDLE); /* raise error condition */
512}
513
514
515/*****************************************************************************
516 * Name : DWORD HMHandleTranslateToOS2
517 * Purpose : translate a 16-bit Win32 handle to the associated OS/2 handle
518 * Parameters: ULONG hHandle16 - the windows handle
519 * PULONG phHandle32 - the associated OS/2 handle
520 * Variables :
521 * Result : API returncode
522 * Remark :
523 * Status :
524 *
525 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
526 *****************************************************************************/
527
528DWORD HMHandleTranslateToOS2 (ULONG hHandle16,
529 PULONG phHandleOS2)
530{
531#ifdef DEBUG_LOCAL
532 dprintf(("KERNEL32: HMHandleTranslateToOS2 (%08xh, %08xh)\n",
533 hHandle16,
534 phHandleOS2));
535#endif
536
537 if (HMHandleValidate(hHandle16) == NO_ERROR) /* verify handle */
538 {
539 *phHandleOS2 = TabWin32Handles[hHandle16].hmHandleData.hHMHandle;
540 return (NO_ERROR);
541 }
542
543 return (ERROR_INVALID_HANDLE); /* raise error condition */
544}
545
546
547/*****************************************************************************
548 * Name : DWORD HMHandleTranslateToOS2i
549 * Purpose : translate a 16-bit Win32 handle to the associated OS/2 handle
550 * Parameters: ULONG hHandle16 - the windows handle
551 * Variables :
552 * Result : OS/2 handle
553 * Remark : no checkinf
554 * Status :
555 *
556 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
557 *****************************************************************************/
558
559DWORD HMHandleTranslateToOS2i (ULONG hHandle16)
560{
561#ifdef DEBUG_LOCAL
562 dprintf(("KERNEL32: HMHandleTranslateToOS2i (%08xh)\n",
563 hHandle16));
564#endif
565
566 return(TabWin32Handles[hHandle16].hmHandleData.hHMHandle);
567}
568
569
570
571/*****************************************************************************
572 * Name : HANDLE _HMGetStdHandle
573 * Purpose : replacement for Open32's GetStdHandle function
574 * Parameters: DWORD nStdHandle
575 * Variables :
576 * Result : HANDLE to standard device
577 * Remark :
578 * Status :
579 *
580 * Author : Patrick Haller [Wed, 1998/02/12 20:44]
581 *****************************************************************************/
582
583HANDLE HMGetStdHandle(DWORD nStdHandle)
584{
585 switch (nStdHandle)
586 {
587 case STD_INPUT_HANDLE: return (HMGlobals.hStandardIn);
588 case STD_OUTPUT_HANDLE: return (HMGlobals.hStandardOut);
589 case STD_ERROR_HANDLE: return (HMGlobals.hStandardError);
590
591 default:
592 {
593 SetLastError(ERROR_INVALID_PARAMETER); /* set error information */
594 return (INVALID_HANDLE_VALUE); /* raise error condition */
595 }
596 }
597}
598
599
600/*****************************************************************************
601 * Name : HANDLE _HMSetStdHandle
602 * Purpose : replacement for Open32's SetStdHandle function
603 * Parameters: DWORD nStdHandle
604 * HANDLE hHandle
605 * Variables :
606 * Result : BOOL fSuccess
607 * Remark :
608 * Status :
609 *
610 * Author : Patrick Haller [Wed, 1998/02/12 20:44]
611 *****************************************************************************/
612
613BOOL HMSetStdHandle(DWORD nStdHandle,
614 HANDLE hHandle)
615{
616 switch (nStdHandle)
617 {
618 case STD_INPUT_HANDLE: HMGlobals.hStandardIn = hHandle; return TRUE;
619 case STD_OUTPUT_HANDLE: HMGlobals.hStandardOut = hHandle; return TRUE;
620 case STD_ERROR_HANDLE: HMGlobals.hStandardError = hHandle; return TRUE;
621
622 default:
623 {
624 SetLastError(ERROR_INVALID_PARAMETER); /* set error information */
625 return (FALSE); /* raise error condition */
626 }
627 }
628}
629
630
631/*****************************************************************************
632 * Name : HANDLE HMCreateFile
633 * Purpose : Wrapper for the CreateFile() API
634 * Parameters:
635 * Variables :
636 * Result :
637 * Remark : Fix parameters passed to the HMDeviceManager::CreateFile
638 * Supply access mode and share mode validation routines
639 * Status :
640 *
641 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
642 *****************************************************************************/
643
644HFILE HMCreateFile(LPCSTR lpFileName,
645 DWORD dwDesiredAccess,
646 DWORD dwShareMode,
647 LPSECURITY_ATTRIBUTES lpSecurityAttributes,
648 DWORD dwCreationDisposition,
649 DWORD dwFlagsAndAttributes,
650 HANDLE hTemplateFile)
651{
652 int iIndex; /* index into the handle table */
653 int iIndexNew; /* index into the handle table */
654 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
655 HANDLE hResult;
656 DWORD rc; /* API return code */
657 PHMHANDLEDATA pHMHandleData;
658 HMHANDLEDATA HMHandleTemp; /* temporary buffer for handle data */
659
660 /* create new handle by either lpFileName or hTemplateFile */
661 if (lpFileName == NULL) /* this indicates creation from template */
662 {
663 iIndex = _HMHandleQuery(hTemplateFile); /* query table for template */
664 if (-1 == iIndex) /* this device is unknown to us */
665 {
666 SetLastError (ERROR_INVALID_HANDLE);
667 return INVALID_HANDLE_VALUE;
668 }
669 else
670 {
671 /* to pass to handler */
672 pHMHandleData = &TabWin32Handles[iIndex].hmHandleData;
673 pDeviceHandler = TabWin32Handles[iIndex].pDeviceHandler;
674 }
675 }
676 else
677 {
678 pDeviceHandler = _HMDeviceFind((PSZ)lpFileName); /* find device */
679
680 if (NULL == pDeviceHandler) /* this name is unknown to us */
681 {
682 SetLastError(ERROR_FILE_NOT_FOUND);
683 return (INVALID_HANDLE_VALUE); /* signal error */
684 }
685 else
686 pHMHandleData = NULL;
687 }
688
689
690 iIndexNew = _HMHandleGetFree(); /* get free handle */
691 if (-1 == iIndexNew) /* oops, no free handles ! */
692 {
693 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
694 return (INVALID_HANDLE_VALUE); /* signal error */
695 }
696
697
698 /* initialize the complete HMHANDLEDATA structure */
699 if (lpFileName == NULL) /* create from template */
700 memcpy (&TabWin32Handles[iIndexNew].hmHandleData,
701 &TabWin32Handles[iIndex].hmHandleData,
702 sizeof(HMHANDLEDATA));
703 else
704 {
705 HMHandleTemp.dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
706 HMHandleTemp.dwAccess = dwDesiredAccess;
707 HMHandleTemp.dwShare = dwShareMode;
708 HMHandleTemp.dwCreation = dwCreationDisposition;
709 HMHandleTemp.dwFlags = dwFlagsAndAttributes;
710 HMHandleTemp.lpHandlerData = NULL;
711 }
712
713 /* we've got to mark the handle as occupied here, since another device */
714 /* could be created within the device handler -> deadlock */
715
716 /* write appropriate entry into the handle table if open succeeded */
717 HMHandleTemp.hHMHandle = iIndexNew;
718 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
719
720 /* now copy back our temporary handle data */
721 memcpy(&TabWin32Handles[iIndexNew].hmHandleData,
722 &HMHandleTemp,
723 sizeof(HMHANDLEDATA));
724
725 rc = pDeviceHandler->CreateFile(lpFileName, /* call the device handler */
726 &HMHandleTemp,
727 lpSecurityAttributes,
728 pHMHandleData);
729
730#ifdef DEBUG_LOCAL
731 dprintf(("KERNEL32/HandleManager:CheckPoint2: %s lpHandlerData=%08xh\n",
732 lpFileName,
733 HMHandleTemp.lpHandlerData));
734#endif
735
736 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
737 {
738 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
739 SetLastError(rc); /* Hehe, OS/2 and NT are pretty compatible :) */
740 return (INVALID_HANDLE_VALUE); /* signal error */
741 }
742 else
743 {
744 /* copy data fields that might have been modified by CreateFile */
745 memcpy(&TabWin32Handles[iIndexNew].hmHandleData,
746 &HMHandleTemp,
747 sizeof(HMHANDLEDATA));
748 }
749
750
751#ifdef DEBUG_LOCAL
752 dprintf(("KERNEL32/HandleManager: CreateFile(%s)=%08xh\n",
753 lpFileName,
754 iIndexNew));
755#endif
756
757 return (HFILE)iIndexNew; /* return valid handle */
758}
759
760
761/*****************************************************************************
762 * Name : HANDLE HMOpenFile
763 * Purpose : Wrapper for the OpenFile() API
764 * Parameters:
765 * Variables :
766 * Result :
767 * Remark : Fix parameters passed to the HMDeviceManager::OpenFile
768 * Supply access mode and share mode validation routines
769 * Status :
770 *
771 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
772 *****************************************************************************/
773
774
775/***********************************************************************
776 * FILE_ConvertOFMode
777 *
778 * Convert OF_* mode into flags for CreateFile.
779 */
780static void FILE_ConvertOFMode( INT mode, DWORD *access, DWORD *sharing )
781{
782 switch(mode & 0x03)
783 {
784 case OF_READ: *access = GENERIC_READ; break;
785 case OF_WRITE: *access = GENERIC_WRITE; break;
786 case OF_READWRITE: *access = GENERIC_READ | GENERIC_WRITE; break;
787 default: *access = 0; break;
788 }
789 switch(mode & 0x70)
790 {
791 case OF_SHARE_EXCLUSIVE: *sharing = 0; break;
792 case OF_SHARE_DENY_WRITE: *sharing = FILE_SHARE_READ; break;
793 case OF_SHARE_DENY_READ: *sharing = FILE_SHARE_WRITE; break;
794 case OF_SHARE_DENY_NONE:
795 case OF_SHARE_COMPAT:
796 default: *sharing = FILE_SHARE_READ | FILE_SHARE_WRITE; break;
797 }
798}
799
800HANDLE HMOpenFile(LPCSTR lpFileName,
801 OFSTRUCT* pOFStruct,
802 UINT fuMode)
803{
804 int iIndex; /* index into the handle table */
805 int iIndexNew; /* index into the handle table */
806 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
807 PHMHANDLEDATA pHMHandleData;
808 DWORD rc; /* API return code */
809
810
811 pDeviceHandler = _HMDeviceFind((PSZ)lpFileName); /* find device */
812 if (NULL == pDeviceHandler) /* this name is unknown to us */
813 {
814 SetLastError(ERROR_FILE_NOT_FOUND);
815 return (INVALID_HANDLE_VALUE); /* signal error */
816 }
817 else
818 pHMHandleData = NULL;
819
820
821 iIndexNew = _HMHandleGetFree(); /* get free handle */
822 if (-1 == iIndexNew) /* oops, no free handles ! */
823 {
824 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
825 return (INVALID_HANDLE_VALUE); /* signal error */
826 }
827
828
829 /* initialize the complete HMHANDLEDATA structure */
830 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
831 pHMHandleData->dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
832
833 FILE_ConvertOFMode(fuMode, /* map OF_flags */
834 &pHMHandleData->dwAccess,
835 &pHMHandleData->dwShare);
836
837 pHMHandleData->dwCreation = 0;
838 pHMHandleData->dwFlags = 0;
839 pHMHandleData->lpHandlerData = NULL;
840
841
842 /* we've got to mark the handle as occupied here, since another device */
843 /* could be created within the device handler -> deadlock */
844
845 /* write appropriate entry into the handle table if open succeeded */
846 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
847 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
848
849 rc = pDeviceHandler->OpenFile (lpFileName, /* call the device handler */
850 &TabWin32Handles[iIndexNew].hmHandleData,
851 pOFStruct,
852 fuMode);
853
854#ifdef DEBUG_LOCAL
855 dprintf(("KERNEL32/HandleManager:CheckPoint3: %s lpHandlerData=%08xh rc=%08xh\n",
856 lpFileName,
857 &TabWin32Handles[iIndexNew].hmHandleData.lpHandlerData,
858 rc));
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
868#ifdef DEBUG_LOCAL
869 dprintf(("KERNEL32/HandleManager: OpenFile(%s)=%08xh\n",
870 lpFileName,
871 hResult));
872#endif
873
874 return iIndexNew; /* return valid handle */
875}
876
877
878
879/*****************************************************************************
880 * Name : HANDLE HMCloseFile
881 * Purpose : Wrapper for the CloseHandle() API
882 * Parameters:
883 * Variables :
884 * Result :
885 * Remark :
886 * Status :
887 *
888 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
889 *****************************************************************************/
890
891BOOL HMCloseHandle(HANDLE hObject)
892{
893 int iIndex; /* index into the handle table */
894 BOOL fResult; /* result from the device handler's CloseHandle() */
895 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
896
897 /* validate handle */
898 iIndex = _HMHandleQuery(hObject); /* get the index */
899 if (-1 == iIndex) /* error ? */
900 {
901 //@@@PH it may occur someone closes e.g. a semaphore handle
902 // which is not registered through the HandleManager yet.
903 // so we try to pass on to Open32 instead.
904 dprintf(("KERNEL32: HandleManager:HMCloseHandle(%08xh) passed on to Open32.\n",
905 hObject));
906
907 fResult = O32_CloseHandle(hObject);
908 return (fResult);
909
910 //SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
911 //return (FALSE); /* signal failure */
912 }
913
914 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
915 //SvL: Check if pDeviceHandler is set
916 if (pHMHandle->pDeviceHandler) {
917 fResult = pHMHandle->pDeviceHandler->CloseHandle(&pHMHandle->hmHandleData);
918 }
919 else {
920 dprintf(("HMCloseHAndle(%08xh): pDeviceHandler not set", hObject));
921 fResult = TRUE;
922 }
923
924 if (fResult == TRUE) /* remove handle if close succeeded */
925 pHMHandle->hmHandleData.hHMHandle = 0; /* mark handle as free */
926
927 return (fResult); /* deliver return code */
928}
929
930
931/*****************************************************************************
932 * Name : HANDLE HMReadFile
933 * Purpose : Wrapper for the ReadHandle() API
934 * Parameters:
935 * Variables :
936 * Result :
937 * Remark :
938 * Status :
939 *
940 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
941 *****************************************************************************/
942
943BOOL HMReadFile(HANDLE hFile,
944 LPVOID lpBuffer,
945 DWORD nNumberOfBytesToRead,
946 LPDWORD lpNumberOfBytesRead,
947 LPOVERLAPPED lpOverlapped)
948{
949 int iIndex; /* index into the handle table */
950 BOOL fResult; /* result from the device handler's CloseHandle() */
951 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
952
953 /* validate handle */
954 iIndex = _HMHandleQuery(hFile); /* get the index */
955 if (-1 == iIndex) /* error ? */
956 {
957 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
958 return (FALSE); /* signal failure */
959 }
960
961 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
962 fResult = pHMHandle->pDeviceHandler->ReadFile(&pHMHandle->hmHandleData,
963 lpBuffer,
964 nNumberOfBytesToRead,
965 lpNumberOfBytesRead,
966 lpOverlapped);
967
968 return (fResult); /* deliver return code */
969}
970
971
972/*****************************************************************************
973 * Name : HANDLE HMWriteFile
974 * Purpose : Wrapper for the WriteHandle() API
975 * Parameters:
976 * Variables :
977 * Result :
978 * Remark :
979 * Status :
980 *
981 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
982 *****************************************************************************/
983
984BOOL HMWriteFile(HANDLE hFile,
985 LPCVOID lpBuffer,
986 DWORD nNumberOfBytesToWrite,
987 LPDWORD lpNumberOfBytesWritten,
988 LPOVERLAPPED lpOverlapped)
989{
990 int iIndex; /* index into the handle table */
991 BOOL fResult; /* result from the device handler's CloseHandle() */
992 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
993
994 /* validate handle */
995 iIndex = _HMHandleQuery(hFile); /* get the index */
996 if (-1 == iIndex) /* error ? */
997 {
998 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
999 return (FALSE); /* signal failure */
1000 }
1001
1002 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1003 fResult = pHMHandle->pDeviceHandler->WriteFile(&pHMHandle->hmHandleData,
1004 lpBuffer,
1005 nNumberOfBytesToWrite,
1006 lpNumberOfBytesWritten,
1007 lpOverlapped);
1008
1009 return (fResult); /* deliver return code */
1010}
1011
1012
1013/*****************************************************************************
1014 * Name : HANDLE HMGetFileType
1015 * Purpose : Wrapper for the GetFileType() API
1016 * Parameters:
1017 * Variables :
1018 * Result :
1019 * Remark :
1020 * Status :
1021 *
1022 * Author : Patrick Haller [Wed, 1998/02/12 13:37]
1023 *****************************************************************************/
1024
1025DWORD HMGetFileType(HANDLE hFile)
1026{
1027 int iIndex; /* index into the handle table */
1028 DWORD dwResult; /* result from the device handler's API */
1029 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1030
1031 /* validate handle */
1032 iIndex = _HMHandleQuery(hFile); /* get the index */
1033 if (-1 == iIndex) /* error ? */
1034 {
1035 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1036 return (INVALID_HANDLE_ERROR); /* signal failure */
1037 }
1038
1039 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1040 dwResult = pHMHandle->pDeviceHandler->GetFileType(&pHMHandle->hmHandleData);
1041
1042 return (dwResult); /* deliver return code */
1043}
1044
1045
1046/*****************************************************************************
1047 * Name : HMDeviceHandler::_DeviceReuqest
1048 * Purpose : entry method for special request functions
1049 * Parameters: ULONG ulRequestCode
1050 * various parameters as required
1051 * Variables :
1052 * Result :
1053 * Remark : the standard behaviour is to return an error code for non-
1054 * existant request codes
1055 * Status :
1056 *
1057 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
1058 *****************************************************************************/
1059
1060DWORD HMDeviceRequest (HANDLE hFile,
1061 ULONG ulRequestCode,
1062 ULONG arg1,
1063 ULONG arg2,
1064 ULONG arg3,
1065 ULONG arg4)
1066{
1067 int iIndex; /* index into the handle table */
1068 DWORD dwResult; /* result from the device handler's API */
1069 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1070
1071 /* validate handle */
1072 iIndex = _HMHandleQuery(hFile); /* get the index */
1073 if (-1 == iIndex) /* error ? */
1074 {
1075 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1076 return (INVALID_HANDLE_ERROR); /* signal failure */
1077 }
1078
1079 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1080 dwResult = pHMHandle->pDeviceHandler->_DeviceRequest(&pHMHandle->hmHandleData,
1081 ulRequestCode,
1082 arg1,
1083 arg2,
1084 arg3,
1085 arg4);
1086
1087 return (dwResult); /* deliver return code */
1088}
1089
1090
1091/*****************************************************************************
1092 * Name : HMDeviceHandler::GetFileInformationByHandle
1093 * Purpose : router function for GetFileInformationByHandle
1094 * Parameters:
1095 * Variables :
1096 * Result :
1097 * Remark :
1098 * Status :
1099 *
1100 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1101 *****************************************************************************/
1102
1103BOOL HMGetFileInformationByHandle (HANDLE hFile,
1104 BY_HANDLE_FILE_INFORMATION *pHFI)
1105{
1106 int iIndex; /* index into the handle table */
1107 DWORD dwResult; /* result from the device handler's API */
1108 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1109
1110 /* validate handle */
1111 iIndex = _HMHandleQuery(hFile); /* get the index */
1112 if (-1 == iIndex) /* error ? */
1113 {
1114 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1115 return (INVALID_HANDLE_ERROR); /* signal failure */
1116 }
1117
1118 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1119 dwResult = pHMHandle->pDeviceHandler->GetFileInformationByHandle(&pHMHandle->hmHandleData,
1120 pHFI);
1121
1122 return (dwResult); /* deliver return code */
1123}
1124
1125
1126/*****************************************************************************
1127 * Name : HMDeviceHandler::SetEndOfFile
1128 * Purpose : router function for SetEndOfFile
1129 * Parameters:
1130 * Variables :
1131 * Result :
1132 * Remark :
1133 * Status :
1134 *
1135 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1136 *****************************************************************************/
1137
1138BOOL HMSetEndOfFile (HANDLE hFile)
1139{
1140 int iIndex; /* index into the handle table */
1141 BOOL bResult; /* result from the device handler's API */
1142 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1143
1144 /* validate handle */
1145 iIndex = _HMHandleQuery(hFile); /* get the index */
1146 if (-1 == iIndex) /* error ? */
1147 {
1148 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1149 return (INVALID_HANDLE_ERROR); /* signal failure */
1150 }
1151
1152 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1153 bResult = pHMHandle->pDeviceHandler->SetEndOfFile(&pHMHandle->hmHandleData);
1154
1155 return (bResult); /* deliver return code */
1156}
1157
1158
1159/*****************************************************************************
1160 * Name : HMDeviceHandler::SetFileTime
1161 * Purpose : router function for SetFileTime
1162 * Parameters:
1163 * Variables :
1164 * Result :
1165 * Remark :
1166 * Status :
1167 *
1168 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1169 *****************************************************************************/
1170
1171BOOL HMSetFileTime (HANDLE hFile,
1172 const FILETIME *pFT1,
1173 const FILETIME *pFT2,
1174 const FILETIME *pFT3)
1175{
1176 int iIndex; /* index into the handle table */
1177 BOOL bResult; /* result from the device handler's API */
1178 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1179
1180 /* validate handle */
1181 iIndex = _HMHandleQuery(hFile); /* get the index */
1182 if (-1 == iIndex) /* error ? */
1183 {
1184 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1185 return (INVALID_HANDLE_ERROR); /* signal failure */
1186 }
1187
1188 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1189 bResult = pHMHandle->pDeviceHandler->SetFileTime(&pHMHandle->hmHandleData,
1190 (LPFILETIME)pFT1,
1191 (LPFILETIME)pFT2,
1192 (LPFILETIME)pFT3);
1193
1194 return (bResult); /* deliver return code */
1195}
1196
1197
1198/*****************************************************************************
1199 * Name : HMDeviceHandler::GetFileSize
1200 * Purpose : router function for GetFileSize
1201 * Parameters:
1202 * Variables :
1203 * Result :
1204 * Remark :
1205 * Status :
1206 *
1207 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1208 *****************************************************************************/
1209
1210DWORD HMGetFileSize (HANDLE hFile,
1211 PDWORD pSize)
1212{
1213 int iIndex; /* index into the handle table */
1214 DWORD dwResult; /* result from the device handler's API */
1215 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1216
1217 /* validate handle */
1218 iIndex = _HMHandleQuery(hFile); /* get the index */
1219 if (-1 == iIndex) /* error ? */
1220 {
1221 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1222 return (INVALID_HANDLE_ERROR); /* signal failure */
1223 }
1224
1225 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1226 dwResult = pHMHandle->pDeviceHandler->GetFileSize(&pHMHandle->hmHandleData,
1227 pSize);
1228
1229 return (dwResult); /* deliver return code */
1230}
1231
1232
1233/*****************************************************************************
1234 * Name : HMDeviceHandler::SetFilePointer
1235 * Purpose : router function for SetFilePointer
1236 * Parameters:
1237 * Variables :
1238 * Result :
1239 * Remark :
1240 * Status :
1241 *
1242 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1243 *****************************************************************************/
1244
1245DWORD HMSetFilePointer (HANDLE hFile,
1246 LONG lDistanceToMove,
1247 PLONG lpDistanceToMoveHigh,
1248 DWORD dwMoveMethod)
1249{
1250 int iIndex; /* index into the handle table */
1251 DWORD dwResult; /* result from the device handler's API */
1252 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1253
1254 /* validate handle */
1255 iIndex = _HMHandleQuery(hFile); /* get the index */
1256 if (-1 == iIndex) /* error ? */
1257 {
1258 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1259 return (INVALID_HANDLE_ERROR); /* signal failure */
1260 }
1261
1262 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1263 dwResult = pHMHandle->pDeviceHandler->SetFilePointer(&pHMHandle->hmHandleData,
1264 lDistanceToMove,
1265 lpDistanceToMoveHigh,
1266 dwMoveMethod);
1267
1268 return (dwResult); /* deliver return code */
1269}
1270
1271
1272/*****************************************************************************
1273 * Name : HMDeviceHandler::LockFile
1274 * Purpose : router function for LockFile
1275 * Parameters:
1276 * Variables :
1277 * Result :
1278 * Remark :
1279 * Status :
1280 *
1281 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1282 *****************************************************************************/
1283
1284BOOL HMLockFile (HFILE hFile,
1285 DWORD arg2,
1286 DWORD arg3,
1287 DWORD arg4,
1288 DWORD arg5)
1289{
1290 int iIndex; /* index into the handle table */
1291 DWORD dwResult; /* result from the device handler's API */
1292 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1293
1294 /* validate handle */
1295 iIndex = _HMHandleQuery(hFile); /* get the index */
1296 if (-1 == iIndex) /* error ? */
1297 {
1298 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1299 return (INVALID_HANDLE_ERROR); /* signal failure */
1300 }
1301
1302 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1303 dwResult = pHMHandle->pDeviceHandler->LockFile(&pHMHandle->hmHandleData,
1304 arg2,
1305 arg3,
1306 arg4,
1307 arg5);
1308
1309 return (dwResult); /* deliver return code */
1310}
1311
1312
1313/*****************************************************************************
1314 * Name : HMDeviceHandler::LockFileEx
1315 * Purpose : router function for LockFileEx
1316 * Parameters:
1317 * Variables :
1318 * Result :
1319 * Remark :
1320 * Status :
1321 *
1322 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1323 *****************************************************************************/
1324
1325BOOL HMLockFileEx(HANDLE hFile,
1326 DWORD dwFlags,
1327 DWORD dwReserved,
1328 DWORD nNumberOfBytesToLockLow,
1329 DWORD nNumberOfBytesToLockHigh,
1330 LPOVERLAPPED lpOverlapped)
1331{
1332 int iIndex; /* index into the handle table */
1333 DWORD dwResult; /* result from the device handler's API */
1334 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1335
1336 /* validate handle */
1337 iIndex = _HMHandleQuery(hFile); /* get the index */
1338 if (-1 == iIndex) /* error ? */
1339 {
1340 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1341 return (INVALID_HANDLE_ERROR); /* signal failure */
1342 }
1343
1344 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1345 dwResult = pHMHandle->pDeviceHandler->LockFileEx(&pHMHandle->hmHandleData,
1346 dwFlags,
1347 dwReserved,
1348 nNumberOfBytesToLockLow,
1349 nNumberOfBytesToLockHigh,
1350 lpOverlapped);
1351
1352 return (dwResult); /* deliver return code */
1353}
1354
1355
1356
1357/*****************************************************************************
1358 * Name : HMDeviceHandler::UnlockFile
1359 * Purpose : router function for UnlockFile
1360 * Parameters:
1361 * Variables :
1362 * Result :
1363 * Remark :
1364 * Status :
1365 *
1366 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1367 *****************************************************************************/
1368
1369BOOL HMUnlockFile (HFILE hFile,
1370 DWORD arg2,
1371 DWORD arg3,
1372 DWORD arg4,
1373 DWORD arg5)
1374{
1375 int iIndex; /* index into the handle table */
1376 DWORD dwResult; /* result from the device handler's API */
1377 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1378
1379 /* validate handle */
1380 iIndex = _HMHandleQuery(hFile); /* get the index */
1381 if (-1 == iIndex) /* error ? */
1382 {
1383 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1384 return (INVALID_HANDLE_ERROR); /* signal failure */
1385 }
1386
1387 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1388 dwResult = pHMHandle->pDeviceHandler->UnlockFile(&pHMHandle->hmHandleData,
1389 arg2,
1390 arg3,
1391 arg4,
1392 arg5);
1393
1394 return (dwResult); /* deliver return code */
1395}
1396
1397
1398/*****************************************************************************
1399 * Name : HMDeviceHandler::UnlockFileEx
1400 * Purpose : router function for UnlockFileEx
1401 * Parameters:
1402 * Variables :
1403 * Result :
1404 * Remark :
1405 * Status :
1406 *
1407 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1408 *****************************************************************************/
1409
1410BOOL HMUnlockFileEx(HANDLE hFile,
1411 DWORD dwFlags,
1412 DWORD dwReserved,
1413 DWORD nNumberOfBytesToLockLow,
1414 DWORD nNumberOfBytesToLockHigh,
1415 LPOVERLAPPED lpOverlapped)
1416{
1417 int iIndex; /* index into the handle table */
1418 DWORD dwResult; /* result from the device handler's API */
1419 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1420
1421 /* validate handle */
1422 iIndex = _HMHandleQuery(hFile); /* get the index */
1423 if (-1 == iIndex) /* error ? */
1424 {
1425 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1426 return (INVALID_HANDLE_ERROR); /* signal failure */
1427 }
1428
1429 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1430 dwResult = pHMHandle->pDeviceHandler->UnlockFileEx(&pHMHandle->hmHandleData,
1431 dwFlags,
1432 dwReserved,
1433 nNumberOfBytesToLockLow,
1434 nNumberOfBytesToLockHigh,
1435 lpOverlapped);
1436
1437 return (dwResult); /* deliver return code */
1438}
1439
1440
1441/*****************************************************************************
1442 * Name : HMDeviceHandler::WaitForSingleObject
1443 * Purpose : router function for WaitForSingleObject
1444 * Parameters:
1445 * Variables :
1446 * Result :
1447 * Remark :
1448 * Status :
1449 *
1450 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1451 *****************************************************************************/
1452
1453DWORD HMWaitForSingleObject(HANDLE hObject,
1454 DWORD dwTimeout)
1455{
1456 int iIndex; /* index into the handle table */
1457 DWORD dwResult; /* result from the device handler's API */
1458 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1459
1460 /* validate handle */
1461 iIndex = _HMHandleQuery(hObject); /* get the index */
1462 if (-1 == iIndex) /* error ? */
1463 {
1464 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1465 return (INVALID_HANDLE_ERROR); /* signal failure */
1466 }
1467
1468 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1469 dwResult = pHMHandle->pDeviceHandler->WaitForSingleObject(&pHMHandle->hmHandleData,
1470 dwTimeout);
1471
1472 return (dwResult); /* deliver return code */
1473}
1474
1475
1476/*****************************************************************************
1477 * Name : HMDeviceHandler::WaitForSingleObjectEx
1478 * Purpose : router function for WaitForSingleObjectEx
1479 * Parameters:
1480 * Variables :
1481 * Result :
1482 * Remark :
1483 * Status :
1484 *
1485 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1486 *****************************************************************************/
1487
1488DWORD HMWaitForSingleObjectEx(HANDLE hObject,
1489 DWORD dwTimeout,
1490 BOOL fAlertable)
1491{
1492 int iIndex; /* index into the handle table */
1493 DWORD dwResult; /* result from the device handler's API */
1494 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1495
1496 /* validate handle */
1497 iIndex = _HMHandleQuery(hObject); /* get the index */
1498 if (-1 == iIndex) /* error ? */
1499 {
1500 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1501 return (INVALID_HANDLE_ERROR); /* signal failure */
1502 }
1503
1504 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1505 dwResult = pHMHandle->pDeviceHandler->WaitForSingleObjectEx(&pHMHandle->hmHandleData,
1506 dwTimeout,
1507 fAlertable);
1508
1509 return (dwResult); /* deliver return code */
1510}
1511
1512
1513/*****************************************************************************
1514 * Name : HMDeviceHandler::FlushFileBuffers
1515 * Purpose : router function for FlushFileBuffers
1516 * Parameters:
1517 * Variables :
1518 * Result :
1519 * Remark :
1520 * Status :
1521 *
1522 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1523 *****************************************************************************/
1524
1525BOOL HMFlushFileBuffers(HANDLE hFile)
1526{
1527 int iIndex; /* index into the handle table */
1528 DWORD dwResult; /* result from the device handler's API */
1529 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1530
1531 /* validate handle */
1532 iIndex = _HMHandleQuery(hFile); /* get the index */
1533 if (-1 == iIndex) /* error ? */
1534 {
1535 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1536 return (INVALID_HANDLE_ERROR); /* signal failure */
1537 }
1538
1539 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1540 dwResult = pHMHandle->pDeviceHandler->FlushFileBuffers(&pHMHandle->hmHandleData);
1541
1542 return (dwResult); /* deliver return code */
1543}
1544
1545
1546/*****************************************************************************
1547 * Name : HMDeviceHandler::GetOverlappedResult
1548 * Purpose : router function for GetOverlappedResult
1549 * Parameters:
1550 * Variables :
1551 * Result :
1552 * Remark :
1553 * Status :
1554 *
1555 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1556 *****************************************************************************/
1557
1558BOOL HMGetOverlappedResult(HANDLE hObject,
1559 LPOVERLAPPED lpOverlapped,
1560 LPDWORD arg3,
1561 BOOL arg4)
1562{
1563 int iIndex; /* index into the handle table */
1564 DWORD dwResult; /* result from the device handler's API */
1565 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1566
1567 /* validate handle */
1568 iIndex = _HMHandleQuery(hObject); /* get the index */
1569 if (-1 == iIndex) /* error ? */
1570 {
1571 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1572 return (INVALID_HANDLE_ERROR); /* signal failure */
1573 }
1574
1575 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1576 dwResult = pHMHandle->pDeviceHandler->GetOverlappedResult(&pHMHandle->hmHandleData,
1577 lpOverlapped,
1578 arg3,
1579 arg4);
1580
1581 return (dwResult); /* deliver return code */
1582}
1583
1584
1585/*****************************************************************************
1586 * Name : HMReleaseMutex
1587 * Purpose : router function for ReleaseMutex
1588 * Parameters:
1589 * Variables :
1590 * Result :
1591 * Remark :
1592 * Status :
1593 *
1594 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1595 *****************************************************************************/
1596
1597BOOL HMReleaseMutex(HANDLE hObject)
1598{
1599 int iIndex; /* index into the handle table */
1600 DWORD dwResult; /* result from the device handler's API */
1601 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1602
1603 /* validate handle */
1604 iIndex = _HMHandleQuery(hObject); /* get the index */
1605 if (-1 == iIndex) /* error ? */
1606 {
1607 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1608 return (INVALID_HANDLE_ERROR); /* signal failure */
1609 }
1610
1611 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1612 dwResult = pHMHandle->pDeviceHandler->ReleaseMutex(&pHMHandle->hmHandleData);
1613
1614 return (dwResult); /* deliver return code */
1615}
1616
1617
1618/*****************************************************************************
1619 * Name : HMSetEvent
1620 * Purpose : router function for SetEvent
1621 * Parameters:
1622 * Variables :
1623 * Result :
1624 * Remark :
1625 * Status :
1626 *
1627 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1628 *****************************************************************************/
1629
1630BOOL HMSetEvent(HANDLE hEvent)
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(hEvent); /* get the index */
1638 if (-1 == iIndex) /* error ? */
1639 {
1640 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1641 return (INVALID_HANDLE_ERROR); /* signal failure */
1642 }
1643
1644 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1645 dwResult = pHMHandle->pDeviceHandler->SetEvent(&pHMHandle->hmHandleData);
1646
1647 return (dwResult); /* deliver return code */
1648}
1649
1650
1651/*****************************************************************************
1652 * Name : HMPulseEvent
1653 * Purpose : router function for PulseEvent
1654 * Parameters:
1655 * Variables :
1656 * Result :
1657 * Remark :
1658 * Status :
1659 *
1660 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1661 *****************************************************************************/
1662
1663BOOL HMPulseEvent(HANDLE hEvent)
1664{
1665 int iIndex; /* index into the handle table */
1666 DWORD dwResult; /* result from the device handler's API */
1667 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1668
1669 /* validate handle */
1670 iIndex = _HMHandleQuery(hEvent); /* get the index */
1671 if (-1 == iIndex) /* error ? */
1672 {
1673 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1674 return (INVALID_HANDLE_ERROR); /* signal failure */
1675 }
1676
1677 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1678 dwResult = pHMHandle->pDeviceHandler->PulseEvent(&pHMHandle->hmHandleData);
1679
1680 return (dwResult); /* deliver return code */
1681}
1682
1683
1684/*****************************************************************************
1685 * Name : HMResetEvent
1686 * Purpose : router function for ResetEvent
1687 * Parameters:
1688 * Variables :
1689 * Result :
1690 * Remark :
1691 * Status :
1692 *
1693 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1694 *****************************************************************************/
1695
1696BOOL HMResetEvent(HANDLE hEvent)
1697{
1698 int iIndex; /* index into the handle table */
1699 DWORD dwResult; /* result from the device handler's API */
1700 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1701
1702 /* validate handle */
1703 iIndex = _HMHandleQuery(hEvent); /* get the index */
1704 if (-1 == iIndex) /* error ? */
1705 {
1706 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1707 return (INVALID_HANDLE_ERROR); /* signal failure */
1708 }
1709
1710 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1711 dwResult = pHMHandle->pDeviceHandler->ResetEvent(&pHMHandle->hmHandleData);
1712
1713 return (dwResult); /* deliver return code */
1714}
1715
1716
1717/*****************************************************************************
1718 * Name : HANDLE HMCreateEvent
1719 * Purpose : Wrapper for the CreateEvent() API
1720 * Parameters:
1721 * Variables :
1722 * Result :
1723 * Remark :
1724 * Status :
1725 *
1726 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
1727 *****************************************************************************/
1728
1729HANDLE HMCreateEvent(LPSECURITY_ATTRIBUTES lpsa,
1730 BOOL bManualReset,
1731 BOOL bInitialState,
1732 LPCTSTR lpName)
1733{
1734 int iIndex; /* index into the handle table */
1735 int iIndexNew; /* index into the handle table */
1736 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
1737 PHMHANDLEDATA pHMHandleData;
1738 DWORD rc; /* API return code */
1739
1740
1741 pDeviceHandler = HMGlobals.pHMEvent; /* device is predefined */
1742
1743 iIndexNew = _HMHandleGetFree(); /* get free handle */
1744 if (-1 == iIndexNew) /* oops, no free handles ! */
1745 {
1746 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
1747 return (INVALID_HANDLE_VALUE); /* signal error */
1748 }
1749
1750
1751 /* initialize the complete HMHANDLEDATA structure */
1752 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
1753 pHMHandleData->dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
1754 pHMHandleData->dwAccess = 0;
1755 pHMHandleData->dwShare = 0;
1756 pHMHandleData->dwCreation = 0;
1757 pHMHandleData->dwFlags = 0;
1758 pHMHandleData->lpHandlerData = NULL;
1759
1760
1761 /* we've got to mark the handle as occupied here, since another device */
1762 /* could be created within the device handler -> deadlock */
1763
1764 /* write appropriate entry into the handle table if open succeeded */
1765 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
1766 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
1767
1768 /* call the device handler */
1769 rc = pDeviceHandler->CreateEvent(&TabWin32Handles[iIndexNew].hmHandleData,
1770 lpsa,
1771 bManualReset,
1772 bInitialState,
1773 lpName);
1774 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
1775 {
1776 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
1777 SetLastError(rc); /* Hehe, OS/2 and NT are pretty compatible :) */
1778 return (INVALID_HANDLE_VALUE); /* signal error */
1779 }
1780
1781 return iIndexNew; /* return valid handle */
1782}
1783
1784
1785/*****************************************************************************
1786 * Name : HANDLE HMCreateMutex
1787 * Purpose : Wrapper for the CreateMutex() API
1788 * Parameters:
1789 * Variables :
1790 * Result :
1791 * Remark :
1792 * Status :
1793 *
1794 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
1795 *****************************************************************************/
1796
1797HANDLE HMCreateMutex(LPSECURITY_ATTRIBUTES lpsa,
1798 BOOL bInitialOwner,
1799 LPCTSTR lpName)
1800{
1801 int iIndex; /* index into the handle table */
1802 int iIndexNew; /* index into the handle table */
1803 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
1804 PHMHANDLEDATA pHMHandleData;
1805 DWORD rc; /* API return code */
1806
1807
1808 pDeviceHandler = HMGlobals.pHMMutex; /* device is predefined */
1809
1810 iIndexNew = _HMHandleGetFree(); /* get free handle */
1811 if (-1 == iIndexNew) /* oops, no free handles ! */
1812 {
1813 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
1814 return (INVALID_HANDLE_VALUE); /* signal error */
1815 }
1816
1817
1818 /* initialize the complete HMHANDLEDATA structure */
1819 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
1820 pHMHandleData->dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
1821 pHMHandleData->dwAccess = 0;
1822 pHMHandleData->dwShare = 0;
1823 pHMHandleData->dwCreation = 0;
1824 pHMHandleData->dwFlags = 0;
1825 pHMHandleData->lpHandlerData = NULL;
1826
1827
1828 /* we've got to mark the handle as occupied here, since another device */
1829 /* could be created within the device handler -> deadlock */
1830
1831 /* write appropriate entry into the handle table if open succeeded */
1832 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
1833 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
1834
1835 /* call the device handler */
1836 rc = pDeviceHandler->CreateMutex(&TabWin32Handles[iIndexNew].hmHandleData,
1837 lpsa,
1838 bInitialOwner,
1839 lpName);
1840 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
1841 {
1842 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
1843 SetLastError(rc); /* Hehe, OS/2 and NT are pretty compatible :) */
1844 return (INVALID_HANDLE_VALUE); /* signal error */
1845 }
1846
1847 return iIndexNew; /* return valid handle */
1848}
1849
1850
1851/*****************************************************************************
1852 * Name : HANDLE HMOpenEvent
1853 * Purpose : Wrapper for the OpenEvent() API
1854 * Parameters:
1855 * Variables :
1856 * Result :
1857 * Remark :
1858 * Status :
1859 *
1860 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
1861 *****************************************************************************/
1862
1863HANDLE HMOpenEvent(DWORD fdwAccess,
1864 BOOL fInherit,
1865 LPCTSTR lpName)
1866{
1867 int iIndex; /* index into the handle table */
1868 int iIndexNew; /* index into the handle table */
1869 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
1870 PHMHANDLEDATA pHMHandleData;
1871 DWORD rc; /* API return code */
1872
1873
1874 pDeviceHandler = HMGlobals.pHMEvent; /* device is predefined */
1875
1876 iIndexNew = _HMHandleGetFree(); /* get free handle */
1877 if (-1 == iIndexNew) /* oops, no free handles ! */
1878 {
1879 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
1880 return (INVALID_HANDLE_VALUE); /* signal error */
1881 }
1882
1883
1884 /* initialize the complete HMHANDLEDATA structure */
1885 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
1886 pHMHandleData->dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
1887 pHMHandleData->dwAccess = fdwAccess;
1888 pHMHandleData->dwShare = 0;
1889 pHMHandleData->dwCreation = 0;
1890 pHMHandleData->dwFlags = 0;
1891 pHMHandleData->lpHandlerData = NULL;
1892
1893
1894 /* we've got to mark the handle as occupied here, since another device */
1895 /* could be created within the device handler -> deadlock */
1896
1897 /* write appropriate entry into the handle table if open succeeded */
1898 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
1899 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
1900
1901 /* call the device handler */
1902 rc = pDeviceHandler->OpenEvent(&TabWin32Handles[iIndexNew].hmHandleData,
1903 fInherit,
1904 lpName);
1905 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
1906 {
1907 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
1908 SetLastError(rc); /* Hehe, OS/2 and NT are pretty compatible :) */
1909 return (INVALID_HANDLE_VALUE); /* signal error */
1910 }
1911
1912 return iIndexNew; /* return valid handle */
1913}
1914
1915
1916/*****************************************************************************
1917 * Name : HANDLE HMOpenMutex
1918 * Purpose : Wrapper for the OpenMutex() API
1919 * Parameters:
1920 * Variables :
1921 * Result :
1922 * Remark :
1923 * Status :
1924 *
1925 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
1926 *****************************************************************************/
1927
1928HANDLE HMOpenMutex(DWORD fdwAccess,
1929 BOOL fInherit,
1930 LPCTSTR lpName)
1931{
1932 int iIndex; /* index into the handle table */
1933 int iIndexNew; /* index into the handle table */
1934 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
1935 PHMHANDLEDATA pHMHandleData;
1936 DWORD rc; /* API return code */
1937
1938
1939 pDeviceHandler = HMGlobals.pHMMutex; /* device is predefined */
1940
1941 iIndexNew = _HMHandleGetFree(); /* get free handle */
1942 if (-1 == iIndexNew) /* oops, no free handles ! */
1943 {
1944 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
1945 return (INVALID_HANDLE_VALUE); /* signal error */
1946 }
1947
1948
1949 /* initialize the complete HMHANDLEDATA structure */
1950 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
1951 pHMHandleData->dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
1952 pHMHandleData->dwAccess = fdwAccess;
1953 pHMHandleData->dwShare = 0;
1954 pHMHandleData->dwCreation = 0;
1955 pHMHandleData->dwFlags = 0;
1956 pHMHandleData->lpHandlerData = NULL;
1957
1958
1959 /* we've got to mark the handle as occupied here, since another device */
1960 /* could be created within the device handler -> deadlock */
1961
1962 /* write appropriate entry into the handle table if open succeeded */
1963 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
1964 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
1965
1966 /* call the device handler */
1967 rc = pDeviceHandler->OpenMutex(&TabWin32Handles[iIndexNew].hmHandleData,
1968 fInherit,
1969 lpName);
1970 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
1971 {
1972 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
1973 SetLastError(rc); /* Hehe, OS/2 and NT are pretty compatible :) */
1974 return (INVALID_HANDLE_VALUE); /* signal error */
1975 }
1976
1977 return iIndexNew; /* return valid handle */
1978}
1979
1980
1981/*****************************************************************************
1982 * Name : HANDLE HMCreateSemaphore
1983 * Purpose : Wrapper for the CreateSemaphore() API
1984 * Parameters:
1985 * Variables :
1986 * Result :
1987 * Remark :
1988 * Status :
1989 *
1990 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
1991 *****************************************************************************/
1992
1993HANDLE HMCreateSemaphore(LPSECURITY_ATTRIBUTES lpsa,
1994 LONG lInitialCount,
1995 LONG lMaximumCount,
1996 LPCTSTR lpName)
1997{
1998 int iIndex; /* index into the handle table */
1999 int iIndexNew; /* index into the handle table */
2000 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
2001 PHMHANDLEDATA pHMHandleData;
2002 DWORD rc; /* API return code */
2003
2004
2005 pDeviceHandler = HMGlobals.pHMEvent; /* device is predefined */
2006
2007 iIndexNew = _HMHandleGetFree(); /* get free handle */
2008 if (-1 == iIndexNew) /* oops, no free handles ! */
2009 {
2010 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
2011 return (INVALID_HANDLE_VALUE); /* signal error */
2012 }
2013
2014
2015 /* initialize the complete HMHANDLEDATA structure */
2016 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
2017 pHMHandleData->dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
2018 pHMHandleData->dwAccess = 0;
2019 pHMHandleData->dwShare = 0;
2020 pHMHandleData->dwCreation = 0;
2021 pHMHandleData->dwFlags = 0;
2022 pHMHandleData->lpHandlerData = NULL;
2023
2024
2025 /* we've got to mark the handle as occupied here, since another device */
2026 /* could be created within the device handler -> deadlock */
2027
2028 /* write appropriate entry into the handle table if open succeeded */
2029 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
2030 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
2031
2032 /* call the device handler */
2033 rc = pDeviceHandler->CreateSemaphore(&TabWin32Handles[iIndexNew].hmHandleData,
2034 lpsa,
2035 lInitialCount,
2036 lMaximumCount,
2037 lpName);
2038 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
2039 {
2040 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
2041 SetLastError(rc); /* Hehe, OS/2 and NT are pretty compatible :) */
2042 return (INVALID_HANDLE_VALUE); /* signal error */
2043 }
2044
2045 return iIndexNew; /* return valid handle */
2046}
2047
2048
2049/*****************************************************************************
2050 * Name : HANDLE HMOpenSemaphore
2051 * Purpose : Wrapper for the OpenSemaphore() API
2052 * Parameters:
2053 * Variables :
2054 * Result :
2055 * Remark :
2056 * Status :
2057 *
2058 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
2059 *****************************************************************************/
2060
2061HANDLE HMOpenSemaphore(DWORD fdwAccess,
2062 BOOL fInherit,
2063 LPCTSTR lpName)
2064{
2065 int iIndex; /* index into the handle table */
2066 int iIndexNew; /* index into the handle table */
2067 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
2068 PHMHANDLEDATA pHMHandleData;
2069 DWORD rc; /* API return code */
2070
2071
2072 pDeviceHandler = HMGlobals.pHMMutex; /* device is predefined */
2073
2074 iIndexNew = _HMHandleGetFree(); /* get free handle */
2075 if (-1 == iIndexNew) /* oops, no free handles ! */
2076 {
2077 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
2078 return (INVALID_HANDLE_VALUE); /* signal error */
2079 }
2080
2081
2082 /* initialize the complete HMHANDLEDATA structure */
2083 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
2084 pHMHandleData->dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
2085 pHMHandleData->dwAccess = fdwAccess;
2086 pHMHandleData->dwShare = 0;
2087 pHMHandleData->dwCreation = 0;
2088 pHMHandleData->dwFlags = 0;
2089 pHMHandleData->lpHandlerData = NULL;
2090
2091
2092 /* we've got to mark the handle as occupied here, since another device */
2093 /* could be created within the device handler -> deadlock */
2094
2095 /* write appropriate entry into the handle table if open succeeded */
2096 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
2097 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
2098
2099 /* call the device handler */
2100 rc = pDeviceHandler->OpenSemaphore(&TabWin32Handles[iIndexNew].hmHandleData,
2101 fInherit,
2102 lpName);
2103 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
2104 {
2105 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
2106 SetLastError(rc); /* Hehe, OS/2 and NT are pretty compatible :) */
2107 return (INVALID_HANDLE_VALUE); /* signal error */
2108 }
2109
2110 return iIndexNew; /* return valid handle */
2111}
2112
2113
2114/*****************************************************************************
2115 * Name : HMReleaseSemaphore
2116 * Purpose : router function for ReleaseSemaphore
2117 * Parameters:
2118 * Variables :
2119 * Result :
2120 * Remark :
2121 * Status :
2122 *
2123 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
2124 *****************************************************************************/
2125
2126BOOL HMReleaseSemaphore(HANDLE hEvent,
2127 LONG cReleaseCount,
2128 LPLONG lpPreviousCount)
2129{
2130 int iIndex; /* index into the handle table */
2131 DWORD dwResult; /* result from the device handler's API */
2132 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
2133
2134 /* validate handle */
2135 iIndex = _HMHandleQuery(hEvent); /* get the index */
2136 if (-1 == iIndex) /* error ? */
2137 {
2138 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
2139 return (INVALID_HANDLE_ERROR); /* signal failure */
2140 }
2141
2142 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
2143 dwResult = pHMHandle->pDeviceHandler->ReleaseSemaphore(&pHMHandle->hmHandleData,
2144 cReleaseCount,
2145 lpPreviousCount);
2146
2147 return (dwResult); /* deliver return code */
2148}
2149
2150
2151/*****************************************************************************
2152 * Name : HMWaitForMultipleObjects
2153 * Purpose : router function for WaitForMultipleObjects
2154 * Parameters:
2155 * Variables :
2156 * Result :
2157 * Remark :
2158 * Status :
2159 *
2160 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
2161 *****************************************************************************/
2162
2163DWORD HMWaitForMultipleObjects (DWORD cObjects,
2164 PHANDLE lphObjects,
2165 BOOL fWaitAll,
2166 DWORD dwTimeout)
2167{
2168 ULONG ulIndex;
2169 PHANDLE pArrayOfHandles;
2170 PHANDLE pLoop1 = lphObjects;
2171 PHANDLE pLoop2;
2172 DWORD rc;
2173
2174 // allocate array for handle table
2175 pArrayOfHandles = (PHANDLE)malloc(cObjects * sizeof(HANDLE));
2176 if (pArrayOfHandles == NULL)
2177 {
2178 O32_SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2179 return WAIT_FAILED;
2180 }
2181 else
2182 pLoop2 = pArrayOfHandles;
2183
2184 // convert array to odin handles
2185 for (ulIndex = 0;
2186
2187 ulIndex < cObjects;
2188
2189 ulIndex++,
2190 pLoop1++,
2191 pLoop2++)
2192 {
2193 rc = HMHandleTranslateToOS2 (*pLoop1, // translate handle
2194 pLoop2);
2195
2196 if (rc != NO_ERROR)
2197 {
2198 free (pArrayOfHandles); // free memory
2199 O32_SetLastError(ERROR_INVALID_HANDLE);
2200 return (WAIT_FAILED);
2201 }
2202 }
2203
2204 // OK, now forward to Open32.
2205 // @@@PH: Note this will fail on handles that do NOT belong to Open32
2206 // but to i.e. the console subsystem!
2207 rc = O32_WaitForMultipleObjects(cObjects,
2208 pArrayOfHandles,
2209 fWaitAll,
2210 dwTimeout);
2211
2212 free(pArrayOfHandles); // free memory
2213 return (rc); // OK, done
2214}
2215
2216
2217/*****************************************************************************
2218 * Name : HMWaitForMultipleObjectsEx
2219 * Purpose : router function for WaitForMultipleObjectsEx
2220 * Parameters:
2221 * Variables :
2222 * Result :
2223 * Remark :
2224 * Status :
2225 *
2226 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
2227 *****************************************************************************/
2228
2229DWORD HMWaitForMultipleObjectsEx (DWORD cObjects,
2230 PHANDLE lphObjects,
2231 BOOL fWaitAll,
2232 DWORD dwTimeout,
2233 BOOL fAlertable)
2234{
2235 // @@@PH: Note: fAlertable is ignored !
2236 return (HMWaitForMultipleObjects(cObjects,
2237 lphObjects,
2238 fWaitAll,
2239 dwTimeout));
2240}
2241
Note: See TracBrowser for help on using the repository browser.