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

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

Extra check in CloseHandle

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