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

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

Fix: SetLastError(ERROR_SUCCESS) for most handle based operations

File size: 98.5 KB
Line 
1/* $Id: HandleManager.cpp,v 1.21 1999-10-27 10:08:32 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 LPSTR 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(LPSTR 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 (LPSTR 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(LPSTR 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 else
719 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
720
721 *desthandle = iIndexNew;
722 return TRUE; /* return valid handle */
723}
724
725/*****************************************************************************
726 * Name : HANDLE HMCreateFile
727 * Purpose : Wrapper for the CreateFile() API
728 * Parameters:
729 * Variables :
730 * Result :
731 * Remark : Fix parameters passed to the HMDeviceManager::CreateFile
732 * Supply access mode and share mode validation routines
733 * Status :
734 *
735 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
736 *****************************************************************************/
737
738HFILE HMCreateFile(LPCSTR lpFileName,
739 DWORD dwDesiredAccess,
740 DWORD dwShareMode,
741 LPSECURITY_ATTRIBUTES lpSecurityAttributes,
742 DWORD dwCreationDisposition,
743 DWORD dwFlagsAndAttributes,
744 HANDLE hTemplateFile)
745{
746 int iIndex; /* index into the handle table */
747 int iIndexNew; /* index into the handle table */
748 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
749 HANDLE hResult;
750 DWORD rc; /* API return code */
751 PHMHANDLEDATA pHMHandleData;
752 HMHANDLEDATA HMHandleTemp; /* temporary buffer for handle data */
753
754 /* create new handle by either lpFileName or hTemplateFile */
755 if (lpFileName == NULL) /* this indicates creation from template */
756 {
757 iIndex = _HMHandleQuery(hTemplateFile); /* query table for template */
758 if (-1 == iIndex) /* this device is unknown to us */
759 {
760 SetLastError (ERROR_INVALID_HANDLE);
761 return INVALID_HANDLE_VALUE;
762 }
763 else
764 {
765 /* to pass to handler */
766 pHMHandleData = &TabWin32Handles[iIndex].hmHandleData;
767 pDeviceHandler = TabWin32Handles[iIndex].pDeviceHandler;
768 }
769 }
770 else
771 {
772 pDeviceHandler = _HMDeviceFind((LPSTR)lpFileName); /* find device */
773
774 if (NULL == pDeviceHandler) /* this name is unknown to us */
775 {
776 SetLastError(ERROR_FILE_NOT_FOUND);
777 return (INVALID_HANDLE_VALUE); /* signal error */
778 }
779 else
780 pHMHandleData = NULL;
781 }
782
783
784 iIndexNew = _HMHandleGetFree(); /* get free handle */
785 if (-1 == iIndexNew) /* oops, no free handles ! */
786 {
787 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
788 return (INVALID_HANDLE_VALUE); /* signal error */
789 }
790
791
792 /* initialize the complete HMHANDLEDATA structure */
793 if (lpFileName == NULL) /* create from template */
794 memcpy (&TabWin32Handles[iIndexNew].hmHandleData,
795 &TabWin32Handles[iIndex].hmHandleData,
796 sizeof(HMHANDLEDATA));
797 else
798 {
799 HMHandleTemp.dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
800 HMHandleTemp.dwAccess = dwDesiredAccess;
801 HMHandleTemp.dwShare = dwShareMode;
802 HMHandleTemp.dwCreation = dwCreationDisposition;
803 HMHandleTemp.dwFlags = dwFlagsAndAttributes;
804 HMHandleTemp.lpHandlerData = NULL;
805 }
806
807 /* we've got to mark the handle as occupied here, since another device */
808 /* could be created within the device handler -> deadlock */
809
810 /* write appropriate entry into the handle table if open succeeded */
811 HMHandleTemp.hHMHandle = iIndexNew;
812 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
813
814 /* now copy back our temporary handle data */
815 memcpy(&TabWin32Handles[iIndexNew].hmHandleData,
816 &HMHandleTemp,
817 sizeof(HMHANDLEDATA));
818
819 rc = pDeviceHandler->CreateFile(lpFileName, /* call the device handler */
820 &HMHandleTemp,
821 lpSecurityAttributes,
822 pHMHandleData);
823
824#ifdef DEBUG_LOCAL
825 dprintf(("KERNEL32/HandleManager:CheckPoint2: %s lpHandlerData=%08xh\n",
826 lpFileName,
827 HMHandleTemp.lpHandlerData));
828#endif
829
830 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
831 {
832 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
833 SetLastError(rc); /* Hehe, OS/2 and NT are pretty compatible :) */
834 return (INVALID_HANDLE_VALUE); /* signal error */
835 }
836 else
837 {
838 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
839
840 /* copy data fields that might have been modified by CreateFile */
841 memcpy(&TabWin32Handles[iIndexNew].hmHandleData,
842 &HMHandleTemp,
843 sizeof(HMHANDLEDATA));
844 }
845
846
847#ifdef DEBUG_LOCAL
848 dprintf(("KERNEL32/HandleManager: CreateFile(%s)=%08xh\n",
849 lpFileName,
850 iIndexNew));
851#endif
852
853 return (HFILE)iIndexNew; /* return valid handle */
854}
855
856
857/*****************************************************************************
858 * Name : HANDLE HMOpenFile
859 * Purpose : Wrapper for the OpenFile() API
860 * Parameters:
861 * Variables :
862 * Result :
863 * Remark : Fix parameters passed to the HMDeviceManager::OpenFile
864 * Supply access mode and share mode validation routines
865 * Status :
866 *
867 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
868 *****************************************************************************/
869
870
871/***********************************************************************
872 * FILE_ConvertOFMode
873 *
874 * Convert OF_* mode into flags for CreateFile.
875 */
876static void FILE_ConvertOFMode( INT mode, DWORD *access, DWORD *sharing )
877{
878 switch(mode & 0x03)
879 {
880 case OF_READ: *access = GENERIC_READ; break;
881 case OF_WRITE: *access = GENERIC_WRITE; break;
882 case OF_READWRITE: *access = GENERIC_READ | GENERIC_WRITE; break;
883 default: *access = 0; break;
884 }
885 switch(mode & 0x70)
886 {
887 case OF_SHARE_EXCLUSIVE: *sharing = 0; break;
888 case OF_SHARE_DENY_WRITE: *sharing = FILE_SHARE_READ; break;
889 case OF_SHARE_DENY_READ: *sharing = FILE_SHARE_WRITE; break;
890 case OF_SHARE_DENY_NONE:
891 case OF_SHARE_COMPAT:
892 default: *sharing = FILE_SHARE_READ | FILE_SHARE_WRITE; break;
893 }
894}
895
896HANDLE HMOpenFile(LPCSTR lpFileName,
897 OFSTRUCT* pOFStruct,
898 UINT fuMode)
899{
900 int iIndex; /* index into the handle table */
901 int iIndexNew; /* index into the handle table */
902 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
903 PHMHANDLEDATA pHMHandleData;
904 DWORD rc; /* API return code */
905
906
907 pDeviceHandler = _HMDeviceFind((LPSTR)lpFileName); /* find device */
908 if (NULL == pDeviceHandler) /* this name is unknown to us */
909 {
910 SetLastError(ERROR_FILE_NOT_FOUND);
911 return (INVALID_HANDLE_VALUE); /* signal error */
912 }
913 else
914 pHMHandleData = NULL;
915
916
917 iIndexNew = _HMHandleGetFree(); /* get free handle */
918 if (-1 == iIndexNew) /* oops, no free handles ! */
919 {
920 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
921 return (INVALID_HANDLE_VALUE); /* signal error */
922 }
923
924
925 /* initialize the complete HMHANDLEDATA structure */
926 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
927 pHMHandleData->dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
928
929 FILE_ConvertOFMode(fuMode, /* map OF_flags */
930 &pHMHandleData->dwAccess,
931 &pHMHandleData->dwShare);
932
933 pHMHandleData->dwCreation = 0;
934 pHMHandleData->dwFlags = 0;
935 pHMHandleData->lpHandlerData = NULL;
936
937
938 /* we've got to mark the handle as occupied here, since another device */
939 /* could be created within the device handler -> deadlock */
940
941 /* write appropriate entry into the handle table if open succeeded */
942 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
943 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
944
945 rc = pDeviceHandler->OpenFile (lpFileName, /* call the device handler */
946 &TabWin32Handles[iIndexNew].hmHandleData,
947 pOFStruct,
948 fuMode);
949
950#ifdef DEBUG_LOCAL
951 dprintf(("KERNEL32/HandleManager:CheckPoint3: %s lpHandlerData=%08xh rc=%08xh\n",
952 lpFileName,
953 &TabWin32Handles[iIndexNew].hmHandleData.lpHandlerData,
954 rc));
955#endif
956
957 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
958 {
959 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
960 SetLastError(rc); /* Hehe, OS/2 and NT are pretty compatible :) */
961 return (INVALID_HANDLE_VALUE); /* signal error */
962 }
963 else
964 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
965
966#ifdef DEBUG_LOCAL
967 dprintf(("KERNEL32/HandleManager: OpenFile(%s)=%08xh\n",
968 lpFileName,
969 hResult));
970#endif
971
972 return iIndexNew; /* return valid handle */
973}
974
975
976
977/*****************************************************************************
978 * Name : HANDLE HMCloseFile
979 * Purpose : Wrapper for the CloseHandle() API
980 * Parameters:
981 * Variables :
982 * Result :
983 * Remark :
984 * Status :
985 *
986 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
987 *****************************************************************************/
988
989BOOL HMCloseHandle(HANDLE hObject)
990{
991 int iIndex; /* index into the handle table */
992 BOOL fResult; /* result from the device handler's CloseHandle() */
993 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
994
995 /* validate handle */
996 iIndex = _HMHandleQuery(hObject); /* get the index */
997 if (-1 == iIndex) /* error ? */
998 {
999 //@@@PH it may occur someone closes e.g. a semaphore handle
1000 // which is not registered through the HandleManager yet.
1001 // so we try to pass on to Open32 instead.
1002 dprintf(("KERNEL32: HandleManager:HMCloseHandle(%08xh) passed on to Open32.\n",
1003 hObject));
1004
1005 fResult = O32_CloseHandle(hObject);
1006 return (fResult);
1007
1008 //SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1009 //return (FALSE); /* signal failure */
1010 }
1011
1012 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1013 //SvL: Check if pDeviceHandler is set
1014 if (pHMHandle->pDeviceHandler)
1015 {
1016 fResult = pHMHandle->pDeviceHandler->CloseHandle(&pHMHandle->hmHandleData);
1017 }
1018 else
1019 {
1020 dprintf(("HMCloseHAndle(%08xh): pDeviceHandler not set", hObject));
1021 fResult = TRUE;
1022 }
1023
1024 if (fResult == TRUE) /* remove handle if close succeeded */
1025 {
1026 pHMHandle->hmHandleData.hHMHandle = 0; /* mark handle as free */
1027 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
1028 }
1029
1030 return (fResult); /* deliver return code */
1031}
1032
1033
1034/*****************************************************************************
1035 * Name : HANDLE HMReadFile
1036 * Purpose : Wrapper for the ReadHandle() API
1037 * Parameters:
1038 * Variables :
1039 * Result :
1040 * Remark :
1041 * Status :
1042 *
1043 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
1044 *****************************************************************************/
1045
1046BOOL HMReadFile(HANDLE hFile,
1047 LPVOID lpBuffer,
1048 DWORD nNumberOfBytesToRead,
1049 LPDWORD lpNumberOfBytesRead,
1050 LPOVERLAPPED lpOverlapped)
1051{
1052 int iIndex; /* index into the handle table */
1053 BOOL fResult; /* result from the device handler's CloseHandle() */
1054 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1055
1056 /* validate handle */
1057 iIndex = _HMHandleQuery(hFile); /* get the index */
1058 if (-1 == iIndex) /* error ? */
1059 {
1060 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1061 return (FALSE); /* signal failure */
1062 }
1063
1064 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1065 fResult = pHMHandle->pDeviceHandler->ReadFile(&pHMHandle->hmHandleData,
1066 lpBuffer,
1067 nNumberOfBytesToRead,
1068 lpNumberOfBytesRead,
1069 lpOverlapped);
1070
1071 return (fResult); /* deliver return code */
1072}
1073
1074
1075/*****************************************************************************
1076 * Name : HANDLE HMWriteFile
1077 * Purpose : Wrapper for the WriteHandle() API
1078 * Parameters:
1079 * Variables :
1080 * Result :
1081 * Remark :
1082 * Status :
1083 *
1084 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
1085 *****************************************************************************/
1086
1087BOOL HMWriteFile(HANDLE hFile,
1088 LPCVOID lpBuffer,
1089 DWORD nNumberOfBytesToWrite,
1090 LPDWORD lpNumberOfBytesWritten,
1091 LPOVERLAPPED lpOverlapped)
1092{
1093 int iIndex; /* index into the handle table */
1094 BOOL fResult; /* result from the device handler's CloseHandle() */
1095 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1096
1097 /* validate handle */
1098 iIndex = _HMHandleQuery(hFile); /* get the index */
1099 if (-1 == iIndex) /* error ? */
1100 {
1101 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1102 return (FALSE); /* signal failure */
1103 }
1104
1105 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1106 fResult = pHMHandle->pDeviceHandler->WriteFile(&pHMHandle->hmHandleData,
1107 lpBuffer,
1108 nNumberOfBytesToWrite,
1109 lpNumberOfBytesWritten,
1110 lpOverlapped);
1111
1112 return (fResult); /* deliver return code */
1113}
1114
1115
1116/*****************************************************************************
1117 * Name : HANDLE HMGetFileType
1118 * Purpose : Wrapper for the GetFileType() API
1119 * Parameters:
1120 * Variables :
1121 * Result :
1122 * Remark :
1123 * Status :
1124 *
1125 * Author : Patrick Haller [Wed, 1998/02/12 13:37]
1126 *****************************************************************************/
1127
1128DWORD HMGetFileType(HANDLE hFile)
1129{
1130 int iIndex; /* index into the handle table */
1131 DWORD dwResult; /* result from the device handler's API */
1132 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1133
1134 /* validate handle */
1135 iIndex = _HMHandleQuery(hFile); /* get the index */
1136 if (-1 == iIndex) /* error ? */
1137 {
1138 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1139 return (INVALID_HANDLE_ERROR); /* signal failure */
1140 }
1141
1142 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1143 dwResult = pHMHandle->pDeviceHandler->GetFileType(&pHMHandle->hmHandleData);
1144
1145 return (dwResult); /* deliver return code */
1146}
1147
1148
1149/*****************************************************************************
1150 * Name : HMDeviceHandler::_DeviceReuqest
1151 * Purpose : entry method for special request functions
1152 * Parameters: ULONG ulRequestCode
1153 * various parameters as required
1154 * Variables :
1155 * Result :
1156 * Remark : the standard behaviour is to return an error code for non-
1157 * existant request codes
1158 * Status :
1159 *
1160 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
1161 *****************************************************************************/
1162
1163DWORD HMDeviceRequest (HANDLE hFile,
1164 ULONG ulRequestCode,
1165 ULONG arg1,
1166 ULONG arg2,
1167 ULONG arg3,
1168 ULONG arg4)
1169{
1170 int iIndex; /* index into the handle table */
1171 DWORD dwResult; /* result from the device handler's API */
1172 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1173
1174 /* validate handle */
1175 iIndex = _HMHandleQuery(hFile); /* get the index */
1176 if (-1 == iIndex) /* error ? */
1177 {
1178 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1179 return (INVALID_HANDLE_ERROR); /* signal failure */
1180 }
1181
1182 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1183 dwResult = pHMHandle->pDeviceHandler->_DeviceRequest(&pHMHandle->hmHandleData,
1184 ulRequestCode,
1185 arg1,
1186 arg2,
1187 arg3,
1188 arg4);
1189
1190 return (dwResult); /* deliver return code */
1191}
1192
1193
1194/*****************************************************************************
1195 * Name : HMDeviceHandler::GetFileInformationByHandle
1196 * Purpose : router function for GetFileInformationByHandle
1197 * Parameters:
1198 * Variables :
1199 * Result :
1200 * Remark :
1201 * Status :
1202 *
1203 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1204 *****************************************************************************/
1205
1206BOOL HMGetFileInformationByHandle (HANDLE hFile,
1207 BY_HANDLE_FILE_INFORMATION *pHFI)
1208{
1209 int iIndex; /* index into the handle table */
1210 DWORD dwResult; /* result from the device handler's API */
1211 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1212
1213 /* validate handle */
1214 iIndex = _HMHandleQuery(hFile); /* get the index */
1215 if (-1 == iIndex) /* error ? */
1216 {
1217 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1218 return (INVALID_HANDLE_ERROR); /* signal failure */
1219 }
1220
1221 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1222 dwResult = pHMHandle->pDeviceHandler->GetFileInformationByHandle(&pHMHandle->hmHandleData,
1223 pHFI);
1224
1225 return (dwResult); /* deliver return code */
1226}
1227
1228
1229/*****************************************************************************
1230 * Name : HMDeviceHandler::SetEndOfFile
1231 * Purpose : router function for SetEndOfFile
1232 * Parameters:
1233 * Variables :
1234 * Result :
1235 * Remark :
1236 * Status :
1237 *
1238 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1239 *****************************************************************************/
1240
1241BOOL HMSetEndOfFile (HANDLE hFile)
1242{
1243 int iIndex; /* index into the handle table */
1244 BOOL bResult; /* result from the device handler's API */
1245 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1246
1247 /* validate handle */
1248 iIndex = _HMHandleQuery(hFile); /* get the index */
1249 if (-1 == iIndex) /* error ? */
1250 {
1251 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1252 return (INVALID_HANDLE_ERROR); /* signal failure */
1253 }
1254
1255 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1256 bResult = pHMHandle->pDeviceHandler->SetEndOfFile(&pHMHandle->hmHandleData);
1257
1258 return (bResult); /* deliver return code */
1259}
1260
1261
1262/*****************************************************************************
1263 * Name : HMDeviceHandler::SetFileTime
1264 * Purpose : router function for SetFileTime
1265 * Parameters:
1266 * Variables :
1267 * Result :
1268 * Remark :
1269 * Status :
1270 *
1271 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1272 *****************************************************************************/
1273
1274BOOL HMSetFileTime (HANDLE hFile,
1275 const FILETIME *pFT1,
1276 const FILETIME *pFT2,
1277 const FILETIME *pFT3)
1278{
1279 int iIndex; /* index into the handle table */
1280 BOOL bResult; /* result from the device handler's API */
1281 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1282
1283 /* validate handle */
1284 iIndex = _HMHandleQuery(hFile); /* get the index */
1285 if (-1 == iIndex) /* error ? */
1286 {
1287 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1288 return (INVALID_HANDLE_ERROR); /* signal failure */
1289 }
1290
1291 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1292 bResult = pHMHandle->pDeviceHandler->SetFileTime(&pHMHandle->hmHandleData,
1293 (LPFILETIME)pFT1,
1294 (LPFILETIME)pFT2,
1295 (LPFILETIME)pFT3);
1296
1297 return (bResult); /* deliver return code */
1298}
1299
1300
1301/*****************************************************************************
1302 * Name : HMDeviceHandler::GetFileSize
1303 * Purpose : router function for GetFileSize
1304 * Parameters:
1305 * Variables :
1306 * Result :
1307 * Remark :
1308 * Status :
1309 *
1310 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1311 *****************************************************************************/
1312
1313DWORD HMGetFileSize (HANDLE hFile,
1314 PDWORD pSize)
1315{
1316 int iIndex; /* index into the handle table */
1317 DWORD dwResult; /* result from the device handler's API */
1318 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1319
1320 /* validate handle */
1321 iIndex = _HMHandleQuery(hFile); /* get the index */
1322 if (-1 == iIndex) /* error ? */
1323 {
1324 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1325 return (INVALID_HANDLE_ERROR); /* signal failure */
1326 }
1327
1328 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1329 dwResult = pHMHandle->pDeviceHandler->GetFileSize(&pHMHandle->hmHandleData,
1330 pSize);
1331
1332 return (dwResult); /* deliver return code */
1333}
1334
1335
1336/*****************************************************************************
1337 * Name : HMDeviceHandler::SetFilePointer
1338 * Purpose : router function for SetFilePointer
1339 * Parameters:
1340 * Variables :
1341 * Result :
1342 * Remark :
1343 * Status :
1344 *
1345 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1346 *****************************************************************************/
1347
1348DWORD HMSetFilePointer (HANDLE hFile,
1349 LONG lDistanceToMove,
1350 PLONG lpDistanceToMoveHigh,
1351 DWORD dwMoveMethod)
1352{
1353 int iIndex; /* index into the handle table */
1354 DWORD dwResult; /* result from the device handler's API */
1355 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1356
1357 /* validate handle */
1358 iIndex = _HMHandleQuery(hFile); /* get the index */
1359 if (-1 == iIndex) /* error ? */
1360 {
1361 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1362 return (INVALID_HANDLE_ERROR); /* signal failure */
1363 }
1364
1365 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1366 dwResult = pHMHandle->pDeviceHandler->SetFilePointer(&pHMHandle->hmHandleData,
1367 lDistanceToMove,
1368 lpDistanceToMoveHigh,
1369 dwMoveMethod);
1370
1371 return (dwResult); /* deliver return code */
1372}
1373
1374
1375/*****************************************************************************
1376 * Name : HMDeviceHandler::LockFile
1377 * Purpose : router function for LockFile
1378 * Parameters:
1379 * Variables :
1380 * Result :
1381 * Remark :
1382 * Status :
1383 *
1384 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1385 *****************************************************************************/
1386
1387BOOL HMLockFile (HFILE hFile,
1388 DWORD arg2,
1389 DWORD arg3,
1390 DWORD arg4,
1391 DWORD arg5)
1392{
1393 int iIndex; /* index into the handle table */
1394 DWORD dwResult; /* result from the device handler's API */
1395 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1396
1397 /* validate handle */
1398 iIndex = _HMHandleQuery(hFile); /* get the index */
1399 if (-1 == iIndex) /* error ? */
1400 {
1401 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1402 return (INVALID_HANDLE_ERROR); /* signal failure */
1403 }
1404
1405 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1406 dwResult = pHMHandle->pDeviceHandler->LockFile(&pHMHandle->hmHandleData,
1407 arg2,
1408 arg3,
1409 arg4,
1410 arg5);
1411
1412 return (dwResult); /* deliver return code */
1413}
1414
1415
1416/*****************************************************************************
1417 * Name : HMDeviceHandler::LockFileEx
1418 * Purpose : router function for LockFileEx
1419 * Parameters:
1420 * Variables :
1421 * Result :
1422 * Remark :
1423 * Status :
1424 *
1425 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1426 *****************************************************************************/
1427
1428BOOL HMLockFileEx(HANDLE hFile,
1429 DWORD dwFlags,
1430 DWORD dwReserved,
1431 DWORD nNumberOfBytesToLockLow,
1432 DWORD nNumberOfBytesToLockHigh,
1433 LPOVERLAPPED lpOverlapped)
1434{
1435 int iIndex; /* index into the handle table */
1436 DWORD dwResult; /* result from the device handler's API */
1437 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1438
1439 /* validate handle */
1440 iIndex = _HMHandleQuery(hFile); /* get the index */
1441 if (-1 == iIndex) /* error ? */
1442 {
1443 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1444 return (INVALID_HANDLE_ERROR); /* signal failure */
1445 }
1446
1447 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1448 dwResult = pHMHandle->pDeviceHandler->LockFileEx(&pHMHandle->hmHandleData,
1449 dwFlags,
1450 dwReserved,
1451 nNumberOfBytesToLockLow,
1452 nNumberOfBytesToLockHigh,
1453 lpOverlapped);
1454
1455 return (dwResult); /* deliver return code */
1456}
1457
1458
1459
1460/*****************************************************************************
1461 * Name : HMDeviceHandler::UnlockFile
1462 * Purpose : router function for UnlockFile
1463 * Parameters:
1464 * Variables :
1465 * Result :
1466 * Remark :
1467 * Status :
1468 *
1469 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1470 *****************************************************************************/
1471
1472BOOL HMUnlockFile (HFILE hFile,
1473 DWORD arg2,
1474 DWORD arg3,
1475 DWORD arg4,
1476 DWORD arg5)
1477{
1478 int iIndex; /* index into the handle table */
1479 DWORD dwResult; /* result from the device handler's API */
1480 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1481
1482 /* validate handle */
1483 iIndex = _HMHandleQuery(hFile); /* get the index */
1484 if (-1 == iIndex) /* error ? */
1485 {
1486 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1487 return (INVALID_HANDLE_ERROR); /* signal failure */
1488 }
1489
1490 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1491 dwResult = pHMHandle->pDeviceHandler->UnlockFile(&pHMHandle->hmHandleData,
1492 arg2,
1493 arg3,
1494 arg4,
1495 arg5);
1496
1497 return (dwResult); /* deliver return code */
1498}
1499
1500
1501/*****************************************************************************
1502 * Name : HMDeviceHandler::UnlockFileEx
1503 * Purpose : router function for UnlockFileEx
1504 * Parameters:
1505 * Variables :
1506 * Result :
1507 * Remark :
1508 * Status :
1509 *
1510 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1511 *****************************************************************************/
1512
1513BOOL HMUnlockFileEx(HANDLE hFile,
1514 DWORD dwFlags,
1515 DWORD dwReserved,
1516 DWORD nNumberOfBytesToLockLow,
1517 DWORD nNumberOfBytesToLockHigh,
1518 LPOVERLAPPED lpOverlapped)
1519{
1520 int iIndex; /* index into the handle table */
1521 DWORD dwResult; /* result from the device handler's API */
1522 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1523
1524 /* validate handle */
1525 iIndex = _HMHandleQuery(hFile); /* get the index */
1526 if (-1 == iIndex) /* error ? */
1527 {
1528 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1529 return (INVALID_HANDLE_ERROR); /* signal failure */
1530 }
1531
1532 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1533 dwResult = pHMHandle->pDeviceHandler->UnlockFileEx(&pHMHandle->hmHandleData,
1534 dwFlags,
1535 dwReserved,
1536 nNumberOfBytesToLockLow,
1537 nNumberOfBytesToLockHigh,
1538 lpOverlapped);
1539
1540 return (dwResult); /* deliver return code */
1541}
1542
1543
1544/*****************************************************************************
1545 * Name : HMDeviceHandler::WaitForSingleObject
1546 * Purpose : router function for WaitForSingleObject
1547 * Parameters:
1548 * Variables :
1549 * Result :
1550 * Remark :
1551 * Status :
1552 *
1553 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1554 *****************************************************************************/
1555
1556DWORD HMWaitForSingleObject(HANDLE hObject,
1557 DWORD dwTimeout)
1558{
1559 int iIndex; /* index into the handle table */
1560 DWORD dwResult; /* result from the device handler's API */
1561 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1562
1563 /* validate handle */
1564 iIndex = _HMHandleQuery(hObject); /* get the index */
1565 if (-1 == iIndex) /* error ? */
1566 {
1567 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1568 return (INVALID_HANDLE_ERROR); /* signal failure */
1569 }
1570
1571 // @@@PH Problem: wrong class (base class) is called instead of
1572 // open32 class ?! Why ?!
1573 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1574 dwResult = pHMHandle->pDeviceHandler->WaitForSingleObject(&pHMHandle->hmHandleData,
1575 dwTimeout);
1576
1577 return (dwResult); /* deliver return code */
1578}
1579
1580
1581/*****************************************************************************
1582 * Name : HMDeviceHandler::WaitForSingleObjectEx
1583 * Purpose : router function for WaitForSingleObjectEx
1584 * Parameters:
1585 * Variables :
1586 * Result :
1587 * Remark :
1588 * Status :
1589 *
1590 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1591 *****************************************************************************/
1592
1593DWORD HMWaitForSingleObjectEx(HANDLE hObject,
1594 DWORD dwTimeout,
1595 BOOL fAlertable)
1596{
1597 int iIndex; /* index into the handle table */
1598 DWORD dwResult; /* result from the device handler's API */
1599 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1600
1601 /* validate handle */
1602 iIndex = _HMHandleQuery(hObject); /* get the index */
1603 if (-1 == iIndex) /* error ? */
1604 {
1605 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1606 return (INVALID_HANDLE_ERROR); /* signal failure */
1607 }
1608
1609 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1610 dwResult = pHMHandle->pDeviceHandler->WaitForSingleObjectEx(&pHMHandle->hmHandleData,
1611 dwTimeout,
1612 fAlertable);
1613
1614 return (dwResult); /* deliver return code */
1615}
1616
1617
1618/*****************************************************************************
1619 * Name : HMDeviceHandler::FlushFileBuffers
1620 * Purpose : router function for FlushFileBuffers
1621 * Parameters:
1622 * Variables :
1623 * Result :
1624 * Remark :
1625 * Status :
1626 *
1627 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1628 *****************************************************************************/
1629
1630BOOL HMFlushFileBuffers(HANDLE hFile)
1631{
1632 int iIndex; /* index into the handle table */
1633 DWORD dwResult; /* result from the device handler's API */
1634 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1635
1636 /* validate handle */
1637 iIndex = _HMHandleQuery(hFile); /* get the index */
1638 if (-1 == iIndex) /* error ? */
1639 {
1640 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1641 return (INVALID_HANDLE_ERROR); /* signal failure */
1642 }
1643
1644 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1645 dwResult = pHMHandle->pDeviceHandler->FlushFileBuffers(&pHMHandle->hmHandleData);
1646
1647 return (dwResult); /* deliver return code */
1648}
1649
1650
1651/*****************************************************************************
1652 * Name : HMDeviceHandler::GetOverlappedResult
1653 * Purpose : router function for GetOverlappedResult
1654 * Parameters:
1655 * Variables :
1656 * Result :
1657 * Remark :
1658 * Status :
1659 *
1660 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1661 *****************************************************************************/
1662
1663BOOL HMGetOverlappedResult(HANDLE hObject,
1664 LPOVERLAPPED lpOverlapped,
1665 LPDWORD arg3,
1666 BOOL arg4)
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(hObject); /* 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->GetOverlappedResult(&pHMHandle->hmHandleData,
1682 lpOverlapped,
1683 arg3,
1684 arg4);
1685
1686 return (dwResult); /* deliver return code */
1687}
1688
1689
1690/*****************************************************************************
1691 * Name : HMReleaseMutex
1692 * Purpose : router function for ReleaseMutex
1693 * Parameters:
1694 * Variables :
1695 * Result :
1696 * Remark :
1697 * Status :
1698 *
1699 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1700 *****************************************************************************/
1701
1702BOOL HMReleaseMutex(HANDLE hObject)
1703{
1704 int iIndex; /* index into the handle table */
1705 DWORD dwResult; /* result from the device handler's API */
1706 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1707
1708 /* validate handle */
1709 iIndex = _HMHandleQuery(hObject); /* get the index */
1710 if (-1 == iIndex) /* error ? */
1711 {
1712 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1713 return (INVALID_HANDLE_ERROR); /* signal failure */
1714 }
1715
1716 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1717 dwResult = pHMHandle->pDeviceHandler->ReleaseMutex(&pHMHandle->hmHandleData);
1718
1719 return (dwResult); /* deliver return code */
1720}
1721
1722
1723/*****************************************************************************
1724 * Name : HMSetEvent
1725 * Purpose : router function for SetEvent
1726 * Parameters:
1727 * Variables :
1728 * Result :
1729 * Remark :
1730 * Status :
1731 *
1732 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1733 *****************************************************************************/
1734
1735BOOL HMSetEvent(HANDLE hEvent)
1736{
1737 int iIndex; /* index into the handle table */
1738 DWORD dwResult; /* result from the device handler's API */
1739 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1740
1741 /* validate handle */
1742 iIndex = _HMHandleQuery(hEvent); /* get the index */
1743 if (-1 == iIndex) /* error ? */
1744 {
1745 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1746 return (INVALID_HANDLE_ERROR); /* signal failure */
1747 }
1748
1749 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1750 dwResult = pHMHandle->pDeviceHandler->SetEvent(&pHMHandle->hmHandleData);
1751
1752 return (dwResult); /* deliver return code */
1753}
1754
1755
1756/*****************************************************************************
1757 * Name : HMPulseEvent
1758 * Purpose : router function for PulseEvent
1759 * Parameters:
1760 * Variables :
1761 * Result :
1762 * Remark :
1763 * Status :
1764 *
1765 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1766 *****************************************************************************/
1767
1768BOOL HMPulseEvent(HANDLE hEvent)
1769{
1770 int iIndex; /* index into the handle table */
1771 DWORD dwResult; /* result from the device handler's API */
1772 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1773
1774 /* validate handle */
1775 iIndex = _HMHandleQuery(hEvent); /* get the index */
1776 if (-1 == iIndex) /* error ? */
1777 {
1778 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1779 return (INVALID_HANDLE_ERROR); /* signal failure */
1780 }
1781
1782 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1783 dwResult = pHMHandle->pDeviceHandler->PulseEvent(&pHMHandle->hmHandleData);
1784
1785 return (dwResult); /* deliver return code */
1786}
1787
1788
1789/*****************************************************************************
1790 * Name : HMResetEvent
1791 * Purpose : router function for ResetEvent
1792 * Parameters:
1793 * Variables :
1794 * Result :
1795 * Remark :
1796 * Status :
1797 *
1798 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1799 *****************************************************************************/
1800
1801BOOL HMResetEvent(HANDLE hEvent)
1802{
1803 int iIndex; /* index into the handle table */
1804 DWORD dwResult; /* result from the device handler's API */
1805 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1806
1807 /* validate handle */
1808 iIndex = _HMHandleQuery(hEvent); /* get the index */
1809 if (-1 == iIndex) /* error ? */
1810 {
1811 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1812 return (INVALID_HANDLE_ERROR); /* signal failure */
1813 }
1814
1815 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1816 dwResult = pHMHandle->pDeviceHandler->ResetEvent(&pHMHandle->hmHandleData);
1817
1818 return (dwResult); /* deliver return code */
1819}
1820
1821
1822/*****************************************************************************
1823 * Name : HANDLE HMCreateEvent
1824 * Purpose : Wrapper for the CreateEvent() API
1825 * Parameters:
1826 * Variables :
1827 * Result :
1828 * Remark :
1829 * Status :
1830 *
1831 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
1832 *****************************************************************************/
1833
1834HANDLE HMCreateEvent(LPSECURITY_ATTRIBUTES lpsa,
1835 BOOL bManualReset,
1836 BOOL bInitialState,
1837 LPCTSTR lpName)
1838{
1839 int iIndex; /* index into the handle table */
1840 int iIndexNew; /* index into the handle table */
1841 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
1842 PHMHANDLEDATA pHMHandleData;
1843 DWORD rc; /* API return code */
1844
1845
1846 pDeviceHandler = HMGlobals.pHMEvent; /* device is predefined */
1847
1848 iIndexNew = _HMHandleGetFree(); /* get free handle */
1849 if (-1 == iIndexNew) /* oops, no free handles ! */
1850 {
1851 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
1852 return (INVALID_HANDLE_VALUE); /* signal error */
1853 }
1854
1855
1856 /* initialize the complete HMHANDLEDATA structure */
1857 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
1858 pHMHandleData->dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
1859 pHMHandleData->dwAccess = 0;
1860 pHMHandleData->dwShare = 0;
1861 pHMHandleData->dwCreation = 0;
1862 pHMHandleData->dwFlags = 0;
1863 pHMHandleData->lpHandlerData = NULL;
1864
1865
1866 /* we've got to mark the handle as occupied here, since another device */
1867 /* could be created within the device handler -> deadlock */
1868
1869 /* write appropriate entry into the handle table if open succeeded */
1870 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
1871 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
1872
1873 /* call the device handler */
1874 rc = pDeviceHandler->CreateEvent(&TabWin32Handles[iIndexNew].hmHandleData,
1875 lpsa,
1876 bManualReset,
1877 bInitialState,
1878 lpName);
1879 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
1880 {
1881 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
1882 SetLastError(rc); /* Hehe, OS/2 and NT are pretty compatible :) */
1883 return (INVALID_HANDLE_VALUE); /* signal error */
1884 }
1885 else
1886 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
1887
1888 return iIndexNew; /* return valid handle */
1889}
1890
1891
1892/*****************************************************************************
1893 * Name : HANDLE HMCreateMutex
1894 * Purpose : Wrapper for the CreateMutex() API
1895 * Parameters:
1896 * Variables :
1897 * Result :
1898 * Remark :
1899 * Status :
1900 *
1901 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
1902 *****************************************************************************/
1903
1904HANDLE HMCreateMutex(LPSECURITY_ATTRIBUTES lpsa,
1905 BOOL bInitialOwner,
1906 LPCTSTR lpName)
1907{
1908 int iIndex; /* index into the handle table */
1909 int iIndexNew; /* index into the handle table */
1910 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
1911 PHMHANDLEDATA pHMHandleData;
1912 DWORD rc; /* API return code */
1913
1914
1915 pDeviceHandler = HMGlobals.pHMMutex; /* device is predefined */
1916
1917 iIndexNew = _HMHandleGetFree(); /* get free handle */
1918 if (-1 == iIndexNew) /* oops, no free handles ! */
1919 {
1920 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
1921 return (INVALID_HANDLE_VALUE); /* signal error */
1922 }
1923
1924
1925 /* initialize the complete HMHANDLEDATA structure */
1926 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
1927 pHMHandleData->dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
1928 pHMHandleData->dwAccess = 0;
1929 pHMHandleData->dwShare = 0;
1930 pHMHandleData->dwCreation = 0;
1931 pHMHandleData->dwFlags = 0;
1932 pHMHandleData->lpHandlerData = NULL;
1933
1934
1935 /* we've got to mark the handle as occupied here, since another device */
1936 /* could be created within the device handler -> deadlock */
1937
1938 /* write appropriate entry into the handle table if open succeeded */
1939 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
1940 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
1941
1942 /* call the device handler */
1943 rc = pDeviceHandler->CreateMutex(&TabWin32Handles[iIndexNew].hmHandleData,
1944 lpsa,
1945 bInitialOwner,
1946 lpName);
1947 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
1948 {
1949 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
1950 SetLastError(rc); /* Hehe, OS/2 and NT are pretty compatible :) */
1951 return (INVALID_HANDLE_VALUE); /* signal error */
1952 }
1953 else
1954 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
1955
1956 return iIndexNew; /* return valid handle */
1957}
1958
1959
1960/*****************************************************************************
1961 * Name : HANDLE HMOpenEvent
1962 * Purpose : Wrapper for the OpenEvent() API
1963 * Parameters:
1964 * Variables :
1965 * Result :
1966 * Remark :
1967 * Status :
1968 *
1969 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
1970 *****************************************************************************/
1971
1972HANDLE HMOpenEvent(DWORD fdwAccess,
1973 BOOL fInherit,
1974 LPCTSTR lpName)
1975{
1976 int iIndex; /* index into the handle table */
1977 int iIndexNew; /* index into the handle table */
1978 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
1979 PHMHANDLEDATA pHMHandleData;
1980 DWORD rc; /* API return code */
1981
1982
1983 pDeviceHandler = HMGlobals.pHMEvent; /* device is predefined */
1984
1985 iIndexNew = _HMHandleGetFree(); /* get free handle */
1986 if (-1 == iIndexNew) /* oops, no free handles ! */
1987 {
1988 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
1989 return (INVALID_HANDLE_VALUE); /* signal error */
1990 }
1991
1992
1993 /* initialize the complete HMHANDLEDATA structure */
1994 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
1995 pHMHandleData->dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
1996 pHMHandleData->dwAccess = fdwAccess;
1997 pHMHandleData->dwShare = 0;
1998 pHMHandleData->dwCreation = 0;
1999 pHMHandleData->dwFlags = 0;
2000 pHMHandleData->lpHandlerData = NULL;
2001
2002
2003 /* we've got to mark the handle as occupied here, since another device */
2004 /* could be created within the device handler -> deadlock */
2005
2006 /* write appropriate entry into the handle table if open succeeded */
2007 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
2008 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
2009
2010 /* call the device handler */
2011 rc = pDeviceHandler->OpenEvent(&TabWin32Handles[iIndexNew].hmHandleData,
2012 fInherit,
2013 lpName);
2014 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
2015 {
2016 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
2017 SetLastError(rc); /* Hehe, OS/2 and NT are pretty compatible :) */
2018 return (INVALID_HANDLE_VALUE); /* signal error */
2019 }
2020 else
2021 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
2022
2023 return iIndexNew; /* return valid handle */
2024}
2025
2026
2027/*****************************************************************************
2028 * Name : HANDLE HMOpenMutex
2029 * Purpose : Wrapper for the OpenMutex() API
2030 * Parameters:
2031 * Variables :
2032 * Result :
2033 * Remark :
2034 * Status :
2035 *
2036 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
2037 *****************************************************************************/
2038
2039HANDLE HMOpenMutex(DWORD fdwAccess,
2040 BOOL fInherit,
2041 LPCTSTR lpName)
2042{
2043 int iIndex; /* index into the handle table */
2044 int iIndexNew; /* index into the handle table */
2045 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
2046 PHMHANDLEDATA pHMHandleData;
2047 DWORD rc; /* API return code */
2048
2049
2050 pDeviceHandler = HMGlobals.pHMMutex; /* device is predefined */
2051
2052 iIndexNew = _HMHandleGetFree(); /* get free handle */
2053 if (-1 == iIndexNew) /* oops, no free handles ! */
2054 {
2055 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
2056 return (INVALID_HANDLE_VALUE); /* signal error */
2057 }
2058
2059
2060 /* initialize the complete HMHANDLEDATA structure */
2061 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
2062 pHMHandleData->dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
2063 pHMHandleData->dwAccess = fdwAccess;
2064 pHMHandleData->dwShare = 0;
2065 pHMHandleData->dwCreation = 0;
2066 pHMHandleData->dwFlags = 0;
2067 pHMHandleData->lpHandlerData = NULL;
2068
2069
2070 /* we've got to mark the handle as occupied here, since another device */
2071 /* could be created within the device handler -> deadlock */
2072
2073 /* write appropriate entry into the handle table if open succeeded */
2074 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
2075 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
2076
2077 /* call the device handler */
2078 rc = pDeviceHandler->OpenMutex(&TabWin32Handles[iIndexNew].hmHandleData,
2079 fInherit,
2080 lpName);
2081 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
2082 {
2083 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
2084 SetLastError(rc); /* Hehe, OS/2 and NT are pretty compatible :) */
2085 return (INVALID_HANDLE_VALUE); /* signal error */
2086 }
2087 else
2088 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
2089
2090 return iIndexNew; /* return valid handle */
2091}
2092
2093
2094/*****************************************************************************
2095 * Name : HANDLE HMCreateSemaphore
2096 * Purpose : Wrapper for the CreateSemaphore() API
2097 * Parameters:
2098 * Variables :
2099 * Result :
2100 * Remark :
2101 * Status :
2102 *
2103 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
2104 *****************************************************************************/
2105
2106HANDLE HMCreateSemaphore(LPSECURITY_ATTRIBUTES lpsa,
2107 LONG lInitialCount,
2108 LONG lMaximumCount,
2109 LPCTSTR lpName)
2110{
2111 int iIndex; /* index into the handle table */
2112 int iIndexNew; /* index into the handle table */
2113 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
2114 PHMHANDLEDATA pHMHandleData;
2115 DWORD rc; /* API return code */
2116
2117
2118 pDeviceHandler = HMGlobals.pHMEvent; /* device is predefined */
2119
2120 iIndexNew = _HMHandleGetFree(); /* get free handle */
2121 if (-1 == iIndexNew) /* oops, no free handles ! */
2122 {
2123 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
2124 return (INVALID_HANDLE_VALUE); /* signal error */
2125 }
2126
2127
2128 /* initialize the complete HMHANDLEDATA structure */
2129 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
2130 pHMHandleData->dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
2131 pHMHandleData->dwAccess = 0;
2132 pHMHandleData->dwShare = 0;
2133 pHMHandleData->dwCreation = 0;
2134 pHMHandleData->dwFlags = 0;
2135 pHMHandleData->lpHandlerData = NULL;
2136
2137
2138 /* we've got to mark the handle as occupied here, since another device */
2139 /* could be created within the device handler -> deadlock */
2140
2141 /* write appropriate entry into the handle table if open succeeded */
2142 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
2143 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
2144
2145 /* call the device handler */
2146 rc = pDeviceHandler->CreateSemaphore(&TabWin32Handles[iIndexNew].hmHandleData,
2147 lpsa,
2148 lInitialCount,
2149 lMaximumCount,
2150 lpName);
2151 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
2152 {
2153 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
2154 SetLastError(rc); /* Hehe, OS/2 and NT are pretty compatible :) */
2155 return (INVALID_HANDLE_VALUE); /* signal error */
2156 }
2157 else
2158 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
2159
2160 return iIndexNew; /* return valid handle */
2161}
2162
2163
2164/*****************************************************************************
2165 * Name : HANDLE HMOpenSemaphore
2166 * Purpose : Wrapper for the OpenSemaphore() API
2167 * Parameters:
2168 * Variables :
2169 * Result :
2170 * Remark :
2171 * Status :
2172 *
2173 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
2174 *****************************************************************************/
2175
2176HANDLE HMOpenSemaphore(DWORD fdwAccess,
2177 BOOL fInherit,
2178 LPCTSTR lpName)
2179{
2180 int iIndex; /* index into the handle table */
2181 int iIndexNew; /* index into the handle table */
2182 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
2183 PHMHANDLEDATA pHMHandleData;
2184 DWORD rc; /* API return code */
2185
2186
2187 pDeviceHandler = HMGlobals.pHMMutex; /* device is predefined */
2188
2189 iIndexNew = _HMHandleGetFree(); /* get free handle */
2190 if (-1 == iIndexNew) /* oops, no free handles ! */
2191 {
2192 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
2193 return (INVALID_HANDLE_VALUE); /* signal error */
2194 }
2195
2196
2197 /* initialize the complete HMHANDLEDATA structure */
2198 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
2199 pHMHandleData->dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
2200 pHMHandleData->dwAccess = fdwAccess;
2201 pHMHandleData->dwShare = 0;
2202 pHMHandleData->dwCreation = 0;
2203 pHMHandleData->dwFlags = 0;
2204 pHMHandleData->lpHandlerData = NULL;
2205
2206
2207 /* we've got to mark the handle as occupied here, since another device */
2208 /* could be created within the device handler -> deadlock */
2209
2210 /* write appropriate entry into the handle table if open succeeded */
2211 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
2212 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
2213
2214 /* call the device handler */
2215 rc = pDeviceHandler->OpenSemaphore(&TabWin32Handles[iIndexNew].hmHandleData,
2216 fInherit,
2217 lpName);
2218 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
2219 {
2220 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
2221 SetLastError(rc); /* Hehe, OS/2 and NT are pretty compatible :) */
2222 return (INVALID_HANDLE_VALUE); /* signal error */
2223 }
2224 else
2225 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
2226
2227 return iIndexNew; /* return valid handle */
2228}
2229
2230
2231/*****************************************************************************
2232 * Name : HMReleaseSemaphore
2233 * Purpose : router function for ReleaseSemaphore
2234 * Parameters:
2235 * Variables :
2236 * Result :
2237 * Remark :
2238 * Status :
2239 *
2240 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
2241 *****************************************************************************/
2242
2243BOOL HMReleaseSemaphore(HANDLE hEvent,
2244 LONG cReleaseCount,
2245 LPLONG lpPreviousCount)
2246{
2247 int iIndex; /* index into the handle table */
2248 DWORD dwResult; /* result from the device handler's API */
2249 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
2250
2251 /* validate handle */
2252 iIndex = _HMHandleQuery(hEvent); /* get the index */
2253 if (-1 == iIndex) /* error ? */
2254 {
2255 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
2256 return (INVALID_HANDLE_ERROR); /* signal failure */
2257 }
2258 else
2259 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
2260
2261 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
2262 dwResult = pHMHandle->pDeviceHandler->ReleaseSemaphore(&pHMHandle->hmHandleData,
2263 cReleaseCount,
2264 lpPreviousCount);
2265
2266 return (dwResult); /* deliver return code */
2267}
2268
2269
2270/*****************************************************************************
2271 * Name : HANDLE HMCreateFileMapping
2272 * Purpose : Wrapper for the CreateFileMapping() API
2273 * Parameters:
2274 * Variables :
2275 * Result :
2276 * Remark :
2277 * Status :
2278 *
2279 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
2280 *****************************************************************************/
2281
2282HANDLE HMCreateFileMapping(HANDLE hFile,
2283 LPSECURITY_ATTRIBUTES lpFileMappingAttributes,
2284 DWORD flProtect,
2285 DWORD dwMaximumSizeHigh,
2286 DWORD dwMaximumSizeLow,
2287 LPCTSTR lpName)
2288{
2289 int iIndex; /* index into the handle table */
2290 int iIndexNew; /* index into the handle table */
2291 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
2292 PHMHANDLEDATA pHMHandleData;
2293 DWORD rc; /* API return code */
2294
2295
2296 pDeviceHandler = HMGlobals.pHMFileMapping; /* device is predefined */
2297
2298 iIndexNew = _HMHandleGetFree(); /* get free handle */
2299 if (-1 == iIndexNew) /* oops, no free handles ! */
2300 {
2301 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
2302 return (INVALID_HANDLE_VALUE); /* signal error */
2303 }
2304
2305
2306 /* initialize the complete HMHANDLEDATA structure */
2307 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
2308 pHMHandleData->dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
2309 pHMHandleData->dwAccess = 0;
2310 pHMHandleData->dwShare = 0;
2311 pHMHandleData->dwCreation = 0;
2312 pHMHandleData->dwFlags = 0;
2313 pHMHandleData->lpHandlerData = NULL;
2314
2315
2316 /* we've got to mark the handle as occupied here, since another device */
2317 /* could be created within the device handler -> deadlock */
2318
2319 /* write appropriate entry into the handle table if open succeeded */
2320 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
2321 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
2322
2323 /* call the device handler */
2324
2325 // @@@PH: Note: hFile is a Win32-style handle, it's not (yet) converted to
2326 // a valid HandleManager-internal handle!
2327 rc = pDeviceHandler->CreateFileMapping(&TabWin32Handles[iIndexNew].hmHandleData,
2328 hFile,
2329 lpFileMappingAttributes,
2330 flProtect,
2331 dwMaximumSizeHigh,
2332 dwMaximumSizeLow,
2333 lpName);
2334
2335 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
2336 {
2337 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
2338 SetLastError(rc); /* Hehe, OS/2 and NT are pretty compatible :) */
2339 return (NULL); /* signal error */
2340 }
2341 else
2342 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
2343
2344 return iIndexNew; /* return valid handle */
2345}
2346
2347
2348/*****************************************************************************
2349 * Name : HANDLE HMOpenFileMapping
2350 * Purpose : Wrapper for the OpenFileMapping() API
2351 * Parameters:
2352 * Variables :
2353 * Result : HANDLE if succeeded,
2354 * NULL if failed.
2355 * Remark :
2356 * Status :
2357 *
2358 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
2359 *****************************************************************************/
2360
2361HANDLE HMOpenFileMapping(DWORD fdwAccess,
2362 BOOL fInherit,
2363 LPCTSTR lpName)
2364{
2365 int iIndex; /* index into the handle table */
2366 int iIndexNew; /* index into the handle table */
2367 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
2368 PHMHANDLEDATA pHMHandleData;
2369 DWORD rc; /* API return code */
2370
2371
2372 pDeviceHandler = HMGlobals.pHMFileMapping; /* device is predefined */
2373
2374 iIndexNew = _HMHandleGetFree(); /* get free handle */
2375 if (-1 == iIndexNew) /* oops, no free handles ! */
2376 {
2377 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
2378 return (NULL); /* signal error */
2379 }
2380
2381 /* initialize the complete HMHANDLEDATA structure */
2382 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
2383 pHMHandleData->dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
2384 pHMHandleData->dwAccess = fdwAccess;
2385 pHMHandleData->dwShare = 0;
2386 pHMHandleData->dwCreation = 0;
2387 pHMHandleData->dwFlags = 0;
2388 pHMHandleData->lpHandlerData = NULL;
2389
2390
2391 /* we've got to mark the handle as occupied here, since another device */
2392 /* could be created within the device handler -> deadlock */
2393
2394 /* write appropriate entry into the handle table if open succeeded */
2395 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
2396 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
2397
2398 /* call the device handler */
2399 rc = pDeviceHandler->OpenFileMapping(&TabWin32Handles[iIndexNew].hmHandleData,
2400 fdwAccess,
2401 fInherit,
2402 lpName);
2403 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
2404 {
2405 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
2406 SetLastError(rc); /* Hehe, OS/2 and NT are pretty compatible :) */
2407 return (NULL); /* signal error */
2408 }
2409 else
2410 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
2411
2412 return iIndexNew; /* return valid handle */
2413}
2414
2415
2416/*****************************************************************************
2417 * Name : HMMapViewOfFileEx
2418 * Purpose : router function for MapViewOfFileEx
2419 * Parameters:
2420 * Variables :
2421 * Result :
2422 * Remark :
2423 * Status :
2424 *
2425 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
2426 *****************************************************************************/
2427
2428LPVOID HMMapViewOfFileEx(HANDLE hFileMappingObject,
2429 DWORD dwDesiredAccess,
2430 DWORD dwFileOffsetHigh,
2431 DWORD dwFileOffsetLow,
2432 DWORD dwNumberOfBytesToMap,
2433 LPVOID lpBaseAddress)
2434{
2435 int iIndex; /* index into the handle table */
2436 LPVOID lpResult; /* result from the device handler's API */
2437 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
2438
2439 /* validate handle */
2440 iIndex = _HMHandleQuery(hFileMappingObject); /* get the index */
2441 if (-1 == iIndex) /* error ? */
2442 {
2443 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
2444 return (NULL); /* signal failure */
2445 }
2446
2447 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
2448 lpResult = pHMHandle->pDeviceHandler->MapViewOfFileEx(&pHMHandle->hmHandleData,
2449 dwDesiredAccess,
2450 dwFileOffsetHigh,
2451 dwFileOffsetLow,
2452 dwNumberOfBytesToMap,
2453 lpBaseAddress);
2454
2455 return (lpResult); /* deliver return code */
2456}
2457
2458/*****************************************************************************
2459 * Name : HMWaitForMultipleObjects
2460 * Purpose : router function for WaitForMultipleObjects
2461 * Parameters:
2462 * Variables :
2463 * Result :
2464 * Remark :
2465 * Status :
2466 *
2467 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
2468 *****************************************************************************/
2469
2470DWORD HMWaitForMultipleObjects (DWORD cObjects,
2471 PHANDLE lphObjects,
2472 BOOL fWaitAll,
2473 DWORD dwTimeout)
2474{
2475 ULONG ulIndex;
2476 PHANDLE pArrayOfHandles;
2477 PHANDLE pLoop1 = lphObjects;
2478 PHANDLE pLoop2;
2479 DWORD rc;
2480
2481 // allocate array for handle table
2482 pArrayOfHandles = (PHANDLE)malloc(cObjects * sizeof(HANDLE));
2483 if (pArrayOfHandles == NULL)
2484 {
2485 O32_SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2486 return WAIT_FAILED;
2487 }
2488 else
2489 pLoop2 = pArrayOfHandles;
2490
2491 // convert array to odin handles
2492 for (ulIndex = 0;
2493
2494 ulIndex < cObjects;
2495
2496 ulIndex++,
2497 pLoop1++,
2498 pLoop2++)
2499 {
2500 rc = HMHandleTranslateToOS2 (*pLoop1, // translate handle
2501 pLoop2);
2502
2503 if (rc != NO_ERROR)
2504 {
2505 free (pArrayOfHandles); // free memory
2506 O32_SetLastError(ERROR_INVALID_HANDLE);
2507 return (WAIT_FAILED);
2508 }
2509 }
2510
2511 // OK, now forward to Open32.
2512 // @@@PH: Note this will fail on handles that do NOT belong to Open32
2513 // but to i.e. the console subsystem!
2514 rc = O32_WaitForMultipleObjects(cObjects,
2515 pArrayOfHandles,
2516 fWaitAll,
2517 dwTimeout);
2518
2519 free(pArrayOfHandles); // free memory
2520 return (rc); // OK, done
2521}
2522
2523
2524/*****************************************************************************
2525 * Name : HMWaitForMultipleObjectsEx
2526 * Purpose : router function for WaitForMultipleObjectsEx
2527 * Parameters:
2528 * Variables :
2529 * Result :
2530 * Remark :
2531 * Status :
2532 *
2533 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
2534 *****************************************************************************/
2535
2536DWORD HMWaitForMultipleObjectsEx (DWORD cObjects,
2537 PHANDLE lphObjects,
2538 BOOL fWaitAll,
2539 DWORD dwTimeout,
2540 BOOL fAlertable)
2541{
2542 // @@@PH: Note: fAlertable is ignored !
2543 return (HMWaitForMultipleObjects(cObjects,
2544 lphObjects,
2545 fWaitAll,
2546 dwTimeout));
2547}
2548
Note: See TracBrowser for help on using the repository browser.