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

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

added NUL device

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