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

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

Fix: HandleManager cosmetic

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