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

Last change on this file since 7474 was 7474, checked in by phaller, 24 years ago

Fixes in HandleManager and Device Handlers

File size: 193.1 KB
Line 
1/* $Id: HandleManager.cpp,v 1.79 2001-11-28 23:33:34 phaller Exp $ */
2
3/*
4 * Win32 Unified Handle Manager for OS/2
5 *
6 * 1998/02/11 PH Patrick Haller (haller@zebra.fh-weingarten.de)
7 *
8 * @(#) HandleManager.Cpp 1.0.0 1998/02/11 PH start
9 *
10 * Project Odin Software License can be found in LICENSE.TXT
11 *
12 */
13
14//#undef DEBUG_LOCAL
15//#define DEBUG_LOCAL
16
17
18/*****************************************************************************
19 * Remark *
20 *****************************************************************************
21
22 1998/02/11 PH Even overlapped I/O could be simulated by another subsystem
23 thread with a request queue. We'll see if required ...
24
25
26 Flush (flush handle buffer)
27 WaitForSingleObject
28 WaitForMultipleObjects (?)
29
30 1998/02/12 PH IBM and Microsoft disagree about the definition of FILE_TYPE_xxx
31 Interesting, Open32 returns Microsoft's values ...
32
33 1998/02/12 PH Handles should be equipped with a locking mechanism, in particular
34 as we publish a pointer into the handle table via HMHandleQueryHandleData
35
36 */
37
38
39/*****************************************************************************
40 * Includes *
41 *****************************************************************************/
42
43#include <os2win.h>
44#include <stdlib.h>
45#include <string.h>
46
47#include "unicode.h"
48#include "misc.h"
49
50#include "HandleManager.H"
51#include "handlenames.h"
52#include "HMDevice.h"
53#include "HMDisk.h"
54#include "HMOpen32.h"
55#include "HMEvent.h"
56#include "HMFile.h"
57#include "HMMutex.h"
58#include "HMSemaphore.h"
59#include "HMMMap.h"
60#include "HMComm.h"
61#include "HMParPort.h"
62#include "HMToken.h"
63#include "HMThread.h"
64#include "HMNPipe.h"
65#include "HMStd.h"
66#include "HMMailslot.h"
67
68#include <vmutex.h>
69#include <win\thread.h>
70
71#define DBG_LOCALLOG DBG_handlemanager
72#include "dbglocal.h"
73
74/*****************************************************************************
75 * Defines *
76 *****************************************************************************/
77
78 /* this is the size of our currently static handle table */
79#define MAX_OS2_HMHANDLES 4096
80
81
82/*****************************************************************************
83 * Structures *
84 *****************************************************************************/
85
86typedef struct _HMDEVICE;
87
88typedef struct _HMHANDLE
89{
90 HMDeviceHandler *pDeviceHandler; /* handler for this pseudo-device */
91 HMHANDLEDATA hmHandleData; /* attributes of the handle */
92} HMHANDLE, *PHMHANDLE;
93
94
95typedef struct _HMDEVICE
96{
97 struct _HMDEVICE *pNext; /* pointer to next device in chain */
98
99 LPSTR pszDeviceName; /* name or alias of the pseudo-device */
100 HMDeviceHandler *pDeviceHandler; /* handler for this pseudo-device */
101 VOID *pDevData; /* Pointer To Device data */
102} HMDEVICE, *PHMDEVICE;
103
104
105/*****************************************************************************
106 * This pseudo-device logs all device requests to the logfile and returns *
107 * ERROR_INVALID_FUNCTION to virtually all requests -> debugging *
108 *****************************************************************************/
109class HMDeviceDebugClass : public HMDeviceHandler
110{
111 public:
112 HMDeviceDebugClass(LPCSTR lpDeviceName) : HMDeviceHandler(lpDeviceName) {}
113};
114
115
116/*****************************************************************************
117 * Process Global Structures *
118 *****************************************************************************/
119
120
121 /* the device name is repeated here to enable device alias names */
122static PHMDEVICE TabWin32Devices = NULL;
123
124static HMHANDLE TabWin32Handles[MAX_OS2_HMHANDLES]; /* static handle table */
125VMutex handleMutex;
126
127struct _HMGlobals
128{
129 HANDLE hStandardIn; /* stdin handle to CONIN$ */
130 HANDLE hStandardOut; /* stdout handle to CONOUT$ */
131 HANDLE hStandardError; /* stderr handle to CONOUT$ */
132
133 BOOL fIsInitialized; /* if HM is initialized already ? */
134 /* this MUST !!! be false initially */
135
136 HMDeviceHandler *pHMStandard; /* default handle manager instance */
137 HMDeviceHandler *pHMOpen32; /* default handle manager instance */
138 HMDeviceHandler *pHMEvent; /* static instances of subsystems */
139 HMDeviceHandler *pHMFile;
140 HMDeviceHandler *pHMDisk;
141 HMDeviceHandler *pHMMutex;
142 HMDeviceHandler *pHMSemaphore;
143 HMDeviceHandler *pHMFileMapping; /* static instances of subsystems */
144 HMDeviceHandler *pHMComm; /* serial communication */
145 HMDeviceHandler *pHMToken; /* security tokens */
146 HMDeviceHandler *pHMThread;
147 HMDeviceHandler *pHMNamedPipe;
148 HMDeviceHandler *pHMMailslot;
149 HMDeviceHandler *pHMParPort; /* parallel communication */
150
151 ULONG ulHandleLast; /* index of last used handle */
152} HMGlobals;
153
154
155/*****************************************************************************
156 * Local Prototypes *
157 *****************************************************************************/
158
159 /* get appropriate device handler by the device name */
160static HMDeviceHandler* _Optlink _HMDeviceFind(LPSTR pszDeviceName);
161
162 /* get next free handle from the handle table */
163static ULONG _Optlink _HMHandleGetFree(void);
164
165 /* get handle table entry from handle */
166static ULONG _Optlink _HMHandleQuery(HANDLE hHandle);
167
168// Get GlobalDeviceData
169static VOID *_HMDeviceGetData (LPSTR pszDeviceName);
170
171
172/*****************************************************************************
173 * Name : static HMDeviceHandler * _HMDeviceFind
174 * Purpose : obtain appropriate device handler from the table by searching
175 * for a device name or alias
176 * Parameters: PSZ pszDeviceName
177 * Variables :
178 * Result : HMDeviceHandler * - pointer to the handler object
179 * Remark :
180 * Status :
181 *
182 * Author : Patrick Haller [Wed, 1998/02/11 20:42]
183 *****************************************************************************/
184
185static HMDeviceHandler *_HMDeviceFind (LPSTR pszDeviceName)
186{
187 PHMDEVICE pHMDevice; /* iterator over the device table */
188 int namelength = strlen(pszDeviceName);
189
190 if (pszDeviceName != NULL)
191 {
192 for (pHMDevice = TabWin32Devices; /* loop over all devices in the table */
193 pHMDevice != NULL;
194 pHMDevice = pHMDevice->pNext)
195 {
196 if(pHMDevice->pDeviceHandler->FindDevice(pHMDevice->pszDeviceName, pszDeviceName, namelength) == TRUE)
197 {
198 return pHMDevice->pDeviceHandler;
199 }
200 }
201 }
202 return (HMGlobals.pHMOpen32); /* haven't found anything, return default */
203}
204/*****************************************************************************
205 * Name : static VOID *_HMDeviceGetData
206 * Purpose : obtain pointer to device data from the table by searching
207 * for a device name or alias
208 * Parameters: PSZ pszDeviceName
209 * Variables :
210 * Result : VOID * - pointer to the handlers device data
211 * Remark :
212 * Status :
213 *
214 * Author : Markus Montkowski
215 *****************************************************************************/
216
217static VOID *_HMDeviceGetData (LPSTR pszDeviceName)
218{
219 PHMDEVICE pHMDevice; /* iterator over the device table */
220 int namelength = strlen(pszDeviceName);
221
222 if (pszDeviceName != NULL)
223 {
224 for (pHMDevice = TabWin32Devices; /* loop over all devices in the table */
225 pHMDevice != NULL;
226 pHMDevice = pHMDevice->pNext)
227 {
228 if(pHMDevice->pDeviceHandler->FindDevice(pHMDevice->pszDeviceName, pszDeviceName, namelength) == TRUE)
229 {
230 return (pHMDevice->pDevData); /* OK, we've found our device */
231 }
232 }
233 }
234 return (NULL); /* haven't found anything, return NULL */
235}
236
237/*****************************************************************************
238 * Name : static int _HMHandleGetFree
239 * Purpose : get index to first free handle in the handle table
240 * Parameters:
241 * Variables :
242 * Result : int iIndex - index to the table or -1 in case of error
243 * Remark :
244 * Status :
245 *
246 * Author : Patrick Haller [Wed, 1998/02/11 20:43]
247 *****************************************************************************/
248
249static ULONG _HMHandleGetFree(void)
250{
251 register ULONG ulLoop;
252
253 handleMutex.enter();
254
255 for (ulLoop = 1; // @@@PH Note, this is an experimental change
256 // 0L as hHandle is sometimes considered invalid!
257 // this will never return 0l as free handle now.
258 ulLoop < MAX_OS2_HMHANDLES;
259 ulLoop++)
260 {
261 /* free handle found ? */
262 if (INVALID_HANDLE_VALUE == TabWin32Handles[ulLoop].hmHandleData.hHMHandle) {
263 //SvL: Mark handle as allocated here. Doing it outside of this function
264 // isn't thread safe. (and not very smart)
265 TabWin32Handles[ulLoop].hmHandleData.hHMHandle = ulLoop;
266 TabWin32Handles[ulLoop].hmHandleData.dwUserData = 0;
267 TabWin32Handles[ulLoop].hmHandleData.dwInternalType = HMTYPE_UNKNOWN;
268 TabWin32Handles[ulLoop].hmHandleData.lpDeviceData = NULL;
269 handleMutex.leave();
270 return (ulLoop); /* OK, then return it to the caller */
271 }
272 }
273
274 handleMutex.leave();
275 return (INVALID_HANDLE_VALUE); /* haven't found any free handle */
276}
277
278
279/*****************************************************************************
280 * Name : HMHandleGetUserData
281 * Purpose : Get the dwUserData dword for a specific handle
282 * Parameters: HANDLE hHandle
283 * Variables :
284 * Result : -1 or dwUserData
285 * Remark :
286 * Status :
287 *
288 * Author : SvL
289 *****************************************************************************/
290DWORD HMHandleGetUserData(ULONG hHandle)
291{
292 if (hHandle > MAX_OS2_HMHANDLES) /* check the table range */
293 return (-1);
294 /* Oops, invalid handle ! */
295 if (INVALID_HANDLE_VALUE == TabWin32Handles[hHandle].hmHandleData.hHMHandle)
296 return (-1); /* nope, ERROR_INVALID_HANDLE */
297
298 return TabWin32Handles[hHandle].hmHandleData.dwUserData;
299}
300
301/*****************************************************************************
302 * Name : HMHandleGetUserData
303 * Purpose : Get the dwUserData dword for a specific handle
304 * Parameters: HANDLE hHandle
305 * Variables :
306 * Result : -1 or dwUserData
307 * Remark :
308 * Status :
309 *
310 * Author : SvL
311 *****************************************************************************/
312DWORD HMHandleSetUserData(ULONG hHandle, ULONG dwUserData)
313{
314 if (hHandle > MAX_OS2_HMHANDLES) /* check the table range */
315 return (-1);
316 /* Oops, invalid handle ! */
317 if (INVALID_HANDLE_VALUE == TabWin32Handles[hHandle].hmHandleData.hHMHandle)
318 return (-1); /* nope, ERROR_INVALID_HANDLE */
319
320 TabWin32Handles[hHandle].hmHandleData.dwUserData = dwUserData;
321 return NO_ERROR;
322}
323
324/*****************************************************************************
325 * Name : static int _HMHandleQuery
326 * Purpose : gets the index of handle table entry as fast as possible from
327 * the specified handle
328 * Parameters: HANDLE hHandle
329 * Variables :
330 * Result : index or -1 in case of error
331 * Remark : Should fail for standard handles (in/out/err)!!!!!!!!!!
332 * HMGetFileType depends on this!!!
333 * Status :
334 *
335 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
336 *****************************************************************************/
337
338static ULONG INLINE _HMHandleQuery(HANDLE hHandle)
339{
340 if (hHandle > MAX_OS2_HMHANDLES) /* check the table range */
341 return (INVALID_HANDLE_VALUE); /* nope, ERROR_INVALID_HANDLE */
342
343 /* Oops, invalid handle ! */
344 if (INVALID_HANDLE_VALUE == TabWin32Handles[hHandle].hmHandleData.hHMHandle)
345 return (INVALID_HANDLE_VALUE); /* nope, ERROR_INVALID_HANDLE */
346
347 return ( hHandle); /* OK, we've got our handle index */
348}
349
350
351/*****************************************************************************
352 * Name : DWORD HMDeviceRegister
353 * Purpose : register a device with the handle manager
354 * Parameters: PSZ pszDeviceName
355 * HMDeviceHandler *pDeviceHandler
356 * Variables :
357 * Result : API returncode
358 * Remark :
359 * Status :
360 *
361 * Author : Patrick Haller [Wed, 1998/02/12 20:44]
362 *****************************************************************************/
363
364DWORD HMDeviceRegisterEx(LPSTR pszDeviceName,
365 HMDeviceHandler *pDeviceHandler,
366 VOID *pDevData)
367{
368 PHMDEVICE pHMDevice; /* our new device to be allocated */
369
370 if ( (pszDeviceName == NULL) || /* check parameters */
371 (pDeviceHandler == NULL) )
372 return (ERROR_INVALID_PARAMETER); /* raise error conditon */
373
374
375 pHMDevice = (PHMDEVICE) malloc (sizeof (HMDEVICE) ); /* allocate memory */
376 if (pHMDevice == NULL) /* check proper allocation */
377 return (ERROR_NOT_ENOUGH_MEMORY); /* signal error */
378
379 pHMDevice->pszDeviceName = strdup(pszDeviceName); /* copy name */
380 if (pHMDevice->pszDeviceName == NULL) /* check proper allocation */
381 {
382 free (pHMDevice); /* free previously allocated memory */
383 return (ERROR_NOT_ENOUGH_MEMORY); /* signal error */
384 }
385
386 pHMDevice->pDeviceHandler = pDeviceHandler; /* store pointer to device */
387 pHMDevice->pNext = TabWin32Devices; /* establish linkage */
388 pHMDevice->pDevData = pDevData;
389
390 TabWin32Devices = pHMDevice; /* insert new node as root in the list */
391
392 return (NO_ERROR);
393}
394
395DWORD HMDeviceRegister(LPSTR pszDeviceName,
396 HMDeviceHandler *pDeviceHandler)
397{
398 return HMDeviceRegisterEx(pszDeviceName, pDeviceHandler, NULL);
399}
400
401/*****************************************************************************
402 * Name : DWORD HMInitialize
403 * Purpose : Initialize the handlemanager
404 * Parameters: -
405 * Variables : -
406 * Result : always NO_ERROR
407 * Remark : this routine just stores the standard handles in the
408 * internal table within the HandleManager
409 * Status :
410 *
411 * Author : Patrick Haller [Wed, 1998/02/12 20:44]
412 *****************************************************************************/
413
414DWORD HMInitialize(void)
415{
416 ULONG ulIndex;
417
418 if (HMGlobals.fIsInitialized != TRUE)
419 {
420 handleMutex.enter();
421 // fill handle table
422 for(ulIndex = 0; ulIndex < MAX_OS2_HMHANDLES; ulIndex++) {
423 TabWin32Handles[ulIndex].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
424 }
425 handleMutex.leave();
426
427 dprintf(("KERNEL32:HandleManager:HMInitialize() storing handles.\n"));
428
429 memset(&HMGlobals, /* zero out the structure first */
430 0,
431 sizeof(HMGlobals));
432
433 HMGlobals.fIsInitialized = TRUE; /* OK, done */
434
435#if 1
436 //This is a very bad idea. \\\\.\\NTICE -> NTICE, if the file exits, then
437 //it will open the file instead of the device driver
438 /* add standard symbolic links first, so local symbolic links in the
439 * device handlers get precedence over the default here.
440 *
441 * Device handlers are supposed to place the appropriate
442 * symbolic links such as "\\.\\COM1", "COM1"
443 *
444 * - "\\.\f:\temp\readme.txt" is a valid file
445 * - "com1" is a valid device in case serial port 1 exists,
446 * otherwise it'd be a valid file.
447 * - "\\.\filename" is the only misleading case (to be verified)
448 *
449 * Note:
450 * the Open32 "device" definately MUST be the last device to be
451 * asked to accept the specified name.
452 */
453 {
454 // strings are placed in read-only segment
455 PSZ pszDrive = strdup("\\\\.\\x:");
456 PSZ pszDrive2 = strdup("\\\\.\\x:");
457 for (char ch = 'A'; ch <= 'Z'; ch++)
458 {
459 pszDrive[4] = ch;
460 pszDrive2[4] = ch;
461 HandleNamesAddSymbolicLink(pszDrive, pszDrive+4);
462 HandleNamesAddSymbolicLink(pszDrive2, pszDrive2+4);
463 }
464 free(pszDrive);
465 free(pszDrive2);
466 HandleNamesAddSymbolicLink("\\\\?\\UNC\\", "\\\\");
467 }
468#endif
469
470 /* copy standard handles from OS/2's Open32 Subsystem */
471 HMGlobals.pHMStandard = new HMDeviceStandardClass("\\\\STANDARD_HANDLE\\");
472 HMSetStdHandle(STD_INPUT_HANDLE, O32_GetStdHandle(STD_INPUT_HANDLE));
473 HMSetStdHandle(STD_OUTPUT_HANDLE, O32_GetStdHandle(STD_OUTPUT_HANDLE));
474 HMSetStdHandle(STD_ERROR_HANDLE, O32_GetStdHandle(STD_ERROR_HANDLE));
475
476 /* create handle manager instance for Open32 handles */
477 HMGlobals.pHMOpen32 = new HMDeviceOpen32Class("\\\\.\\");
478 HMGlobals.pHMEvent = new HMDeviceEventClass("\\\\EVENT\\");
479 HMGlobals.pHMFile = new HMDeviceFileClass("\\\\FILE\\");
480 HMGlobals.pHMDisk = new HMDeviceDiskClass("\\\\DISK\\");
481 HMGlobals.pHMMutex = new HMDeviceMutexClass("\\\\MUTEX\\");
482 HMGlobals.pHMSemaphore = new HMDeviceSemaphoreClass("\\\\SEM\\");
483 HMGlobals.pHMFileMapping= new HMDeviceMemMapClass("\\\\MEMMAP\\");
484 HMGlobals.pHMComm = new HMDeviceCommClass("\\\\COM\\");
485 HMGlobals.pHMToken = new HMDeviceTokenClass("\\\\TOKEN\\");
486 HMGlobals.pHMThread = new HMDeviceThreadClass("\\\\THREAD\\");
487 HMGlobals.pHMNamedPipe = new HMDeviceNamedPipeClass("\\\\PIPE\\");
488 HMGlobals.pHMMailslot = new HMMailslotClass("\\MAILSLOT\\");
489 HMGlobals.pHMParPort = new HMDeviceParPortClass("\\\\LPT\\");
490 }
491 return (NO_ERROR);
492}
493
494
495/*****************************************************************************
496 * Name : DWORD HMTerminate
497 * Purpose : Terminate the handlemanager
498 * Parameters:
499 * Variables :
500 * Result :
501 * Remark :
502 * Status :
503 *
504 * Author : Patrick Haller [Wed, 1998/02/12 20:44]
505 *****************************************************************************/
506
507DWORD HMTerminate(void)
508{
509 /* @@@PH we could deallocate the device list here */
510
511 if(HMGlobals.pHMOpen32)
512 delete HMGlobals.pHMOpen32;
513 if(HMGlobals.pHMEvent)
514 delete HMGlobals.pHMEvent;
515 if(HMGlobals.pHMFile)
516 delete HMGlobals.pHMFile;
517 if(HMGlobals.pHMMutex)
518 delete HMGlobals.pHMMutex;
519 if(HMGlobals.pHMSemaphore)
520 delete HMGlobals.pHMSemaphore;
521 if(HMGlobals.pHMFileMapping)
522 delete HMGlobals.pHMFileMapping;
523 if(HMGlobals.pHMComm)
524 delete HMGlobals.pHMComm;
525 if(HMGlobals.pHMToken)
526 delete HMGlobals.pHMToken;
527 if(HMGlobals.pHMThread)
528 delete HMGlobals.pHMThread;
529 if(HMGlobals.pHMNamedPipe)
530 delete HMGlobals.pHMNamedPipe;
531 if(HMGlobals.pHMMailslot)
532 delete HMGlobals.pHMMailslot;
533 if(HMGlobals.pHMDisk)
534 delete HMGlobals.pHMDisk;
535 if(HMGlobals.pHMStandard);
536 delete HMGlobals.pHMStandard;
537 if(HMGlobals.pHMParPort)
538 delete HMGlobals.pHMParPort;
539
540 return (NO_ERROR);
541}
542
543
544/*****************************************************************************/
545/* handle translation buffer management */
546/* */
547/* Since some Win32 applications rely (!) on 16-bit handles, we've got to do */
548/* 32-bit to 16-bit and vs vsa translation here. */
549/* Filehandle-based functions should be routed via the handlemanager instead */
550/* of going to Open32 directly. */
551/*****************************************************************************/
552
553
554/*****************************************************************************
555 * Name : DWORD HMHandleAllocate
556 * Purpose : allocate a handle in the translation table
557 * Parameters: PULONG pHandle16 - to return the allocated handle
558 * ULONG hHandle32 - the associated OS/2 handle
559 * Variables :
560 * Result : API returncode
561 * Remark : no parameter checking is done, phHandle may not be invalid
562 * hHandle32 shouldn't be 0
563 * Should be protected with a HM-Mutex !
564 * Status :
565 *
566 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
567 *****************************************************************************/
568
569DWORD HMHandleAllocate (PULONG phHandle16,
570 ULONG hHandleOS2)
571{
572 register ULONG ulHandle;
573
574#ifdef DEBUG_LOCAL
575 dprintf(("KERNEL32: HMHandleAllocate (%08xh,%08xh)\n",
576 phHandle16,
577 hHandleOS2));
578#endif
579
580 // @@@PH 2001-09-27
581 // prevent too quick re-use of last handle
582 ulHandle = HMGlobals.ulHandleLast + 1; /* get free handle */
583
584 handleMutex.enter();
585
586 if(ulHandle == 0) {
587 ulHandle = 1; //SvL: Start searching from index 1
588 }
589 do
590 {
591 /* check if handle is free */
592 if (TabWin32Handles[ulHandle].hmHandleData.hHMHandle == INVALID_HANDLE_VALUE)
593 {
594 *phHandle16 = ulHandle;
595 TabWin32Handles[ulHandle].hmHandleData.hHMHandle = hHandleOS2;
596 TabWin32Handles[ulHandle].hmHandleData.lpDeviceData = NULL;
597 HMGlobals.ulHandleLast = ulHandle; /* to shorten search times */
598
599 handleMutex.leave();
600 return (NO_ERROR); /* OK */
601 }
602
603 ulHandle++; /* skip to next entry */
604
605 if (ulHandle >= MAX_OS2_HMHANDLES) /* check boundary */
606 ulHandle = 1;
607 }
608 while (ulHandle != HMGlobals.ulHandleLast);
609
610 handleMutex.leave();
611
612 return (ERROR_TOO_MANY_OPEN_FILES); /* OK, we're done */
613}
614
615
616/*****************************************************************************
617 * Name : DWORD HMHandleFree
618 * Purpose : free a handle from the translation table
619 * Parameters: ULONG hHandle16 - the handle to be freed
620 * Variables :
621 * Result : API returncode
622 * Remark : no parameter checking is done, hHandle16 MAY NEVER exceed
623 * the MAX_TRANSLATION_HANDLES boundary
624 * Status :
625 *
626 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
627 *****************************************************************************/
628
629DWORD HMHandleFree (ULONG hHandle16)
630{
631 ULONG rc; /* API returncode */
632
633#ifdef DEBUG_LOCAL
634 dprintf(("KERNEL32: HMHandleFree (%08xh)\n",
635 hHandle16));
636#endif
637
638 rc = HMHandleValidate(hHandle16); /* verify handle */
639 if (rc != NO_ERROR) /* check errors */
640 return (rc); /* raise error condition */
641
642 TabWin32Handles[hHandle16].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
643 /* OK, done */
644
645 return (NO_ERROR);
646}
647
648
649/*****************************************************************************
650 * Name : DWORD HMHandleValidate
651 * Purpose : validate a handle through the translation table
652 * Parameters: ULONG hHandle16 - the handle to be verified
653 * Variables :
654 * Result : API returncode
655 * Remark :
656 * Status :
657 *
658 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
659 *****************************************************************************/
660
661DWORD HMHandleValidate (ULONG hHandle16)
662{
663#ifdef DEBUG_LOCAL
664 dprintf(("KERNEL32: HMHandleValidate (%08xh)\n",
665 hHandle16));
666#endif
667
668 if (hHandle16 >= MAX_OS2_HMHANDLES) /* check boundary */
669 return (ERROR_INVALID_HANDLE); /* raise error condition */
670
671 if (TabWin32Handles[hHandle16].hmHandleData.hHMHandle == INVALID_HANDLE_VALUE)
672 /* valid ? */
673 return (ERROR_INVALID_HANDLE); /* raise error condition */
674
675 return (NO_ERROR);
676}
677//*****************************************************************************
678//*****************************************************************************
679PHMHANDLEDATA HMQueryHandleData(HANDLE handle)
680{
681 int iIndex;
682
683 iIndex = _HMHandleQuery(handle); /* get the index */
684 if (-1 == iIndex) /* error ? */
685 {
686 return NULL;
687 }
688 return &TabWin32Handles[iIndex].hmHandleData; /* call device handler */
689}
690
691/*****************************************************************************
692 * Name : DWORD HMHandleTranslateToWin
693 * Purpose : translate a 32-bit OS/2 handle to the associated windows handle
694 * Parameters: ULONG hHandle32 - the OS/2 handle
695 * PULONG phHandle16 - the associated windows handle
696 * Variables :
697 * Result : API returncode
698 * Remark :
699 * Status :
700 *
701 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
702 *****************************************************************************/
703
704DWORD HMHandleTranslateToWin (ULONG hHandleOS2,
705 PULONG phHandle16)
706{
707 ULONG rc; /* API returncode */
708 register ULONG ulIndex; /* index counter over the table */
709
710#ifdef DEBUG_LOCAL
711 dprintf(("KERNEL32: HMHandleTranslateToWin (%08xh, %08xh)\n",
712 hHandleOS2,
713 phHandle16));
714#endif
715
716 for (ulIndex = 1;
717 ulIndex < MAX_OS2_HMHANDLES;
718 ulIndex++)
719 {
720 /* look for the handle */
721 if (TabWin32Handles[ulIndex].hmHandleData.hHMHandle == hHandleOS2)
722 {
723 *phHandle16 = ulIndex; /* deliver result */
724 return (NO_ERROR); /* OK */
725 }
726 }
727
728 return (ERROR_INVALID_HANDLE); /* raise error condition */
729}
730
731
732/*****************************************************************************
733 * Name : DWORD HMHandleTranslateToOS2
734 * Purpose : translate a 16-bit Win32 handle to the associated OS/2 handle
735 * Parameters: ULONG hHandle16 - the windows handle
736 * PULONG phHandle32 - the associated OS/2 handle
737 * Variables :
738 * Result : API returncode
739 * Remark :
740 * Status :
741 *
742 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
743 *****************************************************************************/
744
745DWORD HMHandleTranslateToOS2 (ULONG hHandle16,
746 PULONG phHandleOS2)
747{
748#ifdef DEBUG_LOCAL
749 dprintf(("KERNEL32: HMHandleTranslateToOS2 (%08xh, %08xh)\n",
750 hHandle16,
751 phHandleOS2));
752#endif
753
754 if (HMHandleValidate(hHandle16) == NO_ERROR) /* verify handle */
755 {
756 *phHandleOS2 = TabWin32Handles[hHandle16].hmHandleData.hHMHandle;
757 return (NO_ERROR);
758 }
759
760 return (ERROR_INVALID_HANDLE); /* raise error condition */
761}
762
763
764/*****************************************************************************
765 * Name : DWORD HMHandleTranslateToOS2i
766 * Purpose : translate a 16-bit Win32 handle to the associated OS/2 handle
767 * Parameters: ULONG hHandle16 - the windows handle
768 * Variables :
769 * Result : OS/2 handle
770 * Remark : no checkinf
771 * Status :
772 *
773 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
774 *****************************************************************************/
775
776DWORD HMHandleTranslateToOS2i (ULONG hHandle16)
777{
778#ifdef DEBUG_LOCAL
779 dprintf(("KERNEL32: HMHandleTranslateToOS2i (%08xh)\n",
780 hHandle16));
781#endif
782
783 return(TabWin32Handles[hHandle16].hmHandleData.hHMHandle);
784}
785
786/*****************************************************************************
787 * Name : HANDLE _HMGetStdHandle
788 * Purpose : replacement for Open32's GetStdHandle function
789 * Parameters: DWORD nStdHandle
790 * Variables :
791 * Result : HANDLE to standard device
792 * Remark :
793 * Status :
794 *
795 * Author : Patrick Haller [Wed, 1998/02/12 20:44]
796 *****************************************************************************/
797
798HANDLE HMGetStdHandle(DWORD nStdHandle)
799{
800 switch (nStdHandle)
801 {
802 case STD_INPUT_HANDLE: return (HMGlobals.hStandardIn);
803 case STD_OUTPUT_HANDLE: return (HMGlobals.hStandardOut);
804 case STD_ERROR_HANDLE: return (HMGlobals.hStandardError);
805
806 default:
807 {
808 SetLastError(ERROR_INVALID_PARAMETER); /* set error information */
809 return (INVALID_HANDLE_VALUE); /* raise error condition */
810 }
811 }
812}
813
814
815/*****************************************************************************
816 * Name : HANDLE _HMSetStdHandle
817 * Purpose : replacement for Open32's SetStdHandle function
818 * Parameters: DWORD nStdHandle
819 * HANDLE hHandle
820 * Variables :
821 * Result : BOOL fSuccess
822 * Remark :
823 * Status :
824 *
825 * Author : Patrick Haller [Wed, 1998/02/12 20:44]
826 *****************************************************************************/
827
828BOOL HMSetStdHandle(DWORD nStdHandle, HANDLE hHandleOpen32)
829{
830 PHMHANDLEDATA pHMHandleData;
831 HANDLE hHandle;
832
833 hHandle = _HMHandleGetFree(); /* get free handle */
834 if (hHandle == -1) /* oops, no free handles ! */
835 {
836 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
837 return 0;
838 }
839
840 /* initialize the complete HMHANDLEDATA structure */
841 pHMHandleData = &TabWin32Handles[hHandle].hmHandleData;
842 pHMHandleData->dwType = FILE_TYPE_CHAR;
843 pHMHandleData->dwAccess = 0;
844 pHMHandleData->dwShare = 0;
845 pHMHandleData->dwCreation = 0;
846 pHMHandleData->dwFlags = 0;
847 pHMHandleData->dwUserData = nStdHandle;
848 pHMHandleData->hHMHandle = hHandleOpen32;
849 pHMHandleData->lpHandlerData = NULL;
850
851 TabWin32Handles[hHandle].pDeviceHandler = HMGlobals.pHMStandard;
852
853 switch (nStdHandle)
854 {
855 case STD_INPUT_HANDLE: HMGlobals.hStandardIn = hHandle; return TRUE;
856 case STD_OUTPUT_HANDLE: HMGlobals.hStandardOut = hHandle; return TRUE;
857 case STD_ERROR_HANDLE: HMGlobals.hStandardError = hHandle; return TRUE;
858
859 default:
860 {
861 SetLastError(ERROR_INVALID_PARAMETER); /* set error information */
862 return (FALSE); /* raise error condition */
863 }
864 }
865}
866
867
868/*****************************************************************************
869 * Name : HANDLE HMDuplicateHandle
870 * Purpose : replacement for Open32's HMDuplicateHandle function
871 * Parameters:
872 *
873 * Variables :
874 * Result : BOOL fSuccess
875 * Remark :
876 * Status :
877 *
878 * Author : Sander van Leeuwen [Wed, 1999/08/25 15:44]
879 *****************************************************************************/
880
881BOOL HMDuplicateHandle(HANDLE srcprocess,
882 HANDLE srchandle,
883 HANDLE destprocess,
884 PHANDLE desthandle,
885 DWORD fdwAccess,
886 BOOL fInherit,
887 DWORD fdwOptions,
888 DWORD fdwOdinOptions)
889{
890 int iIndex; /* index into the handle table */
891 int iIndexNew; /* index into the handle table */
892 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
893 PHMHANDLEDATA pHMHandleData;
894 BOOL rc; /* API return code */
895
896 if(HMHandleValidate(srchandle) != NO_ERROR)
897 {
898 dprintf(("KERNEL32: HMDuplicateHandle: invalid handle %x", srchandle));
899 SetLastError(ERROR_INVALID_HANDLE); /* use this as error message */
900 return FALSE;
901 }
902
903 pDeviceHandler = TabWin32Handles[srchandle].pDeviceHandler; /* device is predefined */
904 iIndexNew = _HMHandleGetFree(); /* get free handle */
905 if (-1 == iIndexNew) /* oops, no free handles ! */
906 {
907 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
908 return FALSE; /* signal error */
909 }
910
911 /* initialize the complete HMHANDLEDATA structure */
912 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
913 pHMHandleData->dwType = TabWin32Handles[srchandle].hmHandleData.dwType;
914 if (fdwOptions & DUPLICATE_SAME_ACCESS)
915 pHMHandleData->dwAccess = TabWin32Handles[srchandle].hmHandleData.dwAccess;
916 else
917 pHMHandleData->dwAccess = fdwAccess;
918
919 if((fdwOdinOptions & DUPLICATE_ACCESS_READWRITE) == DUPLICATE_ACCESS_READWRITE) {
920 pHMHandleData->dwAccess = GENERIC_READ | GENERIC_WRITE;
921 }
922 else
923 if(fdwOdinOptions & DUPLICATE_ACCESS_READ) {
924 pHMHandleData->dwAccess = GENERIC_READ;
925 }
926
927 if(fdwOdinOptions & DUPLICATE_SHARE_READ) {
928 pHMHandleData->dwShare = FILE_SHARE_READ;
929 }
930 else
931 if(fdwOdinOptions & DUPLICATE_SHARE_DENYNONE) {
932 pHMHandleData->dwShare = FILE_SHARE_READ | FILE_SHARE_WRITE;
933 }
934 else pHMHandleData->dwShare = TabWin32Handles[srchandle].hmHandleData.dwShare;
935
936 pHMHandleData->dwCreation = TabWin32Handles[srchandle].hmHandleData.dwCreation;
937 pHMHandleData->dwFlags = TabWin32Handles[srchandle].hmHandleData.dwFlags;
938 pHMHandleData->lpHandlerData = TabWin32Handles[srchandle].hmHandleData.lpHandlerData;
939
940
941 /* we've got to mark the handle as occupied here, since another device */
942 /* could be created within the device handler -> deadlock */
943
944 /* write appropriate entry into the handle table if open succeeded */
945 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
946 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
947 /* call the device handler */
948 rc = pDeviceHandler->DuplicateHandle(&TabWin32Handles[iIndexNew].hmHandleData,
949 srcprocess,
950 &TabWin32Handles[srchandle].hmHandleData,
951 destprocess,
952 desthandle,
953 fdwAccess,
954 fInherit,
955 fdwOptions & ~DUPLICATE_CLOSE_SOURCE,
956 fdwOdinOptions);
957
958 //Don't let Open32 close it for us, but do it manually (regardless of error; see SDK docs))
959 if (fdwOptions & DUPLICATE_CLOSE_SOURCE)
960 HMCloseHandle(srchandle);
961
962 if(rc == FALSE) /* oops, creation failed within the device handler */
963 {
964 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
965 return FALSE; /* signal error */
966 }
967 else
968 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
969
970 *desthandle = iIndexNew;
971 return TRUE; /* return valid handle */
972}
973
974/*****************************************************************************
975 * Name : HANDLE HMCreateFile
976 * Purpose : Wrapper for the CreateFile() API
977 * Parameters:
978 * Variables :
979 * Result :
980 * Remark : Fix parameters passed to the HMDeviceManager::CreateFile
981 * Supply access mode and share mode validation routines
982 * Status :
983 *
984 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
985 *****************************************************************************/
986
987HFILE HMCreateFile(LPCSTR lpFileName,
988 DWORD dwDesiredAccess,
989 DWORD dwShareMode,
990 LPSECURITY_ATTRIBUTES lpSecurityAttributes,
991 DWORD dwCreationDisposition,
992 DWORD dwFlagsAndAttributes,
993 HANDLE hTemplateFile)
994{
995 int iIndex; /* index into the handle table */
996 int iIndexNew; /* index into the handle table */
997 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
998 HANDLE hResult;
999 DWORD rc; /* API return code */
1000 PHMHANDLEDATA pHMHandleData;
1001 HMHANDLEDATA HMHandleTemp; /* temporary buffer for handle data */
1002 VOID *pDevData;
1003
1004 /* create new handle by either lpFileName or hTemplateFile */
1005 if (lpFileName == NULL) /* this indicates creation from template */
1006 {
1007 iIndex = _HMHandleQuery(hTemplateFile); /* query table for template */
1008 if (-1 == iIndex) /* this device is unknown to us */
1009 {
1010 SetLastError (ERROR_INVALID_HANDLE);
1011 return INVALID_HANDLE_VALUE;
1012 }
1013 else
1014 {
1015 /* to pass to handler */
1016 pHMHandleData = &TabWin32Handles[iIndex].hmHandleData;
1017 pDeviceHandler = TabWin32Handles[iIndex].pDeviceHandler;
1018 }
1019 }
1020 else
1021 {
1022 // name resolving
1023 CHAR szFilename[260];
1024 if (TRUE == HandleNamesResolveName((PSZ)lpFileName,
1025 szFilename,
1026 sizeof(szFilename),
1027 TRUE))
1028 {
1029 // use the resolved name
1030 lpFileName = szFilename;
1031 }
1032
1033
1034 pDeviceHandler = _HMDeviceFind((LPSTR)lpFileName); /* find device */
1035 if (NULL == pDeviceHandler) /* this name is unknown to us */
1036 {
1037 SetLastError(ERROR_FILE_NOT_FOUND);
1038 return (INVALID_HANDLE_VALUE); /* signal error */
1039 }
1040 else
1041 pHMHandleData = NULL;
1042
1043 pDevData = _HMDeviceGetData((LPSTR)lpFileName);
1044
1045 if(pDeviceHandler == HMGlobals.pHMOpen32) {
1046 pDeviceHandler = HMGlobals.pHMFile;
1047 }
1048 }
1049
1050
1051 iIndexNew = _HMHandleGetFree(); /* get free handle */
1052 if (-1 == iIndexNew) /* oops, no free handles ! */
1053 {
1054 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
1055 return (INVALID_HANDLE_VALUE); /* signal error */
1056 }
1057
1058
1059 /* initialize the complete HMHANDLEDATA structure */
1060 if (lpFileName == NULL) /* create from template */
1061 memcpy (&HMHandleTemp,
1062 &TabWin32Handles[iIndex].hmHandleData,
1063 sizeof(HMHANDLEDATA));
1064 else
1065 {
1066 HMHandleTemp.dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
1067 HMHandleTemp.dwAccess = dwDesiredAccess;
1068 HMHandleTemp.dwShare = dwShareMode;
1069 HMHandleTemp.dwCreation = dwCreationDisposition;
1070 HMHandleTemp.dwFlags = dwFlagsAndAttributes;
1071 HMHandleTemp.lpHandlerData = NULL;
1072 HMHandleTemp.lpDeviceData = pDevData;
1073 }
1074
1075 /* we've got to mark the handle as occupied here, since another device */
1076 /* could be created within the device handler -> deadlock */
1077
1078 /* write appropriate entry into the handle table if open succeeded */
1079 HMHandleTemp.hHMHandle = iIndexNew;
1080 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
1081 /* now copy back our temporary handle data */
1082 memcpy(&TabWin32Handles[iIndexNew].hmHandleData,
1083 &HMHandleTemp,
1084 sizeof(HMHANDLEDATA));
1085
1086 rc = pDeviceHandler->CreateFile((HANDLE)iIndexNew, lpFileName, /* call the device handler */
1087 &HMHandleTemp,
1088 lpSecurityAttributes,
1089 pHMHandleData);
1090
1091#ifdef DEBUG_LOCAL
1092 dprintf(("KERNEL32/HandleManager:CheckPoint2: %s lpHandlerData=%08xh\n",
1093 lpFileName,
1094 HMHandleTemp.lpHandlerData));
1095#endif
1096
1097 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
1098 {
1099 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
1100
1101
1102 // Note:
1103 // device handlers have to return an Win32-style error code
1104 // from CreateFile() !
1105 SetLastError(rc);
1106 return (INVALID_HANDLE_VALUE); /* signal error */
1107 }
1108 else
1109 {
1110 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
1111
1112 /* copy data fields that might have been modified by CreateFile */
1113 memcpy(&TabWin32Handles[iIndexNew].hmHandleData,
1114 &HMHandleTemp,
1115 sizeof(HMHANDLEDATA));
1116 }
1117
1118
1119#ifdef DEBUG_LOCAL
1120 dprintf(("KERNEL32/HandleManager: CreateFile(%s)=%08xh\n",
1121 lpFileName,
1122 iIndexNew));
1123#endif
1124
1125 return (HFILE)iIndexNew; /* return valid handle */
1126}
1127
1128
1129/*****************************************************************************
1130 * Name : HANDLE HMOpenFile
1131 * Purpose : Wrapper for the OpenFile() API
1132 * Parameters:
1133 * Variables :
1134 * Result :
1135 * Remark : Fix parameters passed to the HMDeviceManager::OpenFile
1136 * Supply access mode and share mode validation routines
1137 * Status :
1138 *
1139 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
1140 *****************************************************************************/
1141
1142
1143/***********************************************************************
1144 * FILE_ConvertOFMode
1145 *
1146 * Convert OF_* mode into flags for CreateFile.
1147 */
1148static void FILE_ConvertOFMode( INT mode, DWORD *access, DWORD *sharing )
1149{
1150 switch(mode & 0x03)
1151 {
1152 case OF_READ: *access = GENERIC_READ; break;
1153 case OF_WRITE: *access = GENERIC_WRITE; break;
1154 case OF_READWRITE: *access = GENERIC_READ | GENERIC_WRITE; break;
1155 default: *access = 0; break;
1156 }
1157 switch(mode & 0x70)
1158 {
1159 case OF_SHARE_EXCLUSIVE: *sharing = 0; break;
1160 case OF_SHARE_DENY_WRITE: *sharing = FILE_SHARE_READ; break;
1161 case OF_SHARE_DENY_READ: *sharing = FILE_SHARE_WRITE; break;
1162 case OF_SHARE_DENY_NONE:
1163 case OF_SHARE_COMPAT:
1164 default: *sharing = FILE_SHARE_READ | FILE_SHARE_WRITE; break;
1165 }
1166}
1167
1168HANDLE HMOpenFile(LPCSTR lpFileName,
1169 OFSTRUCT* pOFStruct,
1170 UINT fuMode)
1171{
1172 int iIndex; /* index into the handle table */
1173 int iIndexNew; /* index into the handle table */
1174 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
1175 VOID *pDevData;
1176 PHMHANDLEDATA pHMHandleData;
1177 DWORD rc; /* API return code */
1178
1179 // name resolving
1180 CHAR szFilename[260];
1181 if (TRUE == HandleNamesResolveName((PSZ)lpFileName,
1182 szFilename,
1183 sizeof(szFilename),
1184 TRUE))
1185 {
1186 // use the resolved name
1187 lpFileName = szFilename;
1188 }
1189
1190
1191 if(fuMode & OF_REOPEN) {
1192 pDeviceHandler = _HMDeviceFind((LPSTR)pOFStruct->szPathName); /* find device */
1193 }
1194 else pDeviceHandler = _HMDeviceFind((LPSTR)lpFileName); /* find device */
1195 if (NULL == pDeviceHandler) /* this name is unknown to us */
1196 {
1197 SetLastError(ERROR_FILE_NOT_FOUND);
1198 return (INVALID_HANDLE_VALUE); /* signal error */
1199 }
1200 else
1201 pHMHandleData = NULL;
1202
1203 if(fuMode & OF_REOPEN) {
1204 pDevData = _HMDeviceGetData((LPSTR)pOFStruct->szPathName);
1205 }
1206 else pDevData = _HMDeviceGetData((LPSTR)lpFileName);
1207
1208
1209 if(pDeviceHandler == HMGlobals.pHMOpen32) {
1210 pDeviceHandler = HMGlobals.pHMFile;
1211 }
1212
1213 iIndexNew = _HMHandleGetFree(); /* get free handle */
1214 if (-1 == iIndexNew) /* oops, no free handles ! */
1215 {
1216 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
1217 return (INVALID_HANDLE_VALUE); /* signal error */
1218 }
1219
1220
1221 /* initialize the complete HMHANDLEDATA structure */
1222 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
1223 pHMHandleData->dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
1224
1225 FILE_ConvertOFMode(fuMode, /* map OF_flags */
1226 &pHMHandleData->dwAccess,
1227 &pHMHandleData->dwShare);
1228
1229 //SvL; Must be OPEN_EXISTING because mmaps depend on it (to duplicate
1230 // the file handle when this handle is a parameter for CreateFileMappingA/W
1231 pHMHandleData->dwCreation = OPEN_EXISTING;
1232 pHMHandleData->dwFlags = 0;
1233 pHMHandleData->lpHandlerData = NULL;
1234 pHMHandleData->lpDeviceData = pDevData;
1235
1236 /* we've got to mark the handle as occupied here, since another device */
1237 /* could be created within the device handler -> deadlock */
1238
1239 /* write appropriate entry into the handle table if open succeeded */
1240 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
1241 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
1242
1243 rc = pDeviceHandler->OpenFile ((HANDLE)iIndexNew, lpFileName, /* call the device handler */
1244 &TabWin32Handles[iIndexNew].hmHandleData,
1245 pOFStruct,
1246 fuMode);
1247
1248#ifdef DEBUG_LOCAL
1249 dprintf(("KERNEL32/HandleManager:CheckPoint3: %s lpHandlerData=%08xh rc=%08xh\n",
1250 lpFileName,
1251 &TabWin32Handles[iIndexNew].hmHandleData.lpHandlerData,
1252 rc));
1253#endif
1254
1255 if(rc != NO_ERROR) /* oops, creation failed within the device handler */
1256 {
1257 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
1258 SetLastError(pOFStruct->nErrCode);
1259 return (INVALID_HANDLE_VALUE); /* signal error */
1260 }
1261 else {
1262 if(fuMode & (OF_DELETE|OF_EXIST)) {
1263 //file handle already closed
1264 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
1265 return TRUE; //TODO: correct?
1266 }
1267 if(fuMode & OF_PARSE) {
1268 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
1269 return 0;
1270 }
1271 if(fuMode & OF_VERIFY) {
1272 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
1273 return 1; //TODO: correct?
1274 }
1275 }
1276
1277#ifdef DEBUG_LOCAL
1278 dprintf(("KERNEL32/HandleManager: OpenFile(%s)=%08xh\n",
1279 lpFileName,
1280 iIndexNew));
1281#endif
1282
1283 return iIndexNew; /* return valid handle */
1284}
1285
1286
1287
1288/*****************************************************************************
1289 * Name : HANDLE HMCloseFile
1290 * Purpose : Wrapper for the CloseHandle() API
1291 * Parameters:
1292 * Variables :
1293 * Result :
1294 * Remark :
1295 * Status :
1296 *
1297 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
1298 *****************************************************************************/
1299
1300BOOL HMCloseHandle(HANDLE hObject)
1301{
1302 int iIndex; /* index into the handle table */
1303 BOOL fResult; /* result from the device handler's CloseHandle() */
1304 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1305
1306 /* validate handle */
1307 iIndex = _HMHandleQuery(hObject); /* get the index */
1308 if (-1 == iIndex) /* error ? */
1309 {
1310 //@@@PH it may occur someone closes e.g. a semaphore handle
1311 // which is not registered through the HandleManager yet.
1312 // so we try to pass on to Open32 instead.
1313 dprintf(("KERNEL32: HandleManager:HMCloseHandle(%08xh) passed on to Open32.\n",
1314 hObject));
1315
1316 fResult = O32_CloseHandle(hObject);
1317 return (fResult);
1318
1319 //SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1320 //return (FALSE); /* signal failure */
1321 }
1322
1323 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1324 //SvL: Check if pDeviceHandler is set
1325 if (pHMHandle->pDeviceHandler)
1326 {
1327 fResult = pHMHandle->pDeviceHandler->CloseHandle(&pHMHandle->hmHandleData);
1328 }
1329 else
1330 {
1331 dprintf(("HMCloseHAndle(%08xh): pDeviceHandler not set", hObject));
1332 fResult = TRUE;
1333 }
1334
1335 if (fResult == TRUE) /* remove handle if close succeeded */
1336 {
1337 pHMHandle->hmHandleData.hHMHandle = INVALID_HANDLE_VALUE; /* mark handle as free */
1338 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
1339 }
1340
1341 return (fResult); /* deliver return code */
1342}
1343
1344
1345/*****************************************************************************
1346 * Name : HANDLE HMReadFile
1347 * Purpose : Wrapper for the ReadHandle() API
1348 * Parameters:
1349 * Variables :
1350 * Result :
1351 * Remark :
1352 * Status :
1353 *
1354 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
1355 *****************************************************************************/
1356
1357BOOL HMReadFile(HANDLE hFile,
1358 LPVOID lpBuffer,
1359 DWORD nNumberOfBytesToRead,
1360 LPDWORD lpNumberOfBytesRead,
1361 LPOVERLAPPED lpOverlapped)
1362{
1363 int iIndex; /* index into the handle table */
1364 BOOL fResult; /* result from the device handler's CloseHandle() */
1365 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1366
1367 /* validate handle */
1368 iIndex = _HMHandleQuery(hFile); /* get the index */
1369 if (-1 == iIndex) /* error ? */
1370 {
1371 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1372 return (FALSE); /* signal failure */
1373 }
1374
1375 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1376 fResult = pHMHandle->pDeviceHandler->ReadFile(&pHMHandle->hmHandleData,
1377 lpBuffer,
1378 nNumberOfBytesToRead,
1379 lpNumberOfBytesRead,
1380 lpOverlapped);
1381
1382 return (fResult); /* deliver return code */
1383}
1384/*****************************************************************************
1385 * Name : HANDLE HMReadFileEx
1386 * Purpose : Wrapper for the ReadFileEx() API
1387 * Parameters:
1388 * Variables :
1389 * Result :
1390 * Remark :
1391 * Status :
1392 *
1393 * Author : SvL
1394 *****************************************************************************/
1395BOOL HMReadFileEx(HANDLE hFile,
1396 LPVOID lpBuffer,
1397 DWORD nNumberOfBytesToRead,
1398 LPOVERLAPPED lpOverlapped,
1399 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
1400{
1401 int iIndex; /* index into the handle table */
1402 BOOL fResult; /* result from the device handler's CloseHandle() */
1403 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1404
1405 /* validate handle */
1406 iIndex = _HMHandleQuery(hFile); /* get the index */
1407 if (-1 == iIndex) /* error ? */
1408 {
1409 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1410 return (FALSE); /* signal failure */
1411 }
1412
1413 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1414 fResult = pHMHandle->pDeviceHandler->ReadFileEx(&pHMHandle->hmHandleData,
1415 lpBuffer,
1416 nNumberOfBytesToRead,
1417 lpOverlapped,
1418 lpCompletionRoutine);
1419
1420 return (fResult); /* deliver return code */
1421}
1422
1423/*****************************************************************************
1424 * Name : HANDLE HMWriteFile
1425 * Purpose : Wrapper for the WriteHandle() API
1426 * Parameters:
1427 * Variables :
1428 * Result :
1429 * Remark :
1430 * Status :
1431 *
1432 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
1433 *****************************************************************************/
1434
1435BOOL HMWriteFile(HANDLE hFile,
1436 LPCVOID lpBuffer,
1437 DWORD nNumberOfBytesToWrite,
1438 LPDWORD lpNumberOfBytesWritten,
1439 LPOVERLAPPED lpOverlapped)
1440{
1441 int iIndex; /* index into the handle table */
1442 BOOL fResult; /* result from the device handler's CloseHandle() */
1443 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1444
1445 /* validate handle */
1446 iIndex = _HMHandleQuery(hFile); /* get the index */
1447 if (-1 == iIndex) /* error ? */
1448 {
1449 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1450 return (FALSE); /* signal failure */
1451 }
1452
1453 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1454 fResult = pHMHandle->pDeviceHandler->WriteFile(&pHMHandle->hmHandleData,
1455 lpBuffer,
1456 nNumberOfBytesToWrite,
1457 lpNumberOfBytesWritten,
1458 lpOverlapped);
1459
1460 return (fResult); /* deliver return code */
1461}
1462
1463/*****************************************************************************
1464 * Name : HANDLE HMWriteFileEx
1465 * Purpose : Wrapper for the WriteFileEx() API
1466 * Parameters:
1467 * Variables :
1468 * Result :
1469 * Remark :
1470 * Status :
1471 *
1472 * Author : SvL
1473 *****************************************************************************/
1474BOOL HMWriteFileEx(HANDLE hFile,
1475 LPVOID lpBuffer,
1476 DWORD nNumberOfBytesToWrite,
1477 LPOVERLAPPED lpOverlapped,
1478 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
1479{
1480 int iIndex; /* index into the handle table */
1481 BOOL fResult; /* result from the device handler's CloseHandle() */
1482 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1483
1484 /* validate handle */
1485 iIndex = _HMHandleQuery(hFile); /* get the index */
1486 if (-1 == iIndex) /* error ? */
1487 {
1488 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1489 return (FALSE); /* signal failure */
1490 }
1491
1492 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1493 fResult = pHMHandle->pDeviceHandler->WriteFileEx(&pHMHandle->hmHandleData,
1494 lpBuffer,
1495 nNumberOfBytesToWrite,
1496 lpOverlapped,
1497 lpCompletionRoutine);
1498
1499 return (fResult); /* deliver return code */
1500}
1501
1502
1503/*****************************************************************************
1504 * Name : HANDLE HMGetFileType
1505 * Purpose : Wrapper for the GetFileType() API
1506 * Parameters:
1507 * Variables :
1508 * Result :
1509 * Remark :
1510 * Status :
1511 *
1512 * Author : Patrick Haller [Wed, 1998/02/12 13:37]
1513 *****************************************************************************/
1514
1515DWORD HMGetFileType(HANDLE hFile)
1516{
1517 int iIndex; /* index into the handle table */
1518 DWORD dwResult; /* result from the device handler's API */
1519 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1520
1521 /* validate handle */
1522 iIndex = _HMHandleQuery(hFile); /* get the index */
1523 if (-1 == iIndex) /* error ? */
1524 {
1525 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1526 return FILE_TYPE_UNKNOWN; /* signal failure */
1527 }
1528
1529 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1530 dwResult = pHMHandle->pDeviceHandler->GetFileType(&pHMHandle->hmHandleData);
1531
1532 return (dwResult); /* deliver return code */
1533}
1534
1535
1536/*****************************************************************************
1537 * Name : HMDeviceHandler::_DeviceReuqest
1538 * Purpose : entry method for special request functions
1539 * Parameters: ULONG ulRequestCode
1540 * various parameters as required
1541 * Variables :
1542 * Result :
1543 * Remark : the standard behaviour is to return an error code for non-
1544 * existant request codes
1545 * Status :
1546 *
1547 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
1548 *****************************************************************************/
1549
1550DWORD HMDeviceRequest (HANDLE hFile,
1551 ULONG ulRequestCode,
1552 ULONG arg1,
1553 ULONG arg2,
1554 ULONG arg3,
1555 ULONG arg4)
1556{
1557 int iIndex; /* index into the handle table */
1558 DWORD dwResult; /* result from the device handler's API */
1559 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1560
1561 /* validate handle */
1562 iIndex = _HMHandleQuery(hFile); /* get the index */
1563 if (-1 == iIndex) /* error ? */
1564 {
1565 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1566 return (INVALID_HANDLE_ERROR); /* signal failure */
1567 }
1568
1569 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1570 dwResult = pHMHandle->pDeviceHandler->_DeviceRequest(&pHMHandle->hmHandleData,
1571 ulRequestCode,
1572 arg1,
1573 arg2,
1574 arg3,
1575 arg4);
1576
1577 return (dwResult); /* deliver return code */
1578}
1579
1580
1581/*****************************************************************************
1582 * Name : HMDeviceHandler::GetFileInformationByHandle
1583 * Purpose : router function for GetFileInformationByHandle
1584 * Parameters:
1585 * Variables :
1586 * Result :
1587 * Remark :
1588 * Status :
1589 *
1590 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1591 *****************************************************************************/
1592
1593BOOL HMGetFileInformationByHandle (HANDLE hFile,
1594 BY_HANDLE_FILE_INFORMATION *pHFI)
1595{
1596 int iIndex; /* index into the handle table */
1597 DWORD dwResult; /* result from the device handler's API */
1598 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1599
1600 /* validate handle */
1601 iIndex = _HMHandleQuery(hFile); /* get the index */
1602 if (-1 == iIndex) /* error ? */
1603 {
1604 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1605 return FALSE; /* signal failure */
1606 }
1607
1608 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1609 dwResult = pHMHandle->pDeviceHandler->GetFileInformationByHandle(&pHMHandle->hmHandleData,
1610 pHFI);
1611
1612 return (dwResult); /* deliver return code */
1613}
1614
1615
1616/*****************************************************************************
1617 * Name : HMDeviceHandler::SetEndOfFile
1618 * Purpose : router function for SetEndOfFile
1619 * Parameters:
1620 * Variables :
1621 * Result :
1622 * Remark :
1623 * Status :
1624 *
1625 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1626 *****************************************************************************/
1627
1628BOOL HMSetEndOfFile (HANDLE hFile)
1629{
1630 int iIndex; /* index into the handle table */
1631 BOOL bResult; /* result from the device handler's API */
1632 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1633
1634 /* validate handle */
1635 iIndex = _HMHandleQuery(hFile); /* get the index */
1636 if (-1 == iIndex) /* error ? */
1637 {
1638 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1639 return FALSE; /* signal failure */
1640 }
1641
1642 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1643 bResult = pHMHandle->pDeviceHandler->SetEndOfFile(&pHMHandle->hmHandleData);
1644
1645 return (bResult); /* deliver return code */
1646}
1647
1648
1649/*****************************************************************************
1650 * Name : HMDeviceHandler::SetFileTime
1651 * Purpose : router function for SetFileTime
1652 * Parameters:
1653 * Variables :
1654 * Result :
1655 * Remark :
1656 * Status :
1657 *
1658 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1659 *****************************************************************************/
1660
1661BOOL HMSetFileTime (HANDLE hFile,
1662 const FILETIME *pFT1,
1663 const FILETIME *pFT2,
1664 const FILETIME *pFT3)
1665{
1666 int iIndex; /* index into the handle table */
1667 BOOL bResult; /* result from the device handler's API */
1668 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1669
1670 /* validate handle */
1671 iIndex = _HMHandleQuery(hFile); /* get the index */
1672 if (-1 == iIndex) /* error ? */
1673 {
1674 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1675 return FALSE; /* signal failure */
1676 }
1677
1678 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1679 bResult = pHMHandle->pDeviceHandler->SetFileTime(&pHMHandle->hmHandleData,
1680 (LPFILETIME)pFT1,
1681 (LPFILETIME)pFT2,
1682 (LPFILETIME)pFT3);
1683
1684 return (bResult); /* deliver return code */
1685}
1686
1687/*****************************************************************************
1688 * Name : HMDeviceHandler::GetFileTime
1689 * Purpose : router function for SetFileTime
1690 * Parameters:
1691 * Variables :
1692 * Result :
1693 * Remark :
1694 * Status :
1695 *
1696 * Author : SvL
1697 *****************************************************************************/
1698
1699BOOL HMGetFileTime (HANDLE hFile,
1700 const FILETIME *pFT1,
1701 const FILETIME *pFT2,
1702 const FILETIME *pFT3)
1703{
1704 int iIndex; /* index into the handle table */
1705 BOOL bResult; /* result from the device handler's API */
1706 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1707
1708 /* validate handle */
1709 iIndex = _HMHandleQuery(hFile); /* get the index */
1710 if (-1 == iIndex) /* error ? */
1711 {
1712 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1713 return FALSE; /* signal failure */
1714 }
1715
1716 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1717 bResult = pHMHandle->pDeviceHandler->GetFileTime(&pHMHandle->hmHandleData,
1718 (LPFILETIME)pFT1,
1719 (LPFILETIME)pFT2,
1720 (LPFILETIME)pFT3);
1721
1722 return (bResult); /* deliver return code */
1723}
1724
1725
1726/*****************************************************************************
1727 * Name : HMDeviceHandler::GetFileSize
1728 * Purpose : router function for GetFileSize
1729 * Parameters:
1730 * Variables :
1731 * Result :
1732 * Remark :
1733 * Status :
1734 *
1735 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1736 *****************************************************************************/
1737
1738DWORD HMGetFileSize (HANDLE hFile,
1739 PDWORD pSize)
1740{
1741 int iIndex; /* index into the handle table */
1742 DWORD dwResult; /* result from the device handler's API */
1743 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1744
1745 /* validate handle */
1746 iIndex = _HMHandleQuery(hFile); /* get the index */
1747 if (-1 == iIndex) /* error ? */
1748 {
1749 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1750 return (INVALID_HANDLE_ERROR); /* signal failure */
1751 }
1752
1753 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1754 dwResult = pHMHandle->pDeviceHandler->GetFileSize(&pHMHandle->hmHandleData,
1755 pSize);
1756
1757 return (dwResult); /* deliver return code */
1758}
1759
1760
1761/*****************************************************************************
1762 * Name : HMDeviceHandler::SetFilePointer
1763 * Purpose : router function for SetFilePointer
1764 * Parameters:
1765 * Variables :
1766 * Result :
1767 * Remark :
1768 * Status :
1769 *
1770 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1771 *****************************************************************************/
1772
1773DWORD HMSetFilePointer (HANDLE hFile,
1774 LONG lDistanceToMove,
1775 PLONG lpDistanceToMoveHigh,
1776 DWORD dwMoveMethod)
1777{
1778 int iIndex; /* index into the handle table */
1779 DWORD dwResult; /* result from the device handler's API */
1780 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1781
1782 /* validate handle */
1783 iIndex = _HMHandleQuery(hFile); /* get the index */
1784 if (-1 == iIndex) /* error ? */
1785 {
1786 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1787 return (INVALID_HANDLE_ERROR); /* signal failure */
1788 }
1789
1790 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1791 dwResult = pHMHandle->pDeviceHandler->SetFilePointer(&pHMHandle->hmHandleData,
1792 lDistanceToMove,
1793 lpDistanceToMoveHigh,
1794 dwMoveMethod);
1795
1796 return (dwResult); /* deliver return code */
1797}
1798
1799
1800/*****************************************************************************
1801 * Name : HMDeviceHandler::LockFile
1802 * Purpose : router function for LockFile
1803 * Parameters:
1804 * Variables :
1805 * Result :
1806 * Remark :
1807 * Status :
1808 *
1809 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1810 *****************************************************************************/
1811
1812BOOL HMLockFile (HFILE hFile,
1813 DWORD arg2,
1814 DWORD arg3,
1815 DWORD arg4,
1816 DWORD arg5)
1817{
1818 int iIndex; /* index into the handle table */
1819 DWORD dwResult; /* result from the device handler's API */
1820 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1821
1822 /* validate handle */
1823 iIndex = _HMHandleQuery(hFile); /* get the index */
1824 if (-1 == iIndex) /* error ? */
1825 {
1826 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1827 return FALSE; /* signal failure */
1828 }
1829
1830 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1831 dwResult = pHMHandle->pDeviceHandler->LockFile(&pHMHandle->hmHandleData,
1832 arg2,
1833 arg3,
1834 arg4,
1835 arg5);
1836
1837 return (dwResult); /* deliver return code */
1838}
1839
1840
1841/*****************************************************************************
1842 * Name : HMDeviceHandler::LockFileEx
1843 * Purpose : router function for LockFileEx
1844 * Parameters:
1845 * Variables :
1846 * Result :
1847 * Remark :
1848 * Status :
1849 *
1850 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1851 *****************************************************************************/
1852
1853BOOL HMLockFileEx(HANDLE hFile,
1854 DWORD dwFlags,
1855 DWORD dwReserved,
1856 DWORD nNumberOfBytesToLockLow,
1857 DWORD nNumberOfBytesToLockHigh,
1858 LPOVERLAPPED lpOverlapped)
1859{
1860 int iIndex; /* index into the handle table */
1861 DWORD dwResult; /* result from the device handler's API */
1862 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1863
1864 /* validate handle */
1865 iIndex = _HMHandleQuery(hFile); /* get the index */
1866 if (-1 == iIndex) /* error ? */
1867 {
1868 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1869 return FALSE; /* signal failure */
1870 }
1871
1872 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1873 dwResult = pHMHandle->pDeviceHandler->LockFileEx(&pHMHandle->hmHandleData,
1874 dwFlags,
1875 dwReserved,
1876 nNumberOfBytesToLockLow,
1877 nNumberOfBytesToLockHigh,
1878 lpOverlapped);
1879
1880 return (dwResult); /* deliver return code */
1881}
1882
1883
1884
1885/*****************************************************************************
1886 * Name : HMDeviceHandler::UnlockFile
1887 * Purpose : router function for UnlockFile
1888 * Parameters:
1889 * Variables :
1890 * Result :
1891 * Remark :
1892 * Status :
1893 *
1894 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1895 *****************************************************************************/
1896
1897BOOL HMUnlockFile (HFILE hFile,
1898 DWORD arg2,
1899 DWORD arg3,
1900 DWORD arg4,
1901 DWORD arg5)
1902{
1903 int iIndex; /* index into the handle table */
1904 DWORD dwResult; /* result from the device handler's API */
1905 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1906
1907 /* validate handle */
1908 iIndex = _HMHandleQuery(hFile); /* get the index */
1909 if (-1 == iIndex) /* error ? */
1910 {
1911 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1912 return FALSE; /* signal failure */
1913 }
1914
1915 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1916 dwResult = pHMHandle->pDeviceHandler->UnlockFile(&pHMHandle->hmHandleData,
1917 arg2,
1918 arg3,
1919 arg4,
1920 arg5);
1921
1922 return (dwResult); /* deliver return code */
1923}
1924
1925
1926/*****************************************************************************
1927 * Name : HMDeviceHandler::UnlockFileEx
1928 * Purpose : router function for UnlockFileEx
1929 * Parameters:
1930 * Variables :
1931 * Result :
1932 * Remark :
1933 * Status :
1934 *
1935 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1936 *****************************************************************************/
1937
1938BOOL HMUnlockFileEx(HANDLE hFile,
1939 DWORD dwReserved,
1940 DWORD nNumberOfBytesToLockLow,
1941 DWORD nNumberOfBytesToLockHigh,
1942 LPOVERLAPPED lpOverlapped)
1943{
1944 int iIndex; /* index into the handle table */
1945 DWORD dwResult; /* result from the device handler's API */
1946 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1947
1948 /* validate handle */
1949 iIndex = _HMHandleQuery(hFile); /* get the index */
1950 if (-1 == iIndex) /* error ? */
1951 {
1952 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
1953 return FALSE; /* signal failure */
1954 }
1955
1956 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
1957 dwResult = pHMHandle->pDeviceHandler->UnlockFileEx(&pHMHandle->hmHandleData,
1958 dwReserved,
1959 nNumberOfBytesToLockLow,
1960 nNumberOfBytesToLockHigh,
1961 lpOverlapped);
1962
1963 return (dwResult); /* deliver return code */
1964}
1965
1966
1967/*****************************************************************************
1968 * Name : HMDeviceHandler::WaitForSingleObject
1969 * Purpose : router function for WaitForSingleObject
1970 * Parameters:
1971 * Variables :
1972 * Result :
1973 * Remark :
1974 * Status :
1975 *
1976 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
1977 *****************************************************************************/
1978
1979DWORD HMWaitForSingleObject(HANDLE hObject,
1980 DWORD dwTimeout)
1981{
1982 int iIndex; /* index into the handle table */
1983 DWORD dwResult; /* result from the device handler's API */
1984 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1985
1986 /* validate handle */
1987 iIndex = _HMHandleQuery(hObject); /* get the index */
1988 if (-1 == iIndex) /* error ? */
1989 {
1990 dprintf(("KERNEL32: HandleManager:HMWaitForSingleObject(%08xh) passed on to Open32.\n",
1991 hObject));
1992
1993 if(dwTimeout == INFINITE) {
1994 //Workaround for applications that block the PM input queue
1995 //while waiting for a child process to terminate.
1996 //(WaitSingleObject now calls MsgWaitMultipleObjects and
1997 // processes messages while waiting for the process to die)
1998 //(Napster install now doesn't block PM anymore (forcing a reboot))
1999
2000 HMODULE hUser32 = LoadLibraryA("USER32.DLL");
2001
2002 BOOL (* WINAPI pfnPeekMessageA)(LPMSG,HWND,UINT,UINT,UINT);
2003 LONG (* WINAPI pfnDispatchMessageA)(const MSG*);
2004
2005 *(FARPROC *)&pfnPeekMessageA = GetProcAddress(hUser32,"PeekMessageA");
2006 *(FARPROC *)&pfnDispatchMessageA = GetProcAddress(hUser32,"DispatchMessageA");
2007
2008 TEB *teb = GetThreadTEB();
2009
2010 if(!teb || !pfnPeekMessageA || !pfnDispatchMessageA) {
2011 dprintf(("ERROR: !teb || !pfnPeekMessageA || !pfnDispatchMessageA"));
2012 DebugInt3();
2013 return WAIT_FAILED;
2014 }
2015
2016 //TODO: Ignoring all messages could be dangerous. But processing them,
2017 //while the app doesn't expect any, isn't safe either.
2018//-> must active check in pmwindow.cpp if this is enabled again!
2019// teb->o.odin.fIgnoreMsgs = TRUE;
2020
2021 while(TRUE) {
2022 dwResult = HMMsgWaitForMultipleObjects(1, &hObject, FALSE,
2023 INFINITE, QS_ALLINPUT);
2024 if(dwResult == WAIT_OBJECT_0 + 1) {
2025 MSG msg ;
2026
2027 while (pfnPeekMessageA(&msg, NULL, 0, 0, PM_REMOVE))
2028 {
2029 if (msg.message == WM_QUIT) {
2030 dprintf(("ERROR: WaitForSingleObject call abandoned because WM_QUIT msg was received!!"));
2031// teb->o.odin.fIgnoreMsgs = FALSE;
2032 FreeLibrary(hUser32);
2033 return WAIT_ABANDONED;
2034 }
2035
2036 /* otherwise dispatch it */
2037 pfnDispatchMessageA(&msg);
2038
2039 } // end of PeekMessage while loop
2040 }
2041 else {
2042 dprintf(("WaitForSingleObject: Process %x terminated", hObject));
2043 break;
2044 }
2045 }
2046// teb->o.odin.fIgnoreMsgs = FALSE;
2047 FreeLibrary(hUser32);
2048 return dwResult;
2049 }
2050 else {
2051 // maybe handles from CreateProcess() ...
2052 dwResult = O32_WaitForSingleObject(hObject, dwTimeout);
2053 return (dwResult);
2054 }
2055 }
2056
2057 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
2058 dwResult = pHMHandle->pDeviceHandler->WaitForSingleObject(&pHMHandle->hmHandleData,
2059 dwTimeout);
2060
2061 return (dwResult); /* deliver return code */
2062}
2063
2064
2065/*****************************************************************************
2066 * Name : HMDeviceHandler::WaitForSingleObjectEx
2067 * Purpose : router function for WaitForSingleObjectEx
2068 * Parameters:
2069 * Variables :
2070 * Result :
2071 * Remark :
2072 * Status :
2073 *
2074 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
2075 *****************************************************************************/
2076
2077DWORD HMWaitForSingleObjectEx(HANDLE hObject,
2078 DWORD dwTimeout,
2079 BOOL fAlertable)
2080{
2081 int iIndex; /* index into the handle table */
2082 DWORD dwResult; /* result from the device handler's API */
2083 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
2084
2085 /* validate handle */
2086 iIndex = _HMHandleQuery(hObject); /* get the index */
2087 if (-1 == iIndex) /* error ? */
2088 {
2089 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
2090 return WAIT_FAILED; /* signal failure */
2091 }
2092
2093 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
2094 dwResult = pHMHandle->pDeviceHandler->WaitForSingleObjectEx(&pHMHandle->hmHandleData,
2095 dwTimeout,
2096 fAlertable);
2097
2098 return (dwResult); /* deliver return code */
2099}
2100
2101
2102/*****************************************************************************
2103 * Name : HMDeviceHandler::FlushFileBuffers
2104 * Purpose : router function for FlushFileBuffers
2105 * Parameters:
2106 * Variables :
2107 * Result :
2108 * Remark :
2109 * Status :
2110 *
2111 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
2112 *****************************************************************************/
2113
2114BOOL HMFlushFileBuffers(HANDLE hFile)
2115{
2116 int iIndex; /* index into the handle table */
2117 DWORD dwResult; /* result from the device handler's API */
2118 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
2119
2120 /* validate handle */
2121 iIndex = _HMHandleQuery(hFile); /* get the index */
2122 if (-1 == iIndex) /* error ? */
2123 {
2124 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
2125 return FALSE; /* signal failure */
2126 }
2127
2128 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
2129 dwResult = pHMHandle->pDeviceHandler->FlushFileBuffers(&pHMHandle->hmHandleData);
2130
2131 return (dwResult); /* deliver return code */
2132}
2133
2134
2135/*****************************************************************************
2136 * Name : HMDeviceHandler::GetOverlappedResult
2137 * Purpose : router function for GetOverlappedResult
2138 * Parameters:
2139 * Variables :
2140 * Result :
2141 * Remark :
2142 * Status :
2143 *
2144 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
2145 *****************************************************************************/
2146
2147BOOL HMGetOverlappedResult(HANDLE hObject,
2148 LPOVERLAPPED lpOverlapped,
2149 LPDWORD arg3,
2150 BOOL arg4)
2151{
2152 int iIndex; /* index into the handle table */
2153 DWORD dwResult; /* result from the device handler's API */
2154 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
2155
2156 /* validate handle */
2157 iIndex = _HMHandleQuery(hObject); /* get the index */
2158 if (-1 == iIndex) /* error ? */
2159 {
2160 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
2161 return FALSE; /* signal failure */
2162 }
2163
2164 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
2165 dwResult = pHMHandle->pDeviceHandler->GetOverlappedResult(&pHMHandle->hmHandleData,
2166 lpOverlapped,
2167 arg3,
2168 arg4);
2169
2170 return (dwResult); /* deliver return code */
2171}
2172
2173
2174/*****************************************************************************
2175 * Name : HMReleaseMutex
2176 * Purpose : router function for ReleaseMutex
2177 * Parameters:
2178 * Variables :
2179 * Result :
2180 * Remark :
2181 * Status :
2182 *
2183 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
2184 *****************************************************************************/
2185
2186BOOL HMReleaseMutex(HANDLE hObject)
2187{
2188 int iIndex; /* index into the handle table */
2189 DWORD dwResult; /* result from the device handler's API */
2190 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
2191
2192 /* validate handle */
2193 iIndex = _HMHandleQuery(hObject); /* get the index */
2194 if (-1 == iIndex) /* error ? */
2195 {
2196 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
2197 return FALSE; /* signal failure */
2198 }
2199
2200 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
2201 dwResult = pHMHandle->pDeviceHandler->ReleaseMutex(&pHMHandle->hmHandleData);
2202
2203 return (dwResult); /* deliver return code */
2204}
2205
2206
2207/*****************************************************************************
2208 * Name : HMSetEvent
2209 * Purpose : router function for SetEvent
2210 * Parameters:
2211 * Variables :
2212 * Result :
2213 * Remark :
2214 * Status :
2215 *
2216 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
2217 *****************************************************************************/
2218
2219BOOL HMSetEvent(HANDLE hEvent)
2220{
2221 int iIndex; /* index into the handle table */
2222 DWORD dwResult; /* result from the device handler's API */
2223 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
2224
2225 /* validate handle */
2226 iIndex = _HMHandleQuery(hEvent); /* get the index */
2227 if (-1 == iIndex) /* error ? */
2228 {
2229 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
2230 return FALSE; /* signal failure */
2231 }
2232
2233 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
2234 dwResult = pHMHandle->pDeviceHandler->SetEvent(&pHMHandle->hmHandleData);
2235
2236 return (dwResult); /* deliver return code */
2237}
2238
2239
2240/*****************************************************************************
2241 * Name : HMPulseEvent
2242 * Purpose : router function for PulseEvent
2243 * Parameters:
2244 * Variables :
2245 * Result :
2246 * Remark :
2247 * Status :
2248 *
2249 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
2250 *****************************************************************************/
2251
2252BOOL HMPulseEvent(HANDLE hEvent)
2253{
2254 int iIndex; /* index into the handle table */
2255 DWORD dwResult; /* result from the device handler's API */
2256 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
2257
2258 /* validate handle */
2259 iIndex = _HMHandleQuery(hEvent); /* get the index */
2260 if (-1 == iIndex) /* error ? */
2261 {
2262 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
2263 return FALSE; /* signal failure */
2264 }
2265
2266 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
2267 dwResult = pHMHandle->pDeviceHandler->PulseEvent(&pHMHandle->hmHandleData);
2268
2269 return (dwResult); /* deliver return code */
2270}
2271
2272
2273/*****************************************************************************
2274 * Name : HMResetEvent
2275 * Purpose : router function for ResetEvent
2276 * Parameters:
2277 * Variables :
2278 * Result :
2279 * Remark :
2280 * Status :
2281 *
2282 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
2283 *****************************************************************************/
2284
2285BOOL HMResetEvent(HANDLE hEvent)
2286{
2287 int iIndex; /* index into the handle table */
2288 DWORD dwResult; /* result from the device handler's API */
2289 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
2290
2291 /* validate handle */
2292 iIndex = _HMHandleQuery(hEvent); /* get the index */
2293 if (-1 == iIndex) /* error ? */
2294 {
2295 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
2296 return FALSE; /* signal failure */
2297 }
2298
2299 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
2300 dwResult = pHMHandle->pDeviceHandler->ResetEvent(&pHMHandle->hmHandleData);
2301
2302 return (dwResult); /* deliver return code */
2303}
2304
2305
2306/*****************************************************************************
2307 * Name : HANDLE HMCreateEvent
2308 * Purpose : Wrapper for the CreateEvent() API
2309 * Parameters:
2310 * Variables :
2311 * Result :
2312 * Remark :
2313 * Status :
2314 *
2315 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
2316 *****************************************************************************/
2317
2318HANDLE HMCreateEvent(LPSECURITY_ATTRIBUTES lpsa,
2319 BOOL bManualReset,
2320 BOOL bInitialState,
2321 LPCTSTR lpName)
2322{
2323 int iIndex; /* index into the handle table */
2324 int iIndexNew; /* index into the handle table */
2325 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
2326 PHMHANDLEDATA pHMHandleData;
2327 DWORD rc; /* API return code */
2328
2329
2330 if(lpName) { //check if shared event semaphore already exists
2331 //TODO: No inheritance??
2332 HANDLE handle = HMOpenEvent(EVENT_ALL_ACCESS, FALSE, lpName);
2333 if(handle) {
2334 dprintf(("CreateEvent: return handle of existing event semaphore %x", handle));
2335 SetLastError(ERROR_ALREADY_EXISTS);
2336 return handle;
2337 }
2338 }
2339
2340 pDeviceHandler = HMGlobals.pHMEvent; /* device is predefined */
2341
2342 iIndexNew = _HMHandleGetFree(); /* get free handle */
2343 if (-1 == iIndexNew) /* oops, no free handles ! */
2344 {
2345 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
2346 return 0; /* signal error */
2347 }
2348
2349 /* Initialize the complete HMHANDLEDATA structure */
2350 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
2351 pHMHandleData->dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
2352 pHMHandleData->dwAccess = 0;
2353 pHMHandleData->dwShare = 0;
2354 pHMHandleData->dwCreation = 0;
2355 pHMHandleData->dwFlags = 0;
2356 pHMHandleData->lpHandlerData = NULL;
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->CreateEvent(&TabWin32Handles[iIndexNew].hmHandleData,
2367 lpsa,
2368 bManualReset,
2369 bInitialState,
2370 lpName);
2371 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
2372 {
2373 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
2374 SetLastError(rc); /* Hehe, OS/2 and NT are pretty compatible :) */
2375 return 0; /* signal error */
2376 }
2377 else
2378 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
2379
2380 return iIndexNew; /* return valid handle */
2381}
2382
2383
2384/*****************************************************************************
2385 * Name : HANDLE HMCreateMutex
2386 * Purpose : Wrapper for the CreateMutex() API
2387 * Parameters:
2388 * Variables :
2389 * Result :
2390 * Remark :
2391 * Status :
2392 *
2393 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
2394 *****************************************************************************/
2395
2396HANDLE HMCreateMutex(LPSECURITY_ATTRIBUTES lpsa,
2397 BOOL bInitialOwner,
2398 LPCTSTR lpName)
2399{
2400 int iIndex; /* index into the handle table */
2401 int iIndexNew; /* index into the handle table */
2402 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
2403 PHMHANDLEDATA pHMHandleData;
2404 DWORD rc; /* API return code */
2405
2406
2407 if(lpName) { //check if shared mutex semaphore already exists
2408 //TODO: No inheritance??
2409 HANDLE handle = HMOpenMutex(MUTEX_ALL_ACCESS, FALSE, lpName);
2410 if(handle) {
2411 dprintf(("CreateMutex: return handle of existing mutex semaphore %x", handle));
2412 SetLastError(ERROR_ALREADY_EXISTS);
2413 return handle;
2414 }
2415 }
2416
2417 pDeviceHandler = HMGlobals.pHMMutex; /* device is predefined */
2418
2419 iIndexNew = _HMHandleGetFree(); /* get free handle */
2420 if (-1 == iIndexNew) /* oops, no free handles ! */
2421 {
2422 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
2423 return 0; /* signal error */
2424 }
2425
2426
2427 /* initialize the complete HMHANDLEDATA structure */
2428 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
2429 pHMHandleData->dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
2430 pHMHandleData->dwAccess = 0;
2431 pHMHandleData->dwShare = 0;
2432 pHMHandleData->dwCreation = 0;
2433 pHMHandleData->dwFlags = 0;
2434 pHMHandleData->lpHandlerData = NULL;
2435
2436
2437 /* we've got to mark the handle as occupied here, since another device */
2438 /* could be created within the device handler -> deadlock */
2439
2440 /* write appropriate entry into the handle table if open succeeded */
2441 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
2442 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
2443
2444 /* call the device handler */
2445 rc = pDeviceHandler->CreateMutex(&TabWin32Handles[iIndexNew].hmHandleData,
2446 lpsa,
2447 bInitialOwner,
2448 lpName);
2449 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
2450 {
2451 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
2452 SetLastError(rc); /* Hehe, OS/2 and NT are pretty compatible :) */
2453 return 0; /* signal error */
2454 }
2455 else
2456 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
2457
2458 return iIndexNew; /* return valid handle */
2459}
2460
2461
2462/*****************************************************************************
2463 * Name : HANDLE HMOpenEvent
2464 * Purpose : Wrapper for the OpenEvent() API
2465 * Parameters:
2466 * Variables :
2467 * Result :
2468 * Remark :
2469 * Status :
2470 *
2471 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
2472 *****************************************************************************/
2473
2474HANDLE HMOpenEvent(DWORD fdwAccess,
2475 BOOL fInherit,
2476 LPCTSTR lpName)
2477{
2478 int iIndex; /* index into the handle table */
2479 int iIndexNew; /* index into the handle table */
2480 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
2481 PHMHANDLEDATA pHMHandleData;
2482 DWORD rc; /* API return code */
2483
2484
2485 pDeviceHandler = HMGlobals.pHMEvent; /* device is predefined */
2486
2487 iIndexNew = _HMHandleGetFree(); /* get free handle */
2488 if (-1 == iIndexNew) /* oops, no free handles ! */
2489 {
2490 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
2491 return 0; /* signal error */
2492 }
2493
2494
2495 /* initialize the complete HMHANDLEDATA structure */
2496 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
2497 pHMHandleData->dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
2498 pHMHandleData->dwAccess = fdwAccess;
2499 pHMHandleData->dwShare = 0;
2500 pHMHandleData->dwCreation = 0;
2501 pHMHandleData->dwFlags = 0;
2502 pHMHandleData->lpHandlerData = NULL;
2503
2504
2505 /* we've got to mark the handle as occupied here, since another device */
2506 /* could be created within the device handler -> deadlock */
2507
2508 /* write appropriate entry into the handle table if open succeeded */
2509 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
2510 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
2511
2512 /* call the device handler */
2513 rc = pDeviceHandler->OpenEvent(&TabWin32Handles[iIndexNew].hmHandleData,
2514 fInherit,
2515 lpName);
2516 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
2517 {
2518 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
2519 SetLastError(rc); /* Hehe, OS/2 and NT are pretty compatible :) */
2520 return 0; /* signal error */
2521 }
2522 else
2523 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
2524
2525 return iIndexNew; /* return valid handle */
2526}
2527
2528
2529/*****************************************************************************
2530 * Name : HANDLE HMOpenMutex
2531 * Purpose : Wrapper for the OpenMutex() API
2532 * Parameters:
2533 * Variables :
2534 * Result :
2535 * Remark :
2536 * Status :
2537 *
2538 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
2539 *****************************************************************************/
2540
2541HANDLE HMOpenMutex(DWORD fdwAccess,
2542 BOOL fInherit,
2543 LPCTSTR lpName)
2544{
2545 int iIndex; /* index into the handle table */
2546 int iIndexNew; /* index into the handle table */
2547 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
2548 PHMHANDLEDATA pHMHandleData;
2549 DWORD rc; /* API return code */
2550
2551
2552 pDeviceHandler = HMGlobals.pHMMutex; /* device is predefined */
2553
2554 iIndexNew = _HMHandleGetFree(); /* get free handle */
2555 if (-1 == iIndexNew) /* oops, no free handles ! */
2556 {
2557 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
2558 return 0; /* signal error */
2559 }
2560
2561
2562 /* initialize the complete HMHANDLEDATA structure */
2563 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
2564 pHMHandleData->dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
2565 pHMHandleData->dwAccess = fdwAccess;
2566 pHMHandleData->dwShare = 0;
2567 pHMHandleData->dwCreation = 0;
2568 pHMHandleData->dwFlags = 0;
2569 pHMHandleData->lpHandlerData = NULL;
2570
2571
2572 /* we've got to mark the handle as occupied here, since another device */
2573 /* could be created within the device handler -> deadlock */
2574
2575 /* write appropriate entry into the handle table if open succeeded */
2576 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
2577 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
2578
2579 /* call the device handler */
2580 rc = pDeviceHandler->OpenMutex(&TabWin32Handles[iIndexNew].hmHandleData,
2581 fInherit,
2582 lpName);
2583 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
2584 {
2585 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
2586 SetLastError(rc); /* Hehe, OS/2 and NT are pretty compatible :) */
2587 return 0; /* signal error */
2588 }
2589 else
2590 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
2591
2592 return iIndexNew; /* return valid handle */
2593}
2594
2595
2596/*****************************************************************************
2597 * Name : HANDLE HMCreateSemaphore
2598 * Purpose : Wrapper for the CreateSemaphore() API
2599 * Parameters:
2600 * Variables :
2601 * Result :
2602 * Remark :
2603 * Status :
2604 *
2605 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
2606 *****************************************************************************/
2607
2608HANDLE HMCreateSemaphore(LPSECURITY_ATTRIBUTES lpsa,
2609 LONG lInitialCount,
2610 LONG lMaximumCount,
2611 LPCTSTR lpName)
2612{
2613 int iIndex; /* index into the handle table */
2614 int iIndexNew; /* index into the handle table */
2615 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
2616 PHMHANDLEDATA pHMHandleData;
2617 DWORD rc; /* API return code */
2618
2619 if(lpName) { //check if shared event semaphore already exists
2620 //TODO: No inheritance??
2621 HANDLE handle = HMOpenSemaphore(SEMAPHORE_ALL_ACCESS, FALSE, lpName);
2622 if(handle) {
2623 dprintf(("CreateSemaphore: return handle of existing semaphore %x", handle));
2624 SetLastError(ERROR_ALREADY_EXISTS);
2625 return handle;
2626 }
2627 }
2628
2629 pDeviceHandler = HMGlobals.pHMSemaphore; /* device is predefined */
2630
2631 iIndexNew = _HMHandleGetFree(); /* get free handle */
2632 if (-1 == iIndexNew) /* oops, no free handles ! */
2633 {
2634 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
2635 return 0; /* signal error */
2636 }
2637
2638
2639 /* initialize the complete HMHANDLEDATA structure */
2640 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
2641 pHMHandleData->dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
2642 pHMHandleData->dwAccess = 0;
2643 pHMHandleData->dwShare = 0;
2644 pHMHandleData->dwCreation = 0;
2645 pHMHandleData->dwFlags = 0;
2646 pHMHandleData->lpHandlerData = NULL;
2647
2648
2649 /* we've got to mark the handle as occupied here, since another device */
2650 /* could be created within the device handler -> deadlock */
2651
2652 /* write appropriate entry into the handle table if open succeeded */
2653 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
2654 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
2655
2656 /* call the device handler */
2657 rc = pDeviceHandler->CreateSemaphore(&TabWin32Handles[iIndexNew].hmHandleData,
2658 lpsa,
2659 lInitialCount,
2660 lMaximumCount,
2661 lpName);
2662 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
2663 {
2664 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
2665 SetLastError(rc); /* Hehe, OS/2 and NT are pretty compatible :) */
2666 return 0; /* signal failure */
2667 }
2668 else
2669 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
2670
2671 return iIndexNew; /* return valid handle */
2672}
2673
2674
2675/*****************************************************************************
2676 * Name : HANDLE HMOpenSemaphore
2677 * Purpose : Wrapper for the OpenSemaphore() API
2678 * Parameters:
2679 * Variables :
2680 * Result :
2681 * Remark :
2682 * Status :
2683 *
2684 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
2685 *****************************************************************************/
2686
2687HANDLE HMOpenSemaphore(DWORD fdwAccess,
2688 BOOL fInherit,
2689 LPCTSTR lpName)
2690{
2691 int iIndex; /* index into the handle table */
2692 int iIndexNew; /* index into the handle table */
2693 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
2694 PHMHANDLEDATA pHMHandleData;
2695 DWORD rc; /* API return code */
2696
2697
2698 pDeviceHandler = HMGlobals.pHMSemaphore; /* device is predefined */
2699
2700 iIndexNew = _HMHandleGetFree(); /* get free handle */
2701 if (-1 == iIndexNew) /* oops, no free handles ! */
2702 {
2703 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
2704 return 0; /* signal error */
2705 }
2706
2707
2708 /* initialize the complete HMHANDLEDATA structure */
2709 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
2710 pHMHandleData->dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
2711 pHMHandleData->dwAccess = fdwAccess;
2712 pHMHandleData->dwShare = 0;
2713 pHMHandleData->dwCreation = 0;
2714 pHMHandleData->dwFlags = 0;
2715 pHMHandleData->lpHandlerData = NULL;
2716
2717
2718 /* we've got to mark the handle as occupied here, since another device */
2719 /* could be created within the device handler -> deadlock */
2720
2721 /* write appropriate entry into the handle table if open succeeded */
2722 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
2723 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
2724
2725 /* call the device handler */
2726 rc = pDeviceHandler->OpenSemaphore(&TabWin32Handles[iIndexNew].hmHandleData,
2727 fInherit,
2728 lpName);
2729 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
2730 {
2731 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
2732 SetLastError(rc); /* Hehe, OS/2 and NT are pretty compatible :) */
2733 return 0; /* signal failure */
2734 }
2735 else
2736 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
2737
2738 return iIndexNew; /* return valid handle */
2739}
2740
2741
2742/*****************************************************************************
2743 * Name : HMReleaseSemaphore
2744 * Purpose : router function for ReleaseSemaphore
2745 * Parameters:
2746 * Variables :
2747 * Result :
2748 * Remark :
2749 * Status :
2750 *
2751 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
2752 *****************************************************************************/
2753
2754BOOL HMReleaseSemaphore(HANDLE hEvent,
2755 LONG cReleaseCount,
2756 LPLONG lpPreviousCount)
2757{
2758 int iIndex; /* index into the handle table */
2759 DWORD dwResult; /* result from the device handler's API */
2760 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
2761
2762 /* validate handle */
2763 iIndex = _HMHandleQuery(hEvent); /* get the index */
2764 if (-1 == iIndex) /* error ? */
2765 {
2766 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
2767 return 0; /* signal failure */
2768 }
2769 else
2770 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
2771
2772 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
2773 dwResult = pHMHandle->pDeviceHandler->ReleaseSemaphore(&pHMHandle->hmHandleData,
2774 cReleaseCount,
2775 lpPreviousCount);
2776
2777 return (dwResult); /* deliver return code */
2778}
2779
2780
2781/*****************************************************************************
2782 * Name : HANDLE HMCreateFileMapping
2783 * Purpose : Wrapper for the CreateFileMapping() API
2784 * Parameters:
2785 * Variables :
2786 * Result :
2787 * Remark :
2788 * Status :
2789 *
2790 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
2791 *****************************************************************************/
2792
2793HANDLE HMCreateFileMapping(HANDLE hFile,
2794 LPSECURITY_ATTRIBUTES lpFileMappingAttributes,
2795 DWORD flProtect,
2796 DWORD dwMaximumSizeHigh,
2797 DWORD dwMaximumSizeLow,
2798 LPCTSTR lpName)
2799{
2800 int iIndex; /* index into the handle table */
2801 int iIndexNew; /* index into the handle table */
2802 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
2803 PHMHANDLEDATA pHMHandleData;
2804 DWORD rc; /* API return code */
2805
2806 pDeviceHandler = HMGlobals.pHMFileMapping; /* device is predefined */
2807
2808 iIndexNew = _HMHandleGetFree(); /* get free handle */
2809 if (-1 == iIndexNew) /* oops, no free handles ! */
2810 {
2811 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
2812 return 0; /* signal error */
2813 }
2814
2815 /* initialize the complete HMHANDLEDATA structure */
2816 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
2817 pHMHandleData->dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
2818 pHMHandleData->dwAccess = 0;
2819 pHMHandleData->dwShare = 0;
2820 pHMHandleData->dwCreation = 0;
2821 pHMHandleData->dwFlags = 0;
2822 pHMHandleData->lpHandlerData = NULL;
2823
2824 /* we've got to mark the handle as occupied here, since another device */
2825 /* could be created within the device handler -> deadlock */
2826
2827 /* write appropriate entry into the handle table if open succeeded */
2828 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
2829 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
2830
2831 /* call the device handler */
2832
2833 // @@@PH: Note: hFile is a Win32-style handle, it's not (yet) converted to
2834 // a valid HandleManager-internal handle!
2835 rc = pDeviceHandler->CreateFileMapping(&TabWin32Handles[iIndexNew].hmHandleData,
2836 hFile,
2837 lpFileMappingAttributes,
2838 flProtect,
2839 dwMaximumSizeHigh,
2840 dwMaximumSizeLow,
2841 lpName);
2842
2843 if(rc != NO_ERROR) /* oops, creation failed within the device handler */
2844 {
2845 if(rc != ERROR_ALREADY_EXISTS) {
2846 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
2847 SetLastError(rc); /* Hehe, OS/2 and NT are pretty compatible :) */
2848 return (NULL); /* signal error */
2849 }
2850 SetLastError(ERROR_ALREADY_EXISTS);
2851 return iIndexNew; /* return valid handle */
2852 }
2853 else
2854 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
2855
2856 return iIndexNew; /* return valid handle */
2857}
2858
2859
2860/*****************************************************************************
2861 * Name : HANDLE HMOpenFileMapping
2862 * Purpose : Wrapper for the OpenFileMapping() API
2863 * Parameters:
2864 * Variables :
2865 * Result : HANDLE if succeeded,
2866 * NULL if failed.
2867 * Remark :
2868 * Status :
2869 *
2870 * Author : Patrick Haller [Wed, 1998/02/11 20:44]
2871 *****************************************************************************/
2872
2873HANDLE HMOpenFileMapping(DWORD fdwAccess,
2874 BOOL fInherit,
2875 LPCTSTR lpName)
2876{
2877 int iIndex; /* index into the handle table */
2878 int iIndexNew; /* index into the handle table */
2879 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
2880 PHMHANDLEDATA pHMHandleData;
2881 DWORD rc; /* API return code */
2882
2883
2884 pDeviceHandler = HMGlobals.pHMFileMapping; /* device is predefined */
2885
2886 iIndexNew = _HMHandleGetFree(); /* get free handle */
2887 if (-1 == iIndexNew) /* oops, no free handles ! */
2888 {
2889 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
2890 return (NULL); /* signal error */
2891 }
2892
2893 /* initialize the complete HMHANDLEDATA structure */
2894 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
2895 pHMHandleData->dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
2896 pHMHandleData->dwAccess = fdwAccess;
2897 pHMHandleData->dwShare = 0;
2898 pHMHandleData->dwCreation = 0;
2899 pHMHandleData->dwFlags = 0;
2900 pHMHandleData->lpHandlerData = NULL;
2901
2902
2903 /* we've got to mark the handle as occupied here, since another device */
2904 /* could be created within the device handler -> deadlock */
2905
2906 /* write appropriate entry into the handle table if open succeeded */
2907 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
2908 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
2909
2910 /* call the device handler */
2911 rc = pDeviceHandler->OpenFileMapping(&TabWin32Handles[iIndexNew].hmHandleData,
2912 fdwAccess,
2913 fInherit,
2914 lpName);
2915 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
2916 {
2917 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
2918 SetLastError(rc); /* Hehe, OS/2 and NT are pretty compatible :) */
2919 return (NULL); /* signal error */
2920 }
2921 else
2922 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
2923
2924 return iIndexNew; /* return valid handle */
2925}
2926
2927
2928/*****************************************************************************
2929 * Name : HMMapViewOfFileEx
2930 * Purpose : router function for MapViewOfFileEx
2931 * Parameters:
2932 * Variables :
2933 * Result :
2934 * Remark :
2935 * Status :
2936 *
2937 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
2938 *****************************************************************************/
2939
2940LPVOID HMMapViewOfFileEx(HANDLE hFileMappingObject,
2941 DWORD dwDesiredAccess,
2942 DWORD dwFileOffsetHigh,
2943 DWORD dwFileOffsetLow,
2944 DWORD dwNumberOfBytesToMap,
2945 LPVOID lpBaseAddress)
2946{
2947 int iIndex; /* index into the handle table */
2948 LPVOID lpResult; /* result from the device handler's API */
2949 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
2950
2951 /* validate handle */
2952 iIndex = _HMHandleQuery(hFileMappingObject); /* get the index */
2953 if (-1 == iIndex) /* error ? */
2954 {
2955 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
2956 return (NULL); /* signal failure */
2957 }
2958
2959 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
2960 lpResult = pHMHandle->pDeviceHandler->MapViewOfFileEx(&pHMHandle->hmHandleData,
2961 dwDesiredAccess,
2962 dwFileOffsetHigh,
2963 dwFileOffsetLow,
2964 dwNumberOfBytesToMap,
2965 lpBaseAddress);
2966
2967 return (lpResult); /* deliver return code */
2968}
2969
2970/*****************************************************************************
2971 * Name : HMWaitForMultipleObjects
2972 * Purpose : router function for WaitForMultipleObjects
2973 * Parameters:
2974 * Variables :
2975 * Result :
2976 * Remark :
2977 * Status :
2978 *
2979 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
2980 *****************************************************************************/
2981
2982DWORD HMWaitForMultipleObjects (DWORD cObjects,
2983 PHANDLE lphObjects,
2984 BOOL fWaitAll,
2985 DWORD dwTimeout)
2986{
2987#ifdef USE_OS2SEMAPHORES
2988 int iIndex; /* index into the handle table */
2989 DWORD dwResult; /* result from the device handler's API */
2990 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
2991
2992 if(cObjects == 1) {
2993 return HMWaitForSingleObject(*lphObjects, dwTimeout);
2994 }
2995
2996 if(cObjects > MAXIMUM_WAIT_OBJECTS) {
2997 dprintf(("KERNEL32: HMWaitForMultipleObjects: Too many objects (%d)", cObjects));
2998 SetLastError(ERROR_INVALID_PARAMETER);
2999 return WAIT_FAILED;
3000 }
3001
3002 /* validate handle */
3003 iIndex = _HMHandleQuery(*lphObjects); /* get the index */
3004 if (-1 == iIndex) /* error ? */
3005 {//oh, oh. possible problem here
3006 //TODO: rewrite handling of other handles; don't forward to open32
3007 dprintf(("WANRING: HMWaitForMultipleObjects: unknown handle passed on to Open32 -> will not work if other handles are semaphores"));
3008 return O32_WaitForMultipleObjects(cObjects, lphObjects, fWaitAll, dwTimeout);
3009 }
3010
3011 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
3012 dwResult = pHMHandle->pDeviceHandler->WaitForMultipleObjects(&pHMHandle->hmHandleData,
3013 cObjects, lphObjects, fWaitAll,
3014 dwTimeout);
3015
3016 return (dwResult); /* deliver return code */
3017#else
3018 ULONG ulIndex;
3019 PHANDLE pArrayOfHandles;
3020 PHANDLE pLoop1 = lphObjects;
3021 PHANDLE pLoop2;
3022 DWORD rc;
3023
3024 // allocate array for handle table
3025 pArrayOfHandles = (PHANDLE)alloca(cObjects * sizeof(HANDLE));
3026 if (pArrayOfHandles == NULL)
3027 {
3028 dprintf(("ERROR: HMWaitForMultipleObjects: alloca failed to allocate %d handles", cObjects));
3029 O32_SetLastError(ERROR_NOT_ENOUGH_MEMORY);
3030 return WAIT_FAILED;
3031 }
3032 else
3033 pLoop2 = pArrayOfHandles;
3034
3035 // convert array to odin handles
3036 for (ulIndex = 0;
3037
3038 ulIndex < cObjects;
3039
3040 ulIndex++,
3041 pLoop1++,
3042 pLoop2++)
3043 {
3044 rc = HMHandleTranslateToOS2 (*pLoop1, // translate handle
3045 pLoop2);
3046
3047 dprintf(("KERNEL32: HMWaitForMultipleObjects: handle %3i: ODIN-%08xh, Open32-%08xh\n",
3048 ulIndex,
3049 *pLoop1,
3050 *pLoop2));
3051
3052 // @@@PH to imlpement: check handle type!
3053 // SvL: We still use Open32 handles for threads & processes -> don't fail here!
3054 if (rc != NO_ERROR)
3055 {
3056 dprintf(("KERNEL32: HMWaitForMultipleObjects - WARNING: handle %08xh is NOT an Odin handle (probably Open32 thread or process)\n",
3057 *pLoop1));
3058
3059 *pLoop2 = *pLoop1;
3060//// O32_SetLastError(ERROR_INVALID_HANDLE);
3061//// return (WAIT_FAILED);
3062 }
3063 }
3064
3065 // OK, now forward to Open32.
3066 // @@@PH: Note this will fail on handles that do NOT belong to Open32
3067 // but to i.e. the console subsystem!
3068 rc = O32_WaitForMultipleObjects(cObjects,
3069 pArrayOfHandles,
3070 fWaitAll,
3071 dwTimeout);
3072
3073 return (rc); // OK, done
3074#endif
3075}
3076
3077
3078/*****************************************************************************
3079 * Name : HMWaitForMultipleObjectsEx
3080 * Purpose : router function for WaitForMultipleObjectsEx
3081 * Parameters:
3082 * Variables :
3083 * Result :
3084 * Remark :
3085 * Status :
3086 *
3087 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
3088 *****************************************************************************/
3089
3090DWORD HMWaitForMultipleObjectsEx (DWORD cObjects,
3091 PHANDLE lphObjects,
3092 BOOL fWaitAll,
3093 DWORD dwTimeout,
3094 BOOL fAlertable)
3095{
3096 // @@@PH: Note: fAlertable is ignored !
3097 return (HMWaitForMultipleObjects(cObjects,
3098 lphObjects,
3099 fWaitAll,
3100 dwTimeout));
3101}
3102
3103/*****************************************************************************
3104 * Name : HMMsgWaitForMultipleObjects
3105 * Purpose : router function for MsgWaitForMultipleObjects
3106 * Parameters:
3107 * Variables :
3108 * Result :
3109 * Remark : Open32 doesn't implement this properly! (doesn't check dwWakeMask)
3110 * Status :
3111 *
3112 * Author : Patrick Haller [Wed, 1999/06/17 20:44]
3113 *****************************************************************************/
3114
3115DWORD HMMsgWaitForMultipleObjects (DWORD cObjects,
3116 LPHANDLE lphObjects,
3117 BOOL fWaitAll,
3118 DWORD dwTimeout,
3119 DWORD dwWakeMask)
3120{
3121#ifdef USE_OS2SEMAPHORES
3122 int iIndex; /* index into the handle table */
3123 DWORD dwResult; /* result from the device handler's API */
3124 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
3125
3126
3127 if(dwWakeMask == 0) {
3128 dprintf(("WARNING: wakemask == 0 -> calling WaitForMultipleObjects"));
3129 return HMWaitForMultipleObjects(cObjects, lphObjects, fWaitAll, dwTimeout);
3130 }
3131 /* validate handle */
3132 iIndex = _HMHandleQuery(*lphObjects); /* get the index */
3133 if (-1 == iIndex) /* error ? */
3134 {//oh, oh. possible problem here
3135 //TODO: rewrite handling of other handles; don't forward to open32
3136 dprintf(("WANRING: HMWaitForMultipleObjects: unknown handle passed on to Open32 -> will not work if other handles are semaphores"));
3137 return O32_MsgWaitForMultipleObjects(cObjects, lphObjects, fWaitAll, dwTimeout, dwWakeMask);
3138 }
3139
3140 if(cObjects > MAXIMUM_WAIT_OBJECTS) {
3141 dprintf(("KERNEL32: HMMsgWaitForMultipleObjects: Too many objects (%d)", cObjects));
3142 SetLastError(ERROR_INVALID_PARAMETER);
3143 return WAIT_FAILED;
3144 }
3145
3146 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
3147 dwResult = pHMHandle->pDeviceHandler->MsgWaitForMultipleObjects(&pHMHandle->hmHandleData,
3148 cObjects, lphObjects, fWaitAll,
3149 dwTimeout, dwWakeMask);
3150
3151 return (dwResult); /* deliver return code */
3152#else
3153 ULONG ulIndex;
3154 PHANDLE pArrayOfHandles;
3155 PHANDLE pLoop1 = lphObjects;
3156 PHANDLE pLoop2;
3157 DWORD rc;
3158
3159 // allocate array for handle table
3160 pArrayOfHandles = (PHANDLE)alloca(cObjects * sizeof(HANDLE));
3161 if (pArrayOfHandles == NULL)
3162 {
3163 dprintf(("ERROR: HMMsgWaitForMultipleObjects: alloca failed to allocate %d handles", cObjects));
3164 O32_SetLastError(ERROR_NOT_ENOUGH_MEMORY);
3165 return WAIT_FAILED;
3166 }
3167 else
3168 pLoop2 = pArrayOfHandles;
3169
3170 // convert array to odin handles
3171 for (ulIndex = 0;
3172
3173 ulIndex < cObjects;
3174
3175 ulIndex++,
3176 pLoop1++,
3177 pLoop2++)
3178 {
3179 rc = HMHandleTranslateToOS2 (*pLoop1, // translate handle
3180 pLoop2);
3181
3182 dprintf2(("MsgWaitForMultipleObjects handle %x->%x", *pLoop1, *pLoop2));
3183 // SvL: We still use Open32 handles for threads & processes -> don't fail here!
3184 if (rc != NO_ERROR)
3185 {
3186 dprintf(("KERNEL32: HMMsgWaitForMultipleObjects - WARNING: handle %08xh is NOT an Odin handle (probably Open32 thread or process)\n",
3187 *pLoop1));
3188
3189 *pLoop2 = *pLoop1;
3190//// O32_SetLastError(ERROR_INVALID_HANDLE);
3191//// return (WAIT_FAILED);
3192 }
3193 }
3194
3195 // OK, now forward to Open32.
3196 // @@@PH: Note this will fail on handles that do NOT belong to Open32
3197 // but to i.e. the console subsystem!
3198 rc = O32_MsgWaitForMultipleObjects(cObjects,
3199 pArrayOfHandles,
3200 fWaitAll, dwTimeout,
3201 dwWakeMask);
3202
3203 dprintf2(("MsgWaitForMultipleObjects returned %d", rc));
3204 return (rc); // OK, done
3205#endif
3206}
3207/*****************************************************************************
3208 * Name : HMDeviceIoControl
3209 * Purpose : router function for DeviceIoControl
3210 * Parameters:
3211 * Variables :
3212 * Result :
3213 * Remark :
3214 * Status :
3215 *
3216 * Author : Sander van Leeuwen
3217 *****************************************************************************/
3218
3219BOOL HMDeviceIoControl(HANDLE hDevice, DWORD dwIoControlCode,
3220 LPVOID lpInBuffer, DWORD nInBufferSize,
3221 LPVOID lpOutBuffer, DWORD nOutBufferSize,
3222 LPDWORD lpBytesReturned, LPOVERLAPPED lpOverlapped)
3223{
3224 int iIndex; /* index into the handle table */
3225 BOOL fResult; /* result from the device handler's CloseHandle() */
3226 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
3227
3228 /* validate handle */
3229 iIndex = _HMHandleQuery(hDevice); /* get the index */
3230 if (-1 == iIndex) /* error ? */
3231 {
3232 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
3233 return (FALSE); /* signal failure */
3234 }
3235
3236 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
3237 fResult = pHMHandle->pDeviceHandler->DeviceIoControl(&pHMHandle->hmHandleData,
3238 dwIoControlCode,
3239 lpInBuffer, nInBufferSize,
3240 lpOutBuffer, nOutBufferSize,
3241 lpBytesReturned, lpOverlapped);
3242
3243 return (fResult); /* deliver return code */
3244}
3245/*****************************************************************************
3246 * Name : HMCancelIo
3247 * Purpose : router function for CancelIo
3248 * Parameters:
3249 * Variables :
3250 * Result :
3251 * Remark :
3252 * Status :
3253 *
3254 * Author : Sander van Leeuwen
3255 *****************************************************************************/
3256BOOL HMCancelIo(HANDLE hDevice)
3257{
3258 int iIndex; /* index into the handle table */
3259 BOOL fResult; /* result from the device handler's CloseHandle() */
3260 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
3261
3262 /* validate handle */
3263 iIndex = _HMHandleQuery(hDevice); /* get the index */
3264 if (-1 == iIndex) /* error ? */
3265 {
3266 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
3267 return (FALSE); /* signal failure */
3268 }
3269
3270 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
3271 fResult = pHMHandle->pDeviceHandler->CancelIo(&pHMHandle->hmHandleData);
3272
3273 return (fResult); /* deliver return code */
3274}
3275/*****************************************************************************
3276 * Name : HMCOMGetCommState
3277 * Purpose : router function for GetCommState
3278 * Parameters:
3279 * Variables :
3280 * Result :
3281 * Remark :
3282 * Status :
3283 *
3284 * Author : Achim Hasenmueller [Sat, 1999/11/27 13:40]
3285 *****************************************************************************/
3286
3287BOOL HMCommGetCommState(HANDLE hCommDev, LPDCB lpdcb)
3288{
3289 int iIndex; /* index into the handle table */
3290 BOOL bResult; /* result from the device handler's API */
3291 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
3292
3293 /* validate handle */
3294 iIndex = _HMHandleQuery(hCommDev); /* get the index */
3295 if (-1 == iIndex) /* error ? */
3296 {
3297 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
3298 return (NULL); /* signal failure */
3299 }
3300
3301 if(IsBadWritePtr(lpdcb,sizeof(DCB)))
3302 {
3303 SetLastError(ERROR_INVALID_PARAMETER);
3304 return FALSE;
3305 }
3306
3307 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
3308 bResult = pHMHandle->pDeviceHandler->GetCommState(&pHMHandle->hmHandleData,
3309 lpdcb);
3310
3311 return (bResult); /* deliver return code */
3312}
3313
3314BOOL HMCommWaitCommEvent( HANDLE hCommDev,
3315 LPDWORD lpfdwEvtMask,
3316 LPOVERLAPPED lpo)
3317{
3318 int iIndex; /* index into the handle table */
3319 BOOL bResult; /* result from the device handler's API */
3320 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
3321
3322 /* validate handle */
3323 iIndex = _HMHandleQuery(hCommDev); /* get the index */
3324 if (-1 == iIndex) /* error ? */
3325 {
3326 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
3327 return FALSE; /* signal failure */
3328 }
3329
3330 if(NULL!=lpo && IsBadReadPtr(lpo,sizeof(OVERLAPPED)) )
3331 {
3332 SetLastError(ERROR_INVALID_PARAMETER);
3333 return FALSE;
3334 }
3335
3336 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
3337 bResult = pHMHandle->pDeviceHandler->WaitCommEvent( &pHMHandle->hmHandleData,
3338 lpfdwEvtMask,
3339 lpo);
3340
3341 return (bResult); /* deliver return code */
3342}
3343
3344BOOL HMCommGetCommProperties( HANDLE hCommDev,
3345 LPCOMMPROP lpcmmp)
3346{
3347 int iIndex; /* index into the handle table */
3348 BOOL bResult; /* result from the device handler's API */
3349 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
3350
3351 /* validate handle */
3352 iIndex = _HMHandleQuery(hCommDev); /* get the index */
3353 if (-1 == iIndex) /* error ? */
3354 {
3355 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
3356 return (NULL); /* signal failure */
3357 }
3358
3359 if(IsBadWritePtr(lpcmmp,sizeof(COMMPROP)) )
3360 {
3361 SetLastError(ERROR_INVALID_PARAMETER);
3362 return FALSE;
3363 }
3364
3365 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
3366 bResult = pHMHandle->pDeviceHandler->GetCommProperties( &pHMHandle->hmHandleData,
3367 lpcmmp);
3368
3369 return (bResult); /* deliver return code */
3370}
3371
3372BOOL HMCommGetCommMask( HANDLE hCommDev,
3373 LPDWORD lpfdwEvtMask)
3374{
3375 int iIndex; /* index into the handle table */
3376 BOOL bResult; /* result from the device handler's API */
3377 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
3378
3379 /* validate handle */
3380 iIndex = _HMHandleQuery(hCommDev); /* get the index */
3381 if (-1 == iIndex) /* error ? */
3382 {
3383 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
3384 return (NULL); /* signal failure */
3385 }
3386
3387 if(IsBadWritePtr(lpfdwEvtMask,sizeof(DWORD)) )
3388 {
3389 SetLastError(ERROR_INVALID_PARAMETER);
3390 return FALSE;
3391 }
3392
3393 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
3394 bResult = pHMHandle->pDeviceHandler->GetCommMask( &pHMHandle->hmHandleData,
3395 lpfdwEvtMask);
3396
3397 return (bResult); /* deliver return code */
3398}
3399
3400BOOL HMCommSetCommMask( HANDLE hCommDev,
3401 DWORD fdwEvtMask)
3402{
3403 int iIndex; /* index into the handle table */
3404 BOOL bResult; /* result from the device handler's API */
3405 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
3406
3407 /* validate handle */
3408 iIndex = _HMHandleQuery(hCommDev); /* get the index */
3409 if (-1 == iIndex) /* error ? */
3410 {
3411 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
3412 return (NULL); /* signal failure */
3413 }
3414
3415 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
3416 bResult = pHMHandle->pDeviceHandler->SetCommMask( &pHMHandle->hmHandleData,
3417 fdwEvtMask);
3418
3419 return (bResult); /* deliver return code */
3420}
3421
3422BOOL HMCommPurgeComm( HANDLE hCommDev,
3423 DWORD fdwAction)
3424{
3425 int iIndex; /* index into the handle table */
3426 BOOL bResult; /* result from the device handler's API */
3427 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
3428
3429 /* validate handle */
3430 iIndex = _HMHandleQuery(hCommDev); /* get the index */
3431 if (-1 == iIndex) /* error ? */
3432 {
3433 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
3434 return (NULL); /* signal failure */
3435 }
3436
3437
3438 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
3439 bResult = pHMHandle->pDeviceHandler->PurgeComm( &pHMHandle->hmHandleData,
3440 fdwAction);
3441
3442 return (bResult); /* deliver return code */
3443}
3444
3445BOOL HMCommClearCommError( HANDLE hCommDev,
3446 LPDWORD lpdwErrors,
3447 LPCOMSTAT lpcst)
3448{
3449 int iIndex; /* index into the handle table */
3450 BOOL bResult; /* result from the device handler's API */
3451 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
3452
3453 /* validate handle */
3454 iIndex = _HMHandleQuery(hCommDev); /* get the index */
3455 if (-1 == iIndex) /* error ? */
3456 {
3457 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
3458 return (NULL); /* signal failure */
3459 }
3460
3461 if(IsBadWritePtr(lpdwErrors,sizeof(DWORD)) ||
3462 (NULL!=lpcst && IsBadWritePtr(lpcst,sizeof(COMSTAT)) ) )
3463 {
3464 SetLastError(ERROR_INVALID_PARAMETER);
3465 return FALSE;
3466 }
3467
3468 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
3469 bResult = pHMHandle->pDeviceHandler->ClearCommError(&pHMHandle->hmHandleData,
3470 lpdwErrors,
3471 lpcst);
3472
3473 return (bResult); /* deliver return code */
3474}
3475
3476BOOL HMCommSetCommState( HANDLE hCommDev,
3477 LPDCB lpdcb)
3478{
3479 int iIndex; /* index into the handle table */
3480 BOOL bResult; /* result from the device handler's API */
3481 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
3482
3483 /* validate handle */
3484 iIndex = _HMHandleQuery(hCommDev); /* get the index */
3485 if (-1 == iIndex) /* error ? */
3486 {
3487 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
3488 return (NULL); /* signal failure */
3489 }
3490
3491 if(IsBadReadPtr(lpdcb,sizeof(DCB)) )
3492 {
3493 SetLastError(ERROR_INVALID_PARAMETER);
3494 return FALSE;
3495 }
3496
3497
3498 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
3499 bResult = pHMHandle->pDeviceHandler->SetCommState(&pHMHandle->hmHandleData,
3500 lpdcb);
3501
3502 return (bResult); /* deliver return code */
3503}
3504
3505
3506BOOL HMCommGetCommTimeouts( HANDLE hCommDev,
3507 LPCOMMTIMEOUTS lpctmo)
3508{
3509 int iIndex; /* index into the handle table */
3510 BOOL bResult; /* result from the device handler's API */
3511 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
3512
3513 /* validate handle */
3514 iIndex = _HMHandleQuery(hCommDev); /* get the index */
3515 if (-1 == iIndex) /* error ? */
3516 {
3517 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
3518 return (NULL); /* signal failure */
3519 }
3520
3521 if(IsBadWritePtr(lpctmo,sizeof(COMMTIMEOUTS)) )
3522 {
3523 SetLastError(ERROR_INVALID_PARAMETER);
3524 return FALSE;
3525 }
3526
3527 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
3528 bResult = pHMHandle->pDeviceHandler->GetCommTimeouts( &pHMHandle->hmHandleData,
3529 lpctmo);
3530
3531 return (bResult); /* deliver return code */
3532}
3533BOOL HMCommGetCommModemStatus( HANDLE hCommDev,
3534 LPDWORD lpModemStat )
3535{
3536 int iIndex; /* index into the handle table */
3537 BOOL bResult; /* result from the device handler's API */
3538 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
3539
3540 /* validate handle */
3541 iIndex = _HMHandleQuery(hCommDev); /* get the index */
3542 if (-1 == iIndex) /* error ? */
3543 {
3544 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
3545 return (NULL); /* signal failure */
3546 }
3547
3548 if(IsBadWritePtr(lpModemStat,sizeof(DWORD)) )
3549 {
3550 SetLastError(ERROR_INVALID_PARAMETER);
3551 return FALSE;
3552 }
3553
3554 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
3555 bResult = pHMHandle->pDeviceHandler->GetCommModemStatus( &pHMHandle->hmHandleData,
3556 lpModemStat);
3557
3558 return (bResult); /* deliver return code */
3559}
3560
3561BOOL HMCommSetCommTimeouts( HANDLE hCommDev,
3562 LPCOMMTIMEOUTS lpctmo)
3563{
3564 int iIndex; /* index into the handle table */
3565 BOOL bResult; /* result from the device handler's API */
3566 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
3567
3568 /* validate handle */
3569 iIndex = _HMHandleQuery(hCommDev); /* get the index */
3570 if (-1 == iIndex) /* error ? */
3571 {
3572 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
3573 return (NULL); /* signal failure */
3574 }
3575
3576 if(IsBadReadPtr(lpctmo,sizeof(COMMTIMEOUTS)) )
3577 {
3578 SetLastError(ERROR_INVALID_PARAMETER);
3579 return FALSE;
3580 }
3581
3582 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
3583 bResult = pHMHandle->pDeviceHandler->SetCommTimeouts( &pHMHandle->hmHandleData,
3584 lpctmo);
3585
3586 return (bResult); /* deliver return code */
3587}
3588
3589BOOL HMCommTransmitCommChar( HANDLE hCommDev,
3590 CHAR cChar )
3591{
3592 int iIndex; /* index into the handle table */
3593 BOOL bResult; /* result from the device handler's API */
3594 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
3595
3596 /* validate handle */
3597 iIndex = _HMHandleQuery(hCommDev); /* get the index */
3598 if (-1 == iIndex) /* error ? */
3599 {
3600 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
3601 return (NULL); /* signal failure */
3602 }
3603
3604 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
3605 bResult = pHMHandle->pDeviceHandler->TransmitCommChar( &pHMHandle->hmHandleData,
3606 cChar);
3607
3608 return (bResult); /* deliver return code */
3609}
3610
3611BOOL HMCommSetCommConfig( HANDLE hCommDev,
3612 LPCOMMCONFIG lpCC,
3613 DWORD dwSize )
3614{
3615 int iIndex; /* index into the handle table */
3616 BOOL bResult; /* result from the device handler's API */
3617 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
3618
3619 /* validate handle */
3620 iIndex = _HMHandleQuery(hCommDev); /* get the index */
3621 if (-1 == iIndex) /* error ? */
3622 {
3623 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
3624 return (NULL); /* signal failure */
3625 }
3626
3627 if( IsBadReadPtr(lpCC,sizeof(COMMCONFIG)) ||
3628 dwSize < sizeof(COMMCONFIG) )
3629 {
3630 SetLastError(ERROR_INVALID_PARAMETER);
3631 return FALSE;
3632 }
3633
3634 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
3635 bResult = pHMHandle->pDeviceHandler->SetCommConfig( &pHMHandle->hmHandleData,
3636 lpCC,
3637 dwSize);
3638
3639 return (bResult); /* deliver return code */
3640}
3641
3642BOOL HMCommSetCommBreak( HANDLE hCommDev )
3643{
3644 int iIndex; /* index into the handle table */
3645 BOOL bResult; /* result from the device handler's API */
3646 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
3647
3648 /* validate handle */
3649 iIndex = _HMHandleQuery(hCommDev); /* get the index */
3650 if (-1 == iIndex) /* error ? */
3651 {
3652 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
3653 return (NULL); /* signal failure */
3654 }
3655
3656 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
3657 bResult = pHMHandle->pDeviceHandler->SetCommBreak( &pHMHandle->hmHandleData);
3658
3659 return (bResult); /* deliver return code */
3660}
3661
3662BOOL HMCommGetCommConfig( HANDLE hCommDev,
3663 LPCOMMCONFIG lpCC,
3664 LPDWORD lpdwSize )
3665{
3666 int iIndex; /* index into the handle table */
3667 BOOL bResult; /* result from the device handler's API */
3668 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
3669
3670 /* validate handle */
3671 iIndex = _HMHandleQuery(hCommDev); /* get the index */
3672 if (-1 == iIndex) /* error ? */
3673 {
3674 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
3675 return (NULL); /* signal failure */
3676 }
3677
3678 if(IsBadWritePtr(lpdwSize,sizeof(DWORD)) )
3679 {
3680 SetLastError(ERROR_INVALID_PARAMETER);
3681 return FALSE;
3682 }
3683
3684 if( IsBadWritePtr(lpCC,sizeof(COMMCONFIG)) ||
3685 *lpdwSize< sizeof(COMMCONFIG) )
3686 {
3687 SetLastError(ERROR_INSUFFICIENT_BUFFER);
3688 *lpdwSize= sizeof(COMMCONFIG);
3689 return FALSE;
3690 }
3691
3692 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
3693 bResult = pHMHandle->pDeviceHandler->GetCommConfig( &pHMHandle->hmHandleData,
3694 lpCC,
3695 lpdwSize);
3696
3697 return (bResult); /* deliver return code */
3698}
3699
3700BOOL HMCommEscapeCommFunction( HANDLE hCommDev,
3701 UINT dwFunc )
3702{
3703 int iIndex; /* index into the handle table */
3704 BOOL bResult; /* result from the device handler's API */
3705 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
3706
3707 /* validate handle */
3708 iIndex = _HMHandleQuery(hCommDev); /* get the index */
3709 if (-1 == iIndex) /* error ? */
3710 {
3711 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
3712 return (NULL); /* signal failure */
3713 }
3714
3715 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
3716
3717 switch(dwFunc)
3718 {
3719 case CLRDTR:
3720 case CLRRTS:
3721 case SETDTR:
3722 case SETRTS:
3723 case SETXOFF:
3724 case SETXON:
3725 bResult = pHMHandle->pDeviceHandler->EscapeCommFunction( &pHMHandle->hmHandleData,
3726 dwFunc);
3727 break;
3728 case SETBREAK:
3729 bResult = pHMHandle->pDeviceHandler->SetCommBreak(&pHMHandle->hmHandleData);
3730 break;
3731 case CLRBREAK:
3732 bResult = pHMHandle->pDeviceHandler->ClearCommBreak(&pHMHandle->hmHandleData);
3733 break;
3734 default:
3735 SetLastError(ERROR_INVALID_PARAMETER);
3736 bResult = FALSE;
3737 }
3738
3739
3740 return (bResult); /* deliver return code */
3741}
3742
3743BOOL HMCommSetupComm( HANDLE hCommDev,
3744 DWORD dwInQueue,
3745 DWORD dwOutQueue)
3746{
3747 int iIndex; /* index into the handle table */
3748 BOOL bResult; /* result from the device handler's API */
3749 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
3750
3751 /* validate handle */
3752 iIndex = _HMHandleQuery(hCommDev); /* get the index */
3753 if (-1 == iIndex) /* error ? */
3754 {
3755 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
3756 return (NULL); /* signal failure */
3757 }
3758
3759 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
3760 bResult = pHMHandle->pDeviceHandler->SetupComm(&pHMHandle->hmHandleData,
3761 dwInQueue,
3762 dwOutQueue);
3763
3764 return (bResult); /* deliver return code */
3765}
3766
3767BOOL HMCommClearCommBreak(HANDLE hCommDev)
3768{
3769 int iIndex; /* index into the handle table */
3770 BOOL bResult; /* result from the device handler's API */
3771 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
3772
3773 /* validate handle */
3774 iIndex = _HMHandleQuery(hCommDev); /* get the index */
3775 if (-1 == iIndex) /* error ? */
3776 {
3777 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
3778 return (NULL); /* signal failure */
3779 }
3780
3781 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
3782 bResult = pHMHandle->pDeviceHandler->ClearCommBreak(&pHMHandle->hmHandleData);
3783
3784 return (bResult); /* deliver return code */
3785}
3786
3787BOOL HMCommSetDefaultCommConfig( HANDLE hCommDev,
3788 LPCOMMCONFIG lpCC,
3789 DWORD dwSize)
3790{
3791 int iIndex; /* index into the handle table */
3792 BOOL bResult; /* result from the device handler's API */
3793 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
3794
3795 /* validate handle */
3796 iIndex = _HMHandleQuery(hCommDev); /* get the index */
3797 if (-1 == iIndex) /* error ? */
3798 {
3799 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
3800 return (NULL); /* signal failure */
3801 }
3802
3803 if( (lpCC!=NULL) &&
3804 ( IsBadReadPtr(lpCC,sizeof(COMMCONFIG)) ||
3805 dwSize != sizeof(COMMCONFIG) ) )
3806 {
3807 SetLastError(ERROR_INVALID_PARAMETER);
3808 return FALSE;
3809 }
3810
3811 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
3812 bResult = pHMHandle->pDeviceHandler->SetDefaultCommConfig(&pHMHandle->hmHandleData,
3813 lpCC,
3814 dwSize);
3815
3816 return (bResult); /* deliver return code */
3817}
3818
3819BOOL HMCommGetDefaultCommConfig( HANDLE hCommDev,
3820 LPCOMMCONFIG lpCC,
3821 LPDWORD lpdwSize)
3822{
3823 int iIndex; /* index into the handle table */
3824 BOOL bResult; /* result from the device handler's API */
3825 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
3826
3827 /* validate handle */
3828 iIndex = _HMHandleQuery(hCommDev); /* get the index */
3829 if (-1 == iIndex) /* error ? */
3830 {
3831 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
3832 return (NULL); /* signal failure */
3833 }
3834
3835 if(IsBadWritePtr(lpdwSize,sizeof(DWORD)))
3836 {
3837 SetLastError(ERROR_INVALID_PARAMETER);
3838 return FALSE;
3839 }
3840
3841 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
3842 bResult = pHMHandle->pDeviceHandler->GetDefaultCommConfig( &pHMHandle->hmHandleData,
3843 lpCC,
3844 lpdwSize);
3845
3846 return (bResult); /* deliver return code */
3847}
3848
3849/*****************************************************************************
3850 * Name : HMOpenThreadToken
3851 * Purpose : router function for NtOpenThreadToken
3852 * Parameters:
3853 * Variables :
3854 * Result :
3855 * Remark :
3856 * Status :
3857 *
3858 * Author : SvL
3859 *****************************************************************************/
3860
3861DWORD HMOpenThreadToken(HANDLE ThreadHandle,
3862 DWORD DesiredAccess,
3863 DWORD OpenAsSelf,
3864 HANDLE *TokenHandle)
3865{
3866 int iIndex; /* index into the handle table */
3867 int iIndexNew; /* index into the handle table */
3868 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
3869 PHMHANDLEDATA pHMHandleData;
3870 DWORD rc; /* API return code */
3871
3872 pDeviceHandler = HMGlobals.pHMToken; /* device is predefined */
3873
3874 iIndexNew = _HMHandleGetFree(); /* get free handle */
3875 if (-1 == iIndexNew) /* oops, no free handles ! */
3876 {
3877 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
3878 return ERROR_NOT_ENOUGH_MEMORY;
3879 }
3880
3881 /* initialize the complete HMHANDLEDATA structure */
3882 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
3883 pHMHandleData->dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
3884 pHMHandleData->dwAccess = DesiredAccess;
3885 pHMHandleData->dwShare = 0;
3886 pHMHandleData->dwCreation = 0;
3887 pHMHandleData->dwFlags = 0;
3888 pHMHandleData->lpHandlerData = NULL;
3889
3890
3891 /* we've got to mark the handle as occupied here, since another device */
3892 /* could be created within the device handler -> deadlock */
3893
3894 /* write appropriate entry into the handle table if open succeeded */
3895 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
3896 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
3897
3898 /* call the device handler */
3899
3900 // @@@PH: Note: hFile is a Win32-style handle, it's not (yet) converted to
3901 // a valid HandleManager-internal handle!
3902 rc = pDeviceHandler->OpenThreadToken(&TabWin32Handles[iIndexNew].hmHandleData,
3903 ThreadHandle,
3904 OpenAsSelf);
3905
3906 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
3907 {
3908 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
3909 return (rc); /* signal error */
3910 }
3911 else
3912 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
3913
3914 *TokenHandle = iIndexNew; /* return valid handle */
3915 return STATUS_SUCCESS;
3916}
3917
3918/*****************************************************************************
3919 * Name : HMOpenProcessToken
3920 * Purpose : router function for NtOpenProcessToken
3921 * Parameters:
3922 * Variables :
3923 * Result :
3924 * Remark :
3925 * Status :
3926 *
3927 * Author : SvL
3928 *****************************************************************************/
3929DWORD HMOpenProcessToken(HANDLE ProcessHandle,
3930 DWORD DesiredAccess,
3931 DWORD dwUserData,
3932 HANDLE *TokenHandle)
3933{
3934 int iIndex; /* index into the handle table */
3935 int iIndexNew; /* index into the handle table */
3936 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
3937 PHMHANDLEDATA pHMHandleData;
3938 DWORD rc; /* API return code */
3939
3940 pDeviceHandler = HMGlobals.pHMToken; /* device is predefined */
3941
3942 iIndexNew = _HMHandleGetFree(); /* get free handle */
3943 if (-1 == iIndexNew) /* oops, no free handles ! */
3944 {
3945 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
3946 return ERROR_NOT_ENOUGH_MEMORY;
3947 }
3948
3949 /* initialize the complete HMHANDLEDATA structure */
3950 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
3951 pHMHandleData->dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
3952 pHMHandleData->dwAccess = DesiredAccess;
3953 pHMHandleData->dwShare = 0;
3954 pHMHandleData->dwCreation = 0;
3955 pHMHandleData->dwFlags = 0;
3956 pHMHandleData->lpHandlerData = NULL;
3957
3958
3959 /* we've got to mark the handle as occupied here, since another device */
3960 /* could be created within the device handler -> deadlock */
3961
3962 /* write appropriate entry into the handle table if open succeeded */
3963 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
3964 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
3965
3966 /* call the device handler */
3967
3968 // @@@PH: Note: hFile is a Win32-style handle, it's not (yet) converted to
3969 // a valid HandleManager-internal handle!
3970 rc = pDeviceHandler->OpenProcessToken(&TabWin32Handles[iIndexNew].hmHandleData,
3971 dwUserData,
3972 ProcessHandle);
3973
3974 if (rc != NO_ERROR) /* oops, creation failed within the device handler */
3975 {
3976 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
3977 return (rc); /* signal error */
3978 }
3979 else
3980 SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
3981
3982 *TokenHandle = iIndexNew; /* return valid handle */
3983 return STATUS_SUCCESS;
3984}
3985/*****************************************************************************
3986 * Name : HMCreateThread
3987 * Purpose : router function for CreateThread
3988 * Parameters:
3989 * Variables :
3990 * Result :
3991 * Remark :
3992 * Status :
3993 *
3994 * Author : SvL
3995 *****************************************************************************/
3996HANDLE HMCreateThread(LPSECURITY_ATTRIBUTES lpsa,
3997 DWORD cbStack,
3998 LPTHREAD_START_ROUTINE lpStartAddr,
3999 LPVOID lpvThreadParm,
4000 DWORD fdwCreate,
4001 LPDWORD lpIDThread,
4002 BOOL fFirstThread)
4003{
4004 int iIndex; /* index into the handle table */
4005 int iIndexNew; /* index into the handle table */
4006 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
4007 PHMHANDLEDATA pHMHandleData;
4008 HANDLE rc; /* API return code */
4009
4010 SetLastError(ERROR_SUCCESS);
4011
4012 pDeviceHandler = HMGlobals.pHMThread; /* device is predefined */
4013
4014 iIndexNew = _HMHandleGetFree(); /* get free handle */
4015 if (-1 == iIndexNew) /* oops, no free handles ! */
4016 {
4017 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
4018 return 0;
4019 }
4020
4021 /* initialize the complete HMHANDLEDATA structure */
4022 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
4023 pHMHandleData->dwType = FILE_TYPE_UNKNOWN; /* unknown handle type */
4024 pHMHandleData->dwAccess = 0;
4025 pHMHandleData->dwShare = 0;
4026 pHMHandleData->dwCreation = 0;
4027 pHMHandleData->dwFlags = 0;
4028 pHMHandleData->lpHandlerData = NULL;
4029
4030
4031 /* we've got to mark the handle as occupied here, since another device */
4032 /* could be created within the device handler -> deadlock */
4033
4034 /* write appropriate entry into the handle table if open succeeded */
4035 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
4036 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
4037
4038 /* call the device handler */
4039
4040 // @@@PH: Note: hFile is a Win32-style handle, it's not (yet) converted to
4041 // a valid HandleManager-internal handle!
4042 rc = pDeviceHandler->CreateThread(&TabWin32Handles[iIndexNew].hmHandleData,
4043 lpsa, cbStack, lpStartAddr,
4044 lpvThreadParm, fdwCreate, lpIDThread, fFirstThread);
4045
4046 if (rc == 0) /* oops, creation failed within the device handler */
4047 {
4048 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
4049 return 0; /* signal error */
4050 }
4051
4052 return iIndexNew;
4053}
4054/*****************************************************************************
4055 * Name : HMGetThreadPriority
4056 * Purpose : router function for GetThreadPriority
4057 * Parameters:
4058 * Variables :
4059 * Result :
4060 * Remark :
4061 * Status :
4062 *
4063 * Author : SvL
4064 *****************************************************************************/
4065INT HMGetThreadPriority(HANDLE hThread)
4066{
4067 int iIndex; /* index into the handle table */
4068 INT lpResult; /* result from the device handler's API */
4069 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
4070
4071 SetLastError(ERROR_SUCCESS);
4072 /* validate handle */
4073 iIndex = _HMHandleQuery(hThread); /* get the index */
4074 if (-1 == iIndex) /* error ? */
4075 {
4076 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
4077 return (-1); /* signal failure */
4078 }
4079
4080 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
4081 lpResult = pHMHandle->pDeviceHandler->GetThreadPriority(&TabWin32Handles[iIndex].hmHandleData);
4082
4083 return (lpResult); /* deliver return code */
4084}
4085/*****************************************************************************
4086 * Name : HMSuspendThread
4087 * Purpose : router function for SuspendThread
4088 * Parameters:
4089 * Variables :
4090 * Result :
4091 * Remark :
4092 * Status :
4093 *
4094 * Author : SvL
4095 *****************************************************************************/
4096DWORD HMSuspendThread(HANDLE hThread)
4097{
4098 int iIndex; /* index into the handle table */
4099 HANDLE lpResult; /* result from the device handler's API */
4100 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
4101
4102 SetLastError(ERROR_SUCCESS);
4103 /* validate handle */
4104 iIndex = _HMHandleQuery(hThread); /* get the index */
4105 if (-1 == iIndex) /* error ? */
4106 {
4107 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
4108 return -1; /* signal failure */
4109 }
4110
4111 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
4112 lpResult = pHMHandle->pDeviceHandler->SuspendThread(&TabWin32Handles[iIndex].hmHandleData);
4113
4114 return (lpResult); /* deliver return code */
4115}
4116/*****************************************************************************
4117 * Name : HMSetThreadPriority
4118 * Purpose : router function for SetThreadPriority
4119 * Parameters:
4120 * Variables :
4121 * Result :
4122 * Remark :
4123 * Status :
4124 *
4125 * Author : SvL
4126 *****************************************************************************/
4127BOOL HMSetThreadPriority(HANDLE hThread, int priority)
4128{
4129 int iIndex; /* index into the handle table */
4130 BOOL lpResult; /* result from the device handler's API */
4131 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
4132
4133 SetLastError(ERROR_SUCCESS);
4134 /* validate handle */
4135 iIndex = _HMHandleQuery(hThread); /* get the index */
4136 if (-1 == iIndex) /* error ? */
4137 {
4138 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
4139 return FALSE; /* signal failure */
4140 }
4141
4142 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
4143 lpResult = pHMHandle->pDeviceHandler->SetThreadPriority(&TabWin32Handles[iIndex].hmHandleData, priority);
4144
4145 return (lpResult); /* deliver return code */
4146}
4147/*****************************************************************************
4148 * Name : HMGetThreadContext
4149 * Purpose : router function for GetThreadContext
4150 * Parameters:
4151 * Variables :
4152 * Result :
4153 * Remark :
4154 * Status :
4155 *
4156 * Author : SvL
4157 *****************************************************************************/
4158BOOL HMGetThreadContext(HANDLE hThread, CONTEXT *lpContext)
4159{
4160 int iIndex; /* index into the handle table */
4161 BOOL lpResult; /* result from the device handler's API */
4162 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
4163
4164 SetLastError(ERROR_SUCCESS);
4165 /* validate handle */
4166 iIndex = _HMHandleQuery(hThread); /* get the index */
4167 if (-1 == iIndex) /* error ? */
4168 {
4169 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
4170 return FALSE; /* signal failure */
4171 }
4172
4173 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
4174 lpResult = pHMHandle->pDeviceHandler->GetThreadContext(&TabWin32Handles[iIndex].hmHandleData, lpContext);
4175
4176 return (lpResult); /* deliver return code */
4177}
4178/*****************************************************************************
4179 * Name : HMSetThreadContext
4180 * Purpose : router function for SetThreadContext
4181 * Parameters:
4182 * Variables :
4183 * Result :
4184 * Remark :
4185 * Status :
4186 *
4187 * Author : SvL
4188 *****************************************************************************/
4189BOOL HMSetThreadContext(HANDLE hThread, const CONTEXT *lpContext)
4190{
4191 int iIndex; /* index into the handle table */
4192 BOOL lpResult; /* result from the device handler's API */
4193 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
4194
4195 SetLastError(ERROR_SUCCESS);
4196 /* validate handle */
4197 iIndex = _HMHandleQuery(hThread); /* get the index */
4198 if (-1 == iIndex) /* error ? */
4199 {
4200 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
4201 return FALSE; /* signal failure */
4202 }
4203
4204 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
4205 lpResult = pHMHandle->pDeviceHandler->SetThreadContext(&TabWin32Handles[iIndex].hmHandleData, lpContext);
4206
4207 return (lpResult); /* deliver return code */
4208}
4209/*****************************************************************************
4210 * Name : HMTerminateThread
4211 * Purpose : router function for TerminateThread
4212 * Parameters:
4213 * Variables :
4214 * Result :
4215 * Remark :
4216 * Status :
4217 *
4218 * Author : SvL
4219 *****************************************************************************/
4220BOOL HMTerminateThread(HANDLE hThread, DWORD exitcode)
4221{
4222 int iIndex; /* index into the handle table */
4223 BOOL lpResult; /* result from the device handler's API */
4224 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
4225
4226 SetLastError(ERROR_SUCCESS);
4227 /* validate handle */
4228 iIndex = _HMHandleQuery(hThread); /* get the index */
4229 if (-1 == iIndex) /* error ? */
4230 {
4231 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
4232 return FALSE; /* signal failure */
4233 }
4234
4235 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
4236 lpResult = pHMHandle->pDeviceHandler->TerminateThread(&TabWin32Handles[iIndex].hmHandleData, exitcode);
4237
4238 return (lpResult); /* deliver return code */
4239}
4240/*****************************************************************************
4241 * Name : HMResumeThread
4242 * Purpose : router function for ResumeThread
4243 * Parameters:
4244 * Variables :
4245 * Result :
4246 * Remark :
4247 * Status :
4248 *
4249 * Author : SvL
4250 *****************************************************************************/
4251DWORD HMResumeThread(HANDLE hThread)
4252{
4253 int iIndex; /* index into the handle table */
4254 DWORD lpResult; /* result from the device handler's API */
4255 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
4256
4257 SetLastError(ERROR_SUCCESS);
4258 /* validate handle */
4259 iIndex = _HMHandleQuery(hThread); /* get the index */
4260 if (-1 == iIndex) /* error ? */
4261 {
4262 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
4263 return -1; /* signal failure */
4264 }
4265
4266 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
4267 lpResult = pHMHandle->pDeviceHandler->ResumeThread(&TabWin32Handles[iIndex].hmHandleData);
4268
4269 return (lpResult); /* deliver return code */
4270}
4271
4272/*****************************************************************************
4273 * Name : HMGetExitCodeThread
4274 * Purpose : router function for GetExitCodeThread
4275 * Parameters:
4276 * Variables :
4277 * Result :
4278 * Remark :
4279 * Status :
4280 *
4281 * Author : SvL
4282 *****************************************************************************/
4283BOOL HMGetExitCodeThread(HANDLE hThread, LPDWORD lpExitCode)
4284{
4285 int iIndex; /* index into the handle table */
4286 BOOL lpResult; /* result from the device handler's API */
4287 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
4288
4289 SetLastError(ERROR_SUCCESS);
4290 /* validate handle */
4291 iIndex = _HMHandleQuery(hThread); /* get the index */
4292 if (-1 == iIndex) /* error ? */
4293 {
4294 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
4295 return FALSE; /* signal failure */
4296 }
4297
4298 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
4299 lpResult = pHMHandle->pDeviceHandler->GetExitCodeThread(&TabWin32Handles[iIndex].hmHandleData, lpExitCode);
4300
4301 return (lpResult); /* deliver return code */
4302}
4303/*****************************************************************************
4304 * Name : HMSetThreadTerminated
4305 * Purpose :
4306 * Parameters:
4307 * Variables :
4308 * Result :
4309 * Remark :
4310 * Status :
4311 *
4312 * Author : SvL
4313 *****************************************************************************/
4314BOOL HMSetThreadTerminated(HANDLE hThread)
4315{
4316 int iIndex; /* index into the handle table */
4317 BOOL lpResult; /* result from the device handler's API */
4318 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
4319
4320 SetLastError(ERROR_SUCCESS);
4321 /* validate handle */
4322 iIndex = _HMHandleQuery(hThread); /* get the index */
4323 if (-1 == iIndex) /* error ? */
4324 {
4325 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
4326 return FALSE; /* signal failure */
4327 }
4328
4329 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
4330 lpResult = pHMHandle->pDeviceHandler->SetThreadTerminated(&TabWin32Handles[iIndex].hmHandleData);
4331
4332 return (lpResult); /* deliver return code */
4333}
4334
4335/*****************************************************************************
4336 * Name : HMPeekNamedPipe
4337 * Purpose :
4338 * Parameters:
4339 * Variables :
4340 * Result :
4341 * Remark :
4342 * Status :
4343 *
4344 * Author : Przemyslaw Dobrowolski
4345 *****************************************************************************/
4346BOOL HMPeekNamedPipe(HANDLE hPipe,
4347 LPVOID lpvBuffer,
4348 DWORD cbBuffer,
4349 LPDWORD lpcbRead,
4350 LPDWORD lpcbAvail,
4351 LPDWORD lpcbMessage)
4352{
4353 int iIndex; /* index into the handle table */
4354 BOOL lpResult; /* result from the device handler's API */
4355 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
4356
4357 SetLastError(ERROR_SUCCESS);
4358 /* validate handle */
4359 iIndex = _HMHandleQuery(hPipe); /* get the index */
4360 if (-1 == iIndex) /* error ? */
4361 {
4362 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
4363 return FALSE; /* signal failure */
4364 }
4365
4366 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
4367 lpResult = pHMHandle->pDeviceHandler->PeekNamedPipe(&TabWin32Handles[iIndex].hmHandleData,
4368 lpvBuffer,
4369 cbBuffer,
4370 lpcbRead,
4371 lpcbAvail,
4372 lpcbMessage);
4373
4374 return (lpResult); /* deliver return code */
4375}
4376
4377/*****************************************************************************
4378 * Name : HMCreateNamedPipe
4379 * Purpose :
4380 * Parameters:
4381 * Variables :
4382 * Result :
4383 * Remark :
4384 * Status :
4385 *
4386 * Author : Przemyslaw Dobrowolski
4387 *****************************************************************************/
4388DWORD HMCreateNamedPipe(LPCTSTR lpName,
4389 DWORD dwOpenMode,
4390 DWORD dwPipeMode,
4391 DWORD nMaxInstances,
4392 DWORD nOutBufferSize,
4393 DWORD nInBufferSize,
4394 DWORD nDefaultTimeOut,
4395 LPSECURITY_ATTRIBUTES lpSecurityAttributes)
4396{
4397 int iIndex; /* index into the handle table */
4398 int iIndexNew; /* index into the handle table */
4399 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
4400 PHMHANDLEDATA pHMHandleData;
4401 HANDLE rc; /* API return code */
4402
4403 SetLastError(ERROR_SUCCESS);
4404
4405 pDeviceHandler = HMGlobals.pHMNamedPipe; /* device is predefined */
4406
4407 iIndexNew = _HMHandleGetFree(); /* get free handle */
4408 if (-1 == iIndexNew) /* oops, no free handles ! */
4409 {
4410 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
4411 return 0;
4412 }
4413
4414 /* initialize the complete HMHANDLEDATA structure */
4415 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
4416 pHMHandleData->dwType = FILE_TYPE_PIPE;
4417 pHMHandleData->dwAccess = 0;
4418 pHMHandleData->dwShare = 0;
4419 pHMHandleData->dwCreation = 0;
4420 pHMHandleData->dwFlags = 0;
4421 pHMHandleData->lpHandlerData = NULL;
4422
4423 /* we've got to mark the handle as occupied here, since another device */
4424 /* could be created within the device handler -> deadlock */
4425
4426 /* write appropriate entry into the handle table if open succeeded */
4427 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
4428 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
4429
4430 /* call the device handler */
4431
4432 rc = pDeviceHandler->CreateNamedPipe(&TabWin32Handles[iIndexNew].hmHandleData,
4433 lpName,dwOpenMode,
4434 dwPipeMode,nMaxInstances,
4435 nOutBufferSize,nInBufferSize,
4436 nDefaultTimeOut,lpSecurityAttributes);
4437
4438 if (rc == -1) /* oops, creation failed within the device handler */
4439 {
4440 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
4441 return 0; /* signal error */
4442 }
4443
4444 dprintf(("Win32 Handle -> %08x",iIndexNew));
4445
4446 return iIndexNew;
4447}
4448
4449/*****************************************************************************
4450 * Name : HMConnectNamedPipe
4451 * Purpose :
4452 * Parameters:
4453 * Variables :
4454 * Result :
4455 * Remark :
4456 * Status :
4457 *
4458 * Author : Przemyslaw Dobrowolski
4459 *****************************************************************************/
4460BOOL HMConnectNamedPipe(HANDLE hPipe, LPOVERLAPPED lpOverlapped)
4461{
4462 int iIndex; /* index into the handle table */
4463 BOOL lpResult; /* result from the device handler's API */
4464 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
4465
4466 SetLastError(ERROR_SUCCESS);
4467 /* validate handle */
4468 iIndex = _HMHandleQuery(hPipe); /* get the index */
4469 if (-1 == iIndex) /* error ? */
4470 {
4471 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
4472 return FALSE; /* signal failure */
4473 }
4474
4475 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
4476 lpResult = pHMHandle->pDeviceHandler->ConnectNamedPipe(&TabWin32Handles[iIndex].hmHandleData,
4477 lpOverlapped);
4478
4479 return (lpResult); /* deliver return code */
4480}
4481
4482/*****************************************************************************
4483 * Name : HMDisconnectNamedPipe
4484 * Purpose :
4485 * Parameters:
4486 * Variables :
4487 * Result :
4488 * Remark :
4489 * Status :
4490 *
4491 * Author : Przemyslaw Dobrowolski
4492 *****************************************************************************/
4493BOOL HMDisconnectNamedPipe(HANDLE hPipe)
4494{
4495 int iIndex; /* index into the handle table */
4496 BOOL lpResult; /* result from the device handler's API */
4497 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
4498
4499 SetLastError(ERROR_SUCCESS);
4500 /* validate handle */
4501 iIndex = _HMHandleQuery(hPipe); /* get the index */
4502 if (-1 == iIndex) /* error ? */
4503 {
4504 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
4505 return FALSE; /* signal failure */
4506 }
4507
4508 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
4509 lpResult = pHMHandle->pDeviceHandler->DisconnectNamedPipe(&TabWin32Handles[iIndex].hmHandleData);
4510
4511 return (lpResult); /* deliver return code */
4512}
4513
4514/*****************************************************************************
4515 * Name : HMGetNamedPipeHandleState
4516 * Purpose :
4517 * Parameters:
4518 * Variables :
4519 * Result :
4520 * Remark :
4521 * Status :
4522 *
4523 * Author : Przemyslaw Dobrowolski
4524 *****************************************************************************/
4525BOOL HMGetNamedPipeHandleState(HANDLE hPipe,
4526 LPDWORD lpState,
4527 LPDWORD lpCurInstances,
4528 LPDWORD lpMaxCollectionCount,
4529 LPDWORD lpCollectDataTimeout,
4530 LPTSTR lpUserName,
4531 DWORD nMaxUserNameSize)
4532{
4533 int iIndex; /* index into the handle table */
4534 BOOL lpResult; /* result from the device handler's API */
4535 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
4536
4537 SetLastError(ERROR_SUCCESS);
4538 /* validate handle */
4539 iIndex = _HMHandleQuery(hPipe); /* get the index */
4540 if (-1 == iIndex) /* error ? */
4541 {
4542 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
4543 return FALSE; /* signal failure */
4544 }
4545
4546 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
4547 lpResult = pHMHandle->pDeviceHandler->GetNamedPipeHandleState(&TabWin32Handles[iIndex].hmHandleData,
4548 lpState,
4549 lpCurInstances,
4550 lpMaxCollectionCount,
4551 lpCollectDataTimeout,
4552 lpUserName,
4553 nMaxUserNameSize);
4554
4555
4556 return (lpResult); /* deliver return code */
4557}
4558
4559/*****************************************************************************
4560 * Name : HMGetNamedPipeInfo
4561 * Purpose :
4562 * Parameters:
4563 * Variables :
4564 * Result :
4565 * Remark :
4566 * Status :
4567 *
4568 * Author : Przemyslaw Dobrowolski
4569 *****************************************************************************/
4570BOOL HMGetNamedPipeInfo(HANDLE hPipe,
4571 LPDWORD lpFlags,
4572 LPDWORD lpOutBufferSize,
4573 LPDWORD lpInBufferSize,
4574 LPDWORD lpMaxInstances)
4575{
4576 int iIndex; /* index into the handle table */
4577 BOOL lpResult; /* result from the device handler's API */
4578 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
4579
4580 SetLastError(ERROR_SUCCESS);
4581 /* validate handle */
4582 iIndex = _HMHandleQuery(hPipe); /* get the index */
4583 if (-1 == iIndex) /* error ? */
4584 {
4585 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
4586 return FALSE; /* signal failure */
4587 }
4588
4589 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
4590 lpResult = pHMHandle->pDeviceHandler->GetNamedPipeInfo(&TabWin32Handles[iIndex].hmHandleData,
4591 lpFlags,
4592 lpOutBufferSize,
4593 lpInBufferSize,
4594 lpMaxInstances);
4595
4596 return (lpResult); /* deliver return code */
4597}
4598
4599/*****************************************************************************
4600 * Name : HMTransactNamedPipe
4601 * Purpose :
4602 * Parameters:
4603 * Variables :
4604 * Result :
4605 * Remark :
4606 * Status :
4607 *
4608 * Author : Przemyslaw Dobrowolski
4609 *****************************************************************************/
4610DWORD HMTransactNamedPipe(HANDLE hPipe,
4611 LPVOID lpvWriteBuf,
4612 DWORD cbWriteBuf,
4613 LPVOID lpvReadBuf,
4614 DWORD cbReadBuf,
4615 LPDWORD lpcbRead,
4616 LPOVERLAPPED lpo)
4617{
4618 int iIndex; /* index into the handle table */
4619 DWORD lpResult; /* result from the device handler's API */
4620 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
4621
4622 SetLastError(ERROR_SUCCESS);
4623 /* validate handle */
4624 iIndex = _HMHandleQuery(hPipe); /* get the index */
4625 if (-1 == iIndex) /* error ? */
4626 {
4627 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
4628 return FALSE; /* signal failure */
4629 }
4630
4631 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
4632 lpResult = pHMHandle->pDeviceHandler->TransactNamedPipe(&TabWin32Handles[iIndex].hmHandleData,
4633 lpvWriteBuf,
4634 cbWriteBuf,
4635 lpvReadBuf,
4636 cbReadBuf,
4637 lpcbRead,
4638 lpo);
4639
4640 return (lpResult); /* deliver return code */
4641}
4642
4643/*****************************************************************************
4644 * Name : HMSetNamedPipeHandleState
4645 * Purpose :
4646 * Parameters:
4647 * Variables :
4648 * Result :
4649 * Remark :
4650 * Status :
4651 *
4652 * Author : Przemyslaw Dobrowolski
4653 *****************************************************************************/
4654BOOL HMSetNamedPipeHandleState(HANDLE hPipe,
4655 LPDWORD lpdwMode,
4656 LPDWORD lpcbMaxCollect,
4657 LPDWORD lpdwCollectDataTimeout)
4658{
4659 int iIndex; /* index into the handle table */
4660 BOOL lpResult; /* result from the device handler's API */
4661 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
4662
4663 SetLastError(ERROR_SUCCESS);
4664 /* validate handle */
4665 iIndex = _HMHandleQuery(hPipe); /* get the index */
4666 if (-1 == iIndex) /* error ? */
4667 {
4668 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
4669 return FALSE; /* signal failure */
4670 }
4671
4672 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
4673 lpResult = pHMHandle->pDeviceHandler->SetNamedPipeHandleState(&TabWin32Handles[iIndex].hmHandleData,
4674 lpdwMode,
4675 lpcbMaxCollect,
4676 lpdwCollectDataTimeout);
4677
4678 return (lpResult); /* deliver return code */
4679}
4680
4681/*****************************************************************************
4682 * Name : HMCreatePipe
4683 * Purpose :
4684 * Parameters:
4685 * Variables :
4686 * Result :
4687 * Remark :
4688 * Status : NOT TESTED!
4689 *
4690 * Author : Przemyslaw Dobrowolski
4691 *****************************************************************************/
4692BOOL HMCreatePipe(PHANDLE phRead,
4693 PHANDLE phWrite,
4694 LPSECURITY_ATTRIBUTES lpsa,
4695 DWORD cbPipe)
4696{
4697 int iIndex; /* index into the handle table */
4698 int iIndexNewRead; /* index into the handle table */
4699 int iIndexNewWrite; /* index into the handle table */
4700 HMDeviceHandler *pDeviceHandler; /* device handler for this handle */
4701 PHMHANDLEDATA pHMHandleData;
4702 HANDLE rc; /* API return code */
4703
4704 SetLastError(ERROR_SUCCESS);
4705
4706 pDeviceHandler = HMGlobals.pHMNamedPipe; /* device is predefined */
4707
4708 iIndexNewRead = _HMHandleGetFree(); /* get free handle */
4709 if (-1 == iIndexNewRead) /* oops, no free handles ! */
4710 {
4711 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
4712 return 0;
4713 }
4714
4715 iIndexNewWrite = _HMHandleGetFree(); /* get free handle */
4716 if (-1 == iIndexNewWrite) /* oops, no free handles ! */
4717 {
4718 HMHandleFree(iIndexNewRead);
4719 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
4720 return 0;
4721 }
4722
4723
4724 /* initialize the complete HMHANDLEDATA structure */
4725 pHMHandleData = &TabWin32Handles[iIndexNewRead].hmHandleData;
4726 pHMHandleData->dwType = FILE_TYPE_PIPE;
4727 pHMHandleData->dwAccess = 0;
4728 pHMHandleData->dwShare = 0;
4729 pHMHandleData->dwCreation = 0;
4730 pHMHandleData->dwFlags = 0;
4731 pHMHandleData->lpHandlerData = NULL;
4732
4733 /* we've got to mark the handle as occupied here, since another device */
4734 /* could be created within the device handler -> deadlock */
4735
4736 /* write appropriate entry into the handle table if open succeeded */
4737 TabWin32Handles[iIndexNewRead].hmHandleData.hHMHandle = iIndexNewRead;
4738 TabWin32Handles[iIndexNewRead].pDeviceHandler = pDeviceHandler;
4739
4740 /* initialize the complete HMHANDLEDATA structure */
4741 pHMHandleData = &TabWin32Handles[iIndexNewWrite].hmHandleData;
4742 pHMHandleData->dwType = FILE_TYPE_PIPE;
4743 pHMHandleData->dwAccess = 0;
4744 pHMHandleData->dwShare = 0;
4745 pHMHandleData->dwCreation = 0;
4746 pHMHandleData->dwFlags = 0;
4747 pHMHandleData->lpHandlerData = NULL;
4748
4749 /* we've got to mark the handle as occupied here, since another device */
4750 /* could be created within the device handler -> deadlock */
4751
4752 /* write appropriate entry into the handle table if open succeeded */
4753 TabWin32Handles[iIndexNewWrite].hmHandleData.hHMHandle = iIndexNewWrite;
4754 TabWin32Handles[iIndexNewWrite].pDeviceHandler = pDeviceHandler;
4755 /* call the device handler */
4756
4757 rc = pDeviceHandler->CreatePipe(&TabWin32Handles[iIndexNewRead].hmHandleData,
4758 &TabWin32Handles[iIndexNewWrite].hmHandleData,
4759 lpsa,
4760 cbPipe);
4761
4762 if (rc == 0) /* oops, creation failed within the device handler */
4763 {
4764 HMHandleFree(iIndexNewRead);
4765 HMHandleFree(iIndexNewWrite);
4766 return FALSE; /* signal error */
4767 }
4768
4769 *phRead = iIndexNewRead;
4770 *phWrite = iIndexNewWrite;
4771
4772 return TRUE;
4773}
4774
4775/*****************************************************************************
4776 * Name : HMCreateMailslotA
4777 * Purpose :
4778 * Parameters:
4779 * Variables :
4780 * Result :
4781 * Remark :
4782 * Status :
4783 *
4784 * Author : SvL
4785 *****************************************************************************/
4786HANDLE HMCreateMailslotA(LPCSTR lpName, DWORD nMaxMessageSize,
4787 DWORD lReadTimeout,
4788 LPSECURITY_ATTRIBUTES lpSecurityAttributes)
4789{
4790 int iIndex; /* index into the handle table */
4791 int iIndexNew; /* index into the handle table */
4792 HMMailslotClass *pDeviceHandler; /* device handler for this handle */
4793 PHMHANDLEDATA pHMHandleData;
4794 BOOL rc; /* API return code */
4795
4796 SetLastError(ERROR_SUCCESS);
4797
4798 pDeviceHandler = (HMMailslotClass *)HMGlobals.pHMMailslot; /* device is predefined */
4799
4800 iIndexNew = _HMHandleGetFree(); /* get free handle */
4801 if (-1 == iIndexNew) /* oops, no free handles ! */
4802 {
4803 SetLastError(ERROR_NOT_ENOUGH_MEMORY); /* use this as error message */
4804 return 0;
4805 }
4806
4807 /* initialize the complete HMHANDLEDATA structure */
4808 pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
4809 pHMHandleData->dwType = FILE_TYPE_UNKNOWN;
4810 pHMHandleData->dwAccess = 0;
4811 pHMHandleData->dwShare = 0;
4812 pHMHandleData->dwCreation = 0;
4813 pHMHandleData->dwFlags = 0;
4814 pHMHandleData->lpHandlerData = NULL;
4815
4816 /* we've got to mark the handle as occupied here, since another device */
4817 /* could be created within the device handler -> deadlock */
4818
4819 /* write appropriate entry into the handle table if open succeeded */
4820 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
4821 TabWin32Handles[iIndexNew].pDeviceHandler = pDeviceHandler;
4822
4823 /* call the device handler */
4824
4825 rc = pDeviceHandler->CreateMailslotA(&TabWin32Handles[iIndexNew].hmHandleData,
4826 lpName, nMaxMessageSize,
4827 lReadTimeout, lpSecurityAttributes);
4828
4829 if (rc == FALSE) /* oops, creation failed within the device handler */
4830 {
4831 TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
4832 return 0; /* signal error */
4833 }
4834
4835 return iIndexNew;
4836}
4837/*****************************************************************************
4838 * Name : HMGetMailslotInfo
4839 * Purpose :
4840 * Parameters:
4841 * Variables :
4842 * Result :
4843 * Remark :
4844 * Status :
4845 *
4846 * Author : SvL
4847 *****************************************************************************/
4848BOOL HMGetMailslotInfo(HANDLE hMailslot,
4849 LPDWORD lpMaxMessageSize,
4850 LPDWORD lpNextSize,
4851 LPDWORD lpMessageCount,
4852 LPDWORD lpReadTimeout)
4853{
4854 int iIndex; /* index into the handle table */
4855 BOOL lpResult; /* result from the device handler's API */
4856 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
4857
4858 SetLastError(ERROR_SUCCESS);
4859 /* validate handle */
4860 iIndex = _HMHandleQuery(hMailslot); /* get the index */
4861 if (-1 == iIndex) /* error ? */
4862 {
4863 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
4864 return FALSE; /* signal failure */
4865 }
4866
4867 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
4868 lpResult = pHMHandle->pDeviceHandler->GetMailslotInfo(&TabWin32Handles[iIndex].hmHandleData,
4869 lpMaxMessageSize,
4870 lpNextSize,
4871 lpMessageCount,
4872 lpReadTimeout);
4873 return (lpResult); /* deliver return code */
4874}
4875/*****************************************************************************
4876 * Name : HMSetMailslotInfo
4877 * Purpose :
4878 * Parameters:
4879 * Variables :
4880 * Result :
4881 * Remark :
4882 * Status :
4883 *
4884 * Author : SvL
4885 *****************************************************************************/
4886BOOL HMSetMailslotInfo(HANDLE hMailslot,
4887 DWORD dwReadTimeout)
4888{
4889 int iIndex; /* index into the handle table */
4890 BOOL lpResult; /* result from the device handler's API */
4891 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
4892
4893 SetLastError(ERROR_SUCCESS);
4894 /* validate handle */
4895 iIndex = _HMHandleQuery(hMailslot); /* get the index */
4896 if (-1 == iIndex) /* error ? */
4897 {
4898 SetLastError(ERROR_INVALID_HANDLE); /* set win32 error information */
4899 return FALSE; /* signal failure */
4900 }
4901
4902 pHMHandle = &TabWin32Handles[iIndex]; /* call device handler */
4903 lpResult = pHMHandle->pDeviceHandler->SetMailslotInfo(&TabWin32Handles[iIndex].hmHandleData,
4904 dwReadTimeout);
4905
4906 return (lpResult); /* deliver return code */
4907}
Note: See TracBrowser for help on using the repository browser.