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

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

preliminary changes for new overlapped io framework

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