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.@)
1720 * should be done as a single operation in the wineserver or kernel
1722 BOOL WINAPI
TransactNamedPipe(
1723 HANDLE handle
, LPVOID write_buf
, DWORD write_size
, LPVOID read_buf
,
1724 DWORD read_size
, LPDWORD bytes_read
, LPOVERLAPPED overlapped
)
1729 TRACE("%p %p %d %p %d %p %p\n",
1730 handle
, write_buf
, write_size
, read_buf
,
1731 read_size
, bytes_read
, overlapped
);
1734 FIXME("Doesn't support overlapped operation as yet\n");
1736 r
= WriteFile(handle
, write_buf
, write_size
, &count
, NULL
);
1738 r
= ReadFile(handle
, read_buf
, read_size
, bytes_read
, NULL
);
1743 /***********************************************************************
1744 * GetNamedPipeInfo (KERNEL32.@)
1746 BOOL WINAPI
GetNamedPipeInfo(
1747 HANDLE hNamedPipe
, LPDWORD lpFlags
, LPDWORD lpOutputBufferSize
,
1748 LPDWORD lpInputBufferSize
, LPDWORD lpMaxInstances
)
1750 FILE_PIPE_LOCAL_INFORMATION fpli
;
1751 IO_STATUS_BLOCK iosb
;
1754 status
= NtQueryInformationFile(hNamedPipe
, &iosb
, &fpli
, sizeof(fpli
),
1755 FilePipeLocalInformation
);
1758 SetLastError( RtlNtStatusToDosError(status
) );
1764 *lpFlags
= (fpli
.NamedPipeEnd
& FILE_PIPE_SERVER_END
) ?
1765 PIPE_SERVER_END
: PIPE_CLIENT_END
;
1766 *lpFlags
|= (fpli
.NamedPipeType
& FILE_PIPE_TYPE_MESSAGE
) ?
1767 PIPE_TYPE_MESSAGE
: PIPE_TYPE_BYTE
;
1770 if (lpOutputBufferSize
) *lpOutputBufferSize
= fpli
.OutboundQuota
;
1771 if (lpInputBufferSize
) *lpInputBufferSize
= fpli
.InboundQuota
;
1772 if (lpMaxInstances
) *lpMaxInstances
= fpli
.MaximumInstances
;
1777 /***********************************************************************
1778 * GetNamedPipeHandleStateA (KERNEL32.@)
1780 BOOL WINAPI
GetNamedPipeHandleStateA(
1781 HANDLE hNamedPipe
, LPDWORD lpState
, LPDWORD lpCurInstances
,
1782 LPDWORD lpMaxCollectionCount
, LPDWORD lpCollectDataTimeout
,
1783 LPSTR lpUsername
, DWORD nUsernameMaxSize
)
1785 WCHAR
*username
= NULL
;
1788 WARN("%p %p %p %p %p %p %d: semi-stub\n", hNamedPipe
, lpState
, lpCurInstances
,
1789 lpMaxCollectionCount
, lpCollectDataTimeout
, lpUsername
, nUsernameMaxSize
);
1791 if (lpUsername
&& nUsernameMaxSize
&&
1792 !(username
= HeapAlloc(GetProcessHeap(), 0, nUsernameMaxSize
* sizeof(WCHAR
)))) return FALSE
;
1794 ret
= GetNamedPipeHandleStateW(hNamedPipe
, lpState
, lpCurInstances
, lpMaxCollectionCount
,
1795 lpCollectDataTimeout
, username
, nUsernameMaxSize
);
1796 if (ret
&& username
)
1797 WideCharToMultiByte(CP_ACP
, 0, username
, -1, lpUsername
, nUsernameMaxSize
, NULL
, NULL
);
1799 HeapFree(GetProcessHeap(), 0, username
);
1803 /***********************************************************************
1804 * GetNamedPipeHandleStateW (KERNEL32.@)
1806 BOOL WINAPI
GetNamedPipeHandleStateW(
1807 HANDLE hNamedPipe
, LPDWORD lpState
, LPDWORD lpCurInstances
,
1808 LPDWORD lpMaxCollectionCount
, LPDWORD lpCollectDataTimeout
,
1809 LPWSTR lpUsername
, DWORD nUsernameMaxSize
)
1811 IO_STATUS_BLOCK iosb
;
1814 FIXME("%p %p %p %p %p %p %d: semi-stub\n", hNamedPipe
, lpState
, lpCurInstances
,
1815 lpMaxCollectionCount
, lpCollectDataTimeout
, lpUsername
, nUsernameMaxSize
);
1817 if (lpMaxCollectionCount
)
1818 *lpMaxCollectionCount
= 0;
1820 if (lpCollectDataTimeout
)
1821 *lpCollectDataTimeout
= 0;
1823 if (lpUsername
&& nUsernameMaxSize
)
1825 const char *username
= wine_get_user_name();
1826 int len
= MultiByteToWideChar(CP_UNIXCP
, 0, username
, -1, lpUsername
, nUsernameMaxSize
);
1827 if (!len
) *lpUsername
= 0;
1832 FILE_PIPE_INFORMATION fpi
;
1833 status
= NtQueryInformationFile(hNamedPipe
, &iosb
, &fpi
, sizeof(fpi
),
1834 FilePipeInformation
);
1837 SetLastError( RtlNtStatusToDosError(status
) );
1841 *lpState
= (fpi
.ReadMode
? PIPE_READMODE_MESSAGE
: PIPE_READMODE_BYTE
) |
1842 (fpi
.CompletionMode
? PIPE_NOWAIT
: PIPE_WAIT
);
1847 FILE_PIPE_LOCAL_INFORMATION fpli
;
1848 status
= NtQueryInformationFile(hNamedPipe
, &iosb
, &fpli
, sizeof(fpli
),
1849 FilePipeLocalInformation
);
1852 SetLastError( RtlNtStatusToDosError(status
) );
1856 *lpCurInstances
= fpli
.CurrentInstances
;
1862 /***********************************************************************
1863 * SetNamedPipeHandleState (KERNEL32.@)
1865 BOOL WINAPI
SetNamedPipeHandleState(
1866 HANDLE hNamedPipe
, LPDWORD lpMode
, LPDWORD lpMaxCollectionCount
,
1867 LPDWORD lpCollectDataTimeout
)
1869 /* should be a fixme, but this function is called a lot by the RPC
1870 * runtime, and it slows down InstallShield a fair bit. */
1871 WARN("semi-stub: %p %p/%d %p %p\n",
1872 hNamedPipe
, lpMode
, lpMode
? *lpMode
: 0, lpMaxCollectionCount
, lpCollectDataTimeout
);
1876 FILE_PIPE_INFORMATION fpi
;
1877 IO_STATUS_BLOCK iosb
;
1880 if (*lpMode
& ~(PIPE_READMODE_MESSAGE
| PIPE_NOWAIT
))
1881 status
= STATUS_INVALID_PARAMETER
;
1884 fpi
.CompletionMode
= (*lpMode
& PIPE_NOWAIT
) ?
1885 FILE_PIPE_COMPLETE_OPERATION
: FILE_PIPE_QUEUE_OPERATION
;
1886 fpi
.ReadMode
= (*lpMode
& PIPE_READMODE_MESSAGE
) ?
1887 FILE_PIPE_MESSAGE_MODE
: FILE_PIPE_BYTE_STREAM_MODE
;
1888 status
= NtSetInformationFile(hNamedPipe
, &iosb
, &fpi
, sizeof(fpi
), FilePipeInformation
);
1893 SetLastError( RtlNtStatusToDosError(status
) );
1901 /***********************************************************************
1902 * CallNamedPipeA (KERNEL32.@)
1904 BOOL WINAPI
CallNamedPipeA(
1905 LPCSTR lpNamedPipeName
, LPVOID lpInput
, DWORD dwInputSize
,
1906 LPVOID lpOutput
, DWORD dwOutputSize
,
1907 LPDWORD lpBytesRead
, DWORD nTimeout
)
1913 TRACE("%s %p %d %p %d %p %d\n",
1914 debugstr_a(lpNamedPipeName
), lpInput
, dwInputSize
,
1915 lpOutput
, dwOutputSize
, lpBytesRead
, nTimeout
);
1917 if( lpNamedPipeName
)
1919 len
= MultiByteToWideChar( CP_ACP
, 0, lpNamedPipeName
, -1, NULL
, 0 );
1920 str
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
1921 MultiByteToWideChar( CP_ACP
, 0, lpNamedPipeName
, -1, str
, len
);
1923 ret
= CallNamedPipeW( str
, lpInput
, dwInputSize
, lpOutput
,
1924 dwOutputSize
, lpBytesRead
, nTimeout
);
1925 if( lpNamedPipeName
)
1926 HeapFree( GetProcessHeap(), 0, str
);
1931 /***********************************************************************
1932 * CallNamedPipeW (KERNEL32.@)
1934 BOOL WINAPI
CallNamedPipeW(
1935 LPCWSTR lpNamedPipeName
, LPVOID lpInput
, DWORD lpInputSize
,
1936 LPVOID lpOutput
, DWORD lpOutputSize
,
1937 LPDWORD lpBytesRead
, DWORD nTimeout
)
1943 TRACE("%s %p %d %p %d %p %d\n",
1944 debugstr_w(lpNamedPipeName
), lpInput
, lpInputSize
,
1945 lpOutput
, lpOutputSize
, lpBytesRead
, nTimeout
);
1947 pipe
= CreateFileW(lpNamedPipeName
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, NULL
);
1948 if (pipe
== INVALID_HANDLE_VALUE
)
1950 ret
= WaitNamedPipeW(lpNamedPipeName
, nTimeout
);
1953 pipe
= CreateFileW(lpNamedPipeName
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, NULL
);
1954 if (pipe
== INVALID_HANDLE_VALUE
)
1958 mode
= PIPE_READMODE_MESSAGE
;
1959 ret
= SetNamedPipeHandleState(pipe
, &mode
, NULL
, NULL
);
1966 ret
= TransactNamedPipe(pipe
, lpInput
, lpInputSize
, lpOutput
, lpOutputSize
, lpBytesRead
, NULL
);
1974 /******************************************************************
1975 * CreatePipe (KERNEL32.@)
1978 BOOL WINAPI
CreatePipe( PHANDLE hReadPipe
, PHANDLE hWritePipe
,
1979 LPSECURITY_ATTRIBUTES sa
, DWORD size
)
1981 static unsigned index
/* = 0 */;
1984 unsigned in_index
= index
;
1985 UNICODE_STRING nt_name
;
1986 OBJECT_ATTRIBUTES attr
;
1988 IO_STATUS_BLOCK iosb
;
1989 LARGE_INTEGER timeout
;
1991 *hReadPipe
= *hWritePipe
= INVALID_HANDLE_VALUE
;
1993 attr
.Length
= sizeof(attr
);
1994 attr
.RootDirectory
= 0;
1995 attr
.ObjectName
= &nt_name
;
1996 attr
.Attributes
= OBJ_CASE_INSENSITIVE
|
1997 ((sa
&& sa
->bInheritHandle
) ? OBJ_INHERIT
: 0);
1998 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
1999 attr
.SecurityQualityOfService
= NULL
;
2001 if (!size
) size
= 4096;
2003 timeout
.QuadPart
= (ULONGLONG
)NMPWAIT_USE_DEFAULT_WAIT
* -10000;
2004 /* generate a unique pipe name (system wide) */
2007 static const WCHAR nameFmt
[] = { '\\','?','?','\\','p','i','p','e',
2008 '\\','W','i','n','3','2','.','P','i','p','e','s','.','%','0','8','l',
2009 'u','.','%','0','8','u','\0' };
2011 snprintfW(name
, sizeof(name
) / sizeof(name
[0]), nameFmt
,
2012 GetCurrentProcessId(), ++index
);
2013 RtlInitUnicodeString(&nt_name
, name
);
2014 status
= NtCreateNamedPipeFile(&hr
, GENERIC_READ
| SYNCHRONIZE
, &attr
, &iosb
,
2015 FILE_SHARE_WRITE
, FILE_OVERWRITE_IF
,
2016 FILE_SYNCHRONOUS_IO_NONALERT
,
2017 FALSE
, FALSE
, FALSE
,
2018 1, size
, size
, &timeout
);
2021 SetLastError( RtlNtStatusToDosError(status
) );
2022 hr
= INVALID_HANDLE_VALUE
;
2024 } while (hr
== INVALID_HANDLE_VALUE
&& index
!= in_index
);
2025 /* from completion sakeness, I think system resources might be exhausted before this happens !! */
2026 if (hr
== INVALID_HANDLE_VALUE
) return FALSE
;
2028 status
= NtOpenFile(&hw
, GENERIC_WRITE
| SYNCHRONIZE
, &attr
, &iosb
, 0,
2029 FILE_SYNCHRONOUS_IO_NONALERT
| FILE_NON_DIRECTORY_FILE
);
2033 SetLastError( RtlNtStatusToDosError(status
) );
2044 /******************************************************************************
2045 * CreateMailslotA [KERNEL32.@]
2047 * See CreateMailslotW.
2049 HANDLE WINAPI
CreateMailslotA( LPCSTR lpName
, DWORD nMaxMessageSize
,
2050 DWORD lReadTimeout
, LPSECURITY_ATTRIBUTES sa
)
2056 TRACE("%s %d %d %p\n", debugstr_a(lpName
),
2057 nMaxMessageSize
, lReadTimeout
, sa
);
2061 len
= MultiByteToWideChar( CP_ACP
, 0, lpName
, -1, NULL
, 0 );
2062 name
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
2063 MultiByteToWideChar( CP_ACP
, 0, lpName
, -1, name
, len
);
2066 handle
= CreateMailslotW( name
, nMaxMessageSize
, lReadTimeout
, sa
);
2068 HeapFree( GetProcessHeap(), 0, name
);
2074 /******************************************************************************
2075 * CreateMailslotW [KERNEL32.@]
2077 * Create a mailslot with specified name.
2080 * lpName [I] Pointer to string for mailslot name
2081 * nMaxMessageSize [I] Maximum message size
2082 * lReadTimeout [I] Milliseconds before read time-out
2083 * sa [I] Pointer to security structure
2086 * Success: Handle to mailslot
2087 * Failure: INVALID_HANDLE_VALUE
2089 HANDLE WINAPI
CreateMailslotW( LPCWSTR lpName
, DWORD nMaxMessageSize
,
2090 DWORD lReadTimeout
, LPSECURITY_ATTRIBUTES sa
)
2092 HANDLE handle
= INVALID_HANDLE_VALUE
;
2093 OBJECT_ATTRIBUTES attr
;
2094 UNICODE_STRING nameW
;
2095 LARGE_INTEGER timeout
;
2096 IO_STATUS_BLOCK iosb
;
2099 TRACE("%s %d %d %p\n", debugstr_w(lpName
),
2100 nMaxMessageSize
, lReadTimeout
, sa
);
2102 if (!RtlDosPathNameToNtPathName_U( lpName
, &nameW
, NULL
, NULL
))
2104 SetLastError( ERROR_PATH_NOT_FOUND
);
2105 return INVALID_HANDLE_VALUE
;
2108 if (nameW
.Length
>= MAX_PATH
* sizeof(WCHAR
) )
2110 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
2111 RtlFreeUnicodeString( &nameW
);
2112 return INVALID_HANDLE_VALUE
;
2115 attr
.Length
= sizeof(attr
);
2116 attr
.RootDirectory
= 0;
2117 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2118 attr
.ObjectName
= &nameW
;
2119 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
2120 attr
.SecurityQualityOfService
= NULL
;
2122 if (lReadTimeout
!= MAILSLOT_WAIT_FOREVER
)
2123 timeout
.QuadPart
= (ULONGLONG
) lReadTimeout
* -10000;
2125 timeout
.QuadPart
= ((LONGLONG
)0x7fffffff << 32) | 0xffffffff;
2127 status
= NtCreateMailslotFile( &handle
, GENERIC_READ
| SYNCHRONIZE
, &attr
,
2128 &iosb
, 0, 0, nMaxMessageSize
, &timeout
);
2131 SetLastError( RtlNtStatusToDosError(status
) );
2132 handle
= INVALID_HANDLE_VALUE
;
2135 RtlFreeUnicodeString( &nameW
);
2140 /******************************************************************************
2141 * GetMailslotInfo [KERNEL32.@]
2143 * Retrieve information about a mailslot.
2146 * hMailslot [I] Mailslot handle
2147 * lpMaxMessageSize [O] Address of maximum message size
2148 * lpNextSize [O] Address of size of next message
2149 * lpMessageCount [O] Address of number of messages
2150 * lpReadTimeout [O] Address of read time-out
2156 BOOL WINAPI
GetMailslotInfo( HANDLE hMailslot
, LPDWORD lpMaxMessageSize
,
2157 LPDWORD lpNextSize
, LPDWORD lpMessageCount
,
2158 LPDWORD lpReadTimeout
)
2160 FILE_MAILSLOT_QUERY_INFORMATION info
;
2161 IO_STATUS_BLOCK iosb
;
2164 TRACE("%p %p %p %p %p\n",hMailslot
, lpMaxMessageSize
,
2165 lpNextSize
, lpMessageCount
, lpReadTimeout
);
2167 status
= NtQueryInformationFile( hMailslot
, &iosb
, &info
, sizeof info
,
2168 FileMailslotQueryInformation
);
2170 if( status
!= STATUS_SUCCESS
)
2172 SetLastError( RtlNtStatusToDosError(status
) );
2176 if( lpMaxMessageSize
)
2177 *lpMaxMessageSize
= info
.MaximumMessageSize
;
2179 *lpNextSize
= info
.NextMessageSize
;
2180 if( lpMessageCount
)
2181 *lpMessageCount
= info
.MessagesAvailable
;
2184 if (info
.ReadTimeout
.QuadPart
== (((LONGLONG
)0x7fffffff << 32) | 0xffffffff))
2185 *lpReadTimeout
= MAILSLOT_WAIT_FOREVER
;
2187 *lpReadTimeout
= info
.ReadTimeout
.QuadPart
/ -10000;
2193 /******************************************************************************
2194 * SetMailslotInfo [KERNEL32.@]
2196 * Set the read timeout of a mailslot.
2199 * hMailslot [I] Mailslot handle
2200 * dwReadTimeout [I] Timeout in milliseconds.
2206 BOOL WINAPI
SetMailslotInfo( HANDLE hMailslot
, DWORD dwReadTimeout
)
2208 FILE_MAILSLOT_SET_INFORMATION info
;
2209 IO_STATUS_BLOCK iosb
;
2212 TRACE("%p %d\n", hMailslot
, dwReadTimeout
);
2214 if (dwReadTimeout
!= MAILSLOT_WAIT_FOREVER
)
2215 info
.ReadTimeout
.QuadPart
= (ULONGLONG
)dwReadTimeout
* -10000;
2217 info
.ReadTimeout
.QuadPart
= ((LONGLONG
)0x7fffffff << 32) | 0xffffffff;
2218 status
= NtSetInformationFile( hMailslot
, &iosb
, &info
, sizeof info
,
2219 FileMailslotSetInformation
);
2220 if( status
!= STATUS_SUCCESS
)
2222 SetLastError( RtlNtStatusToDosError(status
) );
2229 /******************************************************************************
2230 * CreateIoCompletionPort (KERNEL32.@)
2232 HANDLE WINAPI
CreateIoCompletionPort(HANDLE hFileHandle
, HANDLE hExistingCompletionPort
,
2233 ULONG_PTR CompletionKey
, DWORD dwNumberOfConcurrentThreads
)
2238 TRACE("(%p, %p, %08lx, %08x)\n",
2239 hFileHandle
, hExistingCompletionPort
, CompletionKey
, dwNumberOfConcurrentThreads
);
2241 if (hExistingCompletionPort
&& hFileHandle
== INVALID_HANDLE_VALUE
)
2243 SetLastError( ERROR_INVALID_PARAMETER
);
2247 if (hExistingCompletionPort
)
2248 ret
= hExistingCompletionPort
;
2251 status
= NtCreateIoCompletion( &ret
, IO_COMPLETION_ALL_ACCESS
, NULL
, dwNumberOfConcurrentThreads
);
2252 if (status
!= STATUS_SUCCESS
) goto fail
;
2255 if (hFileHandle
!= INVALID_HANDLE_VALUE
)
2257 FILE_COMPLETION_INFORMATION info
;
2258 IO_STATUS_BLOCK iosb
;
2260 info
.CompletionPort
= ret
;
2261 info
.CompletionKey
= CompletionKey
;
2262 status
= NtSetInformationFile( hFileHandle
, &iosb
, &info
, sizeof(info
), FileCompletionInformation
);
2263 if (status
!= STATUS_SUCCESS
) goto fail
;
2269 if (ret
&& !hExistingCompletionPort
)
2271 SetLastError( RtlNtStatusToDosError(status
) );
2275 /******************************************************************************
2276 * GetQueuedCompletionStatus (KERNEL32.@)
2278 BOOL WINAPI
GetQueuedCompletionStatus( HANDLE CompletionPort
, LPDWORD lpNumberOfBytesTransferred
,
2279 PULONG_PTR pCompletionKey
, LPOVERLAPPED
*lpOverlapped
,
2280 DWORD dwMilliseconds
)
2283 IO_STATUS_BLOCK iosb
;
2284 LARGE_INTEGER wait_time
;
2286 TRACE("(%p,%p,%p,%p,%d)\n",
2287 CompletionPort
,lpNumberOfBytesTransferred
,pCompletionKey
,lpOverlapped
,dwMilliseconds
);
2289 *lpOverlapped
= NULL
;
2291 status
= NtRemoveIoCompletion( CompletionPort
, pCompletionKey
, (PULONG_PTR
)lpOverlapped
,
2292 &iosb
, get_nt_timeout( &wait_time
, dwMilliseconds
) );
2293 if (status
== STATUS_SUCCESS
)
2295 *lpNumberOfBytesTransferred
= iosb
.Information
;
2296 if (iosb
.u
.Status
>= 0) return TRUE
;
2297 SetLastError( RtlNtStatusToDosError(iosb
.u
.Status
) );
2301 if (status
== STATUS_TIMEOUT
) SetLastError( WAIT_TIMEOUT
);
2302 else SetLastError( RtlNtStatusToDosError(status
) );
2307 /******************************************************************************
2308 * PostQueuedCompletionStatus (KERNEL32.@)
2310 BOOL WINAPI
PostQueuedCompletionStatus( HANDLE CompletionPort
, DWORD dwNumberOfBytes
,
2311 ULONG_PTR dwCompletionKey
, LPOVERLAPPED lpOverlapped
)
2315 TRACE("%p %d %08lx %p\n", CompletionPort
, dwNumberOfBytes
, dwCompletionKey
, lpOverlapped
);
2317 status
= NtSetIoCompletion( CompletionPort
, dwCompletionKey
, (ULONG_PTR
)lpOverlapped
,
2318 STATUS_SUCCESS
, dwNumberOfBytes
);
2320 if (status
== STATUS_SUCCESS
) return TRUE
;
2321 SetLastError( RtlNtStatusToDosError(status
) );
2325 /******************************************************************************
2326 * BindIoCompletionCallback (KERNEL32.@)
2328 BOOL WINAPI
BindIoCompletionCallback( HANDLE FileHandle
, LPOVERLAPPED_COMPLETION_ROUTINE Function
, ULONG Flags
)
2332 TRACE("(%p, %p, %d)\n", FileHandle
, Function
, Flags
);
2334 status
= RtlSetIoCompletionCallback( FileHandle
, (PRTL_OVERLAPPED_COMPLETION_ROUTINE
)Function
, Flags
);
2335 if (status
== STATUS_SUCCESS
) return TRUE
;
2336 SetLastError( RtlNtStatusToDosError(status
) );
2341 /***********************************************************************
2342 * CreateMemoryResourceNotification (KERNEL32.@)
2344 HANDLE WINAPI
CreateMemoryResourceNotification(MEMORY_RESOURCE_NOTIFICATION_TYPE type
)
2346 static const WCHAR lowmemW
[] =
2347 {'\\','K','e','r','n','e','l','O','b','j','e','c','t','s',
2348 '\\','L','o','w','M','e','m','o','r','y','C','o','n','d','i','t','i','o','n',0};
2349 static const WCHAR highmemW
[] =
2350 {'\\','K','e','r','n','e','l','O','b','j','e','c','t','s',
2351 '\\','H','i','g','h','M','e','m','o','r','y','C','o','n','d','i','t','i','o','n',0};
2353 UNICODE_STRING nameW
;
2354 OBJECT_ATTRIBUTES attr
;
2359 case LowMemoryResourceNotification
:
2360 RtlInitUnicodeString( &nameW
, lowmemW
);
2362 case HighMemoryResourceNotification
:
2363 RtlInitUnicodeString( &nameW
, highmemW
);
2366 SetLastError( ERROR_INVALID_PARAMETER
);
2370 attr
.Length
= sizeof(attr
);
2371 attr
.RootDirectory
= 0;
2372 attr
.ObjectName
= &nameW
;
2373 attr
.Attributes
= 0;
2374 attr
.SecurityDescriptor
= NULL
;
2375 attr
.SecurityQualityOfService
= NULL
;
2376 status
= NtOpenEvent( &ret
, EVENT_ALL_ACCESS
, &attr
);
2377 if (status
!= STATUS_SUCCESS
)
2379 SetLastError( RtlNtStatusToDosError(status
) );
2385 /***********************************************************************
2386 * QueryMemoryResourceNotification (KERNEL32.@)
2388 BOOL WINAPI
QueryMemoryResourceNotification(HANDLE handle
, PBOOL state
)
2390 switch (WaitForSingleObject( handle
, 0 ))
2399 SetLastError( ERROR_INVALID_PARAMETER
);
2403 /***********************************************************************
2404 * InitOnceBeginInitialize (KERNEL32.@)
2406 BOOL WINAPI
InitOnceBeginInitialize( INIT_ONCE
*once
, DWORD flags
, BOOL
*pending
, void **context
)
2408 NTSTATUS status
= RtlRunOnceBeginInitialize( once
, flags
, context
);
2409 if (status
>= 0) *pending
= (status
== STATUS_PENDING
);
2410 else SetLastError( RtlNtStatusToDosError(status
) );
2414 /***********************************************************************
2415 * InitOnceComplete (KERNEL32.@)
2417 BOOL WINAPI
InitOnceComplete( INIT_ONCE
*once
, DWORD flags
, void *context
)
2419 NTSTATUS status
= RtlRunOnceComplete( once
, flags
, context
);
2420 if (status
!= STATUS_SUCCESS
) SetLastError( RtlNtStatusToDosError(status
) );
2424 /***********************************************************************
2425 * InitOnceExecuteOnce (KERNEL32.@)
2427 BOOL WINAPI
InitOnceExecuteOnce( INIT_ONCE
*once
, PINIT_ONCE_FN func
, void *param
, void **context
)
2429 return !RtlRunOnceExecuteOnce( once
, (PRTL_RUN_ONCE_INIT_FN
)func
, param
, context
);
2434 /***********************************************************************
2435 * InterlockedCompareExchange (KERNEL32.@)
2437 /* LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare ); */
2438 __ASM_STDCALL_FUNC(InterlockedCompareExchange
, 12,
2439 "movl 12(%esp),%eax\n\t"
2440 "movl 8(%esp),%ecx\n\t"
2441 "movl 4(%esp),%edx\n\t"
2442 "lock; cmpxchgl %ecx,(%edx)\n\t"
2445 /***********************************************************************
2446 * InterlockedExchange (KERNEL32.@)
2448 /* LONG WINAPI InterlockedExchange( PLONG dest, LONG val ); */
2449 __ASM_STDCALL_FUNC(InterlockedExchange
, 8,
2450 "movl 8(%esp),%eax\n\t"
2451 "movl 4(%esp),%edx\n\t"
2452 "lock; xchgl %eax,(%edx)\n\t"
2455 /***********************************************************************
2456 * InterlockedExchangeAdd (KERNEL32.@)
2458 /* LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr ); */
2459 __ASM_STDCALL_FUNC(InterlockedExchangeAdd
, 8,
2460 "movl 8(%esp),%eax\n\t"
2461 "movl 4(%esp),%edx\n\t"
2462 "lock; xaddl %eax,(%edx)\n\t"
2465 /***********************************************************************
2466 * InterlockedIncrement (KERNEL32.@)
2468 /* LONG WINAPI InterlockedIncrement( PLONG dest ); */
2469 __ASM_STDCALL_FUNC(InterlockedIncrement
, 4,
2470 "movl 4(%esp),%edx\n\t"
2472 "lock; xaddl %eax,(%edx)\n\t"
2476 /***********************************************************************
2477 * InterlockedDecrement (KERNEL32.@)
2479 __ASM_STDCALL_FUNC(InterlockedDecrement
, 4,
2480 "movl 4(%esp),%edx\n\t"
2482 "lock; xaddl %eax,(%edx)\n\t"
2486 #endif /* __i386__ */
2488 /***********************************************************************
2489 * SleepConditionVariableCS (KERNEL32.@)
2491 BOOL WINAPI
SleepConditionVariableCS( CONDITION_VARIABLE
*variable
, CRITICAL_SECTION
*crit
, DWORD timeout
)
2496 status
= RtlSleepConditionVariableCS( variable
, crit
, get_nt_timeout( &time
, timeout
) );
2498 if (status
!= STATUS_SUCCESS
)
2500 SetLastError( RtlNtStatusToDosError(status
) );
2506 /***********************************************************************
2507 * SleepConditionVariableSRW (KERNEL32.@)
2509 BOOL WINAPI
SleepConditionVariableSRW( RTL_CONDITION_VARIABLE
*variable
, RTL_SRWLOCK
*lock
, DWORD timeout
, ULONG flags
)
2514 status
= RtlSleepConditionVariableSRW( variable
, lock
, get_nt_timeout( &time
, timeout
), flags
);
2516 if (status
!= STATUS_SUCCESS
)
2518 SetLastError( RtlNtStatusToDosError(status
) );