1 /*
2 * Kernel synchronization objects
3 *
4 * Copyright 1998 Alexandre Julliard
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <string.h>
25 #ifdef HAVE_UNISTD_H
26 # include <unistd.h>
27 #endif
28 #include <errno.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31
32 #define NONAMELESSUNION
33 #define NONAMELESSSTRUCT
34
35 #include "ntstatus.h"
36 #define WIN32_NO_STATUS
37 #include "windef.h"
38 #include "winbase.h"
39 #include "winerror.h"
40 #include "winnls.h"
41 #include "winternl.h"
42 #include "winioctl.h"
43 #include "ddk/wdm.h"
44
45 #include "wine/unicode.h"
46 #include "kernel_private.h"
47
48 #include "wine/debug.h"
49
50 WINE_DEFAULT_DEBUG_CHANNEL(sync);
51
52 /* check if current version is NT or Win95 */
53 static inline int is_version_nt(void)
54 {
55 return !(GetVersion() & 0x80000000);
56 }
57
58 /* returns directory handle to \\BaseNamedObjects */
59 HANDLE get_BaseNamedObjects_handle(void)
60 {
61 static HANDLE handle = NULL;
62 static const WCHAR basenameW[] =
63 {'\\','B','a','s','e','N','a','m','e','d','O','b','j','e','c','t','s',0};
64 UNICODE_STRING str;
65 OBJECT_ATTRIBUTES attr;
66
67 if (!handle)
68 {
69 HANDLE dir;
70
71 RtlInitUnicodeString(&str, basenameW);
72 InitializeObjectAttributes(&attr, &str, 0, 0, NULL);
73 NtOpenDirectoryObject(&dir, DIRECTORY_CREATE_OBJECT|DIRECTORY_TRAVERSE,
74 &attr);
75 if (InterlockedCompareExchangePointer( &handle, dir, 0 ) != 0)
76 {
77 /* someone beat us here... */
78 CloseHandle( dir );
79 }
80 }
81 return handle;
82 }
83
84 /* helper for kernel32->ntdll timeout format conversion */
85 static inline PLARGE_INTEGER get_nt_timeout( PLARGE_INTEGER pTime, DWORD timeout )
86 {
87 if (timeout == INFINITE) return NULL;
88 pTime->QuadPart = (ULONGLONG)timeout * -10000;
89 return pTime;
90 }
91
92 /***********************************************************************
93 * Sleep (KERNEL32.@)
94 */
95 VOID WINAPI Sleep( DWORD timeout )
96 {
97 SleepEx( timeout, FALSE );
98 }
99
100 /******************************************************************************
101 * SleepEx (KERNEL32.@)
102 */
103 DWORD WINAPI SleepEx( DWORD timeout, BOOL alertable )
104 {
105 NTSTATUS status;
106 LARGE_INTEGER time;
107
108 status = NtDelayExecution( alertable, get_nt_timeout( &time, timeout ) );
109 if (status == STATUS_USER_APC) return WAIT_IO_COMPLETION;
110 return 0;
111 }
112
113
114 /***********************************************************************
115 * SwitchToThread (KERNEL32.@)
116 */
117 BOOL WINAPI SwitchToThread(void)
118 {
119 return (NtYieldExecution() != STATUS_NO_YIELD_PERFORMED);
120 }
121
122
123 /***********************************************************************
124 * WaitForSingleObject (KERNEL32.@)
125 */
126 DWORD WINAPI WaitForSingleObject( HANDLE handle, DWORD timeout )
127 {
128 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, FALSE );
129 }
130
131
132 /***********************************************************************
133 * WaitForSingleObjectEx (KERNEL32.@)
134 */
135 DWORD WINAPI WaitForSingleObjectEx( HANDLE handle, DWORD timeout,
136 BOOL alertable )
137 {
138 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, alertable );
139 }
140
141
142 /***********************************************************************
143 * WaitForMultipleObjects (KERNEL32.@)
144 */
145 DWORD WINAPI WaitForMultipleObjects( DWORD count, const HANDLE *handles,
146 BOOL wait_all, DWORD timeout )
147 {
148 return WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
149 }
150
151
152 /***********************************************************************
153 * WaitForMultipleObjectsEx (KERNEL32.@)
154 */
155 DWORD WINAPI WaitForMultipleObjectsEx( DWORD count, const HANDLE *handles,
156 BOOL wait_all, DWORD timeout,
157 BOOL alertable )
158 {
159 NTSTATUS status;
160 HANDLE hloc[MAXIMUM_WAIT_OBJECTS];
161 LARGE_INTEGER time;
162 unsigned int i;
163
164 if (count > MAXIMUM_WAIT_OBJECTS)
165 {
166 SetLastError(ERROR_INVALID_PARAMETER);
167 return WAIT_FAILED;
168 }
169 for (i = 0; i < count; i++)
170 {
171 if ((handles[i] == (HANDLE)STD_INPUT_HANDLE) ||
172 (handles[i] == (HANDLE)STD_OUTPUT_HANDLE) ||
173 (handles[i] == (HANDLE)STD_ERROR_HANDLE))
174 hloc[i] = GetStdHandle( HandleToULong(handles[i]) );
175 else
176 hloc[i] = handles[i];
177
178 /* yes, even screen buffer console handles are waitable, and are
179 * handled as a handle to the console itself !!
180 */
181 if (is_console_handle(hloc[i]))
182 {
183 if (!VerifyConsoleIoHandle(hloc[i]))
184 {
185 return FALSE;
186 }
187 hloc[i] = GetConsoleInputWaitHandle();
188 }
189 }
190
191 status = NtWaitForMultipleObjects( count, hloc, wait_all, alertable,
192 get_nt_timeout( &time, timeout ) );
193
194 if (HIWORD(status)) /* is it an error code? */
195 {
196 SetLastError( RtlNtStatusToDosError(status) );
197 status = WAIT_FAILED;
198 }
199 return status;
200 }
201
202
203 /***********************************************************************
204 * RegisterWaitForSingleObject (KERNEL32.@)
205 */
206 BOOL WINAPI RegisterWaitForSingleObject(PHANDLE phNewWaitObject, HANDLE hObject,
207 WAITORTIMERCALLBACK Callback, PVOID Context,
208 ULONG dwMilliseconds, ULONG dwFlags)
209 {
210 NTSTATUS status;
211
212 TRACE("%p %p %p %p %d %d\n",
213 phNewWaitObject,hObject,Callback,Context,dwMilliseconds,dwFlags);
214
215 status = RtlRegisterWait( phNewWaitObject, hObject, Callback, Context, dwMilliseconds, dwFlags );
216 if (status != STATUS_SUCCESS)
217 {
218 SetLastError( RtlNtStatusToDosError(status) );
219 return FALSE;
220 }
221 return TRUE;
222 }
223
224 /***********************************************************************
225 * RegisterWaitForSingleObjectEx (KERNEL32.@)
226 */
227 HANDLE WINAPI RegisterWaitForSingleObjectEx( HANDLE hObject,
228 WAITORTIMERCALLBACK Callback, PVOID Context,
229 ULONG dwMilliseconds, ULONG dwFlags )
230 {
231 NTSTATUS status;
232 HANDLE hNewWaitObject;
233
234 TRACE("%p %p %p %d %d\n",
235 hObject,Callback,Context,dwMilliseconds,dwFlags);
236
237 status = RtlRegisterWait( &hNewWaitObject, hObject, Callback, Context, dwMilliseconds, dwFlags );
238 if (status != STATUS_SUCCESS)
239 {
240 SetLastError( RtlNtStatusToDosError(status) );
241 return NULL;
242 }
243 return hNewWaitObject;
244 }
245
246 /***********************************************************************
247 * UnregisterWait (KERNEL32.@)
248 */
249 BOOL WINAPI UnregisterWait( HANDLE WaitHandle )
250 {
251 NTSTATUS status;
252
253 TRACE("%p\n",WaitHandle);
254
255 status = RtlDeregisterWait( WaitHandle );
256 if (status != STATUS_SUCCESS)
257 {
258 SetLastError( RtlNtStatusToDosError(status) );
259 return FALSE;
260 }
261 return TRUE;
262 }
263
264 /***********************************************************************
265 * UnregisterWaitEx (KERNEL32.@)
266 */
267 BOOL WINAPI UnregisterWaitEx( HANDLE WaitHandle, HANDLE CompletionEvent )
268 {
269 NTSTATUS status;
270
271 TRACE("%p %p\n",WaitHandle, CompletionEvent);
272
273 status = RtlDeregisterWaitEx( WaitHandle, CompletionEvent );
274 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
275 return !status;
276 }
277
278 /***********************************************************************
279 * SignalObjectAndWait (KERNEL32.@)
280 *
281 * Allows to atomically signal any of the synchro objects (semaphore,
282 * mutex, event) and wait on another.
283 */
284 DWORD WINAPI SignalObjectAndWait( HANDLE hObjectToSignal, HANDLE hObjectToWaitOn,
285 DWORD dwMilliseconds, BOOL bAlertable )
286 {
287 NTSTATUS status;
288 LARGE_INTEGER timeout;
289
290 TRACE("%p %p %d %d\n", hObjectToSignal,
291 hObjectToWaitOn, dwMilliseconds, bAlertable);
292
293 status = NtSignalAndWaitForSingleObject( hObjectToSignal, hObjectToWaitOn, bAlertable,
294 get_nt_timeout( &timeout, dwMilliseconds ) );
295 if (HIWORD(status))
296 {
297 SetLastError( RtlNtStatusToDosError(status) );
298 status = WAIT_FAILED;
299 }
300 return status;
301 }
302
303 /***********************************************************************
304 * InitializeCriticalSection (KERNEL32.@)
305 *
306 * Initialise a critical section before use.
307 *
308 * PARAMS
309 * crit [O] Critical section to initialise.
310 *
311 * RETURNS
312 * Nothing. If the function fails an exception is raised.
313 */
314 void WINAPI InitializeCriticalSection( CRITICAL_SECTION *crit )
315 {
316 InitializeCriticalSectionEx( crit, 0, 0 );
317 }
318
319 /***********************************************************************
320 * InitializeCriticalSectionAndSpinCount (KERNEL32.@)
321 *
322 * Initialise a critical section with a spin count.
323 *
324 * PARAMS
325 * crit [O] Critical section to initialise.
326 * spincount [I] Number of times to spin upon contention.
327 *
328 * RETURNS
329 * Success: TRUE.
330 * Failure: Nothing. If the function fails an exception is raised.
331 *
332 * NOTES
333 * spincount is ignored on uni-processor systems.
334 */
335 BOOL WINAPI InitializeCriticalSectionAndSpinCount( CRITICAL_SECTION *crit, DWORD spincount )
336 {
337 return InitializeCriticalSectionEx( crit, spincount, 0 );
338 }
339
340 /***********************************************************************
341 * InitializeCriticalSectionEx (KERNEL32.@)
342 *
343 * Initialise a critical section with a spin count and flags.
344 *
345 * PARAMS
346 * crit [O] Critical section to initialise.
347 * spincount [I] Number of times to spin upon contention.
348 * flags [I] CRITICAL_SECTION_ flags from winbase.h.
349 *
350 * RETURNS
351 * Success: TRUE.
352 * Failure: Nothing. If the function fails an exception is raised.
353 *
354 * NOTES
355 * spincount is ignored on uni-processor systems.
356 */
357 BOOL WINAPI InitializeCriticalSectionEx( CRITICAL_SECTION *crit, DWORD spincount, DWORD flags )
358 {
359 NTSTATUS ret = RtlInitializeCriticalSectionEx( crit, spincount, flags );
360 if (ret) RtlRaiseStatus( ret );
361 return !ret;
362 }
363
364 /***********************************************************************
365 * MakeCriticalSectionGlobal (KERNEL32.@)
366 */
367 void WINAPI MakeCriticalSectionGlobal( CRITICAL_SECTION *crit )
368 {
369 /* let's assume that only one thread at a time will try to do this */
370 HANDLE sem = crit->LockSemaphore;
371 if (!sem) NtCreateSemaphore( &sem, SEMAPHORE_ALL_ACCESS, NULL, 0, 1 );
372 crit->LockSemaphore = ConvertToGlobalHandle( sem );
373 RtlFreeHeap( GetProcessHeap(), 0, crit->DebugInfo );
374 crit->DebugInfo = NULL;
375 }
376
377
378 /***********************************************************************
379 * ReinitializeCriticalSection (KERNEL32.@)
380 *
381 * Initialise an already used critical section.
382 *
383 * PARAMS
384 * crit [O] Critical section to initialise.
385 *
386 * RETURNS
387 * Nothing.
388 */
389 void WINAPI ReinitializeCriticalSection( CRITICAL_SECTION *crit )
390 {
391 if ( !crit->LockSemaphore )
392 RtlInitializeCriticalSection( crit );
393 }
394
395
396 /***********************************************************************
397 * UninitializeCriticalSection (KERNEL32.@)
398 *
399 * UnInitialise a critical section after use.
400 *
401 * PARAMS
402 * crit [O] Critical section to uninitialise (destroy).
403 *
404 * RETURNS
405 * Nothing.
406 */
407 void WINAPI UninitializeCriticalSection( CRITICAL_SECTION *crit )
408 {
409 RtlDeleteCriticalSection( crit );
410 }
411
412
413 /***********************************************************************
414 * CreateEventA (KERNEL32.@)
415 */
416 HANDLE WINAPI CreateEventA( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
417 BOOL initial_state, LPCSTR name )
418 {
419 DWORD flags = 0;
420
421 if (manual_reset) flags |= CREATE_EVENT_MANUAL_RESET;
422 if (initial_state) flags |= CREATE_EVENT_INITIAL_SET;
423 return CreateEventExA( sa, name, flags, EVENT_ALL_ACCESS );
424 }
425
426
427 /***********************************************************************
428 * CreateEventW (KERNEL32.@)
429 */
430 HANDLE WINAPI CreateEventW( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
431 BOOL initial_state, LPCWSTR name )
432 {
433 DWORD flags = 0;
434
435 if (manual_reset) flags |= CREATE_EVENT_MANUAL_RESET;
436 if (initial_state) flags |= CREATE_EVENT_INITIAL_SET;
437 return CreateEventExW( sa, name, flags, EVENT_ALL_ACCESS );
438 }
439
440
441 /***********************************************************************
442 * CreateEventExA (KERNEL32.@)
443 */
444 HANDLE WINAPI CreateEventExA( SECURITY_ATTRIBUTES *sa, LPCSTR name, DWORD flags, DWORD access )
445 {
446 WCHAR buffer[MAX_PATH];
447
448 if (!name) return CreateEventExW( sa, NULL, flags, access );
449
450 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
451 {
452 SetLastError( ERROR_FILENAME_EXCED_RANGE );
453 return 0;
454 }
455 return CreateEventExW( sa, buffer, flags, access );
456 }
457
458
459 /***********************************************************************
460 * CreateEventExW (KERNEL32.@)
461 */
462 HANDLE WINAPI CreateEventExW( SECURITY_ATTRIBUTES *sa, LPCWSTR name, DWORD flags, DWORD access )
463 {
464 HANDLE ret;
465 UNICODE_STRING nameW;
466 OBJECT_ATTRIBUTES attr;
467 NTSTATUS status;
468
469 /* one buggy program needs this
470 * ("Van Dale Groot woordenboek der Nederlandse taal")
471 */
472 if (sa && IsBadReadPtr(sa,sizeof(SECURITY_ATTRIBUTES)))
473 {
474 ERR("Bad security attributes pointer %p\n",sa);
475 SetLastError( ERROR_INVALID_PARAMETER);
476 return 0;
477 }
478
479 attr.Length = sizeof(attr);
480 attr.RootDirectory = 0;
481 attr.ObjectName = NULL;
482 attr.Attributes = OBJ_OPENIF | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
483 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
484 attr.SecurityQualityOfService = NULL;
485 if (name)
486 {
487 RtlInitUnicodeString( &nameW, name );
488 attr.ObjectName = &nameW;
489 attr.RootDirectory = get_BaseNamedObjects_handle();
490 }
491
492 status = NtCreateEvent( &ret, access, &attr, (flags & CREATE_EVENT_MANUAL_RESET) != 0,
493 (flags & CREATE_EVENT_INITIAL_SET) != 0 );
494 if (status == STATUS_OBJECT_NAME_EXISTS)
495 SetLastError( ERROR_ALREADY_EXISTS );
496 else
497 SetLastError( RtlNtStatusToDosError(status) );
498 return ret;
499 }
500
501
502 /***********************************************************************
503 * OpenEventA (KERNEL32.@)
504 */
505 HANDLE WINAPI OpenEventA( DWORD access, BOOL inherit, LPCSTR name )
506 {
507 WCHAR buffer[MAX_PATH];
508
509 if (!name) return OpenEventW( access, inherit, NULL );
510
511 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
512 {
513 SetLastError( ERROR_FILENAME_EXCED_RANGE );
514 return 0;
515 }
516 return OpenEventW( access, inherit, buffer );
517 }
518
519
520 /***********************************************************************
521 * OpenEventW (KERNEL32.@)
522 */
523 HANDLE WINAPI OpenEventW( DWORD access, BOOL inherit, LPCWSTR name )
524 {
525 HANDLE ret;
526 UNICODE_STRING nameW;
527 OBJECT_ATTRIBUTES attr;
528 NTSTATUS status;
529
530 if (!is_version_nt()) access = EVENT_ALL_ACCESS;
531
532 attr.Length = sizeof(attr);
533 attr.RootDirectory = 0;
534 attr.ObjectName = NULL;
535 attr.Attributes = inherit ? OBJ_INHERIT : 0;
536 attr.SecurityDescriptor = NULL;
537 attr.SecurityQualityOfService = NULL;
538 if (name)
539 {
540 RtlInitUnicodeString( &nameW, name );
541 attr.ObjectName = &nameW;
542 attr.RootDirectory = get_BaseNamedObjects_handle();
543 }
544
545 status = NtOpenEvent( &ret, access, &attr );
546 if (status != STATUS_SUCCESS)
547 {
548 SetLastError( RtlNtStatusToDosError(status) );
549 return 0;
550 }
551 return ret;
552 }
553
554 /***********************************************************************
555 * PulseEvent (KERNEL32.@)
556 */
557 BOOL WINAPI PulseEvent( HANDLE handle )
558 {
559 NTSTATUS status;
560
561 if ((status = NtPulseEvent( handle, NULL )))
562 SetLastError( RtlNtStatusToDosError(status) );
563 return !status;
564 }
565
566
567 /***********************************************************************
568 * SetEvent (KERNEL32.@)
569 */
570 BOOL WINAPI SetEvent( HANDLE handle )
571 {
572 NTSTATUS status;
573
574 if ((status = NtSetEvent( handle, NULL )))
575 SetLastError( RtlNtStatusToDosError(status) );
576 return !status;
577 }
578
579
580 /***********************************************************************
581 * ResetEvent (KERNEL32.@)
582 */
583 BOOL WINAPI ResetEvent( HANDLE handle )
584 {
585 NTSTATUS status;
586
587 if ((status = NtResetEvent( handle, NULL )))
588 SetLastError( RtlNtStatusToDosError(status) );
589 return !status;
590 }
591
592
593 /***********************************************************************
594 * CreateMutexA (KERNEL32.@)
595 */
596 HANDLE WINAPI CreateMutexA( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCSTR name )
597 {
598 return CreateMutexExA( sa, name, owner ? CREATE_MUTEX_INITIAL_OWNER : 0, MUTEX_ALL_ACCESS );
599 }
600
601
602 /***********************************************************************
603 * CreateMutexW (KERNEL32.@)
604 */
605 HANDLE WINAPI CreateMutexW( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCWSTR name )
606 {
607 return CreateMutexExW( sa, name, owner ? CREATE_MUTEX_INITIAL_OWNER : 0, MUTEX_ALL_ACCESS );
608 }
609
610
611 /***********************************************************************
612 * CreateMutexExA (KERNEL32.@)
613 */
614 HANDLE WINAPI CreateMutexExA( SECURITY_ATTRIBUTES *sa, LPCSTR name, DWORD flags, DWORD access )
615 {
616 WCHAR buffer[MAX_PATH];
617
618 if (!name) return CreateMutexExW( sa, NULL, flags, access );
619
620 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
621 {
622 SetLastError( ERROR_FILENAME_EXCED_RANGE );
623 return 0;
624 }
625 return CreateMutexExW( sa, buffer, flags, access );
626 }
627
628
629 /***********************************************************************
630 * CreateMutexExW (KERNEL32.@)
631 */
632 HANDLE WINAPI CreateMutexExW( SECURITY_ATTRIBUTES *sa, LPCWSTR name, DWORD flags, DWORD access )
633 {
634 HANDLE ret;
635 UNICODE_STRING nameW;
636 OBJECT_ATTRIBUTES attr;
637 NTSTATUS status;
638
639 attr.Length = sizeof(attr);
640 attr.RootDirectory = 0;
641 attr.ObjectName = NULL;
642 attr.Attributes = OBJ_OPENIF | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
643 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
644 attr.SecurityQualityOfService = NULL;
645 if (name)
646 {
647 RtlInitUnicodeString( &nameW, name );
648 attr.ObjectName = &nameW;
649 attr.RootDirectory = get_BaseNamedObjects_handle();
650 }
651
652 status = NtCreateMutant( &ret, access, &attr, (flags & CREATE_MUTEX_INITIAL_OWNER) != 0 );
653 if (status == STATUS_OBJECT_NAME_EXISTS)
654 SetLastError( ERROR_ALREADY_EXISTS );
655 else
656 SetLastError( RtlNtStatusToDosError(status) );
657 return ret;
658 }
659
660
661 /***********************************************************************
662 * OpenMutexA (KERNEL32.@)
663 */
664 HANDLE WINAPI OpenMutexA( DWORD access, BOOL inherit, LPCSTR name )
665 {
666 WCHAR buffer[MAX_PATH];
667
668 if (!name) return OpenMutexW( access, inherit, NULL );
669
670 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
671 {
672 SetLastError( ERROR_FILENAME_EXCED_RANGE );
673 return 0;
674 }
675 return OpenMutexW( access, inherit, buffer );
676 }
677
678
679 /***********************************************************************
680 * OpenMutexW (KERNEL32.@)
681 */
682 HANDLE WINAPI OpenMutexW( DWORD access, BOOL inherit, LPCWSTR name )
683 {
684 HANDLE ret;
685 UNICODE_STRING nameW;
686 OBJECT_ATTRIBUTES attr;
687 NTSTATUS status;
688
689 if (!is_version_nt()) access = MUTEX_ALL_ACCESS;
690
691 attr.Length = sizeof(attr);
692 attr.RootDirectory = 0;
693 attr.ObjectName = NULL;
694 attr.Attributes = inherit ? OBJ_INHERIT : 0;
695 attr.SecurityDescriptor = NULL;
696 attr.SecurityQualityOfService = NULL;
697 if (name)
698 {
699 RtlInitUnicodeString( &nameW, name );
700 attr.ObjectName = &nameW;
701 attr.RootDirectory = get_BaseNamedObjects_handle();
702 }
703
704 status = NtOpenMutant( &ret, access, &attr );
705 if (status != STATUS_SUCCESS)
706 {
707 SetLastError( RtlNtStatusToDosError(status) );
708 return 0;
709 }
710 return ret;
711 }
712
713
714 /***********************************************************************
715 * ReleaseMutex (KERNEL32.@)
716 */
717 BOOL WINAPI ReleaseMutex( HANDLE handle )
718 {
719 NTSTATUS status;
720
721 status = NtReleaseMutant(handle, NULL);
722 if (status != STATUS_SUCCESS)
723 {
724 SetLastError( RtlNtStatusToDosError(status) );
725 return FALSE;
726 }
727 return TRUE;
728 }
729
730
731 /*
732 * Semaphores
733 */
734
735
736 /***********************************************************************
737 * CreateSemaphoreA (KERNEL32.@)
738 */
739 HANDLE WINAPI CreateSemaphoreA( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, LPCSTR name )
740 {
741 return CreateSemaphoreExA( sa, initial, max, name, 0, SEMAPHORE_ALL_ACCESS );
742 }
743
744
745 /***********************************************************************
746 * CreateSemaphoreW (KERNEL32.@)
747 */
748 HANDLE WINAPI CreateSemaphoreW( SECURITY_ATTRIBUTES *sa, LONG initial,
749 LONG max, LPCWSTR name )
750 {
751 return CreateSemaphoreExW( sa, initial, max, name, 0, SEMAPHORE_ALL_ACCESS );
752 }
753
754
755 /***********************************************************************
756 * CreateSemaphoreExA (KERNEL32.@)
757 */
758 HANDLE WINAPI CreateSemaphoreExA( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, LPCSTR name,
759 DWORD flags, DWORD access )
760 {
761 WCHAR buffer[MAX_PATH];
762
763 if (!name) return CreateSemaphoreExW( sa, initial, max, NULL, flags, access );
764
765 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
766 {
767 SetLastError( ERROR_FILENAME_EXCED_RANGE );
768 return 0;
769 }
770 return CreateSemaphoreExW( sa, initial, max, buffer, flags, access );
771 }
772
773
774 /***********************************************************************
775 * CreateSemaphoreExW (KERNEL32.@)
776 */
777 HANDLE WINAPI CreateSemaphoreExW( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, LPCWSTR name,
778 DWORD flags, DWORD access )
779 {
780 HANDLE ret;
781 UNICODE_STRING nameW;
782 OBJECT_ATTRIBUTES attr;
783 NTSTATUS status;
784
785 attr.Length = sizeof(attr);
786 attr.RootDirectory = 0;
787 attr.ObjectName = NULL;
788 attr.Attributes = OBJ_OPENIF | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
789 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
790 attr.SecurityQualityOfService = NULL;
791 if (name)
792 {
793 RtlInitUnicodeString( &nameW, name );
794 attr.ObjectName = &nameW;
795 attr.RootDirectory = get_BaseNamedObjects_handle();
796 }
797
798 status = NtCreateSemaphore( &ret, access, &attr, initial, max );
799 if (status == STATUS_OBJECT_NAME_EXISTS)
800 SetLastError( ERROR_ALREADY_EXISTS );
801 else
802 SetLastError( RtlNtStatusToDosError(status) );
803 return ret;
804 }
805
806
807 /***********************************************************************
808 * OpenSemaphoreA (KERNEL32.@)
809 */
810 HANDLE WINAPI OpenSemaphoreA( DWORD access, BOOL inherit, LPCSTR name )
811 {
812 WCHAR buffer[MAX_PATH];
813
814 if (!name) return OpenSemaphoreW( access, inherit, NULL );
815
816 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
817 {
818 SetLastError( ERROR_FILENAME_EXCED_RANGE );
819 return 0;
820 }
821 return OpenSemaphoreW( access, inherit, buffer );
822 }
823
824
825 /***********************************************************************
826 * OpenSemaphoreW (KERNEL32.@)
827 */
828 HANDLE WINAPI OpenSemaphoreW( DWORD access, BOOL inherit, LPCWSTR name )
829 {
830 HANDLE ret;
831 UNICODE_STRING nameW;
832 OBJECT_ATTRIBUTES attr;
833 NTSTATUS status;
834
835 if (!is_version_nt()) access = SEMAPHORE_ALL_ACCESS;
836
837 attr.Length = sizeof(attr);
838 attr.RootDirectory = 0;
839 attr.ObjectName = NULL;
840 attr.Attributes = inherit ? OBJ_INHERIT : 0;
841 attr.SecurityDescriptor = NULL;
842 attr.SecurityQualityOfService = NULL;
843 if (name)
844 {
845 RtlInitUnicodeString( &nameW, name );
846 attr.ObjectName = &nameW;
847 attr.RootDirectory = get_BaseNamedObjects_handle();
848 }
849
850 status = NtOpenSemaphore( &ret, access, &attr );
851 if (status != STATUS_SUCCESS)
852 {
853 SetLastError( RtlNtStatusToDosError(status) );
854 return 0;
855 }
856 return ret;
857 }
858
859
860 /***********************************************************************
861 * ReleaseSemaphore (KERNEL32.@)
862 */
863 BOOL WINAPI ReleaseSemaphore( HANDLE handle, LONG count, LONG *previous )
864 {
865 NTSTATUS status = NtReleaseSemaphore( handle, count, (PULONG)previous );
866 if (status) SetLastError( RtlNtStatusToDosError(status) );
867 return !status;
868 }
869
870
871 /*
872 * Jobs
873 */
874
875 /******************************************************************************
876 * CreateJobObjectW (KERNEL32.@)
877 */
878 HANDLE WINAPI CreateJobObjectW( LPSECURITY_ATTRIBUTES sa, LPCWSTR name )
879 {
880 HANDLE ret = 0;
881 UNICODE_STRING nameW;
882 OBJECT_ATTRIBUTES attr;
883 NTSTATUS status;
884
885 attr.Length = sizeof(attr);
886 attr.RootDirectory = 0;
887 attr.ObjectName = NULL;
888 attr.Attributes = OBJ_OPENIF | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
889 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
890 attr.SecurityQualityOfService = NULL;
891 if (name)
892 {
893 RtlInitUnicodeString( &nameW, name );
894 attr.ObjectName = &nameW;
895 attr.RootDirectory = get_BaseNamedObjects_handle();
896 }
897
898 status = NtCreateJobObject( &ret, JOB_OBJECT_ALL_ACCESS, &attr );
899 if (status == STATUS_OBJECT_NAME_EXISTS)
900 SetLastError( ERROR_ALREADY_EXISTS );
901 else
902 SetLastError( RtlNtStatusToDosError(status) );
903 return ret;
904 }
905
906 /******************************************************************************
907 * CreateJobObjectA (KERNEL32.@)
908 */
909 HANDLE WINAPI CreateJobObjectA( LPSECURITY_ATTRIBUTES attr, LPCSTR name )
910 {
911 WCHAR buffer[MAX_PATH];
912
913 if (!name) return CreateJobObjectW( attr, NULL );
914
915 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
916 {
917 SetLastError( ERROR_FILENAME_EXCED_RANGE );
918 return 0;
919 }
920 return CreateJobObjectW( attr, buffer );
921 }
922
923 /******************************************************************************
924 * OpenJobObjectW (KERNEL32.@)
925 */
926 HANDLE WINAPI OpenJobObjectW( DWORD access, BOOL inherit, LPCWSTR name )
927 {
928 HANDLE ret;
929 UNICODE_STRING nameW;
930 OBJECT_ATTRIBUTES attr;
931 NTSTATUS status;
932
933 attr.Length = sizeof(attr);
934 attr.RootDirectory = 0;
935 attr.ObjectName = NULL;
936 attr.Attributes = inherit ? OBJ_INHERIT : 0;
937 attr.SecurityDescriptor = NULL;
938 attr.SecurityQualityOfService = NULL;
939 if (name)
940 {
941 RtlInitUnicodeString( &nameW, name );
942 attr.ObjectName = &nameW;
943 attr.RootDirectory = get_BaseNamedObjects_handle();
944 }
945
946 status = NtOpenJobObject( &ret, access, &attr );
947 if (status != STATUS_SUCCESS)
948 {
949 SetLastError( RtlNtStatusToDosError(status) );
950 return 0;
951 }
952 return ret;
953 }
954
955 /******************************************************************************
956 * OpenJobObjectA (KERNEL32.@)
957 */
958 HANDLE WINAPI OpenJobObjectA( DWORD access, BOOL inherit, LPCSTR name )
959 {
960 WCHAR buffer[MAX_PATH];
961
962 if (!name) return OpenJobObjectW( access, inherit, NULL );
963
964 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
965 {
966 SetLastError( ERROR_FILENAME_EXCED_RANGE );
967 return 0;
968 }
969 return OpenJobObjectW( access, inherit, buffer );
970 }
971
972 /******************************************************************************
973 * TerminateJobObject (KERNEL32.@)
974 */
975 BOOL WINAPI TerminateJobObject( HANDLE job, UINT exit_code )
976 {
977 NTSTATUS status = NtTerminateJobObject( job, exit_code );
978 if (status) SetLastError( RtlNtStatusToDosError(status) );
979 return !status;
980 }
981
982 /******************************************************************************
983 * QueryInformationJobObject (KERNEL32.@)
984 */
985 BOOL WINAPI QueryInformationJobObject( HANDLE job, JOBOBJECTINFOCLASS class, LPVOID info,
986 DWORD len, DWORD *ret_len )
987 {
988 NTSTATUS status = NtQueryInformationJobObject( job, class, info, len, ret_len );
989 if (status) SetLastError( RtlNtStatusToDosError(status) );
990 return !status;
991 }
992
993 /******************************************************************************
994 * SetInformationJobObject (KERNEL32.@)
995 */
996 BOOL WINAPI SetInformationJobObject( HANDLE job, JOBOBJECTINFOCLASS class, LPVOID info, DWORD len )
997 {
998 NTSTATUS status = NtSetInformationJobObject( job, class, info, len );
999 if (status) SetLastError( RtlNtStatusToDosError(status) );
1000 return !status;
1001 }
1002
1003 /******************************************************************************
1004 * AssignProcessToJobObject (KERNEL32.@)
1005 */
1006 BOOL WINAPI AssignProcessToJobObject( HANDLE job, HANDLE process )
1007 {
1008 NTSTATUS status = NtAssignProcessToJobObject( job, process );
1009 if (status) SetLastError( RtlNtStatusToDosError(status) );
1010 return !status;
1011 }
1012
1013 /******************************************************************************
1014 * IsProcessInJob (KERNEL32.@)
1015 */
1016 BOOL WINAPI IsProcessInJob( HANDLE process, HANDLE job, PBOOL result )
1017 {
1018 NTSTATUS status = NtIsProcessInJob( job, process );
1019 switch(status)
1020 {
1021 case STATUS_PROCESS_IN_JOB:
1022 *result = TRUE;
1023 return TRUE;
1024 case STATUS_PROCESS_NOT_IN_JOB:
1025 *result = FALSE;
1026 return TRUE;
1027 default:
1028 SetLastError( RtlNtStatusToDosError(status) );
1029 return FALSE;
1030 }
1031 }
1032
1033
1034 /*
1035 * Timers
1036 */
1037
1038
1039 /***********************************************************************
1040 * CreateWaitableTimerA (KERNEL32.@)
1041 */
1042 HANDLE WINAPI CreateWaitableTimerA( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCSTR name )
1043 {
1044 return CreateWaitableTimerExA( sa, name, manual ? CREATE_WAITABLE_TIMER_MANUAL_RESET : 0,
1045 TIMER_ALL_ACCESS );
1046 }
1047
1048
1049 /***********************************************************************
1050 * CreateWaitableTimerW (KERNEL32.@)
1051 */
1052 HANDLE WINAPI CreateWaitableTimerW( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCWSTR name )
1053 {
1054 return CreateWaitableTimerExW( sa, name, manual ? CREATE_WAITABLE_TIMER_MANUAL_RESET : 0,
1055 TIMER_ALL_ACCESS );
1056 }
1057
1058
1059 /***********************************************************************
1060 * CreateWaitableTimerExA (KERNEL32.@)
1061 */
1062 HANDLE WINAPI CreateWaitableTimerExA( SECURITY_ATTRIBUTES *sa, LPCSTR name, DWORD flags, DWORD access )
1063 {
1064 WCHAR buffer[MAX_PATH];
1065
1066 if (!name) return CreateWaitableTimerExW( sa, NULL, flags, access );
1067
1068 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1069 {
1070 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1071 return 0;
1072 }
1073 return CreateWaitableTimerExW( sa, buffer, flags, access );
1074 }
1075
1076
1077 /***********************************************************************
1078 * CreateWaitableTimerExW (KERNEL32.@)
1079 */
1080 HANDLE WINAPI CreateWaitableTimerExW( SECURITY_ATTRIBUTES *sa, LPCWSTR name, DWORD flags, DWORD access )
1081 {
1082 HANDLE handle;
1083 NTSTATUS status;
1084 UNICODE_STRING nameW;
1085 OBJECT_ATTRIBUTES attr;
1086
1087 attr.Length = sizeof(attr);
1088 attr.RootDirectory = 0;
1089 attr.ObjectName = NULL;
1090 attr.Attributes = OBJ_OPENIF | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1091 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1092 attr.SecurityQualityOfService = NULL;
1093 if (name)
1094 {
1095 RtlInitUnicodeString( &nameW, name );
1096 attr.ObjectName = &nameW;
1097 attr.RootDirectory = get_BaseNamedObjects_handle();
1098 }
1099
1100 status = NtCreateTimer( &handle, access, &attr,
1101 (flags & CREATE_WAITABLE_TIMER_MANUAL_RESET) ? NotificationTimer : SynchronizationTimer );
1102 if (status == STATUS_OBJECT_NAME_EXISTS)
1103 SetLastError( ERROR_ALREADY_EXISTS );
1104 else
1105 SetLastError( RtlNtStatusToDosError(status) );
1106 return handle;
1107 }
1108
1109
1110 /***********************************************************************
1111 * OpenWaitableTimerA (KERNEL32.@)
1112 */
1113 HANDLE WINAPI OpenWaitableTimerA( DWORD access, BOOL inherit, LPCSTR name )
1114 {
1115 WCHAR buffer[MAX_PATH];
1116
1117 if (!name) return OpenWaitableTimerW( access, inherit, NULL );
1118
1119 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1120 {
1121 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1122 return 0;
1123 }
1124 return OpenWaitableTimerW( access, inherit, buffer );
1125 }
1126
1127
1128 /***********************************************************************
1129 * OpenWaitableTimerW (KERNEL32.@)
1130 */
1131 HANDLE WINAPI OpenWaitableTimerW( DWORD access, BOOL inherit, LPCWSTR name )
1132 {
1133 HANDLE handle;
1134 UNICODE_STRING nameW;
1135 OBJECT_ATTRIBUTES attr;
1136 NTSTATUS status;
1137
1138 if (!is_version_nt()) access = TIMER_ALL_ACCESS;
1139
1140 attr.Length = sizeof(attr);
1141 attr.RootDirectory = 0;
1142 attr.ObjectName = NULL;
1143 attr.Attributes = inherit ? OBJ_INHERIT : 0;
1144 attr.SecurityDescriptor = NULL;
1145 attr.SecurityQualityOfService = NULL;
1146 if (name)
1147 {
1148 RtlInitUnicodeString( &nameW, name );
1149 attr.ObjectName = &nameW;
1150 attr.RootDirectory = get_BaseNamedObjects_handle();
1151 }
1152
1153 status = NtOpenTimer(&handle, access, &attr);
1154 if (status != STATUS_SUCCESS)
1155 {
1156 SetLastError( RtlNtStatusToDosError(status) );
1157 return 0;
1158 }
1159 return handle;
1160 }
1161
1162
1163 /***********************************************************************
1164 * SetWaitableTimer (KERNEL32.@)
1165 */
1166 BOOL WINAPI SetWaitableTimer( HANDLE handle, const LARGE_INTEGER *when, LONG period,
1167 PTIMERAPCROUTINE callback, LPVOID arg, BOOL resume )
1168 {
1169 NTSTATUS status = NtSetTimer(handle, when, (PTIMER_APC_ROUTINE)callback,
1170 arg, resume, period, NULL);
1171
1172 if (status != STATUS_SUCCESS)
1173 {
1174 SetLastError( RtlNtStatusToDosError(status) );
1175 if (status != STATUS_TIMER_RESUME_IGNORED) return FALSE;
1176 }
1177 return TRUE;
1178 }
1179
1180
1181 /***********************************************************************
1182 * CancelWaitableTimer (KERNEL32.@)
1183 */
1184 BOOL WINAPI CancelWaitableTimer( HANDLE handle )
1185 {
1186 NTSTATUS status;
1187
1188 status = NtCancelTimer(handle, NULL);
1189 if (status != STATUS_SUCCESS)
1190 {
1191 SetLastError( RtlNtStatusToDosError(status) );
1192 return FALSE;
1193 }
1194 return TRUE;
1195 }
1196
1197
1198 /***********************************************************************
1199 * CreateTimerQueue (KERNEL32.@)
1200 */
1201 HANDLE WINAPI CreateTimerQueue(void)
1202 {
1203 HANDLE q;
1204 NTSTATUS status = RtlCreateTimerQueue(&q);
1205
1206 if (status != STATUS_SUCCESS)
1207 {
1208 SetLastError( RtlNtStatusToDosError(status) );
1209 return NULL;
1210 }
1211
1212 return q;
1213 }
1214
1215
1216 /***********************************************************************
1217 * DeleteTimerQueueEx (KERNEL32.@)
1218 */
1219 BOOL WINAPI DeleteTimerQueueEx(HANDLE TimerQueue, HANDLE CompletionEvent)
1220 {
1221 NTSTATUS status = RtlDeleteTimerQueueEx(TimerQueue, CompletionEvent);
1222
1223 if (status != STATUS_SUCCESS)
1224 {
1225 SetLastError( RtlNtStatusToDosError(status) );
1226 return FALSE;
1227 }
1228
1229 return TRUE;
1230 }
1231
1232 /***********************************************************************
1233 * DeleteTimerQueue (KERNEL32.@)
1234 */
1235 BOOL WINAPI DeleteTimerQueue(HANDLE TimerQueue)
1236 {
1237 return DeleteTimerQueueEx(TimerQueue, NULL);
1238 }
1239
1240 /***********************************************************************
1241 * CreateTimerQueueTimer (KERNEL32.@)
1242 *
1243 * Creates a timer-queue timer. This timer expires at the specified due
1244 * time (in ms), then after every specified period (in ms). When the timer
1245 * expires, the callback function is called.
1246 *
1247 * RETURNS
1248 * nonzero on success or zero on failure
1249 */
1250 BOOL WINAPI CreateTimerQueueTimer( PHANDLE phNewTimer, HANDLE TimerQueue,
1251 WAITORTIMERCALLBACK Callback, PVOID Parameter,
1252 DWORD DueTime, DWORD Period, ULONG Flags )
1253 {
1254 NTSTATUS status = RtlCreateTimer(phNewTimer, TimerQueue, Callback,
1255 Parameter, DueTime, Period, Flags);
1256
1257 if (status != STATUS_SUCCESS)
1258 {
1259 SetLastError( RtlNtStatusToDosError(status) );
1260 return FALSE;
1261 }
1262
1263 return TRUE;
1264 }
1265
1266 /***********************************************************************
1267 * ChangeTimerQueueTimer (KERNEL32.@)
1268 *
1269 * Changes the times at which the timer expires.
1270 *
1271 * RETURNS
1272 * nonzero on success or zero on failure
1273 */
1274 BOOL WINAPI ChangeTimerQueueTimer( HANDLE TimerQueue, HANDLE Timer,
1275 ULONG DueTime, ULONG Period )
1276 {
1277 NTSTATUS status = RtlUpdateTimer(TimerQueue, Timer, DueTime, Period);
1278
1279 if (status != STATUS_SUCCESS)
1280 {
1281 SetLastError( RtlNtStatusToDosError(status) );
1282 return FALSE;
1283 }
1284
1285 return TRUE;
1286 }
1287
1288 /***********************************************************************
1289 * DeleteTimerQueueTimer (KERNEL32.@)
1290 *
1291 * Cancels a timer-queue timer.
1292 *
1293 * RETURNS
1294 * nonzero on success or zero on failure
1295 */
1296 BOOL WINAPI DeleteTimerQueueTimer( HANDLE TimerQueue, HANDLE Timer,
1297 HANDLE CompletionEvent )
1298 {
1299 NTSTATUS status = RtlDeleteTimer(TimerQueue, Timer, CompletionEvent);
1300 if (status != STATUS_SUCCESS)
1301 {
1302 SetLastError( RtlNtStatusToDosError(status) );
1303 return FALSE;
1304 }
1305 return TRUE;
1306 }
1307
1308
1309 /*
1310 * Pipes
1311 */
1312
1313
1314 /***********************************************************************
1315 * CreateNamedPipeA (KERNEL32.@)
1316 */
1317 HANDLE WINAPI CreateNamedPipeA( LPCSTR name, DWORD dwOpenMode,
1318 DWORD dwPipeMode, DWORD nMaxInstances,
1319 DWORD nOutBufferSize, DWORD nInBufferSize,
1320 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES attr )
1321 {
1322 WCHAR buffer[MAX_PATH];
1323
1324 if (!name) return CreateNamedPipeW( NULL, dwOpenMode, dwPipeMode, nMaxInstances,
1325 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1326
1327 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1328 {
1329 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1330 return INVALID_HANDLE_VALUE;
1331 }
1332 return CreateNamedPipeW( buffer, dwOpenMode, dwPipeMode, nMaxInstances,
1333 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1334 }
1335
1336
1337 /***********************************************************************
1338 * CreateNamedPipeW (KERNEL32.@)
1339 */
1340 HANDLE WINAPI CreateNamedPipeW( LPCWSTR name, DWORD dwOpenMode,
1341 DWORD dwPipeMode, DWORD nMaxInstances,
1342 DWORD nOutBufferSize, DWORD nInBufferSize,
1343 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES sa )
1344 {
1345 HANDLE handle;
1346 UNICODE_STRING nt_name;
1347 OBJECT_ATTRIBUTES attr;
1348 DWORD access, options;
1349 BOOLEAN pipe_type, read_mode, non_block;
1350 NTSTATUS status;
1351 IO_STATUS_BLOCK iosb;
1352 LARGE_INTEGER timeout;
1353
1354 TRACE("(%s, %#08x, %#08x, %d, %d, %d, %d, %p)\n",
1355 debugstr_w(name), dwOpenMode, dwPipeMode, nMaxInstances,
1356 nOutBufferSize, nInBufferSize, nDefaultTimeOut, sa );
1357
1358 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1359 {
1360 SetLastError( ERROR_PATH_NOT_FOUND );
1361 return INVALID_HANDLE_VALUE;
1362 }
1363 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) )
1364 {
1365 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1366 RtlFreeUnicodeString( &nt_name );
1367 return INVALID_HANDLE_VALUE;
1368 }
1369
1370 attr.Length = sizeof(attr);
1371 attr.RootDirectory = 0;
1372 attr.ObjectName = &nt_name;
1373 attr.Attributes = OBJ_CASE_INSENSITIVE |
1374 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1375 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1376 attr.SecurityQualityOfService = NULL;
1377
1378 switch(dwOpenMode & 3)
1379 {
1380 case PIPE_ACCESS_INBOUND:
1381 options = FILE_PIPE_INBOUND;
1382 access = GENERIC_READ;
1383 break;
1384 case PIPE_ACCESS_OUTBOUND:
1385 options = FILE_PIPE_OUTBOUND;
1386 access = GENERIC_WRITE;
1387 break;
1388 case PIPE_ACCESS_DUPLEX:
1389 options = FILE_PIPE_FULL_DUPLEX;
1390 access = GENERIC_READ | GENERIC_WRITE;
1391 break;
1392 default:
1393 SetLastError( ERROR_INVALID_PARAMETER );
1394 return INVALID_HANDLE_VALUE;
1395 }
1396 access |= SYNCHRONIZE;
1397 if (dwOpenMode & FILE_FLAG_WRITE_THROUGH) options |= FILE_WRITE_THROUGH;
1398 if (!(dwOpenMode & FILE_FLAG_OVERLAPPED)) options |= FILE_SYNCHRONOUS_IO_ALERT;
1399 pipe_type = (dwPipeMode & PIPE_TYPE_MESSAGE) ? TRUE : FALSE;
1400 read_mode = (dwPipeMode & PIPE_READMODE_MESSAGE) ? TRUE : FALSE;
1401 non_block = (dwPipeMode & PIPE_NOWAIT) ? TRUE : FALSE;
1402 if (nMaxInstances >= PIPE_UNLIMITED_INSTANCES) nMaxInstances = ~0U;
1403
1404 timeout.QuadPart = (ULONGLONG)nDefaultTimeOut * -10000;
1405
1406 SetLastError(0);
1407
1408 status = NtCreateNamedPipeFile(&handle, access, &attr, &iosb, 0,
1409 FILE_OVERWRITE_IF, options, pipe_type,
1410 read_mode, non_block, nMaxInstances,
1411 nInBufferSize, nOutBufferSize, &timeout);
1412
1413 RtlFreeUnicodeString( &nt_name );
1414 if (status)
1415 {
1416 handle = INVALID_HANDLE_VALUE;
1417 SetLastError( RtlNtStatusToDosError(status) );
1418 }
1419 return handle;
1420 }
1421
1422
1423 /***********************************************************************
1424 * PeekNamedPipe (KERNEL32.@)
1425 */
1426 BOOL WINAPI PeekNamedPipe( HANDLE hPipe, LPVOID lpvBuffer, DWORD cbBuffer,
1427 LPDWORD lpcbRead, LPDWORD lpcbAvail, LPDWORD lpcbMessage )
1428 {
1429 FILE_PIPE_PEEK_BUFFER local_buffer;
1430 FILE_PIPE_PEEK_BUFFER *buffer = &local_buffer;
1431 IO_STATUS_BLOCK io;
1432 NTSTATUS status;
1433
1434 if (cbBuffer && !(buffer = HeapAlloc( GetProcessHeap(), 0,
1435 FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data[cbBuffer] ))))
1436 {
1437 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1438 return FALSE;
1439 }
1440
1441 status = NtFsControlFile( hPipe, 0, NULL, NULL, &io, FSCTL_PIPE_PEEK, NULL, 0,
1442 buffer, FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data[cbBuffer] ) );
1443 if (!status)
1444 {
1445 ULONG read_size = io.Information - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1446 if (lpcbAvail) *lpcbAvail = buffer->ReadDataAvailable;
1447 if (lpcbRead) *lpcbRead = read_size;
1448 if (lpcbMessage) *lpcbMessage = 0; /* FIXME */
1449 if (lpvBuffer) memcpy( lpvBuffer, buffer->Data, read_size );
1450 }
1451 else SetLastError( RtlNtStatusToDosError(status) );
1452
1453 if (buffer != &local_buffer) HeapFree( GetProcessHeap(), 0, buffer );
1454 return !status;
1455 }
1456
1457 /***********************************************************************
1458 * WaitNamedPipeA (KERNEL32.@)
1459 */
1460 BOOL WINAPI WaitNamedPipeA (LPCSTR name, DWORD nTimeOut)
1461 {
1462 WCHAR buffer[MAX_PATH];
1463
1464 if (!name) return WaitNamedPipeW( NULL, nTimeOut );
1465
1466 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1467 {
1468 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1469 return 0;
1470 }
1471 return WaitNamedPipeW( buffer, nTimeOut );
1472 }
1473
1474
1475 /***********************************************************************
1476 * WaitNamedPipeW (KERNEL32.@)
1477 *
1478 * Waits for a named pipe instance to become available
1479 *
1480 * PARAMS
1481 * name [I] Pointer to a named pipe name to wait for
1482 * nTimeOut [I] How long to wait in ms
1483 *
1484 * RETURNS
1485 * TRUE: Success, named pipe can be opened with CreateFile
1486 * FALSE: Failure, GetLastError can be called for further details
1487 */
1488 BOOL WINAPI WaitNamedPipeW (LPCWSTR name, DWORD nTimeOut)
1489 {
1490 static const WCHAR leadin[] = {'\\','?','?','\\','P','I','P','E','\\'};
1491 NTSTATUS status;
1492 UNICODE_STRING nt_name, pipe_dev_name;
1493 FILE_PIPE_WAIT_FOR_BUFFER *pipe_wait;
1494 IO_STATUS_BLOCK iosb;
1495 OBJECT_ATTRIBUTES attr;
1496 ULONG sz_pipe_wait;
1497 HANDLE pipe_dev;
1498
1499 TRACE("%s 0x%08x\n",debugstr_w(name),nTimeOut);
1500
1501 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1502 return FALSE;
1503
1504 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) ||
1505 nt_name.Length < sizeof(leadin) ||
1506 strncmpiW( nt_name.Buffer, leadin, sizeof(leadin)/sizeof(WCHAR)) != 0)
1507 {
1508 RtlFreeUnicodeString( &nt_name );
1509 SetLastError( ERROR_PATH_NOT_FOUND );
1510 return FALSE;
1511 }
1512
1513 sz_pipe_wait = sizeof(*pipe_wait) + nt_name.Length - sizeof(leadin) - sizeof(WCHAR);
1514 if (!(pipe_wait = HeapAlloc( GetProcessHeap(), 0, sz_pipe_wait)))
1515 {
1516 RtlFreeUnicodeString( &nt_name );
1517 SetLastError( ERROR_OUTOFMEMORY );
1518 return FALSE;
1519 }
1520
1521 pipe_dev_name.Buffer = nt_name.Buffer;
1522 pipe_dev_name.Length = sizeof(leadin);
1523 pipe_dev_name.MaximumLength = sizeof(leadin);
1524 InitializeObjectAttributes(&attr,&pipe_dev_name, OBJ_CASE_INSENSITIVE, NULL, NULL);
1525 status = NtOpenFile( &pipe_dev, FILE_READ_ATTRIBUTES, &attr,
1526 &iosb, FILE_SHARE_READ | FILE_SHARE_WRITE,
1527 FILE_SYNCHRONOUS_IO_NONALERT);
1528 if (status != ERROR_SUCCESS)
1529 {
1530 SetLastError( ERROR_PATH_NOT_FOUND );
1531 return FALSE;
1532 }
1533
1534 pipe_wait->TimeoutSpecified = !(nTimeOut == NMPWAIT_USE_DEFAULT_WAIT);
1535 if (nTimeOut == NMPWAIT_WAIT_FOREVER)
1536 pipe_wait->Timeout.QuadPart = ((ULONGLONG)0x7fffffff << 32) | 0xffffffff;
1537 else
1538 pipe_wait->Timeout.QuadPart = (ULONGLONG)nTimeOut * -10000;
1539 pipe_wait->NameLength = nt_name.Length - sizeof(leadin);
1540 memcpy(pipe_wait->Name, nt_name.Buffer + sizeof(leadin)/sizeof(WCHAR),
1541 pipe_wait->NameLength);
1542 RtlFreeUnicodeString( &nt_name );
1543
1544 status = NtFsControlFile( pipe_dev, NULL, NULL, NULL, &iosb, FSCTL_PIPE_WAIT,
1545 pipe_wait, sz_pipe_wait, NULL, 0 );
1546
1547 HeapFree( GetProcessHeap(), 0, pipe_wait );
1548 NtClose( pipe_dev );
1549
1550 if(status != STATUS_SUCCESS)
1551 {
1552 SetLastError(RtlNtStatusToDosError(status));
1553 return FALSE;
1554 }
1555 else
1556 return TRUE;
1557 }
1558
1559
1560 /***********************************************************************
1561 * ConnectNamedPipe (KERNEL32.@)
1562 *
1563 * Connects to a named pipe
1564 *
1565 * Parameters
1566 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1567 * overlapped: Optional OVERLAPPED struct
1568 *
1569 * Return values
1570 * TRUE: Success
1571 * FALSE: Failure, GetLastError can be called for further details
1572 */
1573 BOOL WINAPI ConnectNamedPipe(HANDLE hPipe, LPOVERLAPPED overlapped)
1574 {
1575 NTSTATUS status;
1576 IO_STATUS_BLOCK status_block;
1577 LPVOID cvalue = NULL;
1578
1579 TRACE("(%p,%p)\n", hPipe, overlapped);
1580
1581 if(overlapped)
1582 {
1583 overlapped->Internal = STATUS_PENDING;
1584 overlapped->InternalHigh = 0;
1585 if (((ULONG_PTR)overlapped->hEvent & 1) == 0) cvalue = overlapped;
1586 }
1587
1588 status = NtFsControlFile(hPipe, overlapped ? overlapped->hEvent : NULL, NULL, cvalue,
1589 overlapped ? (IO_STATUS_BLOCK *)overlapped : &status_block,
1590 FSCTL_PIPE_LISTEN, NULL, 0, NULL, 0);
1591
1592 if (status == STATUS_SUCCESS) return TRUE;
1593 SetLastError( RtlNtStatusToDosError(status) );
1594 return FALSE;
1595 }
1596
1597 /***********************************************************************
1598 * DisconnectNamedPipe (KERNEL32.@)
1599 *
1600 * Disconnects from a named pipe
1601 *
1602 * Parameters
1603 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1604 *
1605 * Return values
1606 * TRUE: Success
1607 * FALSE: Failure, GetLastError can be called for further details
1608 */
1609 BOOL WINAPI DisconnectNamedPipe(HANDLE hPipe)
1610 {
1611 NTSTATUS status;
1612 IO_STATUS_BLOCK io_block;
1613
1614 TRACE("(%p)\n",hPipe);
1615
1616 status = NtFsControlFile(hPipe, 0, NULL, NULL, &io_block, FSCTL_PIPE_DISCONNECT,
1617 NULL, 0, NULL, 0);
1618 if (status == STATUS_SUCCESS) return TRUE;
1619 SetLastError( RtlNtStatusToDosError(status) );
1620 return FALSE;
1621 }
1622
1623 /***********************************************************************
1624 * TransactNamedPipe (KERNEL32.@)
1625 *
1626 * BUGS
1627 * should be done as a single operation in the wineserver or kernel
1628 */
1629 BOOL WINAPI TransactNamedPipe(
1630 HANDLE handle, LPVOID write_buf, DWORD write_size, LPVOID read_buf,
1631 DWORD read_size, LPDWORD bytes_read, LPOVERLAPPED overlapped)
1632 {
1633 BOOL r;
1634 DWORD count;
1635
1636 TRACE("%p %p %d %p %d %p %p\n",
1637 handle, write_buf, write_size, read_buf,
1638 read_size, bytes_read, overlapped);
1639
1640 if (overlapped)
1641 {
1642 FIXME("Doesn't support overlapped operation as yet\n");
1643 return FALSE;
1644 }
1645
1646 r = WriteFile(handle, write_buf, write_size, &count, NULL);
1647 if (r)
1648 r = ReadFile(handle, read_buf, read_size, bytes_read, NULL);
1649
1650 return r;
1651 }
1652
1653 /***********************************************************************
1654 * GetNamedPipeInfo (KERNEL32.@)
1655 */
1656 BOOL WINAPI GetNamedPipeInfo(
1657 HANDLE hNamedPipe, LPDWORD lpFlags, LPDWORD lpOutputBufferSize,
1658 LPDWORD lpInputBufferSize, LPDWORD lpMaxInstances)
1659 {
1660 FILE_PIPE_LOCAL_INFORMATION fpli;
1661 IO_STATUS_BLOCK iosb;
1662 NTSTATUS status;
1663
1664 status = NtQueryInformationFile(hNamedPipe, &iosb, &fpli, sizeof(fpli),
1665 FilePipeLocalInformation);
1666 if (status)
1667 {
1668 SetLastError( RtlNtStatusToDosError(status) );
1669 return FALSE;
1670 }
1671
1672 if (lpFlags)
1673 {
1674 *lpFlags = (fpli.NamedPipeEnd & FILE_PIPE_SERVER_END) ?
1675 PIPE_SERVER_END : PIPE_CLIENT_END;
1676 *lpFlags |= (fpli.NamedPipeType & FILE_PIPE_TYPE_MESSAGE) ?
1677 PIPE_TYPE_MESSAGE : PIPE_TYPE_BYTE;
1678 }
1679
1680 if (lpOutputBufferSize) *lpOutputBufferSize = fpli.OutboundQuota;
1681 if (lpInputBufferSize) *lpInputBufferSize = fpli.InboundQuota;
1682 if (lpMaxInstances) *lpMaxInstances = fpli.MaximumInstances;
1683
1684 return TRUE;
1685 }
1686
1687 /***********************************************************************
1688 * GetNamedPipeHandleStateA (KERNEL32.@)
1689 */
1690 BOOL WINAPI GetNamedPipeHandleStateA(
1691 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1692 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1693 LPSTR lpUsername, DWORD nUsernameMaxSize)
1694 {
1695 FIXME("%p %p %p %p %p %p %d\n",
1696 hNamedPipe, lpState, lpCurInstances,
1697 lpMaxCollectionCount, lpCollectDataTimeout,
1698 lpUsername, nUsernameMaxSize);
1699
1700 return FALSE;
1701 }
1702
1703 /***********************************************************************
1704 * GetNamedPipeHandleStateW (KERNEL32.@)
1705 */
1706 BOOL WINAPI GetNamedPipeHandleStateW(
1707 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1708 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1709 LPWSTR lpUsername, DWORD nUsernameMaxSize)
1710 {
1711 FIXME("%p %p %p %p %p %p %d\n",
1712 hNamedPipe, lpState, lpCurInstances,
1713 lpMaxCollectionCount, lpCollectDataTimeout,
1714 lpUsername, nUsernameMaxSize);
1715
1716 return FALSE;
1717 }
1718
1719 /***********************************************************************
1720 * SetNamedPipeHandleState (KERNEL32.@)
1721 */
1722 BOOL WINAPI SetNamedPipeHandleState(
1723 HANDLE hNamedPipe, LPDWORD lpMode, LPDWORD lpMaxCollectionCount,
1724 LPDWORD lpCollectDataTimeout)
1725 {
1726 /* should be a fixme, but this function is called a lot by the RPC
1727 * runtime, and it slows down InstallShield a fair bit. */
1728 WARN("stub: %p %p/%d %p %p\n",
1729 hNamedPipe, lpMode, lpMode ? *lpMode : 0, lpMaxCollectionCount, lpCollectDataTimeout);
1730 return FALSE;
1731 }
1732
1733 /***********************************************************************
1734 * CallNamedPipeA (KERNEL32.@)
1735 */
1736 BOOL WINAPI CallNamedPipeA(
1737 LPCSTR lpNamedPipeName, LPVOID lpInput, DWORD dwInputSize,
1738 LPVOID lpOutput, DWORD dwOutputSize,
1739 LPDWORD lpBytesRead, DWORD nTimeout)
1740 {
1741 DWORD len;
1742 LPWSTR str = NULL;
1743 BOOL ret;
1744
1745 TRACE("%s %p %d %p %d %p %d\n",
1746 debugstr_a(lpNamedPipeName), lpInput, dwInputSize,
1747 lpOutput, dwOutputSize, lpBytesRead, nTimeout);
1748
1749 if( lpNamedPipeName )
1750 {
1751 len = MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, NULL, 0 );
1752 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1753 MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, str, len );
1754 }
1755 ret = CallNamedPipeW( str, lpInput, dwInputSize, lpOutput,
1756 dwOutputSize, lpBytesRead, nTimeout );
1757 if( lpNamedPipeName )
1758 HeapFree( GetProcessHeap(), 0, str );
1759
1760 return ret;
1761 }
1762
1763 /***********************************************************************
1764 * CallNamedPipeW (KERNEL32.@)
1765 */
1766 BOOL WINAPI CallNamedPipeW(
1767 LPCWSTR lpNamedPipeName, LPVOID lpInput, DWORD lpInputSize,
1768 LPVOID lpOutput, DWORD lpOutputSize,
1769 LPDWORD lpBytesRead, DWORD nTimeout)
1770 {
1771 HANDLE pipe;
1772 BOOL ret;
1773 DWORD mode;
1774
1775 TRACE("%s %p %d %p %d %p %d\n",
1776 debugstr_w(lpNamedPipeName), lpInput, lpInputSize,
1777 lpOutput, lpOutputSize, lpBytesRead, nTimeout);
1778
1779 pipe = CreateFileW(lpNamedPipeName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
1780 if (pipe == INVALID_HANDLE_VALUE)
1781 {
1782 ret = WaitNamedPipeW(lpNamedPipeName, nTimeout);
1783 if (!ret)
1784 return FALSE;
1785 pipe = CreateFileW(lpNamedPipeName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
1786 if (pipe == INVALID_HANDLE_VALUE)
1787 return FALSE;
1788 }
1789
1790 mode = PIPE_READMODE_MESSAGE;
1791 ret = SetNamedPipeHandleState(pipe, &mode, NULL, NULL);
1792
1793 /* Currently SetNamedPipeHandleState() is a stub returning FALSE */
1794 if (ret) FIXME("Now that SetNamedPipeHandleState() is more than a stub, please update CallNamedPipeW\n");
1795 /*
1796 if (!ret)
1797 {
1798 CloseHandle(pipe);
1799 return FALSE;
1800 }*/
1801
1802 ret = TransactNamedPipe(pipe, lpInput, lpInputSize, lpOutput, lpOutputSize, lpBytesRead, NULL);
1803 CloseHandle(pipe);
1804 if (!ret)
1805 return FALSE;
1806
1807 return TRUE;
1808 }
1809
1810 /******************************************************************
1811 * CreatePipe (KERNEL32.@)
1812 *
1813 */
1814 BOOL WINAPI CreatePipe( PHANDLE hReadPipe, PHANDLE hWritePipe,
1815 LPSECURITY_ATTRIBUTES sa, DWORD size )
1816 {
1817 static unsigned index /* = 0 */;
1818 WCHAR name[64];
1819 HANDLE hr, hw;
1820 unsigned in_index = index;
1821 UNICODE_STRING nt_name;
1822 OBJECT_ATTRIBUTES attr;
1823 NTSTATUS status;
1824 IO_STATUS_BLOCK iosb;
1825 LARGE_INTEGER timeout;
1826
1827 *hReadPipe = *hWritePipe = INVALID_HANDLE_VALUE;
1828
1829 attr.Length = sizeof(attr);
1830 attr.RootDirectory = 0;
1831 attr.ObjectName = &nt_name;
1832 attr.Attributes = OBJ_CASE_INSENSITIVE |
1833 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1834 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1835 attr.SecurityQualityOfService = NULL;
1836
1837 timeout.QuadPart = (ULONGLONG)NMPWAIT_USE_DEFAULT_WAIT * -10000;
1838 /* generate a unique pipe name (system wide) */
1839 do
1840 {
1841 static const WCHAR nameFmt[] = { '\\','?','?','\\','p','i','p','e',
1842 '\\','W','i','n','3','2','.','P','i','p','e','s','.','%','','8','l',
1843 'u','.','%','','8','u','\0' };
1844
1845 snprintfW(name, sizeof(name) / sizeof(name[0]), nameFmt,
1846 GetCurrentProcessId(), ++index);
1847 RtlInitUnicodeString(&nt_name, name);
1848 status = NtCreateNamedPipeFile(&hr, GENERIC_READ | SYNCHRONIZE, &attr, &iosb,
1849 0, FILE_OVERWRITE_IF,
1850 FILE_SYNCHRONOUS_IO_ALERT | FILE_PIPE_INBOUND,
1851 FALSE, FALSE, FALSE,
1852 1, size, size, &timeout);
1853 if (status)
1854 {
1855 SetLastError( RtlNtStatusToDosError(status) );
1856 hr = INVALID_HANDLE_VALUE;
1857 }
1858 } while (hr == INVALID_HANDLE_VALUE && index != in_index);
1859 /* from completion sakeness, I think system resources might be exhausted before this happens !! */
1860 if (hr == INVALID_HANDLE_VALUE) return FALSE;
1861
1862 status = NtOpenFile(&hw, GENERIC_WRITE | SYNCHRONIZE, &attr, &iosb, 0,
1863 FILE_SYNCHRONOUS_IO_ALERT | FILE_NON_DIRECTORY_FILE);
1864
1865 if (status)
1866 {
1867 SetLastError( RtlNtStatusToDosError(status) );
1868 NtClose(hr);
1869 return FALSE;
1870 }
1871
1872 *hReadPipe = hr;
1873 *hWritePipe = hw;
1874 return TRUE;
1875 }
1876
1877
1878 /******************************************************************************
1879 * CreateMailslotA [KERNEL32.@]
1880 *
1881 * See CreateMailslotW.
1882 */
1883 HANDLE WINAPI CreateMailslotA( LPCSTR lpName, DWORD nMaxMessageSize,
1884 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1885 {
1886 DWORD len;
1887 HANDLE handle;
1888 LPWSTR name = NULL;
1889
1890 TRACE("%s %d %d %p\n", debugstr_a(lpName),
1891 nMaxMessageSize, lReadTimeout, sa);
1892
1893 if( lpName )
1894 {
1895 len = MultiByteToWideChar( CP_ACP, 0, lpName, -1, NULL, 0 );
1896 name = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1897 MultiByteToWideChar( CP_ACP, 0, lpName, -1, name, len );
1898 }
1899
1900 handle = CreateMailslotW( name, nMaxMessageSize, lReadTimeout, sa );
1901
1902 HeapFree( GetProcessHeap(), 0, name );
1903
1904 return handle;
1905 }
1906
1907
1908 /******************************************************************************
1909 * CreateMailslotW [KERNEL32.@]
1910 *
1911 * Create a mailslot with specified name.
1912 *
1913 * PARAMS
1914 * lpName [I] Pointer to string for mailslot name
1915 * nMaxMessageSize [I] Maximum message size
1916 * lReadTimeout [I] Milliseconds before read time-out
1917 * sa [I] Pointer to security structure
1918 *
1919 * RETURNS
1920 * Success: Handle to mailslot
1921 * Failure: INVALID_HANDLE_VALUE
1922 */
1923 HANDLE WINAPI CreateMailslotW( LPCWSTR lpName, DWORD nMaxMessageSize,
1924 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1925 {
1926 HANDLE handle = INVALID_HANDLE_VALUE;
1927 OBJECT_ATTRIBUTES attr;
1928 UNICODE_STRING nameW;
1929 LARGE_INTEGER timeout;
1930 IO_STATUS_BLOCK iosb;
1931 NTSTATUS status;
1932
1933 TRACE("%s %d %d %p\n", debugstr_w(lpName),
1934 nMaxMessageSize, lReadTimeout, sa);
1935
1936 if (!RtlDosPathNameToNtPathName_U( lpName, &nameW, NULL, NULL ))
1937 {
1938 SetLastError( ERROR_PATH_NOT_FOUND );
1939 return INVALID_HANDLE_VALUE;
1940 }
1941
1942 if (nameW.Length >= MAX_PATH * sizeof(WCHAR) )
1943 {
1944 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1945 RtlFreeUnicodeString( &nameW );
1946 return INVALID_HANDLE_VALUE;
1947 }
1948
1949 attr.Length = sizeof(attr);
1950 attr.RootDirectory = 0;
1951 attr.Attributes = OBJ_CASE_INSENSITIVE;
1952 attr.ObjectName = &nameW;
1953 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1954 attr.SecurityQualityOfService = NULL;
1955
1956 if (lReadTimeout != MAILSLOT_WAIT_FOREVER)
1957 timeout.QuadPart = (ULONGLONG) lReadTimeout * -10000;
1958 else
1959 timeout.QuadPart = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1960
1961 status = NtCreateMailslotFile( &handle, GENERIC_READ | SYNCHRONIZE, &attr,
1962 &iosb, 0, 0, nMaxMessageSize, &timeout );
1963 if (status)
1964 {
1965 SetLastError( RtlNtStatusToDosError(status) );
1966 handle = INVALID_HANDLE_VALUE;
1967 }
1968
1969 RtlFreeUnicodeString( &nameW );
1970 return handle;
1971 }
1972
1973
1974 /******************************************************************************
1975 * GetMailslotInfo [KERNEL32.@]
1976 *
1977 * Retrieve information about a mailslot.
1978 *
1979 * PARAMS
1980 * hMailslot [I] Mailslot handle
1981 * lpMaxMessageSize [O] Address of maximum message size
1982 * lpNextSize [O] Address of size of next message
1983 * lpMessageCount [O] Address of number of messages
1984 * lpReadTimeout [O] Address of read time-out
1985 *
1986 * RETURNS
1987 * Success: TRUE
1988 * Failure: FALSE
1989 */
1990 BOOL WINAPI GetMailslotInfo( HANDLE hMailslot, LPDWORD lpMaxMessageSize,
1991 LPDWORD lpNextSize, LPDWORD lpMessageCount,
1992 LPDWORD lpReadTimeout )
1993 {
1994 FILE_MAILSLOT_QUERY_INFORMATION info;
1995 IO_STATUS_BLOCK iosb;
1996 NTSTATUS status;
1997
1998 TRACE("%p %p %p %p %p\n",hMailslot, lpMaxMessageSize,
1999 lpNextSize, lpMessageCount, lpReadTimeout);
2000
2001 status = NtQueryInformationFile( hMailslot, &iosb, &info, sizeof info,
2002 FileMailslotQueryInformation );
2003
2004 if( status != STATUS_SUCCESS )
2005 {
2006 SetLastError( RtlNtStatusToDosError(status) );
2007 return FALSE;
2008 }
2009
2010 if( lpMaxMessageSize )
2011 *lpMaxMessageSize = info.MaximumMessageSize;
2012 if( lpNextSize )
2013 *lpNextSize = info.NextMessageSize;
2014 if( lpMessageCount )
2015 *lpMessageCount = info.MessagesAvailable;
2016 if( lpReadTimeout )
2017 {
2018 if (info.ReadTimeout.QuadPart == (((LONGLONG)0x7fffffff << 32) | 0xffffffff))
2019 *lpReadTimeout = MAILSLOT_WAIT_FOREVER;
2020 else
2021 *lpReadTimeout = info.ReadTimeout.QuadPart / -10000;
2022 }
2023 return TRUE;
2024 }
2025
2026
2027 /******************************************************************************
2028 * SetMailslotInfo [KERNEL32.@]
2029 *
2030 * Set the read timeout of a mailslot.
2031 *
2032 * PARAMS
2033 * hMailslot [I] Mailslot handle
2034 * dwReadTimeout [I] Timeout in milliseconds.
2035 *
2036 * RETURNS
2037 * Success: TRUE
2038 * Failure: FALSE
2039 */
2040 BOOL WINAPI SetMailslotInfo( HANDLE hMailslot, DWORD dwReadTimeout)
2041 {
2042 FILE_MAILSLOT_SET_INFORMATION info;
2043 IO_STATUS_BLOCK iosb;
2044 NTSTATUS status;
2045
2046 TRACE("%p %d\n", hMailslot, dwReadTimeout);
2047
2048 if (dwReadTimeout != MAILSLOT_WAIT_FOREVER)
2049 info.ReadTimeout.QuadPart = (ULONGLONG)dwReadTimeout * -10000;
2050 else
2051 info.ReadTimeout.QuadPart = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
2052 status = NtSetInformationFile( hMailslot, &iosb, &info, sizeof info,
2053 FileMailslotSetInformation );
2054 if( status != STATUS_SUCCESS )
2055 {
2056 SetLastError( RtlNtStatusToDosError(status) );
2057 return FALSE;
2058 }
2059 return TRUE;
2060 }
2061
2062
2063 /******************************************************************************
2064 * CreateIoCompletionPort (KERNEL32.@)
2065 */
2066 HANDLE WINAPI CreateIoCompletionPort(HANDLE hFileHandle, HANDLE hExistingCompletionPort,
2067 ULONG_PTR CompletionKey, DWORD dwNumberOfConcurrentThreads)
2068 {
2069 NTSTATUS status;
2070 HANDLE ret = 0;
2071
2072 TRACE("(%p, %p, %08lx, %08x)\n",
2073 hFileHandle, hExistingCompletionPort, CompletionKey, dwNumberOfConcurrentThreads);
2074
2075 if (hExistingCompletionPort && hFileHandle == INVALID_HANDLE_VALUE)
2076 {
2077 SetLastError( ERROR_INVALID_PARAMETER);
2078 return NULL;
2079 }
2080
2081 if (hExistingCompletionPort)
2082 ret = hExistingCompletionPort;
2083 else
2084 {
2085 status = NtCreateIoCompletion( &ret, IO_COMPLETION_ALL_ACCESS, NULL, dwNumberOfConcurrentThreads );
2086 if (status != STATUS_SUCCESS) goto fail;
2087 }
2088
2089 if (hFileHandle != INVALID_HANDLE_VALUE)
2090 {
2091 FILE_COMPLETION_INFORMATION info;
2092 IO_STATUS_BLOCK iosb;
2093
2094 info.CompletionPort = ret;
2095 info.CompletionKey = CompletionKey;
2096 status = NtSetInformationFile( hFileHandle, &iosb, &info, sizeof(info), FileCompletionInformation );
2097 if (status != STATUS_SUCCESS) goto fail;
2098 }
2099
2100 return ret;
2101
2102 fail:
2103 if (ret && !hExistingCompletionPort)
2104 CloseHandle( ret );
2105 SetLastError( RtlNtStatusToDosError(status) );
2106 return 0;
2107 }
2108
2109 /******************************************************************************
2110 * GetQueuedCompletionStatus (KERNEL32.@)
2111 */
2112 BOOL WINAPI GetQueuedCompletionStatus( HANDLE CompletionPort, LPDWORD lpNumberOfBytesTransferred,
2113 PULONG_PTR pCompletionKey, LPOVERLAPPED *lpOverlapped,
2114 DWORD dwMilliseconds )
2115 {
2116 NTSTATUS status;
2117 IO_STATUS_BLOCK iosb;
2118 LARGE_INTEGER wait_time;
2119
2120 TRACE("(%p,%p,%p,%p,%d)\n",
2121 CompletionPort,lpNumberOfBytesTransferred,pCompletionKey,lpOverlapped,dwMilliseconds);
2122
2123 *lpOverlapped = NULL;
2124
2125 status = NtRemoveIoCompletion( CompletionPort, pCompletionKey, (PULONG_PTR)lpOverlapped,
2126 &iosb, get_nt_timeout( &wait_time, dwMilliseconds ) );
2127 if (status == STATUS_SUCCESS)
2128 {
2129 *lpNumberOfBytesTransferred = iosb.Information;
2130 return TRUE;
2131 }
2132
2133 if (status == STATUS_TIMEOUT) SetLastError( WAIT_TIMEOUT );
2134 else SetLastError( RtlNtStatusToDosError(status) );
2135 return FALSE;
2136 }
2137
2138
2139 /******************************************************************************
2140 * PostQueuedCompletionStatus (KERNEL32.@)
2141 */
2142 BOOL WINAPI PostQueuedCompletionStatus( HANDLE CompletionPort, DWORD dwNumberOfBytes,
2143 ULONG_PTR dwCompletionKey, LPOVERLAPPED lpOverlapped)
2144 {
2145 NTSTATUS status;
2146
2147 TRACE("%p %d %08lx %p\n", CompletionPort, dwNumberOfBytes, dwCompletionKey, lpOverlapped );
2148
2149 status = NtSetIoCompletion( CompletionPort, dwCompletionKey, (ULONG_PTR)lpOverlapped,
2150 STATUS_SUCCESS, dwNumberOfBytes );
2151
2152 if (status == STATUS_SUCCESS) return TRUE;
2153 SetLastError( RtlNtStatusToDosError(status) );
2154 return FALSE;
2155 }
2156
2157 /******************************************************************************
2158 * BindIoCompletionCallback (KERNEL32.@)
2159 */
2160 BOOL WINAPI BindIoCompletionCallback( HANDLE FileHandle, LPOVERLAPPED_COMPLETION_ROUTINE Function, ULONG Flags)
2161 {
2162 NTSTATUS status;
2163
2164 TRACE("(%p, %p, %d)\n", FileHandle, Function, Flags);
2165
2166 status = RtlSetIoCompletionCallback( FileHandle, (PRTL_OVERLAPPED_COMPLETION_ROUTINE)Function, Flags );
2167 if (status == STATUS_SUCCESS) return TRUE;
2168 SetLastError( RtlNtStatusToDosError(status) );
2169 return FALSE;
2170 }
2171
2172 #ifdef __i386__
2173
2174 /***********************************************************************
2175 * InterlockedCompareExchange (KERNEL32.@)
2176 */
2177 /* LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare ); */
2178 __ASM_STDCALL_FUNC(InterlockedCompareExchange, 12,
2179 "movl 12(%esp),%eax\n\t"
2180 "movl 8(%esp),%ecx\n\t"
2181 "movl 4(%esp),%edx\n\t"
2182 "lock; cmpxchgl %ecx,(%edx)\n\t"
2183 "ret $12")
2184
2185 /***********************************************************************
2186 * InterlockedExchange (KERNEL32.@)
2187 */
2188 /* LONG WINAPI InterlockedExchange( PLONG dest, LONG val ); */
2189 __ASM_STDCALL_FUNC(InterlockedExchange, 8,
2190 "movl 8(%esp),%eax\n\t"
2191 "movl 4(%esp),%edx\n\t"
2192 "lock; xchgl %eax,(%edx)\n\t"
2193 "ret $8")
2194
2195 /***********************************************************************
2196 * InterlockedExchangeAdd (KERNEL32.@)
2197 */
2198 /* LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr ); */
2199 __ASM_STDCALL_FUNC(InterlockedExchangeAdd, 8,
2200 "movl 8(%esp),%eax\n\t"
2201 "movl 4(%esp),%edx\n\t"
2202 "lock; xaddl %eax,(%edx)\n\t"
2203 "ret $8")
2204
2205 /***********************************************************************
2206 * InterlockedIncrement (KERNEL32.@)
2207 */
2208 /* LONG WINAPI InterlockedIncrement( PLONG dest ); */
2209 __ASM_STDCALL_FUNC(InterlockedIncrement, 4,
2210 "movl 4(%esp),%edx\n\t"
2211 "movl $1,%eax\n\t"
2212 "lock; xaddl %eax,(%edx)\n\t"
2213 "incl %eax\n\t"
2214 "ret $4")
2215
2216 /***********************************************************************
2217 * InterlockedDecrement (KERNEL32.@)
2218 */
2219 __ASM_STDCALL_FUNC(InterlockedDecrement, 4,
2220 "movl 4(%esp),%edx\n\t"
2221 "movl $-1,%eax\n\t"
2222 "lock; xaddl %eax,(%edx)\n\t"
2223 "decl %eax\n\t"
2224 "ret $4")
2225
2226 #endif /* __i386__ */
2227
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.