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

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

disk fix

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