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

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

Fix: removed ODINCRT remains

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