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

Last change on this file since 8880 was 8880, checked in by sandervl, 23 years ago

Dynamically allocate handlemanager array to prevent waste of shared memory (as it is a private array (for now))

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