2 * Kernel synchronization objects
4 * Copyright 1998 Alexandre Julliard
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.
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.
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
22 #include "wine/port.h"
33 #define WIN32_NO_STATUS
34 #define NONAMELESSUNION
43 #include "wine/unicode.h"
44 #include "kernel_private.h"
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(sync
);
50 /* check if current version is NT or Win95 */
51 static inline BOOL
is_version_nt(void)
53 return !(GetVersion() & 0x80000000);
56 /* returns directory handle to \\BaseNamedObjects */
57 HANDLE
get_BaseNamedObjects_handle(void)
59 static HANDLE handle
= NULL
;
60 static const WCHAR basenameW
[] = {'\\','S','e','s','s','i','o','n','s','\\','%','u',
61 '\\','B','a','s','e','N','a','m','e','d','O','b','j','e','c','t','s',0};
64 OBJECT_ATTRIBUTES attr
;
70 sprintfW( buffer
, basenameW
, NtCurrentTeb()->Peb
->SessionId
);
71 RtlInitUnicodeString( &str
, buffer
);
72 InitializeObjectAttributes(&attr
, &str
, 0, 0, NULL
);
73 NtOpenDirectoryObject(&dir
, DIRECTORY_CREATE_OBJECT
|DIRECTORY_TRAVERSE
,
75 if (InterlockedCompareExchangePointer( &handle
, dir
, 0 ) != 0)
77 /* someone beat us here... */
84 /* helper for kernel32->ntdll timeout format conversion */
85 static inline PLARGE_INTEGER
get_nt_timeout( PLARGE_INTEGER pTime
, DWORD timeout
)
87 if (timeout
== INFINITE
) return NULL
;
88 pTime
->QuadPart
= (ULONGLONG
)timeout
* -10000;
92 /***********************************************************************
95 VOID WINAPI DECLSPEC_HOTPATCH
Sleep( DWORD timeout
)
97 SleepEx( timeout
, FALSE
);
100 /******************************************************************************
101 * SleepEx (KERNEL32.@)
103 DWORD WINAPI
SleepEx( DWORD timeout
, BOOL alertable
)
108 status
= NtDelayExecution( alertable
, get_nt_timeout( &time
, timeout
) );
109 if (status
== STATUS_USER_APC
) return WAIT_IO_COMPLETION
;
114 /***********************************************************************
115 * SwitchToThread (KERNEL32.@)
117 BOOL WINAPI
SwitchToThread(void)
119 return (NtYieldExecution() != STATUS_NO_YIELD_PERFORMED
);
123 /***********************************************************************
124 * WaitForSingleObject (KERNEL32.@)
126 DWORD WINAPI
WaitForSingleObject( HANDLE handle
, DWORD timeout
)
128 return WaitForMultipleObjectsEx( 1, &handle
, FALSE
, timeout
, FALSE
);
132 /***********************************************************************
133 * WaitForSingleObjectEx (KERNEL32.@)
135 DWORD WINAPI
WaitForSingleObjectEx( HANDLE handle
, DWORD timeout
,
138 return WaitForMultipleObjectsEx( 1, &handle
, FALSE
, timeout
, alertable
);
142 /***********************************************************************
143 * WaitForMultipleObjects (KERNEL32.@)
145 DWORD WINAPI
WaitForMultipleObjects( DWORD count
, const HANDLE
*handles
,
146 BOOL wait_all
, DWORD timeout
)
148 return WaitForMultipleObjectsEx( count
, handles
, wait_all
, timeout
, FALSE
);
151 static HANDLE
normalize_handle_if_console(HANDLE handle
)
153 if ((handle
== (HANDLE
)STD_INPUT_HANDLE
) ||
154 (handle
== (HANDLE
)STD_OUTPUT_HANDLE
) ||
155 (handle
== (HANDLE
)STD_ERROR_HANDLE
))
156 handle
= GetStdHandle( HandleToULong(handle
) );
158 /* yes, even screen buffer console handles are waitable, and are
159 * handled as a handle to the console itself !!
161 if (is_console_handle(handle
))
163 if (VerifyConsoleIoHandle(handle
))
164 handle
= GetConsoleInputWaitHandle();
169 /***********************************************************************
170 * WaitForMultipleObjectsEx (KERNEL32.@)
172 DWORD WINAPI
WaitForMultipleObjectsEx( DWORD count
, const HANDLE
*handles
,
173 BOOL wait_all
, DWORD timeout
,
177 HANDLE hloc
[MAXIMUM_WAIT_OBJECTS
];
181 if (count
> MAXIMUM_WAIT_OBJECTS
)
183 SetLastError(ERROR_INVALID_PARAMETER
);
186 for (i
= 0; i
< count
; i
++)
187 hloc
[i
] = normalize_handle_if_console(handles
[i
]);
189 status
= NtWaitForMultipleObjects( count
, hloc
, !wait_all
, alertable
,
190 get_nt_timeout( &time
, timeout
) );
192 if (HIWORD(status
)) /* is it an error code? */
194 SetLastError( RtlNtStatusToDosError(status
) );
195 status
= WAIT_FAILED
;
201 /***********************************************************************
202 * RegisterWaitForSingleObject (KERNEL32.@)
204 BOOL WINAPI
RegisterWaitForSingleObject(PHANDLE phNewWaitObject
, HANDLE hObject
,
205 WAITORTIMERCALLBACK Callback
, PVOID Context
,
206 ULONG dwMilliseconds
, ULONG dwFlags
)
210 TRACE("%p %p %p %p %d %d\n",
211 phNewWaitObject
,hObject
,Callback
,Context
,dwMilliseconds
,dwFlags
);
213 hObject
= normalize_handle_if_console(hObject
);
214 status
= RtlRegisterWait( phNewWaitObject
, hObject
, Callback
, Context
, dwMilliseconds
, dwFlags
);
215 if (status
!= STATUS_SUCCESS
)
217 SetLastError( RtlNtStatusToDosError(status
) );
223 /***********************************************************************
224 * RegisterWaitForSingleObjectEx (KERNEL32.@)
226 HANDLE WINAPI
RegisterWaitForSingleObjectEx( HANDLE hObject
,
227 WAITORTIMERCALLBACK Callback
, PVOID Context
,
228 ULONG dwMilliseconds
, ULONG dwFlags
)
231 HANDLE hNewWaitObject
;
233 TRACE("%p %p %p %d %d\n",
234 hObject
,Callback
,Context
,dwMilliseconds
,dwFlags
);
236 hObject
= normalize_handle_if_console(hObject
);
237 status
= RtlRegisterWait( &hNewWaitObject
, hObject
, Callback
, Context
, dwMilliseconds
, dwFlags
);
238 if (status
!= STATUS_SUCCESS
)
240 SetLastError( RtlNtStatusToDosError(status
) );
243 return hNewWaitObject
;
246 /***********************************************************************
247 * UnregisterWait (KERNEL32.@)
249 BOOL WINAPI
UnregisterWait( HANDLE WaitHandle
)
253 TRACE("%p\n",WaitHandle
);
255 status
= RtlDeregisterWait( WaitHandle
);
256 if (status
!= STATUS_SUCCESS
)
258 SetLastError( RtlNtStatusToDosError(status
) );
264 /***********************************************************************
265 * UnregisterWaitEx (KERNEL32.@)
267 BOOL WINAPI
UnregisterWaitEx( HANDLE WaitHandle
, HANDLE CompletionEvent
)
271 TRACE("%p %p\n",WaitHandle
, CompletionEvent
);
273 status
= RtlDeregisterWaitEx( WaitHandle
, CompletionEvent
);
274 if (status
!= STATUS_SUCCESS
) SetLastError( RtlNtStatusToDosError(status
) );
278 /***********************************************************************
279 * SignalObjectAndWait (KERNEL32.@)
281 * Makes it possible to atomically signal any of the synchronization
282 * objects (semaphore, mutex, event) and wait on another.
284 DWORD WINAPI
SignalObjectAndWait( HANDLE hObjectToSignal
, HANDLE hObjectToWaitOn
,
285 DWORD dwMilliseconds
, BOOL bAlertable
)
288 LARGE_INTEGER timeout
;
290 TRACE("%p %p %d %d\n", hObjectToSignal
,
291 hObjectToWaitOn
, dwMilliseconds
, bAlertable
);
293 status
= NtSignalAndWaitForSingleObject( hObjectToSignal
, hObjectToWaitOn
, bAlertable
,
294 get_nt_timeout( &timeout
, dwMilliseconds
) );
297 SetLastError( RtlNtStatusToDosError(status
) );
298 status
= WAIT_FAILED
;
303 /***********************************************************************
304 * InitializeCriticalSection (KERNEL32.@)
306 * Initialise a critical section before use.
309 * crit [O] Critical section to initialise.
312 * Nothing. If the function fails an exception is raised.
314 void WINAPI
InitializeCriticalSection( CRITICAL_SECTION
*crit
)
316 InitializeCriticalSectionEx( crit
, 0, 0 );
319 /***********************************************************************
320 * InitializeCriticalSectionAndSpinCount (KERNEL32.@)
322 * Initialise a critical section with a spin count.
325 * crit [O] Critical section to initialise.
326 * spincount [I] Number of times to spin upon contention.
330 * Failure: Nothing. If the function fails an exception is raised.
333 * spincount is ignored on uni-processor systems.
335 BOOL WINAPI
InitializeCriticalSectionAndSpinCount( CRITICAL_SECTION
*crit
, DWORD spincount
)
337 return InitializeCriticalSectionEx( crit
, spincount
, 0 );
340 /***********************************************************************
341 * InitializeCriticalSectionEx (KERNEL32.@)
343 * Initialise a critical section with a spin count and flags.
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.
352 * Failure: Nothing. If the function fails an exception is raised.
355 * spincount is ignored on uni-processor systems.
357 BOOL WINAPI
InitializeCriticalSectionEx( CRITICAL_SECTION
*crit
, DWORD spincount
, DWORD flags
)
359 NTSTATUS ret
= RtlInitializeCriticalSectionEx( crit
, spincount
, flags
);
360 if (ret
) RtlRaiseStatus( ret
);
364 /***********************************************************************
365 * MakeCriticalSectionGlobal (KERNEL32.@)
367 void WINAPI
MakeCriticalSectionGlobal( CRITICAL_SECTION
*crit
)
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
;
378 /***********************************************************************
379 * ReinitializeCriticalSection (KERNEL32.@)
381 * Initialise an already used critical section.
384 * crit [O] Critical section to initialise.
389 void WINAPI
ReinitializeCriticalSection( CRITICAL_SECTION
*crit
)
391 if ( !crit
->LockSemaphore
)
392 RtlInitializeCriticalSection( crit
);
396 /***********************************************************************
397 * UninitializeCriticalSection (KERNEL32.@)
399 * UnInitialise a critical section after use.
402 * crit [O] Critical section to uninitialise (destroy).
407 void WINAPI
UninitializeCriticalSection( CRITICAL_SECTION
*crit
)
409 RtlDeleteCriticalSection( crit
);
413 /***********************************************************************
414 * CreateEventA (KERNEL32.@)
416 HANDLE WINAPI DECLSPEC_HOTPATCH
CreateEventA( SECURITY_ATTRIBUTES
*sa
, BOOL manual_reset
,
417 BOOL initial_state
, LPCSTR name
)
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
);
427 /***********************************************************************
428 * CreateEventW (KERNEL32.@)
430 HANDLE WINAPI DECLSPEC_HOTPATCH
CreateEventW( SECURITY_ATTRIBUTES
*sa
, BOOL manual_reset
,
431 BOOL initial_state
, LPCWSTR name
)
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
);
441 /***********************************************************************
442 * CreateEventExA (KERNEL32.@)
444 HANDLE WINAPI DECLSPEC_HOTPATCH
CreateEventExA( SECURITY_ATTRIBUTES
*sa
, LPCSTR name
, DWORD flags
, DWORD access
)
446 WCHAR buffer
[MAX_PATH
];
448 if (!name
) return CreateEventExW( sa
, NULL
, flags
, access
);
450 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
452 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
455 return CreateEventExW( sa
, buffer
, flags
, access
);
459 /***********************************************************************
460 * CreateEventExW (KERNEL32.@)
462 HANDLE WINAPI DECLSPEC_HOTPATCH
CreateEventExW( SECURITY_ATTRIBUTES
*sa
, LPCWSTR name
, DWORD flags
, DWORD access
)
465 UNICODE_STRING nameW
;
466 OBJECT_ATTRIBUTES attr
;
469 /* one buggy program needs this
470 * ("Van Dale Groot woordenboek der Nederlandse taal")
472 if (sa
&& IsBadReadPtr(sa
,sizeof(SECURITY_ATTRIBUTES
)))
474 ERR("Bad security attributes pointer %p\n",sa
);
475 SetLastError( ERROR_INVALID_PARAMETER
);
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
;
487 RtlInitUnicodeString( &nameW
, name
);
488 attr
.ObjectName
= &nameW
;
489 attr
.RootDirectory
= get_BaseNamedObjects_handle();
492 status
= NtCreateEvent( &ret
, access
, &attr
,
493 (flags
& CREATE_EVENT_MANUAL_RESET
) ? NotificationEvent
: SynchronizationEvent
,
494 (flags
& CREATE_EVENT_INITIAL_SET
) != 0 );
495 if (status
== STATUS_OBJECT_NAME_EXISTS
)
496 SetLastError( ERROR_ALREADY_EXISTS
);
498 SetLastError( RtlNtStatusToDosError(status
) );
503 /***********************************************************************
504 * OpenEventA (KERNEL32.@)
506 HANDLE WINAPI DECLSPEC_HOTPATCH
OpenEventA( DWORD access
, BOOL inherit
, LPCSTR name
)
508 WCHAR buffer
[MAX_PATH
];
510 if (!name
) return OpenEventW( access
, inherit
, NULL
);
512 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
514 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
517 return OpenEventW( access
, inherit
, buffer
);
521 /***********************************************************************
522 * OpenEventW (KERNEL32.@)
524 HANDLE WINAPI DECLSPEC_HOTPATCH
OpenEventW( DWORD access
, BOOL inherit
, LPCWSTR name
)
527 UNICODE_STRING nameW
;
528 OBJECT_ATTRIBUTES attr
;
531 if (!is_version_nt()) access
= EVENT_ALL_ACCESS
;
533 attr
.Length
= sizeof(attr
);
534 attr
.RootDirectory
= 0;
535 attr
.ObjectName
= NULL
;
536 attr
.Attributes
= inherit
? OBJ_INHERIT
: 0;
537 attr
.SecurityDescriptor
= NULL
;
538 attr
.SecurityQualityOfService
= NULL
;
541 RtlInitUnicodeString( &nameW
, name
);
542 attr
.ObjectName
= &nameW
;
543 attr
.RootDirectory
= get_BaseNamedObjects_handle();
546 status
= NtOpenEvent( &ret
, access
, &attr
);
547 if (status
!= STATUS_SUCCESS
)
549 SetLastError( RtlNtStatusToDosError(status
) );
555 /***********************************************************************
556 * PulseEvent (KERNEL32.@)
558 BOOL WINAPI DECLSPEC_HOTPATCH
PulseEvent( HANDLE handle
)
562 if ((status
= NtPulseEvent( handle
, NULL
)))
563 SetLastError( RtlNtStatusToDosError(status
) );
568 /***********************************************************************
569 * SetEvent (KERNEL32.@)
571 BOOL WINAPI DECLSPEC_HOTPATCH
SetEvent( HANDLE handle
)
575 if ((status
= NtSetEvent( handle
, NULL
)))
576 SetLastError( RtlNtStatusToDosError(status
) );
581 /***********************************************************************
582 * ResetEvent (KERNEL32.@)
584 BOOL WINAPI DECLSPEC_HOTPATCH
ResetEvent( HANDLE handle
)
588 if ((status
= NtResetEvent( handle
, NULL
)))
589 SetLastError( RtlNtStatusToDosError(status
) );
594 /***********************************************************************
595 * CreateMutexA (KERNEL32.@)
597 HANDLE WINAPI DECLSPEC_HOTPATCH
CreateMutexA( SECURITY_ATTRIBUTES
*sa
, BOOL owner
, LPCSTR name
)
599 return CreateMutexExA( sa
, name
, owner
? CREATE_MUTEX_INITIAL_OWNER
: 0, MUTEX_ALL_ACCESS
);
603 /***********************************************************************
604 * CreateMutexW (KERNEL32.@)
606 HANDLE WINAPI DECLSPEC_HOTPATCH
CreateMutexW( SECURITY_ATTRIBUTES
*sa
, BOOL owner
, LPCWSTR name
)
608 return CreateMutexExW( sa
, name
, owner
? CREATE_MUTEX_INITIAL_OWNER
: 0, MUTEX_ALL_ACCESS
);
612 /***********************************************************************
613 * CreateMutexExA (KERNEL32.@)
615 HANDLE WINAPI DECLSPEC_HOTPATCH
CreateMutexExA( SECURITY_ATTRIBUTES
*sa
, LPCSTR name
, DWORD flags
, DWORD access
)
620 if (!name
) return CreateMutexExW( sa
, NULL
, flags
, access
);
622 RtlInitAnsiString( &nameA
, name
);
623 status
= RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString
, &nameA
, FALSE
);
624 if (status
!= STATUS_SUCCESS
)
626 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
629 return CreateMutexExW( sa
, NtCurrentTeb()->StaticUnicodeString
.Buffer
, flags
, access
);
633 /***********************************************************************
634 * CreateMutexExW (KERNEL32.@)
636 HANDLE WINAPI DECLSPEC_HOTPATCH
CreateMutexExW( SECURITY_ATTRIBUTES
*sa
, LPCWSTR name
, DWORD flags
, DWORD access
)
639 UNICODE_STRING nameW
;
640 OBJECT_ATTRIBUTES attr
;
643 attr
.Length
= sizeof(attr
);
644 attr
.RootDirectory
= 0;
645 attr
.ObjectName
= NULL
;
646 attr
.Attributes
= OBJ_OPENIF
| ((sa
&& sa
->bInheritHandle
) ? OBJ_INHERIT
: 0);
647 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
648 attr
.SecurityQualityOfService
= NULL
;
651 RtlInitUnicodeString( &nameW
, name
);
652 attr
.ObjectName
= &nameW
;
653 attr
.RootDirectory
= get_BaseNamedObjects_handle();
656 status
= NtCreateMutant( &ret
, access
, &attr
, (flags
& CREATE_MUTEX_INITIAL_OWNER
) != 0 );
657 if (status
== STATUS_OBJECT_NAME_EXISTS
)
658 SetLastError( ERROR_ALREADY_EXISTS
);
660 SetLastError( RtlNtStatusToDosError(status
) );
665 /***********************************************************************
666 * OpenMutexA (KERNEL32.@)
668 HANDLE WINAPI DECLSPEC_HOTPATCH
OpenMutexA( DWORD access
, BOOL inherit
, LPCSTR name
)
670 WCHAR buffer
[MAX_PATH
];
672 if (!name
) return OpenMutexW( access
, inherit
, NULL
);
674 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
676 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
679 return OpenMutexW( access
, inherit
, buffer
);
683 /***********************************************************************
684 * OpenMutexW (KERNEL32.@)
686 HANDLE WINAPI DECLSPEC_HOTPATCH
OpenMutexW( DWORD access
, BOOL inherit
, LPCWSTR name
)
689 UNICODE_STRING nameW
;
690 OBJECT_ATTRIBUTES attr
;
693 if (!is_version_nt()) access
= MUTEX_ALL_ACCESS
;
695 attr
.Length
= sizeof(attr
);
696 attr
.RootDirectory
= 0;
697 attr
.ObjectName
= NULL
;
698 attr
.Attributes
= inherit
? OBJ_INHERIT
: 0;
699 attr
.SecurityDescriptor
= NULL
;
700 attr
.SecurityQualityOfService
= NULL
;
703 RtlInitUnicodeString( &nameW
, name
);
704 attr
.ObjectName
= &nameW
;
705 attr
.RootDirectory
= get_BaseNamedObjects_handle();
708 status
= NtOpenMutant( &ret
, access
, &attr
);
709 if (status
!= STATUS_SUCCESS
)
711 SetLastError( RtlNtStatusToDosError(status
) );
718 /***********************************************************************
719 * ReleaseMutex (KERNEL32.@)
721 BOOL WINAPI DECLSPEC_HOTPATCH
ReleaseMutex( HANDLE handle
)
725 status
= NtReleaseMutant(handle
, NULL
);
726 if (status
!= STATUS_SUCCESS
)
728 SetLastError( RtlNtStatusToDosError(status
) );
740 /***********************************************************************
741 * CreateSemaphoreA (KERNEL32.@)
743 HANDLE WINAPI DECLSPEC_HOTPATCH
CreateSemaphoreA( SECURITY_ATTRIBUTES
*sa
, LONG initial
, LONG max
, LPCSTR name
)
745 return CreateSemaphoreExA( sa
, initial
, max
, name
, 0, SEMAPHORE_ALL_ACCESS
);
749 /***********************************************************************
750 * CreateSemaphoreW (KERNEL32.@)
752 HANDLE WINAPI DECLSPEC_HOTPATCH
CreateSemaphoreW( SECURITY_ATTRIBUTES
*sa
, LONG initial
, LONG max
, LPCWSTR name
)
754 return CreateSemaphoreExW( sa
, initial
, max
, name
, 0, SEMAPHORE_ALL_ACCESS
);
758 /***********************************************************************
759 * CreateSemaphoreExA (KERNEL32.@)
761 HANDLE WINAPI DECLSPEC_HOTPATCH
CreateSemaphoreExA( SECURITY_ATTRIBUTES
*sa
, LONG initial
, LONG max
,
762 LPCSTR name
, DWORD flags
, DWORD access
)
764 WCHAR buffer
[MAX_PATH
];
766 if (!name
) return CreateSemaphoreExW( sa
, initial
, max
, NULL
, flags
, access
);
768 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
770 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
773 return CreateSemaphoreExW( sa
, initial
, max
, buffer
, flags
, access
);
777 /***********************************************************************
778 * CreateSemaphoreExW (KERNEL32.@)
780 HANDLE WINAPI DECLSPEC_HOTPATCH
CreateSemaphoreExW( SECURITY_ATTRIBUTES
*sa
, LONG initial
, LONG max
,
781 LPCWSTR name
, DWORD flags
, DWORD access
)
784 UNICODE_STRING nameW
;
785 OBJECT_ATTRIBUTES attr
;
788 attr
.Length
= sizeof(attr
);
789 attr
.RootDirectory
= 0;
790 attr
.ObjectName
= NULL
;
791 attr
.Attributes
= OBJ_OPENIF
| ((sa
&& sa
->bInheritHandle
) ? OBJ_INHERIT
: 0);
792 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
793 attr
.SecurityQualityOfService
= NULL
;
796 RtlInitUnicodeString( &nameW
, name
);
797 attr
.ObjectName
= &nameW
;
798 attr
.RootDirectory
= get_BaseNamedObjects_handle();
801 status
= NtCreateSemaphore( &ret
, access
, &attr
, initial
, max
);
802 if (status
== STATUS_OBJECT_NAME_EXISTS
)
803 SetLastError( ERROR_ALREADY_EXISTS
);
805 SetLastError( RtlNtStatusToDosError(status
) );
810 /***********************************************************************
811 * OpenSemaphoreA (KERNEL32.@)
813 HANDLE WINAPI DECLSPEC_HOTPATCH
OpenSemaphoreA( DWORD access
, BOOL inherit
, LPCSTR name
)
815 WCHAR buffer
[MAX_PATH
];
817 if (!name
) return OpenSemaphoreW( access
, inherit
, NULL
);
819 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
821 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
824 return OpenSemaphoreW( access
, inherit
, buffer
);
828 /***********************************************************************
829 * OpenSemaphoreW (KERNEL32.@)
831 HANDLE WINAPI DECLSPEC_HOTPATCH
OpenSemaphoreW( DWORD access
, BOOL inherit
, LPCWSTR name
)
834 UNICODE_STRING nameW
;
835 OBJECT_ATTRIBUTES attr
;
838 if (!is_version_nt()) access
= SEMAPHORE_ALL_ACCESS
;
840 attr
.Length
= sizeof(attr
);
841 attr
.RootDirectory
= 0;
842 attr
.ObjectName
= NULL
;
843 attr
.Attributes
= inherit
? OBJ_INHERIT
: 0;
844 attr
.SecurityDescriptor
= NULL
;
845 attr
.SecurityQualityOfService
= NULL
;
848 RtlInitUnicodeString( &nameW
, name
);
849 attr
.ObjectName
= &nameW
;
850 attr
.RootDirectory
= get_BaseNamedObjects_handle();
853 status
= NtOpenSemaphore( &ret
, access
, &attr
);
854 if (status
!= STATUS_SUCCESS
)
856 SetLastError( RtlNtStatusToDosError(status
) );
863 /***********************************************************************
864 * ReleaseSemaphore (KERNEL32.@)
866 BOOL WINAPI DECLSPEC_HOTPATCH
ReleaseSemaphore( HANDLE handle
, LONG count
, LONG
*previous
)
868 NTSTATUS status
= NtReleaseSemaphore( handle
, count
, (PULONG
)previous
);
869 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
878 /******************************************************************************
879 * CreateJobObjectW (KERNEL32.@)
881 HANDLE WINAPI
CreateJobObjectW( LPSECURITY_ATTRIBUTES sa
, LPCWSTR name
)
884 UNICODE_STRING nameW
;
885 OBJECT_ATTRIBUTES attr
;
888 attr
.Length
= sizeof(attr
);
889 attr
.RootDirectory
= 0;
890 attr
.ObjectName
= NULL
;
891 attr
.Attributes
= OBJ_OPENIF
| ((sa
&& sa
->bInheritHandle
) ? OBJ_INHERIT
: 0);
892 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
893 attr
.SecurityQualityOfService
= NULL
;
896 RtlInitUnicodeString( &nameW
, name
);
897 attr
.ObjectName
= &nameW
;
898 attr
.RootDirectory
= get_BaseNamedObjects_handle();
901 status
= NtCreateJobObject( &ret
, JOB_OBJECT_ALL_ACCESS
, &attr
);
902 if (status
== STATUS_OBJECT_NAME_EXISTS
)
903 SetLastError( ERROR_ALREADY_EXISTS
);
905 SetLastError( RtlNtStatusToDosError(status
) );
909 /******************************************************************************
910 * CreateJobObjectA (KERNEL32.@)
912 HANDLE WINAPI
CreateJobObjectA( LPSECURITY_ATTRIBUTES attr
, LPCSTR name
)
914 WCHAR buffer
[MAX_PATH
];
916 if (!name
) return CreateJobObjectW( attr
, NULL
);
918 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
920 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
923 return CreateJobObjectW( attr
, buffer
);
926 /******************************************************************************
927 * OpenJobObjectW (KERNEL32.@)
929 HANDLE WINAPI
OpenJobObjectW( DWORD access
, BOOL inherit
, LPCWSTR name
)
932 UNICODE_STRING nameW
;
933 OBJECT_ATTRIBUTES attr
;
936 attr
.Length
= sizeof(attr
);
937 attr
.RootDirectory
= 0;
938 attr
.ObjectName
= NULL
;
939 attr
.Attributes
= inherit
? OBJ_INHERIT
: 0;
940 attr
.SecurityDescriptor
= NULL
;
941 attr
.SecurityQualityOfService
= NULL
;
944 RtlInitUnicodeString( &nameW
, name
);
945 attr
.ObjectName
= &nameW
;
946 attr
.RootDirectory
= get_BaseNamedObjects_handle();
949 status
= NtOpenJobObject( &ret
, access
, &attr
);
950 if (status
!= STATUS_SUCCESS
)
952 SetLastError( RtlNtStatusToDosError(status
) );
958 /******************************************************************************
959 * OpenJobObjectA (KERNEL32.@)
961 HANDLE WINAPI
OpenJobObjectA( DWORD access
, BOOL inherit
, LPCSTR name
)
963 WCHAR buffer
[MAX_PATH
];
965 if (!name
) return OpenJobObjectW( access
, inherit
, NULL
);
967 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
969 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
972 return OpenJobObjectW( access
, inherit
, buffer
);
975 /******************************************************************************
976 * TerminateJobObject (KERNEL32.@)
978 BOOL WINAPI
TerminateJobObject( HANDLE job
, UINT exit_code
)
980 NTSTATUS status
= NtTerminateJobObject( job
, exit_code
);
981 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
985 /******************************************************************************
986 * QueryInformationJobObject (KERNEL32.@)
988 BOOL WINAPI
QueryInformationJobObject( HANDLE job
, JOBOBJECTINFOCLASS
class, LPVOID info
,
989 DWORD len
, DWORD
*ret_len
)
991 NTSTATUS status
= NtQueryInformationJobObject( job
, class, info
, len
, ret_len
);
992 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
996 /******************************************************************************
997 * SetInformationJobObject (KERNEL32.@)
999 BOOL WINAPI
SetInformationJobObject( HANDLE job
, JOBOBJECTINFOCLASS
class, LPVOID info
, DWORD len
)
1001 NTSTATUS status
= NtSetInformationJobObject( job
, class, info
, len
);
1002 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
1006 /******************************************************************************
1007 * AssignProcessToJobObject (KERNEL32.@)
1009 BOOL WINAPI
AssignProcessToJobObject( HANDLE job
, HANDLE process
)
1011 NTSTATUS status
= NtAssignProcessToJobObject( job
, process
);
1012 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
1016 /******************************************************************************
1017 * IsProcessInJob (KERNEL32.@)
1019 BOOL WINAPI
IsProcessInJob( HANDLE process
, HANDLE job
, PBOOL result
)
1021 NTSTATUS status
= NtIsProcessInJob( process
, job
);
1024 case STATUS_PROCESS_IN_JOB
:
1027 case STATUS_PROCESS_NOT_IN_JOB
:
1031 SetLastError( RtlNtStatusToDosError(status
) );
1042 /***********************************************************************
1043 * CreateWaitableTimerA (KERNEL32.@)
1045 HANDLE WINAPI
CreateWaitableTimerA( SECURITY_ATTRIBUTES
*sa
, BOOL manual
, LPCSTR name
)
1047 return CreateWaitableTimerExA( sa
, name
, manual
? CREATE_WAITABLE_TIMER_MANUAL_RESET
: 0,
1052 /***********************************************************************
1053 * CreateWaitableTimerW (KERNEL32.@)
1055 HANDLE WINAPI
CreateWaitableTimerW( SECURITY_ATTRIBUTES
*sa
, BOOL manual
, LPCWSTR name
)
1057 return CreateWaitableTimerExW( sa
, name
, manual
? CREATE_WAITABLE_TIMER_MANUAL_RESET
: 0,
1062 /***********************************************************************
1063 * CreateWaitableTimerExA (KERNEL32.@)
1065 HANDLE WINAPI
CreateWaitableTimerExA( SECURITY_ATTRIBUTES
*sa
, LPCSTR name
, DWORD flags
, DWORD access
)
1067 WCHAR buffer
[MAX_PATH
];
1069 if (!name
) return CreateWaitableTimerExW( sa
, NULL
, flags
, access
);
1071 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
1073 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
1076 return CreateWaitableTimerExW( sa
, buffer
, flags
, access
);
1080 /***********************************************************************
1081 * CreateWaitableTimerExW (KERNEL32.@)
1083 HANDLE WINAPI
CreateWaitableTimerExW( SECURITY_ATTRIBUTES
*sa
, LPCWSTR name
, DWORD flags
, DWORD access
)
1087 UNICODE_STRING nameW
;
1088 OBJECT_ATTRIBUTES attr
;
1090 attr
.Length
= sizeof(attr
);
1091 attr
.RootDirectory
= 0;
1092 attr
.ObjectName
= NULL
;
1093 attr
.Attributes
= OBJ_OPENIF
| ((sa
&& sa
->bInheritHandle
) ? OBJ_INHERIT
: 0);
1094 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
1095 attr
.SecurityQualityOfService
= NULL
;
1098 RtlInitUnicodeString( &nameW
, name
);
1099 attr
.ObjectName
= &nameW
;
1100 attr
.RootDirectory
= get_BaseNamedObjects_handle();
1103 status
= NtCreateTimer( &handle
, access
, &attr
,
1104 (flags
& CREATE_WAITABLE_TIMER_MANUAL_RESET
) ? NotificationTimer
: SynchronizationTimer
);
1105 if (status
== STATUS_OBJECT_NAME_EXISTS
)
1106 SetLastError( ERROR_ALREADY_EXISTS
);
1108 SetLastError( RtlNtStatusToDosError(status
) );
1113 /***********************************************************************
1114 * OpenWaitableTimerA (KERNEL32.@)
1116 HANDLE WINAPI
OpenWaitableTimerA( DWORD access
, BOOL inherit
, LPCSTR name
)
1118 WCHAR buffer
[MAX_PATH
];
1120 if (!name
) return OpenWaitableTimerW( access
, inherit
, NULL
);
1122 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
1124 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
1127 return OpenWaitableTimerW( access
, inherit
, buffer
);
1131 /***********************************************************************
1132 * OpenWaitableTimerW (KERNEL32.@)
1134 HANDLE WINAPI
OpenWaitableTimerW( DWORD access
, BOOL inherit
, LPCWSTR name
)
1137 UNICODE_STRING nameW
;
1138 OBJECT_ATTRIBUTES attr
;
1141 if (!is_version_nt()) access
= TIMER_ALL_ACCESS
;
1143 attr
.Length
= sizeof(attr
);
1144 attr
.RootDirectory
= 0;
1145 attr
.ObjectName
= NULL
;
1146 attr
.Attributes
= inherit
? OBJ_INHERIT
: 0;
1147 attr
.SecurityDescriptor
= NULL
;
1148 attr
.SecurityQualityOfService
= NULL
;
1151 RtlInitUnicodeString( &nameW
, name
);
1152 attr
.ObjectName
= &nameW
;
1153 attr
.RootDirectory
= get_BaseNamedObjects_handle();
1156 status
= NtOpenTimer(&handle
, access
, &attr
);
1157 if (status
!= STATUS_SUCCESS
)
1159 SetLastError( RtlNtStatusToDosError(status
) );
1166 /***********************************************************************
1167 * SetWaitableTimer (KERNEL32.@)
1169 BOOL WINAPI
SetWaitableTimer( HANDLE handle
, const LARGE_INTEGER
*when
, LONG period
,
1170 PTIMERAPCROUTINE callback
, LPVOID arg
, BOOL resume
)
1172 NTSTATUS status
= NtSetTimer(handle
, when
, (PTIMER_APC_ROUTINE
)callback
,
1173 arg
, resume
, period
, NULL
);
1175 if (status
!= STATUS_SUCCESS
)
1177 SetLastError( RtlNtStatusToDosError(status
) );
1178 if (status
!= STATUS_TIMER_RESUME_IGNORED
) return FALSE
;
1183 /***********************************************************************
1184 * SetWaitableTimerEx (KERNEL32.@)
1186 BOOL WINAPI
SetWaitableTimerEx( HANDLE handle
, const LARGE_INTEGER
*when
, LONG period
,
1187 PTIMERAPCROUTINE callback
, LPVOID arg
, REASON_CONTEXT
*context
, ULONG tolerabledelay
)
1192 FIXME("(%p, %p, %d, %p, %p, %p, %d) semi-stub\n",
1193 handle
, when
, period
, callback
, arg
, context
, tolerabledelay
);
1195 return SetWaitableTimer(handle
, when
, period
, callback
, arg
, FALSE
);
1198 /***********************************************************************
1199 * CancelWaitableTimer (KERNEL32.@)
1201 BOOL WINAPI
CancelWaitableTimer( HANDLE handle
)
1205 status
= NtCancelTimer(handle
, NULL
);
1206 if (status
!= STATUS_SUCCESS
)
1208 SetLastError( RtlNtStatusToDosError(status
) );
1215 /***********************************************************************
1216 * CreateTimerQueue (KERNEL32.@)
1218 HANDLE WINAPI
CreateTimerQueue(void)
1221 NTSTATUS status
= RtlCreateTimerQueue(&q
);
1223 if (status
!= STATUS_SUCCESS
)
1225 SetLastError( RtlNtStatusToDosError(status
) );
1233 /***********************************************************************
1234 * DeleteTimerQueueEx (KERNEL32.@)
1236 BOOL WINAPI
DeleteTimerQueueEx(HANDLE TimerQueue
, HANDLE CompletionEvent
)
1238 NTSTATUS status
= RtlDeleteTimerQueueEx(TimerQueue
, CompletionEvent
);
1240 if (status
!= STATUS_SUCCESS
)
1242 SetLastError( RtlNtStatusToDosError(status
) );
1249 /***********************************************************************
1250 * DeleteTimerQueue (KERNEL32.@)
1252 BOOL WINAPI
DeleteTimerQueue(HANDLE TimerQueue
)
1254 return DeleteTimerQueueEx(TimerQueue
, NULL
);
1257 /***********************************************************************
1258 * CreateTimerQueueTimer (KERNEL32.@)
1260 * Creates a timer-queue timer. This timer expires at the specified due
1261 * time (in ms), then after every specified period (in ms). When the timer
1262 * expires, the callback function is called.
1265 * nonzero on success or zero on failure
1267 BOOL WINAPI
CreateTimerQueueTimer( PHANDLE phNewTimer
, HANDLE TimerQueue
,
1268 WAITORTIMERCALLBACK Callback
, PVOID Parameter
,
1269 DWORD DueTime
, DWORD Period
, ULONG Flags
)
1271 NTSTATUS status
= RtlCreateTimer(phNewTimer
, TimerQueue
, Callback
,
1272 Parameter
, DueTime
, Period
, Flags
);
1274 if (status
!= STATUS_SUCCESS
)
1276 SetLastError( RtlNtStatusToDosError(status
) );
1283 /***********************************************************************
1284 * ChangeTimerQueueTimer (KERNEL32.@)
1286 * Changes the times at which the timer expires.
1289 * nonzero on success or zero on failure
1291 BOOL WINAPI
ChangeTimerQueueTimer( HANDLE TimerQueue
, HANDLE Timer
,
1292 ULONG DueTime
, ULONG Period
)
1294 NTSTATUS status
= RtlUpdateTimer(TimerQueue
, Timer
, DueTime
, Period
);
1296 if (status
!= STATUS_SUCCESS
)
1298 SetLastError( RtlNtStatusToDosError(status
) );
1305 /***********************************************************************
1306 * CancelTimerQueueTimer (KERNEL32.@)
1308 BOOL WINAPI
CancelTimerQueueTimer(HANDLE queue
, HANDLE timer
)
1310 FIXME("stub: %p %p\n", queue
, timer
);
1314 /***********************************************************************
1315 * DeleteTimerQueueTimer (KERNEL32.@)
1317 * Cancels a timer-queue timer.
1320 * nonzero on success or zero on failure
1322 BOOL WINAPI
DeleteTimerQueueTimer( HANDLE TimerQueue
, HANDLE Timer
,
1323 HANDLE CompletionEvent
)
1325 NTSTATUS status
= RtlDeleteTimer(TimerQueue
, Timer
, CompletionEvent
);
1326 if (status
!= STATUS_SUCCESS
)
1328 SetLastError( RtlNtStatusToDosError(status
) );
1340 /***********************************************************************
1341 * CreateNamedPipeA (KERNEL32.@)
1343 HANDLE WINAPI
CreateNamedPipeA( LPCSTR name
, DWORD dwOpenMode
,
1344 DWORD dwPipeMode
, DWORD nMaxInstances
,
1345 DWORD nOutBufferSize
, DWORD nInBufferSize
,
1346 DWORD nDefaultTimeOut
, LPSECURITY_ATTRIBUTES attr
)
1348 WCHAR buffer
[MAX_PATH
];
1350 if (!name
) return CreateNamedPipeW( NULL
, dwOpenMode
, dwPipeMode
, nMaxInstances
,
1351 nOutBufferSize
, nInBufferSize
, nDefaultTimeOut
, attr
);
1353 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
1355 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
1356 return INVALID_HANDLE_VALUE
;
1358 return CreateNamedPipeW( buffer
, dwOpenMode
, dwPipeMode
, nMaxInstances
,
1359 nOutBufferSize
, nInBufferSize
, nDefaultTimeOut
, attr
);
1363 /***********************************************************************
1364 * CreateNamedPipeW (KERNEL32.@)
1366 HANDLE WINAPI
CreateNamedPipeW( LPCWSTR name
, DWORD dwOpenMode
,
1367 DWORD dwPipeMode
, DWORD nMaxInstances
,
1368 DWORD nOutBufferSize
, DWORD nInBufferSize
,
1369 DWORD nDefaultTimeOut
, LPSECURITY_ATTRIBUTES sa
)
1372 UNICODE_STRING nt_name
;
1373 OBJECT_ATTRIBUTES attr
;
1374 DWORD access
, options
, sharing
;
1375 BOOLEAN pipe_type
, read_mode
, non_block
;
1377 IO_STATUS_BLOCK iosb
;
1378 LARGE_INTEGER timeout
;
1380 TRACE("(%s, %#08x, %#08x, %d, %d, %d, %d, %p)\n",
1381 debugstr_w(name
), dwOpenMode
, dwPipeMode
, nMaxInstances
,
1382 nOutBufferSize
, nInBufferSize
, nDefaultTimeOut
, sa
);
1384 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
1386 SetLastError( ERROR_PATH_NOT_FOUND
);
1387 return INVALID_HANDLE_VALUE
;
1389 if (nt_name
.Length
>= MAX_PATH
* sizeof(WCHAR
) )
1391 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
1392 RtlFreeUnicodeString( &nt_name
);
1393 return INVALID_HANDLE_VALUE
;
1396 attr
.Length
= sizeof(attr
);
1397 attr
.RootDirectory
= 0;
1398 attr
.ObjectName
= &nt_name
;
1399 attr
.Attributes
= OBJ_CASE_INSENSITIVE
|
1400 ((sa
&& sa
->bInheritHandle
) ? OBJ_INHERIT
: 0);
1401 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
1402 attr
.SecurityQualityOfService
= NULL
;
1404 switch(dwOpenMode
& 3)
1406 case PIPE_ACCESS_INBOUND
:
1407 sharing
= FILE_SHARE_WRITE
;
1408 access
= GENERIC_READ
;
1410 case PIPE_ACCESS_OUTBOUND
:
1411 sharing
= FILE_SHARE_READ
;
1412 access
= GENERIC_WRITE
;
1414 case PIPE_ACCESS_DUPLEX
:
1415 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1416 access
= GENERIC_READ
| GENERIC_WRITE
;
1419 SetLastError( ERROR_INVALID_PARAMETER
);
1420 return INVALID_HANDLE_VALUE
;
1422 access
|= SYNCHRONIZE
;
1424 if (dwOpenMode
& WRITE_DAC
) access
|= WRITE_DAC
;
1425 if (dwOpenMode
& WRITE_OWNER
) access
|= WRITE_OWNER
;
1426 if (dwOpenMode
& ACCESS_SYSTEM_SECURITY
) access
|= ACCESS_SYSTEM_SECURITY
;
1427 if (dwOpenMode
& FILE_FLAG_WRITE_THROUGH
) options
|= FILE_WRITE_THROUGH
;
1428 if (!(dwOpenMode
& FILE_FLAG_OVERLAPPED
)) options
|= FILE_SYNCHRONOUS_IO_NONALERT
;
1429 pipe_type
= (dwPipeMode
& PIPE_TYPE_MESSAGE
) != 0;
1430 read_mode
= (dwPipeMode
& PIPE_READMODE_MESSAGE
) != 0;
1431 non_block
= (dwPipeMode
& PIPE_NOWAIT
) != 0;
1432 if (nMaxInstances
>= PIPE_UNLIMITED_INSTANCES
) nMaxInstances
= ~0U;
1434 timeout
.QuadPart
= (ULONGLONG
)nDefaultTimeOut
* -10000;
1438 status
= NtCreateNamedPipeFile(&handle
, access
, &attr
, &iosb
, sharing
,
1439 FILE_OVERWRITE_IF
, options
, pipe_type
,
1440 read_mode
, non_block
, nMaxInstances
,
1441 nInBufferSize
, nOutBufferSize
, &timeout
);
1443 RtlFreeUnicodeString( &nt_name
);
1446 handle
= INVALID_HANDLE_VALUE
;
1447 SetLastError( RtlNtStatusToDosError(status
) );
1453 /***********************************************************************
1454 * PeekNamedPipe (KERNEL32.@)
1456 BOOL WINAPI
PeekNamedPipe( HANDLE hPipe
, LPVOID lpvBuffer
, DWORD cbBuffer
,
1457 LPDWORD lpcbRead
, LPDWORD lpcbAvail
, LPDWORD lpcbMessage
)
1459 FILE_PIPE_PEEK_BUFFER local_buffer
;
1460 FILE_PIPE_PEEK_BUFFER
*buffer
= &local_buffer
;
1464 if (cbBuffer
&& !(buffer
= HeapAlloc( GetProcessHeap(), 0,
1465 FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER
, Data
[cbBuffer
] ))))
1467 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1471 status
= NtFsControlFile( hPipe
, 0, NULL
, NULL
, &io
, FSCTL_PIPE_PEEK
, NULL
, 0,
1472 buffer
, FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER
, Data
[cbBuffer
] ) );
1475 ULONG read_size
= io
.Information
- FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER
, Data
);
1476 if (lpcbAvail
) *lpcbAvail
= buffer
->ReadDataAvailable
;
1477 if (lpcbRead
) *lpcbRead
= read_size
;
1478 if (lpcbMessage
) *lpcbMessage
= 0; /* FIXME */
1479 if (lpvBuffer
) memcpy( lpvBuffer
, buffer
->Data
, read_size
);
1481 else SetLastError( RtlNtStatusToDosError(status
) );
1483 if (buffer
!= &local_buffer
) HeapFree( GetProcessHeap(), 0, buffer
);
1487 /***********************************************************************
1488 * WaitNamedPipeA (KERNEL32.@)
1490 BOOL WINAPI
WaitNamedPipeA (LPCSTR name
, DWORD nTimeOut
)
1492 WCHAR buffer
[MAX_PATH
];
1494 if (!name
) return WaitNamedPipeW( NULL
, nTimeOut
);
1496 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
1498 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
1501 return WaitNamedPipeW( buffer
, nTimeOut
);
1505 /***********************************************************************
1506 * WaitNamedPipeW (KERNEL32.@)
1508 * Waits for a named pipe instance to become available
1511 * name [I] Pointer to a named pipe name to wait for
1512 * nTimeOut [I] How long to wait in ms
1515 * TRUE: Success, named pipe can be opened with CreateFile
1516 * FALSE: Failure, GetLastError can be called for further details
1518 BOOL WINAPI
WaitNamedPipeW (LPCWSTR name
, DWORD nTimeOut
)
1520 static const WCHAR leadin
[] = {'\\','?','?','\\','P','I','P','E','\\'};
1522 UNICODE_STRING nt_name
, pipe_dev_name
;
1523 FILE_PIPE_WAIT_FOR_BUFFER
*pipe_wait
;
1524 IO_STATUS_BLOCK iosb
;
1525 OBJECT_ATTRIBUTES attr
;
1529 TRACE("%s 0x%08x\n",debugstr_w(name
),nTimeOut
);
1531 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
1534 if (nt_name
.Length
>= MAX_PATH
* sizeof(WCHAR
) ||
1535 nt_name
.Length
< sizeof(leadin
) ||
1536 strncmpiW( nt_name
.Buffer
, leadin
, sizeof(leadin
)/sizeof(WCHAR
)) != 0)
1538 RtlFreeUnicodeString( &nt_name
);
1539 SetLastError( ERROR_PATH_NOT_FOUND
);
1543 sz_pipe_wait
= sizeof(*pipe_wait
) + nt_name
.Length
- sizeof(leadin
) - sizeof(WCHAR
);
1544 if (!(pipe_wait
= HeapAlloc( GetProcessHeap(), 0, sz_pipe_wait
)))
1546 RtlFreeUnicodeString( &nt_name
);
1547 SetLastError( ERROR_OUTOFMEMORY
);
1551 pipe_dev_name
.Buffer
= nt_name
.Buffer
;
1552 pipe_dev_name
.Length
= sizeof(leadin
);
1553 pipe_dev_name
.MaximumLength
= sizeof(leadin
);
1554 InitializeObjectAttributes(&attr
,&pipe_dev_name
, OBJ_CASE_INSENSITIVE
, NULL
, NULL
);
1555 status
= NtOpenFile( &pipe_dev
, FILE_READ_ATTRIBUTES
| SYNCHRONIZE
, &attr
,
1556 &iosb
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1557 FILE_SYNCHRONOUS_IO_NONALERT
);
1558 if (status
!= STATUS_SUCCESS
)
1560 HeapFree( GetProcessHeap(), 0, pipe_wait
);
1561 RtlFreeUnicodeString( &nt_name
);
1562 SetLastError( ERROR_PATH_NOT_FOUND
);
1566 pipe_wait
->TimeoutSpecified
= !(nTimeOut
== NMPWAIT_USE_DEFAULT_WAIT
);
1567 if (nTimeOut
== NMPWAIT_WAIT_FOREVER
)
1568 pipe_wait
->Timeout
.QuadPart
= ((ULONGLONG
)0x7fffffff << 32) | 0xffffffff;
1570 pipe_wait
->Timeout
.QuadPart
= (ULONGLONG
)nTimeOut
* -10000;
1571 pipe_wait
->NameLength
= nt_name
.Length
- sizeof(leadin
);
1572 memcpy(pipe_wait
->Name
, nt_name
.Buffer
+ sizeof(leadin
)/sizeof(WCHAR
),
1573 pipe_wait
->NameLength
);
1574 RtlFreeUnicodeString( &nt_name
);
1576 status
= NtFsControlFile( pipe_dev
, NULL
, NULL
, NULL
, &iosb
, FSCTL_PIPE_WAIT
,
1577 pipe_wait
, sz_pipe_wait
, NULL
, 0 );
1579 HeapFree( GetProcessHeap(), 0, pipe_wait
);
1580 NtClose( pipe_dev
);
1582 if(status
!= STATUS_SUCCESS
)
1584 SetLastError(RtlNtStatusToDosError(status
));
1592 /***********************************************************************
1593 * ConnectNamedPipe (KERNEL32.@)
1595 * Connects to a named pipe
1598 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1599 * overlapped: Optional OVERLAPPED struct
1603 * FALSE: Failure, GetLastError can be called for further details
1605 BOOL WINAPI
ConnectNamedPipe(HANDLE hPipe
, LPOVERLAPPED overlapped
)
1608 IO_STATUS_BLOCK status_block
;
1609 LPVOID cvalue
= NULL
;
1611 TRACE("(%p,%p)\n", hPipe
, overlapped
);
1615 overlapped
->Internal
= STATUS_PENDING
;
1616 overlapped
->InternalHigh
= 0;
1617 if (((ULONG_PTR
)overlapped
->hEvent
& 1) == 0) cvalue
= overlapped
;
1620 status
= NtFsControlFile(hPipe
, overlapped
? overlapped
->hEvent
: NULL
, NULL
, cvalue
,
1621 overlapped
? (IO_STATUS_BLOCK
*)overlapped
: &status_block
,
1622 FSCTL_PIPE_LISTEN
, NULL
, 0, NULL
, 0);
1624 if (status
== STATUS_SUCCESS
) return TRUE
;
1625 SetLastError( RtlNtStatusToDosError(status
) );
1629 /***********************************************************************
1630 * DisconnectNamedPipe (KERNEL32.@)
1632 * Disconnects from a named pipe
1635 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1639 * FALSE: Failure, GetLastError can be called for further details
1641 BOOL WINAPI
DisconnectNamedPipe(HANDLE hPipe
)
1644 IO_STATUS_BLOCK io_block
;
1646 TRACE("(%p)\n",hPipe
);
1648 status
= NtFsControlFile(hPipe
, 0, NULL
, NULL
, &io_block
, FSCTL_PIPE_DISCONNECT
,
1650 if (status
== STATUS_SUCCESS
) return TRUE
;
1651 SetLastError( RtlNtStatusToDosError(status
) );
1655 /***********************************************************************
1656 * TransactNamedPipe (KERNEL32.@)
1659 * should be done as a single operation in the wineserver or kernel
1661 BOOL WINAPI
TransactNamedPipe(
1662 HANDLE handle
, LPVOID write_buf
, DWORD write_size
, LPVOID read_buf
,
1663 DWORD read_size
, LPDWORD bytes_read
, LPOVERLAPPED overlapped
)
1668 TRACE("%p %p %d %p %d %p %p\n",
1669 handle
, write_buf
, write_size
, read_buf
,
1670 read_size
, bytes_read
, overlapped
);
1674 FIXME("Doesn't support overlapped operation as yet\n");
1678 r
= WriteFile(handle
, write_buf
, write_size
, &count
, NULL
);
1680 r
= ReadFile(handle
, read_buf
, read_size
, bytes_read
, NULL
);
1685 /***********************************************************************
1686 * GetNamedPipeInfo (KERNEL32.@)
1688 BOOL WINAPI
GetNamedPipeInfo(
1689 HANDLE hNamedPipe
, LPDWORD lpFlags
, LPDWORD lpOutputBufferSize
,
1690 LPDWORD lpInputBufferSize
, LPDWORD lpMaxInstances
)
1692 FILE_PIPE_LOCAL_INFORMATION fpli
;
1693 IO_STATUS_BLOCK iosb
;
1696 status
= NtQueryInformationFile(hNamedPipe
, &iosb
, &fpli
, sizeof(fpli
),
1697 FilePipeLocalInformation
);
1700 SetLastError( RtlNtStatusToDosError(status
) );
1706 *lpFlags
= (fpli
.NamedPipeEnd
& FILE_PIPE_SERVER_END
) ?
1707 PIPE_SERVER_END
: PIPE_CLIENT_END
;
1708 *lpFlags
|= (fpli
.NamedPipeType
& FILE_PIPE_TYPE_MESSAGE
) ?
1709 PIPE_TYPE_MESSAGE
: PIPE_TYPE_BYTE
;
1712 if (lpOutputBufferSize
) *lpOutputBufferSize
= fpli
.OutboundQuota
;
1713 if (lpInputBufferSize
) *lpInputBufferSize
= fpli
.InboundQuota
;
1714 if (lpMaxInstances
) *lpMaxInstances
= fpli
.MaximumInstances
;
1719 /***********************************************************************
1720 * GetNamedPipeHandleStateA (KERNEL32.@)
1722 BOOL WINAPI
GetNamedPipeHandleStateA(
1723 HANDLE hNamedPipe
, LPDWORD lpState
, LPDWORD lpCurInstances
,
1724 LPDWORD lpMaxCollectionCount
, LPDWORD lpCollectDataTimeout
,
1725 LPSTR lpUsername
, DWORD nUsernameMaxSize
)
1727 WARN("%p %p %p %p %p %p %d: semi-stub\n",
1728 hNamedPipe
, lpState
, lpCurInstances
,
1729 lpMaxCollectionCount
, lpCollectDataTimeout
,
1730 lpUsername
, nUsernameMaxSize
);
1732 if (lpUsername
&& nUsernameMaxSize
)
1735 return GetNamedPipeHandleStateW(hNamedPipe
, lpState
, lpCurInstances
,
1736 lpMaxCollectionCount
, lpCollectDataTimeout
, NULL
, 0);
1739 /***********************************************************************
1740 * GetNamedPipeHandleStateW (KERNEL32.@)
1742 BOOL WINAPI
GetNamedPipeHandleStateW(
1743 HANDLE hNamedPipe
, LPDWORD lpState
, LPDWORD lpCurInstances
,
1744 LPDWORD lpMaxCollectionCount
, LPDWORD lpCollectDataTimeout
,
1745 LPWSTR lpUsername
, DWORD nUsernameMaxSize
)
1747 IO_STATUS_BLOCK iosb
;
1750 FIXME("%p %p %p %p %p %p %d: semi-stub\n",
1751 hNamedPipe
, lpState
, lpCurInstances
,
1752 lpMaxCollectionCount
, lpCollectDataTimeout
,
1753 lpUsername
, nUsernameMaxSize
);
1755 if (lpMaxCollectionCount
)
1756 *lpMaxCollectionCount
= 0;
1758 if (lpCollectDataTimeout
)
1759 *lpCollectDataTimeout
= 0;
1761 if (lpUsername
&& nUsernameMaxSize
)
1766 FILE_PIPE_INFORMATION fpi
;
1767 status
= NtQueryInformationFile(hNamedPipe
, &iosb
, &fpi
, sizeof(fpi
),
1768 FilePipeInformation
);
1771 SetLastError( RtlNtStatusToDosError(status
) );
1775 *lpState
= (fpi
.ReadMode
? PIPE_READMODE_MESSAGE
: PIPE_READMODE_BYTE
) |
1776 (fpi
.CompletionMode
? PIPE_NOWAIT
: PIPE_WAIT
);
1781 FILE_PIPE_LOCAL_INFORMATION fpli
;
1782 status
= NtQueryInformationFile(hNamedPipe
, &iosb
, &fpli
, sizeof(fpli
),
1783 FilePipeLocalInformation
);
1786 SetLastError( RtlNtStatusToDosError(status
) );
1790 *lpCurInstances
= fpli
.CurrentInstances
;
1796 /***********************************************************************
1797 * SetNamedPipeHandleState (KERNEL32.@)
1799 BOOL WINAPI
SetNamedPipeHandleState(
1800 HANDLE hNamedPipe
, LPDWORD lpMode
, LPDWORD lpMaxCollectionCount
,
1801 LPDWORD lpCollectDataTimeout
)
1803 /* should be a fixme, but this function is called a lot by the RPC
1804 * runtime, and it slows down InstallShield a fair bit. */
1805 WARN("semi-stub: %p %p/%d %p %p\n",
1806 hNamedPipe
, lpMode
, lpMode
? *lpMode
: 0, lpMaxCollectionCount
, lpCollectDataTimeout
);
1810 FILE_PIPE_INFORMATION fpi
;
1811 IO_STATUS_BLOCK iosb
;
1814 if (*lpMode
& ~(PIPE_READMODE_MESSAGE
| PIPE_NOWAIT
))
1815 status
= STATUS_INVALID_PARAMETER
;
1818 fpi
.CompletionMode
= (*lpMode
& PIPE_NOWAIT
) ?
1819 FILE_PIPE_COMPLETE_OPERATION
: FILE_PIPE_QUEUE_OPERATION
;
1820 fpi
.ReadMode
= (*lpMode
& PIPE_READMODE_MESSAGE
) ?
1821 FILE_PIPE_MESSAGE_MODE
: FILE_PIPE_BYTE_STREAM_MODE
;
1822 status
= NtSetInformationFile(hNamedPipe
, &iosb
, &fpi
, sizeof(fpi
), FilePipeInformation
);
1827 SetLastError( RtlNtStatusToDosError(status
) );
1835 /***********************************************************************
1836 * CallNamedPipeA (KERNEL32.@)
1838 BOOL WINAPI
CallNamedPipeA(
1839 LPCSTR lpNamedPipeName
, LPVOID lpInput
, DWORD dwInputSize
,
1840 LPVOID lpOutput
, DWORD dwOutputSize
,
1841 LPDWORD lpBytesRead
, DWORD nTimeout
)
1847 TRACE("%s %p %d %p %d %p %d\n",
1848 debugstr_a(lpNamedPipeName
), lpInput
, dwInputSize
,
1849 lpOutput
, dwOutputSize
, lpBytesRead
, nTimeout
);
1851 if( lpNamedPipeName
)
1853 len
= MultiByteToWideChar( CP_ACP
, 0, lpNamedPipeName
, -1, NULL
, 0 );
1854 str
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
1855 MultiByteToWideChar( CP_ACP
, 0, lpNamedPipeName
, -1, str
, len
);
1857 ret
= CallNamedPipeW( str
, lpInput
, dwInputSize
, lpOutput
,
1858 dwOutputSize
, lpBytesRead
, nTimeout
);
1859 if( lpNamedPipeName
)
1860 HeapFree( GetProcessHeap(), 0, str
);
1865 /***********************************************************************
1866 * CallNamedPipeW (KERNEL32.@)
1868 BOOL WINAPI
CallNamedPipeW(
1869 LPCWSTR lpNamedPipeName
, LPVOID lpInput
, DWORD lpInputSize
,
1870 LPVOID lpOutput
, DWORD lpOutputSize
,
1871 LPDWORD lpBytesRead
, DWORD nTimeout
)
1877 TRACE("%s %p %d %p %d %p %d\n",
1878 debugstr_w(lpNamedPipeName
), lpInput
, lpInputSize
,
1879 lpOutput
, lpOutputSize
, lpBytesRead
, nTimeout
);
1881 pipe
= CreateFileW(lpNamedPipeName
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, NULL
);
1882 if (pipe
== INVALID_HANDLE_VALUE
)
1884 ret
= WaitNamedPipeW(lpNamedPipeName
, nTimeout
);
1887 pipe
= CreateFileW(lpNamedPipeName
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, NULL
);
1888 if (pipe
== INVALID_HANDLE_VALUE
)
1892 mode
= PIPE_READMODE_MESSAGE
;
1893 ret
= SetNamedPipeHandleState(pipe
, &mode
, NULL
, NULL
);
1900 ret
= TransactNamedPipe(pipe
, lpInput
, lpInputSize
, lpOutput
, lpOutputSize
, lpBytesRead
, NULL
);
1908 /******************************************************************
1909 * CreatePipe (KERNEL32.@)
1912 BOOL WINAPI
CreatePipe( PHANDLE hReadPipe
, PHANDLE hWritePipe
,
1913 LPSECURITY_ATTRIBUTES sa
, DWORD size
)
1915 static unsigned index
/* = 0 */;
1918 unsigned in_index
= index
;
1919 UNICODE_STRING nt_name
;
1920 OBJECT_ATTRIBUTES attr
;
1922 IO_STATUS_BLOCK iosb
;
1923 LARGE_INTEGER timeout
;
1925 *hReadPipe
= *hWritePipe
= INVALID_HANDLE_VALUE
;
1927 attr
.Length
= sizeof(attr
);
1928 attr
.RootDirectory
= 0;
1929 attr
.ObjectName
= &nt_name
;
1930 attr
.Attributes
= OBJ_CASE_INSENSITIVE
|
1931 ((sa
&& sa
->bInheritHandle
) ? OBJ_INHERIT
: 0);
1932 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
1933 attr
.SecurityQualityOfService
= NULL
;
1935 timeout
.QuadPart
= (ULONGLONG
)NMPWAIT_USE_DEFAULT_WAIT
* -10000;
1936 /* generate a unique pipe name (system wide) */
1939 static const WCHAR nameFmt
[] = { '\\','?','?','\\','p','i','p','e',
1940 '\\','W','i','n','3','2','.','P','i','p','e','s','.','%','0','8','l',
1941 'u','.','%','0','8','u','\0' };
1943 snprintfW(name
, sizeof(name
) / sizeof(name
[0]), nameFmt
,
1944 GetCurrentProcessId(), ++index
);
1945 RtlInitUnicodeString(&nt_name
, name
);
1946 status
= NtCreateNamedPipeFile(&hr
, GENERIC_READ
| SYNCHRONIZE
, &attr
, &iosb
,
1947 FILE_SHARE_WRITE
, FILE_OVERWRITE_IF
,
1948 FILE_SYNCHRONOUS_IO_NONALERT
,
1949 FALSE
, FALSE
, FALSE
,
1950 1, size
, size
, &timeout
);
1953 SetLastError( RtlNtStatusToDosError(status
) );
1954 hr
= INVALID_HANDLE_VALUE
;
1956 } while (hr
== INVALID_HANDLE_VALUE
&& index
!= in_index
);
1957 /* from completion sakeness, I think system resources might be exhausted before this happens !! */
1958 if (hr
== INVALID_HANDLE_VALUE
) return FALSE
;
1960 status
= NtOpenFile(&hw
, GENERIC_WRITE
| SYNCHRONIZE
, &attr
, &iosb
, 0,
1961 FILE_SYNCHRONOUS_IO_NONALERT
| FILE_NON_DIRECTORY_FILE
);
1965 SetLastError( RtlNtStatusToDosError(status
) );
1976 /******************************************************************************
1977 * CreateMailslotA [KERNEL32.@]
1979 * See CreateMailslotW.
1981 HANDLE WINAPI
CreateMailslotA( LPCSTR lpName
, DWORD nMaxMessageSize
,
1982 DWORD lReadTimeout
, LPSECURITY_ATTRIBUTES sa
)
1988 TRACE("%s %d %d %p\n", debugstr_a(lpName
),
1989 nMaxMessageSize
, lReadTimeout
, sa
);
1993 len
= MultiByteToWideChar( CP_ACP
, 0, lpName
, -1, NULL
, 0 );
1994 name
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
1995 MultiByteToWideChar( CP_ACP
, 0, lpName
, -1, name
, len
);
1998 handle
= CreateMailslotW( name
, nMaxMessageSize
, lReadTimeout
, sa
);
2000 HeapFree( GetProcessHeap(), 0, name
);
2006 /******************************************************************************
2007 * CreateMailslotW [KERNEL32.@]
2009 * Create a mailslot with specified name.
2012 * lpName [I] Pointer to string for mailslot name
2013 * nMaxMessageSize [I] Maximum message size
2014 * lReadTimeout [I] Milliseconds before read time-out
2015 * sa [I] Pointer to security structure
2018 * Success: Handle to mailslot
2019 * Failure: INVALID_HANDLE_VALUE
2021 HANDLE WINAPI
CreateMailslotW( LPCWSTR lpName
, DWORD nMaxMessageSize
,
2022 DWORD lReadTimeout
, LPSECURITY_ATTRIBUTES sa
)
2024 HANDLE handle
= INVALID_HANDLE_VALUE
;
2025 OBJECT_ATTRIBUTES attr
;
2026 UNICODE_STRING nameW
;
2027 LARGE_INTEGER timeout
;
2028 IO_STATUS_BLOCK iosb
;
2031 TRACE("%s %d %d %p\n", debugstr_w(lpName
),
2032 nMaxMessageSize
, lReadTimeout
, sa
);
2034 if (!RtlDosPathNameToNtPathName_U( lpName
, &nameW
, NULL
, NULL
))
2036 SetLastError( ERROR_PATH_NOT_FOUND
);
2037 return INVALID_HANDLE_VALUE
;
2040 if (nameW
.Length
>= MAX_PATH
* sizeof(WCHAR
) )
2042 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
2043 RtlFreeUnicodeString( &nameW
);
2044 return INVALID_HANDLE_VALUE
;
2047 attr
.Length
= sizeof(attr
);
2048 attr
.RootDirectory
= 0;
2049 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2050 attr
.ObjectName
= &nameW
;
2051 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
2052 attr
.SecurityQualityOfService
= NULL
;
2054 if (lReadTimeout
!= MAILSLOT_WAIT_FOREVER
)
2055 timeout
.QuadPart
= (ULONGLONG
) lReadTimeout
* -10000;
2057 timeout
.QuadPart
= ((LONGLONG
)0x7fffffff << 32) | 0xffffffff;
2059 status
= NtCreateMailslotFile( &handle
, GENERIC_READ
| SYNCHRONIZE
, &attr
,
2060 &iosb
, 0, 0, nMaxMessageSize
, &timeout
);
2063 SetLastError( RtlNtStatusToDosError(status
) );
2064 handle
= INVALID_HANDLE_VALUE
;
2067 RtlFreeUnicodeString( &nameW
);
2072 /******************************************************************************
2073 * GetMailslotInfo [KERNEL32.@]
2075 * Retrieve information about a mailslot.
2078 * hMailslot [I] Mailslot handle
2079 * lpMaxMessageSize [O] Address of maximum message size
2080 * lpNextSize [O] Address of size of next message
2081 * lpMessageCount [O] Address of number of messages
2082 * lpReadTimeout [O] Address of read time-out
2088 BOOL WINAPI
GetMailslotInfo( HANDLE hMailslot
, LPDWORD lpMaxMessageSize
,
2089 LPDWORD lpNextSize
, LPDWORD lpMessageCount
,
2090 LPDWORD lpReadTimeout
)
2092 FILE_MAILSLOT_QUERY_INFORMATION info
;
2093 IO_STATUS_BLOCK iosb
;
2096 TRACE("%p %p %p %p %p\n",hMailslot
, lpMaxMessageSize
,
2097 lpNextSize
, lpMessageCount
, lpReadTimeout
);
2099 status
= NtQueryInformationFile( hMailslot
, &iosb
, &info
, sizeof info
,
2100 FileMailslotQueryInformation
);
2102 if( status
!= STATUS_SUCCESS
)
2104 SetLastError( RtlNtStatusToDosError(status
) );
2108 if( lpMaxMessageSize
)
2109 *lpMaxMessageSize
= info
.MaximumMessageSize
;
2111 *lpNextSize
= info
.NextMessageSize
;
2112 if( lpMessageCount
)
2113 *lpMessageCount
= info
.MessagesAvailable
;
2116 if (info
.ReadTimeout
.QuadPart
== (((LONGLONG
)0x7fffffff << 32) | 0xffffffff))
2117 *lpReadTimeout
= MAILSLOT_WAIT_FOREVER
;
2119 *lpReadTimeout
= info
.ReadTimeout
.QuadPart
/ -10000;
2125 /******************************************************************************
2126 * SetMailslotInfo [KERNEL32.@]
2128 * Set the read timeout of a mailslot.
2131 * hMailslot [I] Mailslot handle
2132 * dwReadTimeout [I] Timeout in milliseconds.
2138 BOOL WINAPI
SetMailslotInfo( HANDLE hMailslot
, DWORD dwReadTimeout
)
2140 FILE_MAILSLOT_SET_INFORMATION info
;
2141 IO_STATUS_BLOCK iosb
;
2144 TRACE("%p %d\n", hMailslot
, dwReadTimeout
);
2146 if (dwReadTimeout
!= MAILSLOT_WAIT_FOREVER
)
2147 info
.ReadTimeout
.QuadPart
= (ULONGLONG
)dwReadTimeout
* -10000;
2149 info
.ReadTimeout
.QuadPart
= ((LONGLONG
)0x7fffffff << 32) | 0xffffffff;
2150 status
= NtSetInformationFile( hMailslot
, &iosb
, &info
, sizeof info
,
2151 FileMailslotSetInformation
);
2152 if( status
!= STATUS_SUCCESS
)
2154 SetLastError( RtlNtStatusToDosError(status
) );
2161 /******************************************************************************
2162 * CreateIoCompletionPort (KERNEL32.@)
2164 HANDLE WINAPI
CreateIoCompletionPort(HANDLE hFileHandle
, HANDLE hExistingCompletionPort
,
2165 ULONG_PTR CompletionKey
, DWORD dwNumberOfConcurrentThreads
)
2170 TRACE("(%p, %p, %08lx, %08x)\n",
2171 hFileHandle
, hExistingCompletionPort
, CompletionKey
, dwNumberOfConcurrentThreads
);
2173 if (hExistingCompletionPort
&& hFileHandle
== INVALID_HANDLE_VALUE
)
2175 SetLastError( ERROR_INVALID_PARAMETER
);
2179 if (hExistingCompletionPort
)
2180 ret
= hExistingCompletionPort
;
2183 status
= NtCreateIoCompletion( &ret
, IO_COMPLETION_ALL_ACCESS
, NULL
, dwNumberOfConcurrentThreads
);
2184 if (status
!= STATUS_SUCCESS
) goto fail
;
2187 if (hFileHandle
!= INVALID_HANDLE_VALUE
)
2189 FILE_COMPLETION_INFORMATION info
;
2190 IO_STATUS_BLOCK iosb
;
2192 info
.CompletionPort
= ret
;
2193 info
.CompletionKey
= CompletionKey
;
2194 status
= NtSetInformationFile( hFileHandle
, &iosb
, &info
, sizeof(info
), FileCompletionInformation
);
2195 if (status
!= STATUS_SUCCESS
) goto fail
;
2201 if (ret
&& !hExistingCompletionPort
)
2203 SetLastError( RtlNtStatusToDosError(status
) );
2207 /******************************************************************************
2208 * GetQueuedCompletionStatus (KERNEL32.@)
2210 BOOL WINAPI
GetQueuedCompletionStatus( HANDLE CompletionPort
, LPDWORD lpNumberOfBytesTransferred
,
2211 PULONG_PTR pCompletionKey
, LPOVERLAPPED
*lpOverlapped
,
2212 DWORD dwMilliseconds
)
2215 IO_STATUS_BLOCK iosb
;
2216 LARGE_INTEGER wait_time
;
2218 TRACE("(%p,%p,%p,%p,%d)\n",
2219 CompletionPort
,lpNumberOfBytesTransferred
,pCompletionKey
,lpOverlapped
,dwMilliseconds
);
2221 *lpOverlapped
= NULL
;
2223 status
= NtRemoveIoCompletion( CompletionPort
, pCompletionKey
, (PULONG_PTR
)lpOverlapped
,
2224 &iosb
, get_nt_timeout( &wait_time
, dwMilliseconds
) );
2225 if (status
== STATUS_SUCCESS
)
2227 *lpNumberOfBytesTransferred
= iosb
.Information
;
2228 if (iosb
.u
.Status
>= 0) return TRUE
;
2229 SetLastError( RtlNtStatusToDosError(iosb
.u
.Status
) );
2233 if (status
== STATUS_TIMEOUT
) SetLastError( WAIT_TIMEOUT
);
2234 else SetLastError( RtlNtStatusToDosError(status
) );
2239 /******************************************************************************
2240 * PostQueuedCompletionStatus (KERNEL32.@)
2242 BOOL WINAPI
PostQueuedCompletionStatus( HANDLE CompletionPort
, DWORD dwNumberOfBytes
,
2243 ULONG_PTR dwCompletionKey
, LPOVERLAPPED lpOverlapped
)
2247 TRACE("%p %d %08lx %p\n", CompletionPort
, dwNumberOfBytes
, dwCompletionKey
, lpOverlapped
);
2249 status
= NtSetIoCompletion( CompletionPort
, dwCompletionKey
, (ULONG_PTR
)lpOverlapped
,
2250 STATUS_SUCCESS
, dwNumberOfBytes
);
2252 if (status
== STATUS_SUCCESS
) return TRUE
;
2253 SetLastError( RtlNtStatusToDosError(status
) );
2257 /******************************************************************************
2258 * BindIoCompletionCallback (KERNEL32.@)
2260 BOOL WINAPI
BindIoCompletionCallback( HANDLE FileHandle
, LPOVERLAPPED_COMPLETION_ROUTINE Function
, ULONG Flags
)
2264 TRACE("(%p, %p, %d)\n", FileHandle
, Function
, Flags
);
2266 status
= RtlSetIoCompletionCallback( FileHandle
, (PRTL_OVERLAPPED_COMPLETION_ROUTINE
)Function
, Flags
);
2267 if (status
== STATUS_SUCCESS
) return TRUE
;
2268 SetLastError( RtlNtStatusToDosError(status
) );
2273 /***********************************************************************
2274 * CreateMemoryResourceNotification (KERNEL32.@)
2276 HANDLE WINAPI
CreateMemoryResourceNotification(MEMORY_RESOURCE_NOTIFICATION_TYPE type
)
2278 static const WCHAR lowmemW
[] =
2279 {'\\','K','e','r','n','e','l','O','b','j','e','c','t','s',
2280 '\\','L','o','w','M','e','m','o','r','y','C','o','n','d','i','t','i','o','n',0};
2281 static const WCHAR highmemW
[] =
2282 {'\\','K','e','r','n','e','l','O','b','j','e','c','t','s',
2283 '\\','H','i','g','h','M','e','m','o','r','y','C','o','n','d','i','t','i','o','n',0};
2285 UNICODE_STRING nameW
;
2286 OBJECT_ATTRIBUTES attr
;
2291 case LowMemoryResourceNotification
:
2292 RtlInitUnicodeString( &nameW
, lowmemW
);
2294 case HighMemoryResourceNotification
:
2295 RtlInitUnicodeString( &nameW
, highmemW
);
2298 SetLastError( ERROR_INVALID_PARAMETER
);
2302 attr
.Length
= sizeof(attr
);
2303 attr
.RootDirectory
= 0;
2304 attr
.ObjectName
= &nameW
;
2305 attr
.Attributes
= 0;
2306 attr
.SecurityDescriptor
= NULL
;
2307 attr
.SecurityQualityOfService
= NULL
;
2308 status
= NtOpenEvent( &ret
, EVENT_ALL_ACCESS
, &attr
);
2309 if (status
!= STATUS_SUCCESS
)
2311 SetLastError( RtlNtStatusToDosError(status
) );
2317 /***********************************************************************
2318 * QueryMemoryResourceNotification (KERNEL32.@)
2320 BOOL WINAPI
QueryMemoryResourceNotification(HANDLE handle
, PBOOL state
)
2322 switch (WaitForSingleObject( handle
, 0 ))
2331 SetLastError( ERROR_INVALID_PARAMETER
);
2335 /***********************************************************************
2336 * InitOnceBeginInitialize (KERNEL32.@)
2338 BOOL WINAPI
InitOnceBeginInitialize( INIT_ONCE
*once
, DWORD flags
, BOOL
*pending
, void **context
)
2340 NTSTATUS status
= RtlRunOnceBeginInitialize( once
, flags
, context
);
2341 if (status
>= 0) *pending
= (status
== STATUS_PENDING
);
2342 else SetLastError( RtlNtStatusToDosError(status
) );
2346 /***********************************************************************
2347 * InitOnceComplete (KERNEL32.@)
2349 BOOL WINAPI
InitOnceComplete( INIT_ONCE
*once
, DWORD flags
, void *context
)
2351 NTSTATUS status
= RtlRunOnceComplete( once
, flags
, context
);
2352 if (status
!= STATUS_SUCCESS
) SetLastError( RtlNtStatusToDosError(status
) );
2356 /***********************************************************************
2357 * InitOnceExecuteOnce (KERNEL32.@)
2359 BOOL WINAPI
InitOnceExecuteOnce( INIT_ONCE
*once
, PINIT_ONCE_FN func
, void *param
, void **context
)
2361 return !RtlRunOnceExecuteOnce( once
, (PRTL_RUN_ONCE_INIT_FN
)func
, param
, context
);
2366 /***********************************************************************
2367 * InterlockedCompareExchange (KERNEL32.@)
2369 /* LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare ); */
2370 __ASM_STDCALL_FUNC(InterlockedCompareExchange
, 12,
2371 "movl 12(%esp),%eax\n\t"
2372 "movl 8(%esp),%ecx\n\t"
2373 "movl 4(%esp),%edx\n\t"
2374 "lock; cmpxchgl %ecx,(%edx)\n\t"
2377 /***********************************************************************
2378 * InterlockedExchange (KERNEL32.@)
2380 /* LONG WINAPI InterlockedExchange( PLONG dest, LONG val ); */
2381 __ASM_STDCALL_FUNC(InterlockedExchange
, 8,
2382 "movl 8(%esp),%eax\n\t"
2383 "movl 4(%esp),%edx\n\t"
2384 "lock; xchgl %eax,(%edx)\n\t"
2387 /***********************************************************************
2388 * InterlockedExchangeAdd (KERNEL32.@)
2390 /* LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr ); */
2391 __ASM_STDCALL_FUNC(InterlockedExchangeAdd
, 8,
2392 "movl 8(%esp),%eax\n\t"
2393 "movl 4(%esp),%edx\n\t"
2394 "lock; xaddl %eax,(%edx)\n\t"
2397 /***********************************************************************
2398 * InterlockedIncrement (KERNEL32.@)
2400 /* LONG WINAPI InterlockedIncrement( PLONG dest ); */
2401 __ASM_STDCALL_FUNC(InterlockedIncrement
, 4,
2402 "movl 4(%esp),%edx\n\t"
2404 "lock; xaddl %eax,(%edx)\n\t"
2408 /***********************************************************************
2409 * InterlockedDecrement (KERNEL32.@)
2411 __ASM_STDCALL_FUNC(InterlockedDecrement
, 4,
2412 "movl 4(%esp),%edx\n\t"
2414 "lock; xaddl %eax,(%edx)\n\t"
2418 #endif /* __i386__ */
2420 /***********************************************************************
2421 * SleepConditionVariableCS (KERNEL32.@)
2423 BOOL WINAPI
SleepConditionVariableCS( CONDITION_VARIABLE
*variable
, CRITICAL_SECTION
*crit
, DWORD timeout
)
2428 status
= RtlSleepConditionVariableCS( variable
, crit
, get_nt_timeout( &time
, timeout
) );
2430 if (status
!= STATUS_SUCCESS
)
2432 SetLastError( RtlNtStatusToDosError(status
) );
2438 /***********************************************************************
2439 * SleepConditionVariableSRW (KERNEL32.@)
2441 BOOL WINAPI
SleepConditionVariableSRW( RTL_CONDITION_VARIABLE
*variable
, RTL_SRWLOCK
*lock
, DWORD timeout
, ULONG flags
)
2446 status
= RtlSleepConditionVariableSRW( variable
, lock
, get_nt_timeout( &time
, timeout
), flags
);
2448 if (status
!= STATUS_SUCCESS
)
2450 SetLastError( RtlNtStatusToDosError(status
) );