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

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

Add: HMDeviceMemMapClass fixed

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