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

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

handle manager changes for DuplicateHandle + memory mapped file changes/bugfixes

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