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/library.h"
44 #include "wine/unicode.h"
45 #include "kernel_private.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(sync
);
51 /* check if current version is NT or Win95 */
52 static inline BOOL
is_version_nt(void)
54 return !(GetVersion() & 0x80000000);
57 /* returns directory handle to \\BaseNamedObjects */
58 static HANDLE
get_BaseNamedObjects_handle(void)
60 static HANDLE handle
= NULL
;
61 static const WCHAR basenameW
[] = {'\\','S','e','s','s','i','o','n','s','\\','%','u',
62 '\\','B','a','s','e','N','a','m','e','d','O','b','j','e','c','t','s',0};
65 OBJECT_ATTRIBUTES attr
;
71 sprintfW( buffer
, basenameW
, NtCurrentTeb()->Peb
->SessionId
);
72 RtlInitUnicodeString( &str
, buffer
);
73 InitializeObjectAttributes(&attr
, &str
, 0, 0, NULL
);
74 NtOpenDirectoryObject(&dir
, DIRECTORY_CREATE_OBJECT
|DIRECTORY_TRAVERSE
,
76 if (InterlockedCompareExchangePointer( &handle
, dir
, 0 ) != 0)
78 /* someone beat us here... */
85 static void get_create_object_attributes( OBJECT_ATTRIBUTES
*attr
, UNICODE_STRING
*nameW
,
86 SECURITY_ATTRIBUTES
*sa
, const WCHAR
*name
)
88 attr
->Length
= sizeof(*attr
);
89 attr
->RootDirectory
= 0;
90 attr
->ObjectName
= NULL
;
91 attr
->Attributes
= OBJ_OPENIF
| ((sa
&& sa
->bInheritHandle
) ? OBJ_INHERIT
: 0);
92 attr
->SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
93 attr
->SecurityQualityOfService
= NULL
;
96 RtlInitUnicodeString( nameW
, name
);
97 attr
->ObjectName
= nameW
;
98 attr
->RootDirectory
= get_BaseNamedObjects_handle();
102 static BOOL
get_open_object_attributes( OBJECT_ATTRIBUTES
*attr
, UNICODE_STRING
*nameW
,
103 BOOL inherit
, const WCHAR
*name
)
107 SetLastError( ERROR_INVALID_PARAMETER
);
110 RtlInitUnicodeString( nameW
, name
);
111 InitializeObjectAttributes( attr
, nameW
, inherit
? OBJ_INHERIT
: 0,
112 get_BaseNamedObjects_handle(), NULL
);
116 /* helper for kernel32->ntdll timeout format conversion */
117 static inline PLARGE_INTEGER
get_nt_timeout( PLARGE_INTEGER pTime
, DWORD timeout
)
119 if (timeout
== INFINITE
) return NULL
;
120 pTime
->QuadPart
= (ULONGLONG
)timeout
* -10000;
124 /***********************************************************************
127 VOID WINAPI DECLSPEC_HOTPATCH
Sleep( DWORD timeout
)
129 SleepEx( timeout
, FALSE
);
132 /******************************************************************************
133 * SleepEx (KERNEL32.@)
135 DWORD WINAPI
SleepEx( DWORD timeout
, BOOL alertable
)
140 status
= NtDelayExecution( alertable
, get_nt_timeout( &time
, timeout
) );
141 if (status
== STATUS_USER_APC
) return WAIT_IO_COMPLETION
;
146 /***********************************************************************
147 * SwitchToThread (KERNEL32.@)
149 BOOL WINAPI
SwitchToThread(void)
151 return (NtYieldExecution() != STATUS_NO_YIELD_PERFORMED
);
155 /***********************************************************************
156 * WaitForSingleObject (KERNEL32.@)
158 DWORD WINAPI
WaitForSingleObject( HANDLE handle
, DWORD timeout
)
160 return WaitForMultipleObjectsEx( 1, &handle
, FALSE
, timeout
, FALSE
);
164 /***********************************************************************
165 * WaitForSingleObjectEx (KERNEL32.@)
167 DWORD WINAPI
WaitForSingleObjectEx( HANDLE handle
, DWORD timeout
,
170 return WaitForMultipleObjectsEx( 1, &handle
, FALSE
, timeout
, alertable
);
174 /***********************************************************************
175 * WaitForMultipleObjects (KERNEL32.@)
177 DWORD WINAPI
WaitForMultipleObjects( DWORD count
, const HANDLE
*handles
,
178 BOOL wait_all
, DWORD timeout
)
180 return WaitForMultipleObjectsEx( count
, handles
, wait_all
, timeout
, FALSE
);
183 static HANDLE
normalize_handle_if_console(HANDLE handle
)
185 if ((handle
== (HANDLE
)STD_INPUT_HANDLE
) ||
186 (handle
== (HANDLE
)STD_OUTPUT_HANDLE
) ||
187 (handle
== (HANDLE
)STD_ERROR_HANDLE
))
188 handle
= GetStdHandle( HandleToULong(handle
) );
190 /* yes, even screen buffer console handles are waitable, and are
191 * handled as a handle to the console itself !!
193 if (is_console_handle(handle
))
195 if (VerifyConsoleIoHandle(handle
))
196 handle
= GetConsoleInputWaitHandle();
201 /***********************************************************************
202 * WaitForMultipleObjectsEx (KERNEL32.@)
204 DWORD WINAPI
WaitForMultipleObjectsEx( DWORD count
, const HANDLE
*handles
,
205 BOOL wait_all
, DWORD timeout
,
209 HANDLE hloc
[MAXIMUM_WAIT_OBJECTS
];
213 if (count
> MAXIMUM_WAIT_OBJECTS
)
215 SetLastError(ERROR_INVALID_PARAMETER
);
218 for (i
= 0; i
< count
; i
++)
219 hloc
[i
] = normalize_handle_if_console(handles
[i
]);
221 status
= NtWaitForMultipleObjects( count
, hloc
, !wait_all
, alertable
,
222 get_nt_timeout( &time
, timeout
) );
224 if (HIWORD(status
)) /* is it an error code? */
226 SetLastError( RtlNtStatusToDosError(status
) );
227 status
= WAIT_FAILED
;
233 /***********************************************************************
234 * RegisterWaitForSingleObject (KERNEL32.@)
236 BOOL WINAPI
RegisterWaitForSingleObject(PHANDLE phNewWaitObject
, HANDLE hObject
,
237 WAITORTIMERCALLBACK Callback
, PVOID Context
,
238 ULONG dwMilliseconds
, ULONG dwFlags
)
242 TRACE("%p %p %p %p %d %d\n",
243 phNewWaitObject
,hObject
,Callback
,Context
,dwMilliseconds
,dwFlags
);
245 hObject
= normalize_handle_if_console(hObject
);
246 status
= RtlRegisterWait( phNewWaitObject
, hObject
, Callback
, Context
, dwMilliseconds
, dwFlags
);
247 if (status
!= STATUS_SUCCESS
)
249 SetLastError( RtlNtStatusToDosError(status
) );
255 /***********************************************************************
256 * RegisterWaitForSingleObjectEx (KERNEL32.@)
258 HANDLE WINAPI
RegisterWaitForSingleObjectEx( HANDLE hObject
,
259 WAITORTIMERCALLBACK Callback
, PVOID Context
,
260 ULONG dwMilliseconds
, ULONG dwFlags
)
263 HANDLE hNewWaitObject
;
265 TRACE("%p %p %p %d %d\n",
266 hObject
,Callback
,Context
,dwMilliseconds
,dwFlags
);
268 hObject
= normalize_handle_if_console(hObject
);
269 status
= RtlRegisterWait( &hNewWaitObject
, hObject
, Callback
, Context
, dwMilliseconds
, dwFlags
);
270 if (status
!= STATUS_SUCCESS
)
272 SetLastError( RtlNtStatusToDosError(status
) );
275 return hNewWaitObject
;
278 /***********************************************************************
279 * UnregisterWait (KERNEL32.@)
281 BOOL WINAPI
UnregisterWait( HANDLE WaitHandle
)
285 TRACE("%p\n",WaitHandle
);
287 status
= RtlDeregisterWait( WaitHandle
);
288 if (status
!= STATUS_SUCCESS
)
290 SetLastError( RtlNtStatusToDosError(status
) );
296 /***********************************************************************
297 * UnregisterWaitEx (KERNEL32.@)
299 BOOL WINAPI
UnregisterWaitEx( HANDLE WaitHandle
, HANDLE CompletionEvent
)
303 TRACE("%p %p\n",WaitHandle
, CompletionEvent
);
305 status
= RtlDeregisterWaitEx( WaitHandle
, CompletionEvent
);
306 if (status
!= STATUS_SUCCESS
) SetLastError( RtlNtStatusToDosError(status
) );
310 /***********************************************************************
311 * SignalObjectAndWait (KERNEL32.@)
313 * Makes it possible to atomically signal any of the synchronization
314 * objects (semaphore, mutex, event) and wait on another.
316 DWORD WINAPI
SignalObjectAndWait( HANDLE hObjectToSignal
, HANDLE hObjectToWaitOn
,
317 DWORD dwMilliseconds
, BOOL bAlertable
)
320 LARGE_INTEGER timeout
;
322 TRACE("%p %p %d %d\n", hObjectToSignal
,
323 hObjectToWaitOn
, dwMilliseconds
, bAlertable
);
325 status
= NtSignalAndWaitForSingleObject( hObjectToSignal
, hObjectToWaitOn
, bAlertable
,
326 get_nt_timeout( &timeout
, dwMilliseconds
) );
329 SetLastError( RtlNtStatusToDosError(status
) );
330 status
= WAIT_FAILED
;
335 /***********************************************************************
336 * InitializeCriticalSection (KERNEL32.@)
338 * Initialise a critical section before use.
341 * crit [O] Critical section to initialise.
344 * Nothing. If the function fails an exception is raised.
346 void WINAPI
InitializeCriticalSection( CRITICAL_SECTION
*crit
)
348 InitializeCriticalSectionEx( crit
, 0, 0 );
351 /***********************************************************************
352 * InitializeCriticalSectionAndSpinCount (KERNEL32.@)
354 * Initialise a critical section with a spin count.
357 * crit [O] Critical section to initialise.
358 * spincount [I] Number of times to spin upon contention.
362 * Failure: Nothing. If the function fails an exception is raised.
365 * spincount is ignored on uni-processor systems.
367 BOOL WINAPI
InitializeCriticalSectionAndSpinCount( CRITICAL_SECTION
*crit
, DWORD spincount
)
369 return InitializeCriticalSectionEx( crit
, spincount
, 0 );
372 /***********************************************************************
373 * InitializeCriticalSectionEx (KERNEL32.@)
375 * Initialise a critical section with a spin count and flags.
378 * crit [O] Critical section to initialise.
379 * spincount [I] Number of times to spin upon contention.
380 * flags [I] CRITICAL_SECTION_ flags from winbase.h.
384 * Failure: Nothing. If the function fails an exception is raised.
387 * spincount is ignored on uni-processor systems.
389 BOOL WINAPI
InitializeCriticalSectionEx( CRITICAL_SECTION
*crit
, DWORD spincount
, DWORD flags
)
391 NTSTATUS ret
= RtlInitializeCriticalSectionEx( crit
, spincount
, flags
);
392 if (ret
) RtlRaiseStatus( ret
);
396 /***********************************************************************
397 * MakeCriticalSectionGlobal (KERNEL32.@)
399 void WINAPI
MakeCriticalSectionGlobal( CRITICAL_SECTION
*crit
)
401 /* let's assume that only one thread at a time will try to do this */
402 HANDLE sem
= crit
->LockSemaphore
;
403 if (!sem
) NtCreateSemaphore( &sem
, SEMAPHORE_ALL_ACCESS
, NULL
, 0, 1 );
404 crit
->LockSemaphore
= ConvertToGlobalHandle( sem
);
405 RtlFreeHeap( GetProcessHeap(), 0, crit
->DebugInfo
);
406 crit
->DebugInfo
= NULL
;
410 /***********************************************************************
411 * ReinitializeCriticalSection (KERNEL32.@)
413 * Initialise an already used critical section.
416 * crit [O] Critical section to initialise.
421 void WINAPI
ReinitializeCriticalSection( CRITICAL_SECTION
*crit
)
423 if ( !crit
->LockSemaphore
)
424 RtlInitializeCriticalSection( crit
);
428 /***********************************************************************
429 * UninitializeCriticalSection (KERNEL32.@)
431 * UnInitialise a critical section after use.
434 * crit [O] Critical section to uninitialise (destroy).
439 void WINAPI
UninitializeCriticalSection( CRITICAL_SECTION
*crit
)
441 RtlDeleteCriticalSection( crit
);
445 /***********************************************************************
446 * CreateEventA (KERNEL32.@)
448 HANDLE WINAPI DECLSPEC_HOTPATCH
CreateEventA( SECURITY_ATTRIBUTES
*sa
, BOOL manual_reset
,
449 BOOL initial_state
, LPCSTR name
)
453 if (manual_reset
) flags
|= CREATE_EVENT_MANUAL_RESET
;
454 if (initial_state
) flags
|= CREATE_EVENT_INITIAL_SET
;
455 return CreateEventExA( sa
, name
, flags
, EVENT_ALL_ACCESS
);
459 /***********************************************************************
460 * CreateEventW (KERNEL32.@)
462 HANDLE WINAPI DECLSPEC_HOTPATCH
CreateEventW( SECURITY_ATTRIBUTES
*sa
, BOOL manual_reset
,
463 BOOL initial_state
, LPCWSTR name
)
467 if (manual_reset
) flags
|= CREATE_EVENT_MANUAL_RESET
;
468 if (initial_state
) flags
|= CREATE_EVENT_INITIAL_SET
;
469 return CreateEventExW( sa
, name
, flags
, EVENT_ALL_ACCESS
);
473 /***********************************************************************
474 * CreateEventExA (KERNEL32.@)
476 HANDLE WINAPI DECLSPEC_HOTPATCH
CreateEventExA( SECURITY_ATTRIBUTES
*sa
, LPCSTR name
, DWORD flags
, DWORD access
)
478 WCHAR buffer
[MAX_PATH
];
480 if (!name
) return CreateEventExW( sa
, NULL
, flags
, access
);
482 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
484 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
487 return CreateEventExW( sa
, buffer
, flags
, access
);
491 /***********************************************************************
492 * CreateEventExW (KERNEL32.@)
494 HANDLE WINAPI DECLSPEC_HOTPATCH
CreateEventExW( SECURITY_ATTRIBUTES
*sa
, LPCWSTR name
, DWORD flags
, DWORD access
)
497 UNICODE_STRING nameW
;
498 OBJECT_ATTRIBUTES attr
;
501 /* one buggy program needs this
502 * ("Van Dale Groot woordenboek der Nederlandse taal")
504 if (sa
&& IsBadReadPtr(sa
,sizeof(SECURITY_ATTRIBUTES
)))
506 ERR("Bad security attributes pointer %p\n",sa
);
507 SetLastError( ERROR_INVALID_PARAMETER
);
511 get_create_object_attributes( &attr
, &nameW
, sa
, name
);
513 status
= NtCreateEvent( &ret
, access
, &attr
,
514 (flags
& CREATE_EVENT_MANUAL_RESET
) ? NotificationEvent
: SynchronizationEvent
,
515 (flags
& CREATE_EVENT_INITIAL_SET
) != 0 );
516 if (status
== STATUS_OBJECT_NAME_EXISTS
)
517 SetLastError( ERROR_ALREADY_EXISTS
);
519 SetLastError( RtlNtStatusToDosError(status
) );
524 /***********************************************************************
525 * OpenEventA (KERNEL32.@)
527 HANDLE WINAPI DECLSPEC_HOTPATCH
OpenEventA( DWORD access
, BOOL inherit
, LPCSTR name
)
529 WCHAR buffer
[MAX_PATH
];
531 if (!name
) return OpenEventW( access
, inherit
, NULL
);
533 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
535 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
538 return OpenEventW( access
, inherit
, buffer
);
542 /***********************************************************************
543 * OpenEventW (KERNEL32.@)
545 HANDLE WINAPI DECLSPEC_HOTPATCH
OpenEventW( DWORD access
, BOOL inherit
, LPCWSTR name
)
548 UNICODE_STRING nameW
;
549 OBJECT_ATTRIBUTES attr
;
552 if (!is_version_nt()) access
= EVENT_ALL_ACCESS
;
554 if (!get_open_object_attributes( &attr
, &nameW
, inherit
, name
)) return 0;
556 status
= NtOpenEvent( &ret
, access
, &attr
);
557 if (status
!= STATUS_SUCCESS
)
559 SetLastError( RtlNtStatusToDosError(status
) );
565 /***********************************************************************
566 * PulseEvent (KERNEL32.@)
568 BOOL WINAPI DECLSPEC_HOTPATCH
PulseEvent( HANDLE handle
)
572 if ((status
= NtPulseEvent( handle
, NULL
)))
573 SetLastError( RtlNtStatusToDosError(status
) );
578 /***********************************************************************
579 * SetEvent (KERNEL32.@)
581 BOOL WINAPI DECLSPEC_HOTPATCH
SetEvent( HANDLE handle
)
585 if ((status
= NtSetEvent( handle
, NULL
)))
586 SetLastError( RtlNtStatusToDosError(status
) );
591 /***********************************************************************
592 * ResetEvent (KERNEL32.@)
594 BOOL WINAPI DECLSPEC_HOTPATCH
ResetEvent( HANDLE handle
)
598 if ((status
= NtResetEvent( handle
, NULL
)))
599 SetLastError( RtlNtStatusToDosError(status
) );
604 /***********************************************************************
605 * CreateMutexA (KERNEL32.@)
607 HANDLE WINAPI DECLSPEC_HOTPATCH
CreateMutexA( SECURITY_ATTRIBUTES
*sa
, BOOL owner
, LPCSTR name
)
609 return CreateMutexExA( sa
, name
, owner
? CREATE_MUTEX_INITIAL_OWNER
: 0, MUTEX_ALL_ACCESS
);
613 /***********************************************************************
614 * CreateMutexW (KERNEL32.@)
616 HANDLE WINAPI DECLSPEC_HOTPATCH
CreateMutexW( SECURITY_ATTRIBUTES
*sa
, BOOL owner
, LPCWSTR name
)
618 return CreateMutexExW( sa
, name
, owner
? CREATE_MUTEX_INITIAL_OWNER
: 0, MUTEX_ALL_ACCESS
);
622 /***********************************************************************
623 * CreateMutexExA (KERNEL32.@)
625 HANDLE WINAPI DECLSPEC_HOTPATCH
CreateMutexExA( SECURITY_ATTRIBUTES
*sa
, LPCSTR name
, DWORD flags
, DWORD access
)
630 if (!name
) return CreateMutexExW( sa
, NULL
, flags
, access
);
632 RtlInitAnsiString( &nameA
, name
);
633 status
= RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString
, &nameA
, FALSE
);
634 if (status
!= STATUS_SUCCESS
)
636 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
639 return CreateMutexExW( sa
, NtCurrentTeb()->StaticUnicodeString
.Buffer
, flags
, access
);
643 /***********************************************************************
644 * CreateMutexExW (KERNEL32.@)
646 HANDLE WINAPI DECLSPEC_HOTPATCH
CreateMutexExW( SECURITY_ATTRIBUTES
*sa
, LPCWSTR name
, DWORD flags
, DWORD access
)
649 UNICODE_STRING nameW
;
650 OBJECT_ATTRIBUTES attr
;
653 get_create_object_attributes( &attr
, &nameW
, sa
, name
);
655 status
= NtCreateMutant( &ret
, access
, &attr
, (flags
& CREATE_MUTEX_INITIAL_OWNER
) != 0 );
656 if (status
== STATUS_OBJECT_NAME_EXISTS
)
657 SetLastError( ERROR_ALREADY_EXISTS
);
659 SetLastError( RtlNtStatusToDosError(status
) );
664 /***********************************************************************
665 * OpenMutexA (KERNEL32.@)
667 HANDLE WINAPI DECLSPEC_HOTPATCH
OpenMutexA( DWORD access
, BOOL inherit
, LPCSTR name
)
669 WCHAR buffer
[MAX_PATH
];
671 if (!name
) return OpenMutexW( access
, inherit
, NULL
);
673 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
675 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
678 return OpenMutexW( access
, inherit
, buffer
);
682 /***********************************************************************
683 * OpenMutexW (KERNEL32.@)
685 HANDLE WINAPI DECLSPEC_HOTPATCH
OpenMutexW( DWORD access
, BOOL inherit
, LPCWSTR name
)
688 UNICODE_STRING nameW
;
689 OBJECT_ATTRIBUTES attr
;
692 if (!is_version_nt()) access
= MUTEX_ALL_ACCESS
;
694 if (!get_open_object_attributes( &attr
, &nameW
, inherit
, name
)) return 0;
696 status
= NtOpenMutant( &ret
, access
, &attr
);
697 if (status
!= STATUS_SUCCESS
)
699 SetLastError( RtlNtStatusToDosError(status
) );
706 /***********************************************************************
707 * ReleaseMutex (KERNEL32.@)
709 BOOL WINAPI DECLSPEC_HOTPATCH
ReleaseMutex( HANDLE handle
)
713 status
= NtReleaseMutant(handle
, NULL
);
714 if (status
!= STATUS_SUCCESS
)
716 SetLastError( RtlNtStatusToDosError(status
) );
728 /***********************************************************************
729 * CreateSemaphoreA (KERNEL32.@)
731 HANDLE WINAPI DECLSPEC_HOTPATCH
CreateSemaphoreA( SECURITY_ATTRIBUTES
*sa
, LONG initial
, LONG max
, LPCSTR name
)
733 return CreateSemaphoreExA( sa
, initial
, max
, name
, 0, SEMAPHORE_ALL_ACCESS
);
737 /***********************************************************************
738 * CreateSemaphoreW (KERNEL32.@)
740 HANDLE WINAPI DECLSPEC_HOTPATCH
CreateSemaphoreW( SECURITY_ATTRIBUTES
*sa
, LONG initial
, LONG max
, LPCWSTR name
)
742 return CreateSemaphoreExW( sa
, initial
, max
, name
, 0, SEMAPHORE_ALL_ACCESS
);
746 /***********************************************************************
747 * CreateSemaphoreExA (KERNEL32.@)
749 HANDLE WINAPI DECLSPEC_HOTPATCH
CreateSemaphoreExA( SECURITY_ATTRIBUTES
*sa
, LONG initial
, LONG max
,
750 LPCSTR name
, DWORD flags
, DWORD access
)
752 WCHAR buffer
[MAX_PATH
];
754 if (!name
) return CreateSemaphoreExW( sa
, initial
, max
, NULL
, flags
, access
);
756 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
758 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
761 return CreateSemaphoreExW( sa
, initial
, max
, buffer
, flags
, access
);
765 /***********************************************************************
766 * CreateSemaphoreExW (KERNEL32.@)
768 HANDLE WINAPI DECLSPEC_HOTPATCH
CreateSemaphoreExW( SECURITY_ATTRIBUTES
*sa
, LONG initial
, LONG max
,
769 LPCWSTR name
, DWORD flags
, DWORD access
)
772 UNICODE_STRING nameW
;
773 OBJECT_ATTRIBUTES attr
;
776 get_create_object_attributes( &attr
, &nameW
, sa
, name
);
778 status
= NtCreateSemaphore( &ret
, access
, &attr
, initial
, max
);
779 if (status
== STATUS_OBJECT_NAME_EXISTS
)
780 SetLastError( ERROR_ALREADY_EXISTS
);
782 SetLastError( RtlNtStatusToDosError(status
) );
787 /***********************************************************************
788 * OpenSemaphoreA (KERNEL32.@)
790 HANDLE WINAPI DECLSPEC_HOTPATCH
OpenSemaphoreA( DWORD access
, BOOL inherit
, LPCSTR name
)
792 WCHAR buffer
[MAX_PATH
];
794 if (!name
) return OpenSemaphoreW( access
, inherit
, NULL
);
796 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
798 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
801 return OpenSemaphoreW( access
, inherit
, buffer
);
805 /***********************************************************************
806 * OpenSemaphoreW (KERNEL32.@)
808 HANDLE WINAPI DECLSPEC_HOTPATCH
OpenSemaphoreW( DWORD access
, BOOL inherit
, LPCWSTR name
)
811 UNICODE_STRING nameW
;
812 OBJECT_ATTRIBUTES attr
;
815 if (!is_version_nt()) access
= SEMAPHORE_ALL_ACCESS
;
817 if (!get_open_object_attributes( &attr
, &nameW
, inherit
, name
)) return 0;
819 status
= NtOpenSemaphore( &ret
, access
, &attr
);
820 if (status
!= STATUS_SUCCESS
)
822 SetLastError( RtlNtStatusToDosError(status
) );
829 /***********************************************************************
830 * ReleaseSemaphore (KERNEL32.@)
832 BOOL WINAPI DECLSPEC_HOTPATCH
ReleaseSemaphore( HANDLE handle
, LONG count
, LONG
*previous
)
834 NTSTATUS status
= NtReleaseSemaphore( handle
, count
, (PULONG
)previous
);
835 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
844 /******************************************************************************
845 * CreateJobObjectW (KERNEL32.@)
847 HANDLE WINAPI
CreateJobObjectW( LPSECURITY_ATTRIBUTES sa
, LPCWSTR name
)
850 UNICODE_STRING nameW
;
851 OBJECT_ATTRIBUTES attr
;
854 get_create_object_attributes( &attr
, &nameW
, sa
, name
);
856 status
= NtCreateJobObject( &ret
, JOB_OBJECT_ALL_ACCESS
, &attr
);
857 if (status
== STATUS_OBJECT_NAME_EXISTS
)
858 SetLastError( ERROR_ALREADY_EXISTS
);
860 SetLastError( RtlNtStatusToDosError(status
) );
864 /******************************************************************************
865 * CreateJobObjectA (KERNEL32.@)
867 HANDLE WINAPI
CreateJobObjectA( LPSECURITY_ATTRIBUTES attr
, LPCSTR name
)
869 WCHAR buffer
[MAX_PATH
];
871 if (!name
) return CreateJobObjectW( attr
, NULL
);
873 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
875 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
878 return CreateJobObjectW( attr
, buffer
);
881 /******************************************************************************
882 * OpenJobObjectW (KERNEL32.@)
884 HANDLE WINAPI
OpenJobObjectW( DWORD access
, BOOL inherit
, LPCWSTR name
)
887 UNICODE_STRING nameW
;
888 OBJECT_ATTRIBUTES attr
;
891 if (!get_open_object_attributes( &attr
, &nameW
, inherit
, name
)) return 0;
893 status
= NtOpenJobObject( &ret
, access
, &attr
);
894 if (status
!= STATUS_SUCCESS
)
896 SetLastError( RtlNtStatusToDosError(status
) );
902 /******************************************************************************
903 * OpenJobObjectA (KERNEL32.@)
905 HANDLE WINAPI
OpenJobObjectA( DWORD access
, BOOL inherit
, LPCSTR name
)
907 WCHAR buffer
[MAX_PATH
];
909 if (!name
) return OpenJobObjectW( access
, inherit
, NULL
);
911 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
913 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
916 return OpenJobObjectW( access
, inherit
, buffer
);
919 /******************************************************************************
920 * TerminateJobObject (KERNEL32.@)
922 BOOL WINAPI
TerminateJobObject( HANDLE job
, UINT exit_code
)
924 NTSTATUS status
= NtTerminateJobObject( job
, exit_code
);
925 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
929 /******************************************************************************
930 * QueryInformationJobObject (KERNEL32.@)
932 BOOL WINAPI
QueryInformationJobObject( HANDLE job
, JOBOBJECTINFOCLASS
class, LPVOID info
,
933 DWORD len
, DWORD
*ret_len
)
935 NTSTATUS status
= NtQueryInformationJobObject( job
, class, info
, len
, ret_len
);
936 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
940 /******************************************************************************
941 * SetInformationJobObject (KERNEL32.@)
943 BOOL WINAPI
SetInformationJobObject( HANDLE job
, JOBOBJECTINFOCLASS
class, LPVOID info
, DWORD len
)
945 NTSTATUS status
= NtSetInformationJobObject( job
, class, info
, len
);
946 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
950 /******************************************************************************
951 * AssignProcessToJobObject (KERNEL32.@)
953 BOOL WINAPI
AssignProcessToJobObject( HANDLE job
, HANDLE process
)
955 NTSTATUS status
= NtAssignProcessToJobObject( job
, process
);
956 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
960 /******************************************************************************
961 * IsProcessInJob (KERNEL32.@)
963 BOOL WINAPI
IsProcessInJob( HANDLE process
, HANDLE job
, PBOOL result
)
965 NTSTATUS status
= NtIsProcessInJob( process
, job
);
968 case STATUS_PROCESS_IN_JOB
:
971 case STATUS_PROCESS_NOT_IN_JOB
:
975 SetLastError( RtlNtStatusToDosError(status
) );
986 /***********************************************************************
987 * CreateWaitableTimerA (KERNEL32.@)
989 HANDLE WINAPI
CreateWaitableTimerA( SECURITY_ATTRIBUTES
*sa
, BOOL manual
, LPCSTR name
)
991 return CreateWaitableTimerExA( sa
, name
, manual
? CREATE_WAITABLE_TIMER_MANUAL_RESET
: 0,
996 /***********************************************************************
997 * CreateWaitableTimerW (KERNEL32.@)
999 HANDLE WINAPI
CreateWaitableTimerW( SECURITY_ATTRIBUTES
*sa
, BOOL manual
, LPCWSTR name
)
1001 return CreateWaitableTimerExW( sa
, name
, manual
? CREATE_WAITABLE_TIMER_MANUAL_RESET
: 0,
1006 /***********************************************************************
1007 * CreateWaitableTimerExA (KERNEL32.@)
1009 HANDLE WINAPI
CreateWaitableTimerExA( SECURITY_ATTRIBUTES
*sa
, LPCSTR name
, DWORD flags
, DWORD access
)
1011 WCHAR buffer
[MAX_PATH
];
1013 if (!name
) return CreateWaitableTimerExW( sa
, NULL
, flags
, access
);
1015 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
1017 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
1020 return CreateWaitableTimerExW( sa
, buffer
, flags
, access
);
1024 /***********************************************************************
1025 * CreateWaitableTimerExW (KERNEL32.@)
1027 HANDLE WINAPI
CreateWaitableTimerExW( SECURITY_ATTRIBUTES
*sa
, LPCWSTR name
, DWORD flags
, DWORD access
)
1031 UNICODE_STRING nameW
;
1032 OBJECT_ATTRIBUTES attr
;
1034 get_create_object_attributes( &attr
, &nameW
, sa
, name
);
1036 status
= NtCreateTimer( &handle
, access
, &attr
,
1037 (flags
& CREATE_WAITABLE_TIMER_MANUAL_RESET
) ? NotificationTimer
: SynchronizationTimer
);
1038 if (status
== STATUS_OBJECT_NAME_EXISTS
)
1039 SetLastError( ERROR_ALREADY_EXISTS
);
1041 SetLastError( RtlNtStatusToDosError(status
) );
1046 /***********************************************************************
1047 * OpenWaitableTimerA (KERNEL32.@)
1049 HANDLE WINAPI
OpenWaitableTimerA( DWORD access
, BOOL inherit
, LPCSTR name
)
1051 WCHAR buffer
[MAX_PATH
];
1053 if (!name
) return OpenWaitableTimerW( access
, inherit
, NULL
);
1055 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
1057 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
1060 return OpenWaitableTimerW( access
, inherit
, buffer
);
1064 /***********************************************************************
1065 * OpenWaitableTimerW (KERNEL32.@)
1067 HANDLE WINAPI
OpenWaitableTimerW( DWORD access
, BOOL inherit
, LPCWSTR name
)
1070 UNICODE_STRING nameW
;
1071 OBJECT_ATTRIBUTES attr
;
1074 if (!is_version_nt()) access
= TIMER_ALL_ACCESS
;
1076 if (!get_open_object_attributes( &attr
, &nameW
, inherit
, name
)) return 0;
1078 status
= NtOpenTimer(&handle
, access
, &attr
);
1079 if (status
!= STATUS_SUCCESS
)
1081 SetLastError( RtlNtStatusToDosError(status
) );
1088 /***********************************************************************
1089 * SetWaitableTimer (KERNEL32.@)
1091 BOOL WINAPI
SetWaitableTimer( HANDLE handle
, const LARGE_INTEGER
*when
, LONG period
,
1092 PTIMERAPCROUTINE callback
, LPVOID arg
, BOOL resume
)
1094 NTSTATUS status
= NtSetTimer(handle
, when
, (PTIMER_APC_ROUTINE
)callback
,
1095 arg
, resume
, period
, NULL
);
1097 if (status
!= STATUS_SUCCESS
)
1099 SetLastError( RtlNtStatusToDosError(status
) );
1100 if (status
!= STATUS_TIMER_RESUME_IGNORED
) return FALSE
;
1105 /***********************************************************************
1106 * SetWaitableTimerEx (KERNEL32.@)
1108 BOOL WINAPI
SetWaitableTimerEx( HANDLE handle
, const LARGE_INTEGER
*when
, LONG period
,
1109 PTIMERAPCROUTINE callback
, LPVOID arg
, REASON_CONTEXT
*context
, ULONG tolerabledelay
)
1114 FIXME("(%p, %p, %d, %p, %p, %p, %d) semi-stub\n",
1115 handle
, when
, period
, callback
, arg
, context
, tolerabledelay
);
1117 return SetWaitableTimer(handle
, when
, period
, callback
, arg
, FALSE
);
1120 /***********************************************************************
1121 * CancelWaitableTimer (KERNEL32.@)
1123 BOOL WINAPI
CancelWaitableTimer( HANDLE handle
)
1127 status
= NtCancelTimer(handle
, NULL
);
1128 if (status
!= STATUS_SUCCESS
)
1130 SetLastError( RtlNtStatusToDosError(status
) );
1137 /***********************************************************************
1138 * CreateTimerQueue (KERNEL32.@)
1140 HANDLE WINAPI
CreateTimerQueue(void)
1143 NTSTATUS status
= RtlCreateTimerQueue(&q
);
1145 if (status
!= STATUS_SUCCESS
)
1147 SetLastError( RtlNtStatusToDosError(status
) );
1155 /***********************************************************************
1156 * DeleteTimerQueueEx (KERNEL32.@)
1158 BOOL WINAPI
DeleteTimerQueueEx(HANDLE TimerQueue
, HANDLE CompletionEvent
)
1160 NTSTATUS status
= RtlDeleteTimerQueueEx(TimerQueue
, CompletionEvent
);
1162 if (status
!= STATUS_SUCCESS
)
1164 SetLastError( RtlNtStatusToDosError(status
) );
1171 /***********************************************************************
1172 * DeleteTimerQueue (KERNEL32.@)
1174 BOOL WINAPI
DeleteTimerQueue(HANDLE TimerQueue
)
1176 return DeleteTimerQueueEx(TimerQueue
, NULL
);
1179 /***********************************************************************
1180 * CreateTimerQueueTimer (KERNEL32.@)
1182 * Creates a timer-queue timer. This timer expires at the specified due
1183 * time (in ms), then after every specified period (in ms). When the timer
1184 * expires, the callback function is called.
1187 * nonzero on success or zero on failure
1189 BOOL WINAPI
CreateTimerQueueTimer( PHANDLE phNewTimer
, HANDLE TimerQueue
,
1190 WAITORTIMERCALLBACK Callback
, PVOID Parameter
,
1191 DWORD DueTime
, DWORD Period
, ULONG Flags
)
1193 NTSTATUS status
= RtlCreateTimer(phNewTimer
, TimerQueue
, Callback
,
1194 Parameter
, DueTime
, Period
, Flags
);
1196 if (status
!= STATUS_SUCCESS
)
1198 SetLastError( RtlNtStatusToDosError(status
) );
1205 /***********************************************************************
1206 * ChangeTimerQueueTimer (KERNEL32.@)
1208 * Changes the times at which the timer expires.
1211 * nonzero on success or zero on failure
1213 BOOL WINAPI
ChangeTimerQueueTimer( HANDLE TimerQueue
, HANDLE Timer
,
1214 ULONG DueTime
, ULONG Period
)
1216 NTSTATUS status
= RtlUpdateTimer(TimerQueue
, Timer
, DueTime
, Period
);
1218 if (status
!= STATUS_SUCCESS
)
1220 SetLastError( RtlNtStatusToDosError(status
) );
1227 /***********************************************************************
1228 * CancelTimerQueueTimer (KERNEL32.@)
1230 BOOL WINAPI
CancelTimerQueueTimer(HANDLE queue
, HANDLE timer
)
1232 FIXME("stub: %p %p\n", queue
, timer
);
1236 /***********************************************************************
1237 * DeleteTimerQueueTimer (KERNEL32.@)
1239 * Cancels a timer-queue timer.
1242 * nonzero on success or zero on failure
1244 BOOL WINAPI
DeleteTimerQueueTimer( HANDLE TimerQueue
, HANDLE Timer
,
1245 HANDLE CompletionEvent
)
1247 NTSTATUS status
= RtlDeleteTimer(TimerQueue
, Timer
, CompletionEvent
);
1248 if (status
!= STATUS_SUCCESS
)
1250 SetLastError( RtlNtStatusToDosError(status
) );
1262 /***********************************************************************
1263 * CreateFileMappingA (KERNEL32.@)
1265 HANDLE WINAPI
CreateFileMappingA( HANDLE file
, SECURITY_ATTRIBUTES
*sa
, DWORD protect
,
1266 DWORD size_high
, DWORD size_low
, LPCSTR name
)
1268 WCHAR buffer
[MAX_PATH
];
1270 if (!name
) return CreateFileMappingW( file
, sa
, protect
, size_high
, size_low
, NULL
);
1272 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
1274 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
1277 return CreateFileMappingW( file
, sa
, protect
, size_high
, size_low
, buffer
);
1281 /***********************************************************************
1282 * CreateFileMappingW (KERNEL32.@)
1284 HANDLE WINAPI
CreateFileMappingW( HANDLE file
, LPSECURITY_ATTRIBUTES sa
, DWORD protect
,
1285 DWORD size_high
, DWORD size_low
, LPCWSTR name
)
1287 static const int sec_flags
= (SEC_FILE
| SEC_IMAGE
| SEC_RESERVE
| SEC_COMMIT
|
1288 SEC_NOCACHE
| SEC_WRITECOMBINE
| SEC_LARGE_PAGES
);
1291 DWORD access
, sec_type
;
1293 UNICODE_STRING nameW
;
1294 OBJECT_ATTRIBUTES attr
;
1296 sec_type
= protect
& sec_flags
;
1297 protect
&= ~sec_flags
;
1298 if (!sec_type
) sec_type
= SEC_COMMIT
;
1300 /* Win9x compatibility */
1301 if (!protect
&& !is_version_nt()) protect
= PAGE_READONLY
;
1306 case PAGE_WRITECOPY
:
1307 access
= STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
;
1309 case PAGE_READWRITE
:
1310 access
= STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_WRITE
;
1312 case PAGE_EXECUTE_READ
:
1313 case PAGE_EXECUTE_WRITECOPY
:
1314 access
= STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_EXECUTE
;
1316 case PAGE_EXECUTE_READWRITE
:
1317 access
= STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_WRITE
| SECTION_MAP_EXECUTE
;
1320 SetLastError( ERROR_INVALID_PARAMETER
);
1324 size
.u
.LowPart
= size_low
;
1325 size
.u
.HighPart
= size_high
;
1327 if (file
== INVALID_HANDLE_VALUE
)
1332 SetLastError( ERROR_INVALID_PARAMETER
);
1337 get_create_object_attributes( &attr
, &nameW
, sa
, name
);
1339 status
= NtCreateSection( &ret
, access
, &attr
, &size
, protect
, sec_type
, file
);
1340 if (status
== STATUS_OBJECT_NAME_EXISTS
)
1341 SetLastError( ERROR_ALREADY_EXISTS
);
1343 SetLastError( RtlNtStatusToDosError(status
) );
1348 /***********************************************************************
1349 * OpenFileMappingA (KERNEL32.@)
1351 HANDLE WINAPI
OpenFileMappingA( DWORD access
, BOOL inherit
, LPCSTR name
)
1353 WCHAR buffer
[MAX_PATH
];
1355 if (!name
) return OpenFileMappingW( access
, inherit
, NULL
);
1357 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
1359 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
1362 return OpenFileMappingW( access
, inherit
, buffer
);
1366 /***********************************************************************
1367 * OpenFileMappingW (KERNEL32.@)
1369 HANDLE WINAPI
OpenFileMappingW( DWORD access
, BOOL inherit
, LPCWSTR name
)
1371 OBJECT_ATTRIBUTES attr
;
1372 UNICODE_STRING nameW
;
1376 if (!get_open_object_attributes( &attr
, &nameW
, inherit
, name
)) return 0;
1378 if (access
== FILE_MAP_COPY
) access
= SECTION_MAP_READ
;
1380 if (!is_version_nt())
1382 /* win9x doesn't do access checks, so try with full access first */
1383 if (!NtOpenSection( &ret
, access
| SECTION_MAP_READ
| SECTION_MAP_WRITE
, &attr
)) return ret
;
1386 status
= NtOpenSection( &ret
, access
, &attr
);
1387 if (status
!= STATUS_SUCCESS
)
1389 SetLastError( RtlNtStatusToDosError(status
) );
1401 /***********************************************************************
1402 * CreateNamedPipeA (KERNEL32.@)
1404 HANDLE WINAPI
CreateNamedPipeA( LPCSTR name
, DWORD dwOpenMode
,
1405 DWORD dwPipeMode
, DWORD nMaxInstances
,
1406 DWORD nOutBufferSize
, DWORD nInBufferSize
,
1407 DWORD nDefaultTimeOut
, LPSECURITY_ATTRIBUTES attr
)
1409 WCHAR buffer
[MAX_PATH
];
1411 if (!name
) return CreateNamedPipeW( NULL
, dwOpenMode
, dwPipeMode
, nMaxInstances
,
1412 nOutBufferSize
, nInBufferSize
, nDefaultTimeOut
, attr
);
1414 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
1416 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
1417 return INVALID_HANDLE_VALUE
;
1419 return CreateNamedPipeW( buffer
, dwOpenMode
, dwPipeMode
, nMaxInstances
,
1420 nOutBufferSize
, nInBufferSize
, nDefaultTimeOut
, attr
);
1424 /***********************************************************************
1425 * CreateNamedPipeW (KERNEL32.@)
1427 HANDLE WINAPI
CreateNamedPipeW( LPCWSTR name
, DWORD dwOpenMode
,
1428 DWORD dwPipeMode
, DWORD nMaxInstances
,
1429 DWORD nOutBufferSize
, DWORD nInBufferSize
,
1430 DWORD nDefaultTimeOut
, LPSECURITY_ATTRIBUTES sa
)
1433 UNICODE_STRING nt_name
;
1434 OBJECT_ATTRIBUTES attr
;
1435 DWORD access
, options
, sharing
;
1436 BOOLEAN pipe_type
, read_mode
, non_block
;
1438 IO_STATUS_BLOCK iosb
;
1439 LARGE_INTEGER timeout
;
1441 TRACE("(%s, %#08x, %#08x, %d, %d, %d, %d, %p)\n",
1442 debugstr_w(name
), dwOpenMode
, dwPipeMode
, nMaxInstances
,
1443 nOutBufferSize
, nInBufferSize
, nDefaultTimeOut
, sa
);
1445 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
1447 SetLastError( ERROR_PATH_NOT_FOUND
);
1448 return INVALID_HANDLE_VALUE
;
1450 if (nt_name
.Length
>= MAX_PATH
* sizeof(WCHAR
) )
1452 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
1453 RtlFreeUnicodeString( &nt_name
);
1454 return INVALID_HANDLE_VALUE
;
1457 attr
.Length
= sizeof(attr
);
1458 attr
.RootDirectory
= 0;
1459 attr
.ObjectName
= &nt_name
;
1460 attr
.Attributes
= OBJ_CASE_INSENSITIVE
|
1461 ((sa
&& sa
->bInheritHandle
) ? OBJ_INHERIT
: 0);
1462 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
1463 attr
.SecurityQualityOfService
= NULL
;
1465 switch(dwOpenMode
& 3)
1467 case PIPE_ACCESS_INBOUND
:
1468 sharing
= FILE_SHARE_WRITE
;
1469 access
= GENERIC_READ
;
1471 case PIPE_ACCESS_OUTBOUND
:
1472 sharing
= FILE_SHARE_READ
;
1473 access
= GENERIC_WRITE
;
1475 case PIPE_ACCESS_DUPLEX
:
1476 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1477 access
= GENERIC_READ
| GENERIC_WRITE
;
1480 SetLastError( ERROR_INVALID_PARAMETER
);
1481 return INVALID_HANDLE_VALUE
;
1483 access
|= SYNCHRONIZE
;
1485 if (dwOpenMode
& WRITE_DAC
) access
|= WRITE_DAC
;
1486 if (dwOpenMode
& WRITE_OWNER
) access
|= WRITE_OWNER
;
1487 if (dwOpenMode
& ACCESS_SYSTEM_SECURITY
) access
|= ACCESS_SYSTEM_SECURITY
;
1488 if (dwOpenMode
& FILE_FLAG_WRITE_THROUGH
) options
|= FILE_WRITE_THROUGH
;
1489 if (!(dwOpenMode
& FILE_FLAG_OVERLAPPED
)) options
|= FILE_SYNCHRONOUS_IO_NONALERT
;
1490 pipe_type
= (dwPipeMode
& PIPE_TYPE_MESSAGE
) != 0;
1491 read_mode
= (dwPipeMode
& PIPE_READMODE_MESSAGE
) != 0;
1492 non_block
= (dwPipeMode
& PIPE_NOWAIT
) != 0;
1493 if (nMaxInstances
>= PIPE_UNLIMITED_INSTANCES
) nMaxInstances
= ~0U;
1495 timeout
.QuadPart
= (ULONGLONG
)nDefaultTimeOut
* -10000;
1499 status
= NtCreateNamedPipeFile(&handle
, access
, &attr
, &iosb
, sharing
,
1500 FILE_OVERWRITE_IF
, options
, pipe_type
,
1501 read_mode
, non_block
, nMaxInstances
,
1502 nInBufferSize
, nOutBufferSize
, &timeout
);
1504 RtlFreeUnicodeString( &nt_name
);
1507 handle
= INVALID_HANDLE_VALUE
;
1508 SetLastError( RtlNtStatusToDosError(status
) );
1514 /***********************************************************************
1515 * PeekNamedPipe (KERNEL32.@)
1517 BOOL WINAPI
PeekNamedPipe( HANDLE hPipe
, LPVOID lpvBuffer
, DWORD cbBuffer
,
1518 LPDWORD lpcbRead
, LPDWORD lpcbAvail
, LPDWORD lpcbMessage
)
1520 FILE_PIPE_PEEK_BUFFER local_buffer
;
1521 FILE_PIPE_PEEK_BUFFER
*buffer
= &local_buffer
;
1525 if (cbBuffer
&& !(buffer
= HeapAlloc( GetProcessHeap(), 0,
1526 FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER
, Data
[cbBuffer
] ))))
1528 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1532 status
= NtFsControlFile( hPipe
, 0, NULL
, NULL
, &io
, FSCTL_PIPE_PEEK
, NULL
, 0,
1533 buffer
, FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER
, Data
[cbBuffer
] ) );
1536 ULONG read_size
= io
.Information
- FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER
, Data
);
1537 if (lpcbAvail
) *lpcbAvail
= buffer
->ReadDataAvailable
;
1538 if (lpcbRead
) *lpcbRead
= read_size
;
1539 if (lpcbMessage
) *lpcbMessage
= buffer
->MessageLength
- read_size
;
1540 if (lpvBuffer
) memcpy( lpvBuffer
, buffer
->Data
, read_size
);
1542 else SetLastError( RtlNtStatusToDosError(status
) );
1544 if (buffer
!= &local_buffer
) HeapFree( GetProcessHeap(), 0, buffer
);
1548 /***********************************************************************
1549 * WaitNamedPipeA (KERNEL32.@)
1551 BOOL WINAPI
WaitNamedPipeA (LPCSTR name
, DWORD nTimeOut
)
1553 WCHAR buffer
[MAX_PATH
];
1555 if (!name
) return WaitNamedPipeW( NULL
, nTimeOut
);
1557 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
1559 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
1562 return WaitNamedPipeW( buffer
, nTimeOut
);
1566 /***********************************************************************
1567 * WaitNamedPipeW (KERNEL32.@)
1569 * Waits for a named pipe instance to become available
1572 * name [I] Pointer to a named pipe name to wait for
1573 * nTimeOut [I] How long to wait in ms
1576 * TRUE: Success, named pipe can be opened with CreateFile
1577 * FALSE: Failure, GetLastError can be called for further details
1579 BOOL WINAPI
WaitNamedPipeW (LPCWSTR name
, DWORD nTimeOut
)
1581 static const WCHAR leadin
[] = {'\\','?','?','\\','P','I','P','E','\\'};
1583 UNICODE_STRING nt_name
, pipe_dev_name
;
1584 FILE_PIPE_WAIT_FOR_BUFFER
*pipe_wait
;
1585 IO_STATUS_BLOCK iosb
;
1586 OBJECT_ATTRIBUTES attr
;
1590 TRACE("%s 0x%08x\n",debugstr_w(name
),nTimeOut
);
1592 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
1595 if (nt_name
.Length
>= MAX_PATH
* sizeof(WCHAR
) ||
1596 nt_name
.Length
< sizeof(leadin
) ||
1597 strncmpiW( nt_name
.Buffer
, leadin
, sizeof(leadin
)/sizeof(WCHAR
)) != 0)
1599 RtlFreeUnicodeString( &nt_name
);
1600 SetLastError( ERROR_PATH_NOT_FOUND
);
1604 sz_pipe_wait
= sizeof(*pipe_wait
) + nt_name
.Length
- sizeof(leadin
) - sizeof(WCHAR
);
1605 if (!(pipe_wait
= HeapAlloc( GetProcessHeap(), 0, sz_pipe_wait
)))
1607 RtlFreeUnicodeString( &nt_name
);
1608 SetLastError( ERROR_OUTOFMEMORY
);
1612 pipe_dev_name
.Buffer
= nt_name
.Buffer
;
1613 pipe_dev_name
.Length
= sizeof(leadin
);
1614 pipe_dev_name
.MaximumLength
= sizeof(leadin
);
1615 InitializeObjectAttributes(&attr
,&pipe_dev_name
, OBJ_CASE_INSENSITIVE
, NULL
, NULL
);
1616 status
= NtOpenFile( &pipe_dev
, FILE_READ_ATTRIBUTES
| SYNCHRONIZE
, &attr
,
1617 &iosb
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1618 FILE_SYNCHRONOUS_IO_NONALERT
);
1619 if (status
!= STATUS_SUCCESS
)
1621 HeapFree( GetProcessHeap(), 0, pipe_wait
);
1622 RtlFreeUnicodeString( &nt_name
);
1623 SetLastError( ERROR_PATH_NOT_FOUND
);
1627 pipe_wait
->TimeoutSpecified
= !(nTimeOut
== NMPWAIT_USE_DEFAULT_WAIT
);
1628 if (nTimeOut
== NMPWAIT_WAIT_FOREVER
)
1629 pipe_wait
->Timeout
.QuadPart
= ((ULONGLONG
)0x7fffffff << 32) | 0xffffffff;
1631 pipe_wait
->Timeout
.QuadPart
= (ULONGLONG
)nTimeOut
* -10000;
1632 pipe_wait
->NameLength
= nt_name
.Length
- sizeof(leadin
);
1633 memcpy(pipe_wait
->Name
, nt_name
.Buffer
+ sizeof(leadin
)/sizeof(WCHAR
),
1634 pipe_wait
->NameLength
);
1635 RtlFreeUnicodeString( &nt_name
);
1637 status
= NtFsControlFile( pipe_dev
, NULL
, NULL
, NULL
, &iosb
, FSCTL_PIPE_WAIT
,
1638 pipe_wait
, sz_pipe_wait
, NULL
, 0 );
1640 HeapFree( GetProcessHeap(), 0, pipe_wait
);
1641 NtClose( pipe_dev
);
1643 if(status
!= STATUS_SUCCESS
)
1645 SetLastError(RtlNtStatusToDosError(status
));
1653 /***********************************************************************
1654 * ConnectNamedPipe (KERNEL32.@)
1656 * Connects to a named pipe
1659 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1660 * overlapped: Optional OVERLAPPED struct
1664 * FALSE: Failure, GetLastError can be called for further details
1666 BOOL WINAPI
ConnectNamedPipe(HANDLE hPipe
, LPOVERLAPPED overlapped
)
1669 IO_STATUS_BLOCK status_block
;
1670 LPVOID cvalue
= NULL
;
1672 TRACE("(%p,%p)\n", hPipe
, overlapped
);
1676 overlapped
->Internal
= STATUS_PENDING
;
1677 overlapped
->InternalHigh
= 0;
1678 if (((ULONG_PTR
)overlapped
->hEvent
& 1) == 0) cvalue
= overlapped
;
1681 status
= NtFsControlFile(hPipe
, overlapped
? overlapped
->hEvent
: NULL
, NULL
, cvalue
,
1682 overlapped
? (IO_STATUS_BLOCK
*)overlapped
: &status_block
,
1683 FSCTL_PIPE_LISTEN
, NULL
, 0, NULL
, 0);
1685 if (status
== STATUS_SUCCESS
) return TRUE
;
1686 SetLastError( RtlNtStatusToDosError(status
) );
1690 /***********************************************************************
1691 * DisconnectNamedPipe (KERNEL32.@)
1693 * Disconnects from a named pipe
1696 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1700 * FALSE: Failure, GetLastError can be called for further details
1702 BOOL WINAPI
DisconnectNamedPipe(HANDLE hPipe
)
1705 IO_STATUS_BLOCK io_block
;
1707 TRACE("(%p)\n",hPipe
);
1709 status
= NtFsControlFile(hPipe
, 0, NULL
, NULL
, &io_block
, FSCTL_PIPE_DISCONNECT
,
1711 if (status
== STATUS_SUCCESS
) return TRUE
;
1712 SetLastError( RtlNtStatusToDosError(status
) );
1716 /***********************************************************************
1717 * TransactNamedPipe (KERNEL32.@)
1719 BOOL WINAPI
TransactNamedPipe(
1720 HANDLE handle
, LPVOID write_buf
, DWORD write_size
, LPVOID read_buf
,
1721 DWORD read_size
, LPDWORD bytes_read
, LPOVERLAPPED overlapped
)
1723 IO_STATUS_BLOCK default_iosb
, *iosb
= &default_iosb
;
1724 HANDLE event
= NULL
;
1725 void *cvalue
= NULL
;
1728 TRACE("%p %p %u %p %u %p %p\n", handle
, write_buf
, write_size
, read_buf
,
1729 read_size
, bytes_read
, overlapped
);
1733 event
= overlapped
->hEvent
;
1734 iosb
= (IO_STATUS_BLOCK
*)overlapped
;
1735 if (((ULONG_PTR
)event
& 1) == 0) cvalue
= overlapped
;
1739 iosb
->Information
= 0;
1742 status
= NtFsControlFile(handle
, event
, NULL
, cvalue
, iosb
, FSCTL_PIPE_TRANSCEIVE
,
1743 write_buf
, write_size
, read_buf
, read_size
);
1745 if (bytes_read
) *bytes_read
= overlapped
&& status
? 0 : iosb
->Information
;
1749 SetLastError(RtlNtStatusToDosError(status
));
1755 /***********************************************************************
1756 * GetNamedPipeInfo (KERNEL32.@)
1758 BOOL WINAPI
GetNamedPipeInfo(
1759 HANDLE hNamedPipe
, LPDWORD lpFlags
, LPDWORD lpOutputBufferSize
,
1760 LPDWORD lpInputBufferSize
, LPDWORD lpMaxInstances
)
1762 FILE_PIPE_LOCAL_INFORMATION fpli
;
1763 IO_STATUS_BLOCK iosb
;
1766 status
= NtQueryInformationFile(hNamedPipe
, &iosb
, &fpli
, sizeof(fpli
),
1767 FilePipeLocalInformation
);
1770 SetLastError( RtlNtStatusToDosError(status
) );
1776 *lpFlags
= (fpli
.NamedPipeEnd
& FILE_PIPE_SERVER_END
) ?
1777 PIPE_SERVER_END
: PIPE_CLIENT_END
;
1778 *lpFlags
|= (fpli
.NamedPipeType
& FILE_PIPE_TYPE_MESSAGE
) ?
1779 PIPE_TYPE_MESSAGE
: PIPE_TYPE_BYTE
;
1782 if (lpOutputBufferSize
) *lpOutputBufferSize
= fpli
.OutboundQuota
;
1783 if (lpInputBufferSize
) *lpInputBufferSize
= fpli
.InboundQuota
;
1784 if (lpMaxInstances
) *lpMaxInstances
= fpli
.MaximumInstances
;
1789 /***********************************************************************
1790 * GetNamedPipeClientProcessId (KERNEL32.@)
1792 BOOL WINAPI
GetNamedPipeClientProcessId( HANDLE pipe
, ULONG
*id
)
1794 IO_STATUS_BLOCK iosb
;
1797 TRACE( "%p %p\n", pipe
, id
);
1799 status
= NtFsControlFile( pipe
, NULL
, NULL
, NULL
, &iosb
, FSCTL_PIPE_GET_CONNECTION_ATTRIBUTE
,
1800 (void *)"ClientProcessId", sizeof("ClientProcessId"), id
, sizeof(*id
) );
1803 SetLastError( RtlNtStatusToDosError(status
) );
1809 /***********************************************************************
1810 * GetNamedPipeServerProcessId (KERNEL32.@)
1812 BOOL WINAPI
GetNamedPipeServerProcessId( HANDLE pipe
, ULONG
*id
)
1814 IO_STATUS_BLOCK iosb
;
1817 TRACE( "%p, %p\n", pipe
, id
);
1819 status
= NtFsControlFile( pipe
, NULL
, NULL
, NULL
, &iosb
, FSCTL_PIPE_GET_CONNECTION_ATTRIBUTE
,
1820 (void *)"ServerProcessId", sizeof("ServerProcessId"), id
, sizeof(*id
) );
1823 SetLastError( RtlNtStatusToDosError(status
) );
1829 /***********************************************************************
1830 * GetNamedPipeClientSessionId (KERNEL32.@)
1832 BOOL WINAPI
GetNamedPipeClientSessionId( HANDLE pipe
, ULONG
*id
)
1834 FIXME( "%p, %p\n", pipe
, id
);
1836 if (!id
) return FALSE
;
1837 *id
= NtCurrentTeb()->Peb
->SessionId
;
1841 /***********************************************************************
1842 * GetNamedPipeServerSessionId (KERNEL32.@)
1844 BOOL WINAPI
GetNamedPipeServerSessionId( HANDLE pipe
, ULONG
*id
)
1846 FIXME( "%p, %p\n", pipe
, id
);
1848 if (!id
) return FALSE
;
1849 *id
= NtCurrentTeb()->Peb
->SessionId
;
1853 /***********************************************************************
1854 * GetNamedPipeHandleStateA (KERNEL32.@)
1856 BOOL WINAPI
GetNamedPipeHandleStateA(
1857 HANDLE hNamedPipe
, LPDWORD lpState
, LPDWORD lpCurInstances
,
1858 LPDWORD lpMaxCollectionCount
, LPDWORD lpCollectDataTimeout
,
1859 LPSTR lpUsername
, DWORD nUsernameMaxSize
)
1861 WCHAR
*username
= NULL
;
1864 WARN("%p %p %p %p %p %p %d: semi-stub\n", hNamedPipe
, lpState
, lpCurInstances
,
1865 lpMaxCollectionCount
, lpCollectDataTimeout
, lpUsername
, nUsernameMaxSize
);
1867 if (lpUsername
&& nUsernameMaxSize
&&
1868 !(username
= HeapAlloc(GetProcessHeap(), 0, nUsernameMaxSize
* sizeof(WCHAR
)))) return FALSE
;
1870 ret
= GetNamedPipeHandleStateW(hNamedPipe
, lpState
, lpCurInstances
, lpMaxCollectionCount
,
1871 lpCollectDataTimeout
, username
, nUsernameMaxSize
);
1872 if (ret
&& username
)
1873 WideCharToMultiByte(CP_ACP
, 0, username
, -1, lpUsername
, nUsernameMaxSize
, NULL
, NULL
);
1875 HeapFree(GetProcessHeap(), 0, username
);
1879 /***********************************************************************
1880 * GetNamedPipeHandleStateW (KERNEL32.@)
1882 BOOL WINAPI
GetNamedPipeHandleStateW(
1883 HANDLE hNamedPipe
, LPDWORD lpState
, LPDWORD lpCurInstances
,
1884 LPDWORD lpMaxCollectionCount
, LPDWORD lpCollectDataTimeout
,
1885 LPWSTR lpUsername
, DWORD nUsernameMaxSize
)
1887 IO_STATUS_BLOCK iosb
;
1890 FIXME("%p %p %p %p %p %p %d: semi-stub\n", hNamedPipe
, lpState
, lpCurInstances
,
1891 lpMaxCollectionCount
, lpCollectDataTimeout
, lpUsername
, nUsernameMaxSize
);
1893 if (lpMaxCollectionCount
)
1894 *lpMaxCollectionCount
= 0;
1896 if (lpCollectDataTimeout
)
1897 *lpCollectDataTimeout
= 0;
1899 if (lpUsername
&& nUsernameMaxSize
)
1901 const char *username
= wine_get_user_name();
1902 int len
= MultiByteToWideChar(CP_UNIXCP
, 0, username
, -1, lpUsername
, nUsernameMaxSize
);
1903 if (!len
) *lpUsername
= 0;
1908 FILE_PIPE_INFORMATION fpi
;
1909 status
= NtQueryInformationFile(hNamedPipe
, &iosb
, &fpi
, sizeof(fpi
),
1910 FilePipeInformation
);
1913 SetLastError( RtlNtStatusToDosError(status
) );
1917 *lpState
= (fpi
.ReadMode
? PIPE_READMODE_MESSAGE
: PIPE_READMODE_BYTE
) |
1918 (fpi
.CompletionMode
? PIPE_NOWAIT
: PIPE_WAIT
);
1923 FILE_PIPE_LOCAL_INFORMATION fpli
;
1924 status
= NtQueryInformationFile(hNamedPipe
, &iosb
, &fpli
, sizeof(fpli
),
1925 FilePipeLocalInformation
);
1928 SetLastError( RtlNtStatusToDosError(status
) );
1932 *lpCurInstances
= fpli
.CurrentInstances
;
1938 /***********************************************************************
1939 * SetNamedPipeHandleState (KERNEL32.@)
1941 BOOL WINAPI
SetNamedPipeHandleState(
1942 HANDLE hNamedPipe
, LPDWORD lpMode
, LPDWORD lpMaxCollectionCount
,
1943 LPDWORD lpCollectDataTimeout
)
1945 /* should be a fixme, but this function is called a lot by the RPC
1946 * runtime, and it slows down InstallShield a fair bit. */
1947 WARN("semi-stub: %p %p/%d %p %p\n",
1948 hNamedPipe
, lpMode
, lpMode
? *lpMode
: 0, lpMaxCollectionCount
, lpCollectDataTimeout
);
1952 FILE_PIPE_INFORMATION fpi
;
1953 IO_STATUS_BLOCK iosb
;
1956 if (*lpMode
& ~(PIPE_READMODE_MESSAGE
| PIPE_NOWAIT
))
1957 status
= STATUS_INVALID_PARAMETER
;
1960 fpi
.CompletionMode
= (*lpMode
& PIPE_NOWAIT
) ?
1961 FILE_PIPE_COMPLETE_OPERATION
: FILE_PIPE_QUEUE_OPERATION
;
1962 fpi
.ReadMode
= (*lpMode
& PIPE_READMODE_MESSAGE
) ?
1963 FILE_PIPE_MESSAGE_MODE
: FILE_PIPE_BYTE_STREAM_MODE
;
1964 status
= NtSetInformationFile(hNamedPipe
, &iosb
, &fpi
, sizeof(fpi
), FilePipeInformation
);
1969 SetLastError( RtlNtStatusToDosError(status
) );
1977 /***********************************************************************
1978 * CallNamedPipeA (KERNEL32.@)
1980 BOOL WINAPI
CallNamedPipeA(
1981 LPCSTR lpNamedPipeName
, LPVOID lpInput
, DWORD dwInputSize
,
1982 LPVOID lpOutput
, DWORD dwOutputSize
,
1983 LPDWORD lpBytesRead
, DWORD nTimeout
)
1989 TRACE("%s %p %d %p %d %p %d\n",
1990 debugstr_a(lpNamedPipeName
), lpInput
, dwInputSize
,
1991 lpOutput
, dwOutputSize
, lpBytesRead
, nTimeout
);
1993 if( lpNamedPipeName
)
1995 len
= MultiByteToWideChar( CP_ACP
, 0, lpNamedPipeName
, -1, NULL
, 0 );
1996 str
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
1997 MultiByteToWideChar( CP_ACP
, 0, lpNamedPipeName
, -1, str
, len
);
1999 ret
= CallNamedPipeW( str
, lpInput
, dwInputSize
, lpOutput
,
2000 dwOutputSize
, lpBytesRead
, nTimeout
);
2001 if( lpNamedPipeName
)
2002 HeapFree( GetProcessHeap(), 0, str
);
2007 /***********************************************************************
2008 * CallNamedPipeW (KERNEL32.@)
2010 BOOL WINAPI
CallNamedPipeW(
2011 LPCWSTR lpNamedPipeName
, LPVOID lpInput
, DWORD lpInputSize
,
2012 LPVOID lpOutput
, DWORD lpOutputSize
,
2013 LPDWORD lpBytesRead
, DWORD nTimeout
)
2019 TRACE("%s %p %d %p %d %p %d\n",
2020 debugstr_w(lpNamedPipeName
), lpInput
, lpInputSize
,
2021 lpOutput
, lpOutputSize
, lpBytesRead
, nTimeout
);
2023 pipe
= CreateFileW(lpNamedPipeName
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, NULL
);
2024 if (pipe
== INVALID_HANDLE_VALUE
)
2026 ret
= WaitNamedPipeW(lpNamedPipeName
, nTimeout
);
2029 pipe
= CreateFileW(lpNamedPipeName
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, NULL
);
2030 if (pipe
== INVALID_HANDLE_VALUE
)
2034 mode
= PIPE_READMODE_MESSAGE
;
2035 ret
= SetNamedPipeHandleState(pipe
, &mode
, NULL
, NULL
);
2042 ret
= TransactNamedPipe(pipe
, lpInput
, lpInputSize
, lpOutput
, lpOutputSize
, lpBytesRead
, NULL
);
2050 /******************************************************************
2051 * CreatePipe (KERNEL32.@)
2054 BOOL WINAPI
CreatePipe( PHANDLE hReadPipe
, PHANDLE hWritePipe
,
2055 LPSECURITY_ATTRIBUTES sa
, DWORD size
)
2057 static unsigned index
/* = 0 */;
2060 unsigned in_index
= index
;
2061 UNICODE_STRING nt_name
;
2062 OBJECT_ATTRIBUTES attr
;
2064 IO_STATUS_BLOCK iosb
;
2065 LARGE_INTEGER timeout
;
2067 *hReadPipe
= *hWritePipe
= INVALID_HANDLE_VALUE
;
2069 attr
.Length
= sizeof(attr
);
2070 attr
.RootDirectory
= 0;
2071 attr
.ObjectName
= &nt_name
;
2072 attr
.Attributes
= OBJ_CASE_INSENSITIVE
|
2073 ((sa
&& sa
->bInheritHandle
) ? OBJ_INHERIT
: 0);
2074 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
2075 attr
.SecurityQualityOfService
= NULL
;
2077 if (!size
) size
= 4096;
2079 timeout
.QuadPart
= (ULONGLONG
)NMPWAIT_USE_DEFAULT_WAIT
* -10000;
2080 /* generate a unique pipe name (system wide) */
2083 static const WCHAR nameFmt
[] = { '\\','?','?','\\','p','i','p','e',
2084 '\\','W','i','n','3','2','.','P','i','p','e','s','.','%','0','8','l',
2085 'u','.','%','0','8','u','\0' };
2087 snprintfW(name
, sizeof(name
) / sizeof(name
[0]), nameFmt
,
2088 GetCurrentProcessId(), ++index
);
2089 RtlInitUnicodeString(&nt_name
, name
);
2090 status
= NtCreateNamedPipeFile(&hr
, GENERIC_READ
| SYNCHRONIZE
, &attr
, &iosb
,
2091 FILE_SHARE_WRITE
, FILE_OVERWRITE_IF
,
2092 FILE_SYNCHRONOUS_IO_NONALERT
,
2093 FALSE
, FALSE
, FALSE
,
2094 1, size
, size
, &timeout
);
2097 SetLastError( RtlNtStatusToDosError(status
) );
2098 hr
= INVALID_HANDLE_VALUE
;
2100 } while (hr
== INVALID_HANDLE_VALUE
&& index
!= in_index
);
2101 /* from completion sakeness, I think system resources might be exhausted before this happens !! */
2102 if (hr
== INVALID_HANDLE_VALUE
) return FALSE
;
2104 status
= NtOpenFile(&hw
, GENERIC_WRITE
| SYNCHRONIZE
, &attr
, &iosb
, 0,
2105 FILE_SYNCHRONOUS_IO_NONALERT
| FILE_NON_DIRECTORY_FILE
);
2109 SetLastError( RtlNtStatusToDosError(status
) );
2120 /******************************************************************************
2121 * CreateMailslotA [KERNEL32.@]
2123 * See CreateMailslotW.
2125 HANDLE WINAPI
CreateMailslotA( LPCSTR lpName
, DWORD nMaxMessageSize
,
2126 DWORD lReadTimeout
, LPSECURITY_ATTRIBUTES sa
)
2132 TRACE("%s %d %d %p\n", debugstr_a(lpName
),
2133 nMaxMessageSize
, lReadTimeout
, sa
);
2137 len
= MultiByteToWideChar( CP_ACP
, 0, lpName
, -1, NULL
, 0 );
2138 name
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
2139 MultiByteToWideChar( CP_ACP
, 0, lpName
, -1, name
, len
);
2142 handle
= CreateMailslotW( name
, nMaxMessageSize
, lReadTimeout
, sa
);
2144 HeapFree( GetProcessHeap(), 0, name
);
2150 /******************************************************************************
2151 * CreateMailslotW [KERNEL32.@]
2153 * Create a mailslot with specified name.
2156 * lpName [I] Pointer to string for mailslot name
2157 * nMaxMessageSize [I] Maximum message size
2158 * lReadTimeout [I] Milliseconds before read time-out
2159 * sa [I] Pointer to security structure
2162 * Success: Handle to mailslot
2163 * Failure: INVALID_HANDLE_VALUE
2165 HANDLE WINAPI
CreateMailslotW( LPCWSTR lpName
, DWORD nMaxMessageSize
,
2166 DWORD lReadTimeout
, LPSECURITY_ATTRIBUTES sa
)
2168 HANDLE handle
= INVALID_HANDLE_VALUE
;
2169 OBJECT_ATTRIBUTES attr
;
2170 UNICODE_STRING nameW
;
2171 LARGE_INTEGER timeout
;
2172 IO_STATUS_BLOCK iosb
;
2175 TRACE("%s %d %d %p\n", debugstr_w(lpName
),
2176 nMaxMessageSize
, lReadTimeout
, sa
);
2178 if (!RtlDosPathNameToNtPathName_U( lpName
, &nameW
, NULL
, NULL
))
2180 SetLastError( ERROR_PATH_NOT_FOUND
);
2181 return INVALID_HANDLE_VALUE
;
2184 if (nameW
.Length
>= MAX_PATH
* sizeof(WCHAR
) )
2186 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
2187 RtlFreeUnicodeString( &nameW
);
2188 return INVALID_HANDLE_VALUE
;
2191 attr
.Length
= sizeof(attr
);
2192 attr
.RootDirectory
= 0;
2193 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2194 attr
.ObjectName
= &nameW
;
2195 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
2196 attr
.SecurityQualityOfService
= NULL
;
2198 if (lReadTimeout
!= MAILSLOT_WAIT_FOREVER
)
2199 timeout
.QuadPart
= (ULONGLONG
) lReadTimeout
* -10000;
2201 timeout
.QuadPart
= ((LONGLONG
)0x7fffffff << 32) | 0xffffffff;
2203 status
= NtCreateMailslotFile( &handle
, GENERIC_READ
| SYNCHRONIZE
, &attr
,
2204 &iosb
, 0, 0, nMaxMessageSize
, &timeout
);
2207 SetLastError( RtlNtStatusToDosError(status
) );
2208 handle
= INVALID_HANDLE_VALUE
;
2211 RtlFreeUnicodeString( &nameW
);
2216 /******************************************************************************
2217 * GetMailslotInfo [KERNEL32.@]
2219 * Retrieve information about a mailslot.
2222 * hMailslot [I] Mailslot handle
2223 * lpMaxMessageSize [O] Address of maximum message size
2224 * lpNextSize [O] Address of size of next message
2225 * lpMessageCount [O] Address of number of messages
2226 * lpReadTimeout [O] Address of read time-out
2232 BOOL WINAPI
GetMailslotInfo( HANDLE hMailslot
, LPDWORD lpMaxMessageSize
,
2233 LPDWORD lpNextSize
, LPDWORD lpMessageCount
,
2234 LPDWORD lpReadTimeout
)
2236 FILE_MAILSLOT_QUERY_INFORMATION info
;
2237 IO_STATUS_BLOCK iosb
;
2240 TRACE("%p %p %p %p %p\n",hMailslot
, lpMaxMessageSize
,
2241 lpNextSize
, lpMessageCount
, lpReadTimeout
);
2243 status
= NtQueryInformationFile( hMailslot
, &iosb
, &info
, sizeof info
,
2244 FileMailslotQueryInformation
);
2246 if( status
!= STATUS_SUCCESS
)
2248 SetLastError( RtlNtStatusToDosError(status
) );
2252 if( lpMaxMessageSize
)
2253 *lpMaxMessageSize
= info
.MaximumMessageSize
;
2255 *lpNextSize
= info
.NextMessageSize
;
2256 if( lpMessageCount
)
2257 *lpMessageCount
= info
.MessagesAvailable
;
2260 if (info
.ReadTimeout
.QuadPart
== (((LONGLONG
)0x7fffffff << 32) | 0xffffffff))
2261 *lpReadTimeout
= MAILSLOT_WAIT_FOREVER
;
2263 *lpReadTimeout
= info
.ReadTimeout
.QuadPart
/ -10000;
2269 /******************************************************************************
2270 * SetMailslotInfo [KERNEL32.@]
2272 * Set the read timeout of a mailslot.
2275 * hMailslot [I] Mailslot handle
2276 * dwReadTimeout [I] Timeout in milliseconds.
2282 BOOL WINAPI
SetMailslotInfo( HANDLE hMailslot
, DWORD dwReadTimeout
)
2284 FILE_MAILSLOT_SET_INFORMATION info
;
2285 IO_STATUS_BLOCK iosb
;
2288 TRACE("%p %d\n", hMailslot
, dwReadTimeout
);
2290 if (dwReadTimeout
!= MAILSLOT_WAIT_FOREVER
)
2291 info
.ReadTimeout
.QuadPart
= (ULONGLONG
)dwReadTimeout
* -10000;
2293 info
.ReadTimeout
.QuadPart
= ((LONGLONG
)0x7fffffff << 32) | 0xffffffff;
2294 status
= NtSetInformationFile( hMailslot
, &iosb
, &info
, sizeof info
,
2295 FileMailslotSetInformation
);
2296 if( status
!= STATUS_SUCCESS
)
2298 SetLastError( RtlNtStatusToDosError(status
) );
2305 /******************************************************************************
2306 * CreateIoCompletionPort (KERNEL32.@)
2308 HANDLE WINAPI
CreateIoCompletionPort(HANDLE hFileHandle
, HANDLE hExistingCompletionPort
,
2309 ULONG_PTR CompletionKey
, DWORD dwNumberOfConcurrentThreads
)
2314 TRACE("(%p, %p, %08lx, %08x)\n",
2315 hFileHandle
, hExistingCompletionPort
, CompletionKey
, dwNumberOfConcurrentThreads
);
2317 if (hExistingCompletionPort
&& hFileHandle
== INVALID_HANDLE_VALUE
)
2319 SetLastError( ERROR_INVALID_PARAMETER
);
2323 if (hExistingCompletionPort
)
2324 ret
= hExistingCompletionPort
;
2327 status
= NtCreateIoCompletion( &ret
, IO_COMPLETION_ALL_ACCESS
, NULL
, dwNumberOfConcurrentThreads
);
2328 if (status
!= STATUS_SUCCESS
) goto fail
;
2331 if (hFileHandle
!= INVALID_HANDLE_VALUE
)
2333 FILE_COMPLETION_INFORMATION info
;
2334 IO_STATUS_BLOCK iosb
;
2336 info
.CompletionPort
= ret
;
2337 info
.CompletionKey
= CompletionKey
;
2338 status
= NtSetInformationFile( hFileHandle
, &iosb
, &info
, sizeof(info
), FileCompletionInformation
);
2339 if (status
!= STATUS_SUCCESS
) goto fail
;
2345 if (ret
&& !hExistingCompletionPort
)
2347 SetLastError( RtlNtStatusToDosError(status
) );
2351 /******************************************************************************
2352 * GetQueuedCompletionStatus (KERNEL32.@)
2354 BOOL WINAPI
GetQueuedCompletionStatus( HANDLE CompletionPort
, LPDWORD lpNumberOfBytesTransferred
,
2355 PULONG_PTR pCompletionKey
, LPOVERLAPPED
*lpOverlapped
,
2356 DWORD dwMilliseconds
)
2359 IO_STATUS_BLOCK iosb
;
2360 LARGE_INTEGER wait_time
;
2362 TRACE("(%p,%p,%p,%p,%d)\n",
2363 CompletionPort
,lpNumberOfBytesTransferred
,pCompletionKey
,lpOverlapped
,dwMilliseconds
);
2365 *lpOverlapped
= NULL
;
2367 status
= NtRemoveIoCompletion( CompletionPort
, pCompletionKey
, (PULONG_PTR
)lpOverlapped
,
2368 &iosb
, get_nt_timeout( &wait_time
, dwMilliseconds
) );
2369 if (status
== STATUS_SUCCESS
)
2371 *lpNumberOfBytesTransferred
= iosb
.Information
;
2372 if (iosb
.u
.Status
>= 0) return TRUE
;
2373 SetLastError( RtlNtStatusToDosError(iosb
.u
.Status
) );
2377 if (status
== STATUS_TIMEOUT
) SetLastError( WAIT_TIMEOUT
);
2378 else SetLastError( RtlNtStatusToDosError(status
) );
2383 /******************************************************************************
2384 * PostQueuedCompletionStatus (KERNEL32.@)
2386 BOOL WINAPI
PostQueuedCompletionStatus( HANDLE CompletionPort
, DWORD dwNumberOfBytes
,
2387 ULONG_PTR dwCompletionKey
, LPOVERLAPPED lpOverlapped
)
2391 TRACE("%p %d %08lx %p\n", CompletionPort
, dwNumberOfBytes
, dwCompletionKey
, lpOverlapped
);
2393 status
= NtSetIoCompletion( CompletionPort
, dwCompletionKey
, (ULONG_PTR
)lpOverlapped
,
2394 STATUS_SUCCESS
, dwNumberOfBytes
);
2396 if (status
== STATUS_SUCCESS
) return TRUE
;
2397 SetLastError( RtlNtStatusToDosError(status
) );
2401 /******************************************************************************
2402 * BindIoCompletionCallback (KERNEL32.@)
2404 BOOL WINAPI
BindIoCompletionCallback( HANDLE FileHandle
, LPOVERLAPPED_COMPLETION_ROUTINE Function
, ULONG Flags
)
2408 TRACE("(%p, %p, %d)\n", FileHandle
, Function
, Flags
);
2410 status
= RtlSetIoCompletionCallback( FileHandle
, (PRTL_OVERLAPPED_COMPLETION_ROUTINE
)Function
, Flags
);
2411 if (status
== STATUS_SUCCESS
) return TRUE
;
2412 SetLastError( RtlNtStatusToDosError(status
) );
2417 /***********************************************************************
2418 * CreateMemoryResourceNotification (KERNEL32.@)
2420 HANDLE WINAPI
CreateMemoryResourceNotification(MEMORY_RESOURCE_NOTIFICATION_TYPE type
)
2422 static const WCHAR lowmemW
[] =
2423 {'\\','K','e','r','n','e','l','O','b','j','e','c','t','s',
2424 '\\','L','o','w','M','e','m','o','r','y','C','o','n','d','i','t','i','o','n',0};
2425 static const WCHAR highmemW
[] =
2426 {'\\','K','e','r','n','e','l','O','b','j','e','c','t','s',
2427 '\\','H','i','g','h','M','e','m','o','r','y','C','o','n','d','i','t','i','o','n',0};
2429 UNICODE_STRING nameW
;
2430 OBJECT_ATTRIBUTES attr
;
2435 case LowMemoryResourceNotification
:
2436 RtlInitUnicodeString( &nameW
, lowmemW
);
2438 case HighMemoryResourceNotification
:
2439 RtlInitUnicodeString( &nameW
, highmemW
);
2442 SetLastError( ERROR_INVALID_PARAMETER
);
2446 attr
.Length
= sizeof(attr
);
2447 attr
.RootDirectory
= 0;
2448 attr
.ObjectName
= &nameW
;
2449 attr
.Attributes
= 0;
2450 attr
.SecurityDescriptor
= NULL
;
2451 attr
.SecurityQualityOfService
= NULL
;
2452 status
= NtOpenEvent( &ret
, EVENT_ALL_ACCESS
, &attr
);
2453 if (status
!= STATUS_SUCCESS
)
2455 SetLastError( RtlNtStatusToDosError(status
) );
2461 /***********************************************************************
2462 * QueryMemoryResourceNotification (KERNEL32.@)
2464 BOOL WINAPI
QueryMemoryResourceNotification(HANDLE handle
, PBOOL state
)
2466 switch (WaitForSingleObject( handle
, 0 ))
2475 SetLastError( ERROR_INVALID_PARAMETER
);
2479 /***********************************************************************
2480 * InitOnceBeginInitialize (KERNEL32.@)
2482 BOOL WINAPI
InitOnceBeginInitialize( INIT_ONCE
*once
, DWORD flags
, BOOL
*pending
, void **context
)
2484 NTSTATUS status
= RtlRunOnceBeginInitialize( once
, flags
, context
);
2485 if (status
>= 0) *pending
= (status
== STATUS_PENDING
);
2486 else SetLastError( RtlNtStatusToDosError(status
) );
2490 /***********************************************************************
2491 * InitOnceComplete (KERNEL32.@)
2493 BOOL WINAPI
InitOnceComplete( INIT_ONCE
*once
, DWORD flags
, void *context
)
2495 NTSTATUS status
= RtlRunOnceComplete( once
, flags
, context
);
2496 if (status
!= STATUS_SUCCESS
) SetLastError( RtlNtStatusToDosError(status
) );
2500 /***********************************************************************
2501 * InitOnceExecuteOnce (KERNEL32.@)
2503 BOOL WINAPI
InitOnceExecuteOnce( INIT_ONCE
*once
, PINIT_ONCE_FN func
, void *param
, void **context
)
2505 return !RtlRunOnceExecuteOnce( once
, (PRTL_RUN_ONCE_INIT_FN
)func
, param
, context
);
2510 /***********************************************************************
2511 * InterlockedCompareExchange (KERNEL32.@)
2513 /* LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare ); */
2514 __ASM_STDCALL_FUNC(InterlockedCompareExchange
, 12,
2515 "movl 12(%esp),%eax\n\t"
2516 "movl 8(%esp),%ecx\n\t"
2517 "movl 4(%esp),%edx\n\t"
2518 "lock; cmpxchgl %ecx,(%edx)\n\t"
2521 /***********************************************************************
2522 * InterlockedExchange (KERNEL32.@)
2524 /* LONG WINAPI InterlockedExchange( PLONG dest, LONG val ); */
2525 __ASM_STDCALL_FUNC(InterlockedExchange
, 8,
2526 "movl 8(%esp),%eax\n\t"
2527 "movl 4(%esp),%edx\n\t"
2528 "lock; xchgl %eax,(%edx)\n\t"
2531 /***********************************************************************
2532 * InterlockedExchangeAdd (KERNEL32.@)
2534 /* LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr ); */
2535 __ASM_STDCALL_FUNC(InterlockedExchangeAdd
, 8,
2536 "movl 8(%esp),%eax\n\t"
2537 "movl 4(%esp),%edx\n\t"
2538 "lock; xaddl %eax,(%edx)\n\t"
2541 /***********************************************************************
2542 * InterlockedIncrement (KERNEL32.@)
2544 /* LONG WINAPI InterlockedIncrement( PLONG dest ); */
2545 __ASM_STDCALL_FUNC(InterlockedIncrement
, 4,
2546 "movl 4(%esp),%edx\n\t"
2548 "lock; xaddl %eax,(%edx)\n\t"
2552 /***********************************************************************
2553 * InterlockedDecrement (KERNEL32.@)
2555 __ASM_STDCALL_FUNC(InterlockedDecrement
, 4,
2556 "movl 4(%esp),%edx\n\t"
2558 "lock; xaddl %eax,(%edx)\n\t"
2562 #endif /* __i386__ */
2564 /***********************************************************************
2565 * SleepConditionVariableCS (KERNEL32.@)
2567 BOOL WINAPI
SleepConditionVariableCS( CONDITION_VARIABLE
*variable
, CRITICAL_SECTION
*crit
, DWORD timeout
)
2572 status
= RtlSleepConditionVariableCS( variable
, crit
, get_nt_timeout( &time
, timeout
) );
2574 if (status
!= STATUS_SUCCESS
)
2576 SetLastError( RtlNtStatusToDosError(status
) );
2582 /***********************************************************************
2583 * SleepConditionVariableSRW (KERNEL32.@)
2585 BOOL WINAPI
SleepConditionVariableSRW( RTL_CONDITION_VARIABLE
*variable
, RTL_SRWLOCK
*lock
, DWORD timeout
, ULONG flags
)
2590 status
= RtlSleepConditionVariableSRW( variable
, lock
, get_nt_timeout( &time
, timeout
), flags
);
2592 if (status
!= STATUS_SUCCESS
)
2594 SetLastError( RtlNtStatusToDosError(status
) );