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

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

Fix: header file cleanup (win32type.h)

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