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

Last change on this file since 7579 was 7579, checked in by sandervl, 24 years ago

Added symbolic link for Windows 2000 device driver names (Global)

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