2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Win32 Kernel Library
4 * FILE: lib/kernel32/file/npipe.c
5 * PURPOSE: Named Pipe Functions
6 * PROGRAMMER: Alex Ionescu (alex@relsoft.net)
7 * Ariadne ( ariadne@xs4all.nl)
10 /* INCLUDES *****************************************************************/
15 //#define USING_PROPER_NPFS_WAIT_SEMANTICS
16 #include "../include/debug.h"
18 /* FUNCTIONS ****************************************************************/
25 CreateNamedPipeA(LPCSTR lpName
,
31 DWORD nDefaultTimeOut
,
32 LPSECURITY_ATTRIBUTES lpSecurityAttributes
)
34 PUNICODE_STRING NameU
= &NtCurrentTeb()->StaticUnicodeString
;
37 /* Initialize the string as ANSI_STRING and convert to Unicode */
38 RtlInitAnsiString(&NameA
, (LPSTR
)lpName
);
39 RtlAnsiStringToUnicodeString(NameU
, &NameA
, FALSE
);
41 /* Call the Unicode API */
42 return CreateNamedPipeW(NameU
->Buffer
,
49 lpSecurityAttributes
);
57 CreateNamedPipeW(LPCWSTR lpName
,
63 DWORD nDefaultTimeOut
,
64 LPSECURITY_ATTRIBUTES lpSecurityAttributes
)
66 UNICODE_STRING NamedPipeName
;
69 OBJECT_ATTRIBUTES ObjectAttributes
;
71 ACCESS_MASK DesiredAccess
;
72 ULONG CreateOptions
= 0;
73 ULONG WriteModeMessage
;
74 ULONG ReadModeMessage
;
77 ULONG ShareAccess
= 0, Attributes
;
78 LARGE_INTEGER DefaultTimeOut
;
79 PSECURITY_DESCRIPTOR SecurityDescriptor
= NULL
;
81 /* Check for valid instances */
82 if (nMaxInstances
== 0 || nMaxInstances
> PIPE_UNLIMITED_INSTANCES
)
85 SetLastError(ERROR_INVALID_PARAMETER
);
86 return INVALID_HANDLE_VALUE
;
89 /* Convert to NT syntax */
90 if (nMaxInstances
== PIPE_UNLIMITED_INSTANCES
) nMaxInstances
= -1;
92 /* Convert the name */
93 Result
= RtlDosPathNameToNtPathName_U((LPWSTR
)lpName
,
99 /* Conversion failed */
100 SetLastError(ERROR_PATH_NOT_FOUND
);
101 return(INVALID_HANDLE_VALUE
);
104 DPRINT("Pipe name: %wZ\n", &NamedPipeName
);
105 DPRINT("Pipe name: %S\n", NamedPipeName
.Buffer
);
107 /* Always case insensitive, check if we got extra attributes */
108 Attributes
= OBJ_CASE_INSENSITIVE
;
109 if(lpSecurityAttributes
)
111 /* We did; get the security descriptor */
112 SecurityDescriptor
= lpSecurityAttributes
->lpSecurityDescriptor
;
114 /* And check if this is pipe's handle will beinheritable */
115 if(lpSecurityAttributes
->bInheritHandle
) Attributes
|= OBJ_INHERIT
;
118 /* Now we can initialize the object attributes */
119 InitializeObjectAttributes(&ObjectAttributes
,
125 /* Setup the default Desired Access */
126 DesiredAccess
= SYNCHRONIZE
| (dwOpenMode
& (WRITE_DAC
|
128 ACCESS_SYSTEM_SECURITY
));
130 /* Convert to NT Create Flags */
131 if (dwOpenMode
& FILE_FLAG_WRITE_THROUGH
)
133 CreateOptions
|= FILE_WRITE_THROUGH
;
135 if (!(dwOpenMode
& FILE_FLAG_OVERLAPPED
))
137 CreateOptions
|= FILE_SYNCHRONOUS_IO_NONALERT
;
140 /* Handle all open modes */
141 if (dwOpenMode
& PIPE_ACCESS_OUTBOUND
)
143 ShareAccess
|= FILE_SHARE_READ
;
144 DesiredAccess
|= GENERIC_WRITE
;
146 if (dwOpenMode
& PIPE_ACCESS_INBOUND
)
148 ShareAccess
|= FILE_SHARE_WRITE
;
149 DesiredAccess
|= GENERIC_READ
;
152 /* Handle the type flags */
153 if (dwPipeMode
& PIPE_TYPE_MESSAGE
)
155 WriteModeMessage
= FILE_PIPE_MESSAGE_TYPE
;
159 WriteModeMessage
= FILE_PIPE_BYTE_STREAM_TYPE
;
162 /* Handle the mode flags */
163 if (dwPipeMode
& PIPE_READMODE_MESSAGE
)
165 ReadModeMessage
= FILE_PIPE_MESSAGE_MODE
;
169 ReadModeMessage
= FILE_PIPE_BYTE_STREAM_MODE
;
172 /* Handle the blocking mode */
173 if (dwPipeMode
& PIPE_NOWAIT
)
175 NonBlocking
= FILE_PIPE_COMPLETE_OPERATION
;
179 NonBlocking
= FILE_PIPE_QUEUE_OPERATION
;
182 /* Check if we have a timeout */
185 /* Convert the time to NT format */
186 DefaultTimeOut
.QuadPart
= UInt32x32To64(nDefaultTimeOut
, -10000);
190 /* Use default timeout of 50 ms */
191 DefaultTimeOut
.QuadPart
= -500000;
194 /* Now create the pipe */
195 Status
= NtCreateNamedPipeFile(&PipeHandle
,
210 /* Normalize special error codes */
211 if ((Status
== STATUS_INVALID_DEVICE_REQUEST
) ||
212 (Status
== STATUS_NOT_SUPPORTED
))
214 Status
= STATUS_OBJECT_NAME_INVALID
;
218 RtlFreeUnicodeString(&NamedPipeName
);
221 if (!NT_SUCCESS(Status
))
223 /* Failed to create it */
224 DPRINT1("NtCreateNamedPipe failed (Status %x)!\n", Status
);
225 SetLastErrorByStatus (Status
);
226 return INVALID_HANDLE_VALUE
;
229 /* Return the handle */
238 WaitNamedPipeA(LPCSTR lpNamedPipeName
,
242 UNICODE_STRING NameU
;
244 /* Convert the name to Unicode */
245 Basep8BitStringToLiveUnicodeString(&NameU
, lpNamedPipeName
);
247 /* Call the Unicode API */
248 r
= WaitNamedPipeW(NameU
.Buffer
, nTimeOut
);
250 /* Free the Unicode string */
251 RtlFreeUnicodeString(&NameU
);
258 * When NPFS will work properly, use this code instead. It is compatible with
259 * Microsoft's NPFS.SYS. The main difference is that:
260 * - This code actually respects the timeout instead of ignoring it!
261 * - This code validates and creates the proper names for both UNC and local pipes
262 * - On NT, you open the *root* pipe directory (either \DosDevices\Pipe or
263 * \DosDevices\Unc\Server\Pipe) and then send the pipe to wait on in the
264 * FILE_PIPE_WAIT_FOR_BUFFER structure.
266 #ifdef USING_PROPER_NPFS_WAIT_SEMANTICS
272 WaitNamedPipeW(LPCWSTR lpNamedPipeName
,
275 UNICODE_STRING NamedPipeName
, NewName
, DevicePath
, PipePrefix
;
280 OBJECT_ATTRIBUTES ObjectAttributes
;
283 IO_STATUS_BLOCK IoStatusBlock
;
284 ULONG WaitPipeInfoSize
;
285 PFILE_PIPE_WAIT_FOR_BUFFER WaitPipeInfo
;
287 /* Start by making a unicode string of the name */
288 DPRINT("Sent path: %S\n", lpNamedPipeName
);
289 RtlCreateUnicodeString(&NamedPipeName
, lpNamedPipeName
);
290 NameLength
= NamedPipeName
.Length
/ sizeof(WCHAR
);
292 /* All slashes must become backslashes */
293 for (i
= 0; i
< NameLength
; i
++)
295 /* Check and convert */
296 if (NamedPipeName
.Buffer
[i
] == L
'/') NamedPipeName
.Buffer
[i
] = L
'\\';
299 /* Find the path type of the name we were given */
300 NewName
= NamedPipeName
;
301 Type
= RtlDetermineDosPathNameType_U(lpNamedPipeName
);
303 /* Check if this was a device path, ie : "\\.\pipe\name" */
304 if (Type
== DEVICE_PATH
)
306 /* Make sure it's a valid prefix */
307 RtlInitUnicodeString(&PipePrefix
, L
"\\\\.\\pipe\\");
308 RtlPrefixString((PANSI_STRING
)&PipePrefix
, (PANSI_STRING
)&NewName
, TRUE
);
312 NewName
.Length
-= 9 * sizeof(WCHAR
);
314 /* Initialize the Dos Devices name */
315 DPRINT("NewName: %wZ\n", &NewName
);
316 RtlInitUnicodeString(&DevicePath
, L
"\\DosDevices\\pipe\\");
318 else if (Type
== UNC_PATH
)
320 /* The path is \\server\\pipe\name; find the pipename itself */
321 p
= &NewName
.Buffer
[2];
323 /* First loop to get past the server name */
326 /* Check if this is a backslash */
327 if (*p
== L
'\\') break;
333 /* Now make sure the full name contains "pipe\" */
334 if ((*p
) && !(_wcsnicmp(p
+ 1, L
"pipe\\", sizeof("pipe\\"))))
336 /* Get to the pipe name itself now */
337 p
+= sizeof("pipe\\") - 1;
341 /* The name is invalid */
342 DPRINT1("Invalid name!\n");
343 SetLastErrorByStatus(STATUS_OBJECT_PATH_SYNTAX_BAD
);
347 /* FIXME: Open \DosDevices\Unc\Server\Pipe\Name */
351 DPRINT1("Invalid path type\n");
352 SetLastErrorByStatus(STATUS_OBJECT_PATH_SYNTAX_BAD
);
356 /* Now calculate the total length of the structure and allocate it */
357 WaitPipeInfoSize
= FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER
, Name
[0]) +
359 WaitPipeInfo
= RtlAllocateHeap(RtlGetProcessHeap(), 0, WaitPipeInfoSize
);
360 if (WaitPipeInfo
== NULL
)
362 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
366 /* Initialize the object attributes */
367 DPRINT("Opening: %wZ\n", &DevicePath
);
368 InitializeObjectAttributes(&ObjectAttributes
,
370 OBJ_CASE_INSENSITIVE
,
375 Status
= NtOpenFile(&FileHandle
,
376 FILE_READ_ATTRIBUTES
| SYNCHRONIZE
,
379 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
380 FILE_SYNCHRONOUS_IO_NONALERT
);
381 if (!NT_SUCCESS(Status
))
383 /* Fail; couldn't open */
384 DPRINT1("Status: %lx\n", Status
);
385 SetLastErrorByStatus(Status
);
386 RtlFreeUnicodeString(&NamedPipeName
);
387 RtlFreeHeap(RtlGetProcessHeap(), 0, WaitPipeInfo
);
391 /* Check what timeout we got */
392 if (nTimeOut
== NMPWAIT_USE_DEFAULT_WAIT
)
394 /* Don't use a timeout */
395 WaitPipeInfo
->TimeoutSpecified
= FALSE
;
399 /* Check if we should wait forever */
400 if (nTimeOut
== NMPWAIT_WAIT_FOREVER
)
403 WaitPipeInfo
->Timeout
.LowPart
= 0;
404 WaitPipeInfo
->Timeout
.HighPart
= 0x80000000;
408 /* Convert to NT format */
409 WaitPipeInfo
->Timeout
.QuadPart
= UInt32x32To64(-10000, nTimeOut
);
412 /* In both cases, we do have a timeout */
413 WaitPipeInfo
->TimeoutSpecified
= FALSE
;
416 /* Set the length and copy the name */
417 WaitPipeInfo
->NameLength
= NewName
.Length
;
418 RtlCopyMemory(WaitPipeInfo
->Name
, NewName
.Buffer
, NewName
.Length
);
420 /* Get rid of the full name */
421 RtlFreeUnicodeString(&NamedPipeName
);
423 /* Let NPFS know of our request */
424 Status
= NtFsControlFile(FileHandle
,
435 /* Free our pipe info data and close the handle */
436 RtlFreeHeap(RtlGetProcessHeap(), 0, WaitPipeInfo
);
439 /* Check the status */
440 if (!NT_SUCCESS(Status
))
442 /* Failure to wait on the pipe */
443 DPRINT1("Status: %lx\n", Status
);
444 SetLastErrorByStatus (Status
);
456 WaitNamedPipeW(LPCWSTR lpNamedPipeName
,
459 UNICODE_STRING NamedPipeName
;
462 OBJECT_ATTRIBUTES ObjectAttributes
;
463 FILE_PIPE_WAIT_FOR_BUFFER WaitPipe
;
465 IO_STATUS_BLOCK Iosb
;
467 r
= RtlDosPathNameToNtPathName_U((LPWSTR
)lpNamedPipeName
,
476 InitializeObjectAttributes(&ObjectAttributes
,
478 OBJ_CASE_INSENSITIVE
,
481 Status
= NtOpenFile(&FileHandle
,
482 FILE_READ_ATTRIBUTES
| SYNCHRONIZE
,
485 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
486 FILE_SYNCHRONOUS_IO_NONALERT
);
487 if (!NT_SUCCESS(Status
))
489 SetLastErrorByStatus (Status
);
493 WaitPipe
.Timeout
.QuadPart
= nTimeOut
* -10000LL;
495 Status
= NtFsControlFile(FileHandle
,
506 if (!NT_SUCCESS(Status
))
508 SetLastErrorByStatus (Status
);
520 ConnectNamedPipe(IN HANDLE hNamedPipe
,
521 IN LPOVERLAPPED lpOverlapped
)
525 if (lpOverlapped
!= NULL
)
529 lpOverlapped
->Internal
= STATUS_PENDING
;
530 ApcContext
= (((ULONG_PTR
)lpOverlapped
->hEvent
& 0x1) ? NULL
: lpOverlapped
);
532 Status
= NtFsControlFile(hNamedPipe
,
533 lpOverlapped
->hEvent
,
536 (PIO_STATUS_BLOCK
)lpOverlapped
,
543 /* return FALSE in case of failure and pending operations! */
544 if (!NT_SUCCESS(Status
) || Status
== STATUS_PENDING
)
546 SetLastErrorByStatus(Status
);
552 IO_STATUS_BLOCK Iosb
;
554 Status
= NtFsControlFile(hNamedPipe
,
565 /* wait in case operation is pending */
566 if (Status
== STATUS_PENDING
)
568 Status
= NtWaitForSingleObject(hNamedPipe
,
571 if (NT_SUCCESS(Status
))
573 Status
= Iosb
.Status
;
577 if (!NT_SUCCESS(Status
))
579 SetLastErrorByStatus(Status
);
592 SetNamedPipeHandleState(HANDLE hNamedPipe
,
594 LPDWORD lpMaxCollectionCount
,
595 LPDWORD lpCollectDataTimeout
)
597 IO_STATUS_BLOCK Iosb
;
600 /* Check if the Mode is being changed */
603 FILE_PIPE_INFORMATION Settings
;
605 /* Set the Completion Mode */
606 Settings
.CompletionMode
= (*lpMode
& PIPE_NOWAIT
) ?
607 FILE_PIPE_COMPLETE_OPERATION
: FILE_PIPE_QUEUE_OPERATION
;
609 /* Set the Read Mode */
610 Settings
.ReadMode
= (*lpMode
& PIPE_READMODE_MESSAGE
) ?
611 FILE_PIPE_MESSAGE_MODE
: FILE_PIPE_BYTE_STREAM_MODE
;
613 /* Send the changes to the Driver */
614 Status
= NtSetInformationFile(hNamedPipe
,
617 sizeof(FILE_PIPE_INFORMATION
),
618 FilePipeInformation
);
619 if (!NT_SUCCESS(Status
))
621 SetLastErrorByStatus(Status
);
626 /* Check if the Collection count or Timeout are being changed */
627 if (lpMaxCollectionCount
|| lpCollectDataTimeout
)
629 FILE_PIPE_REMOTE_INFORMATION RemoteSettings
;
631 /* Setting one without the other would delete it, so we read old one */
632 if (!lpMaxCollectionCount
|| !lpCollectDataTimeout
)
634 Status
= NtQueryInformationFile(hNamedPipe
,
637 sizeof(FILE_PIPE_REMOTE_INFORMATION
),
638 FilePipeRemoteInformation
);
640 if (!NT_SUCCESS(Status
))
642 SetLastErrorByStatus(Status
);
647 /* Now set the new settings */
648 RemoteSettings
.MaximumCollectionCount
= (lpMaxCollectionCount
) ?
649 *lpMaxCollectionCount
:
650 RemoteSettings
.MaximumCollectionCount
;
651 if (lpCollectDataTimeout
)
653 /* Convert it to Quad */
654 RemoteSettings
.CollectDataTime
.QuadPart
= -(LONGLONG
)
656 *lpCollectDataTimeout
);
659 /* Tell the driver to change them */
660 Status
= NtSetInformationFile(hNamedPipe
,
663 sizeof(FILE_PIPE_REMOTE_INFORMATION
),
664 FilePipeRemoteInformation
);
666 if (!NT_SUCCESS(Status
))
668 SetLastErrorByStatus(Status
);
682 CallNamedPipeA(LPCSTR lpNamedPipeName
,
686 DWORD nOutBufferSize
,
690 PUNICODE_STRING PipeName
= &NtCurrentTeb()->StaticUnicodeString
;
691 ANSI_STRING AnsiPipe
;
693 /* Initialize the string as ANSI_STRING and convert to Unicode */
694 RtlInitAnsiString(&AnsiPipe
, (LPSTR
)lpNamedPipeName
);
695 RtlAnsiStringToUnicodeString(PipeName
, &AnsiPipe
, FALSE
);
697 /* Call the Unicode function */
698 return CallNamedPipeW(PipeName
->Buffer
,
712 CallNamedPipeW(LPCWSTR lpNamedPipeName
,
716 DWORD nOutBufferSize
,
727 /* Try creating it */
728 hPipe
= CreateFileW(lpNamedPipeName
,
729 GENERIC_READ
| GENERIC_WRITE
,
730 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
733 FILE_ATTRIBUTE_NORMAL
,
736 /* Success, break out */
737 if (hPipe
!= INVALID_HANDLE_VALUE
) break;
739 /* Already tried twice, give up */
740 if (bRetry
== FALSE
) return FALSE
;
743 WaitNamedPipeW(lpNamedPipeName
, nTimeOut
);
745 /* Get ready to try again */
749 /* Set the pipe mode */
750 dwPipeMode
= PIPE_READMODE_MESSAGE
| PIPE_WAIT
;
751 bError
= SetNamedPipeHandleState(hPipe
, &dwPipeMode
, NULL
, NULL
);
754 /* Couldn't change state, fail */
759 /* Do the transact */
760 bError
= TransactNamedPipe(hPipe
,
768 /* Close the handle and return */
778 DisconnectNamedPipe(HANDLE hNamedPipe
)
780 IO_STATUS_BLOCK Iosb
;
783 /* Send the FSCTL to the driver */
784 Status
= NtFsControlFile(hNamedPipe
,
789 FSCTL_PIPE_DISCONNECT
,
794 if (Status
== STATUS_PENDING
)
796 /* Wait on NPFS to finish and get updated status */
797 Status
= NtWaitForSingleObject(hNamedPipe
, FALSE
, NULL
);
798 if (NT_SUCCESS(Status
)) Status
= Iosb
.Status
;
801 /* Check for error */
802 if (!NT_SUCCESS(Status
))
805 SetLastErrorByStatus(Status
);
816 GetNamedPipeHandleStateW(HANDLE hNamedPipe
,
818 LPDWORD lpCurInstances
,
819 LPDWORD lpMaxCollectionCount
,
820 LPDWORD lpCollectDataTimeout
,
822 DWORD nMaxUserNameSize
)
824 IO_STATUS_BLOCK StatusBlock
;
829 FILE_PIPE_INFORMATION PipeInfo
;
831 Status
= NtQueryInformationFile(hNamedPipe
,
834 sizeof(FILE_PIPE_INFORMATION
),
835 FilePipeInformation
);
836 if (!NT_SUCCESS(Status
))
838 SetLastErrorByStatus(Status
);
842 *lpState
= ((PipeInfo
.CompletionMode
!= FILE_PIPE_QUEUE_OPERATION
) ? PIPE_NOWAIT
: PIPE_WAIT
);
843 *lpState
|= ((PipeInfo
.ReadMode
!= FILE_PIPE_BYTE_STREAM_MODE
) ? PIPE_READMODE_MESSAGE
: PIPE_READMODE_BYTE
);
846 if(lpCurInstances
!= NULL
)
848 FILE_PIPE_LOCAL_INFORMATION LocalInfo
;
850 Status
= NtQueryInformationFile(hNamedPipe
,
853 sizeof(FILE_PIPE_LOCAL_INFORMATION
),
854 FilePipeLocalInformation
);
855 if(!NT_SUCCESS(Status
))
857 SetLastErrorByStatus(Status
);
861 *lpCurInstances
= min(LocalInfo
.CurrentInstances
, PIPE_UNLIMITED_INSTANCES
);
864 if(lpMaxCollectionCount
!= NULL
|| lpCollectDataTimeout
!= NULL
)
866 FILE_PIPE_REMOTE_INFORMATION RemoteInfo
;
868 Status
= NtQueryInformationFile(hNamedPipe
,
871 sizeof(FILE_PIPE_REMOTE_INFORMATION
),
872 FilePipeRemoteInformation
);
873 if(!NT_SUCCESS(Status
))
875 SetLastErrorByStatus(Status
);
879 if(lpMaxCollectionCount
!= NULL
)
881 *lpMaxCollectionCount
= RemoteInfo
.MaximumCollectionCount
;
884 if(lpCollectDataTimeout
!= NULL
)
887 *lpCollectDataTimeout
= 0;
891 if(lpUserName
!= NULL
)
893 /* FIXME - open the thread token, call ImpersonateNamedPipeClient() and
894 retreive the user name with GetUserName(), revert the impersonation
895 and finally restore the thread token */
906 GetNamedPipeHandleStateA(HANDLE hNamedPipe
,
908 LPDWORD lpCurInstances
,
909 LPDWORD lpMaxCollectionCount
,
910 LPDWORD lpCollectDataTimeout
,
912 DWORD nMaxUserNameSize
)
914 UNICODE_STRING UserNameW
= {0};
915 ANSI_STRING UserNameA
;
918 if(lpUserName
!= NULL
)
920 UserNameW
.MaximumLength
= nMaxUserNameSize
* sizeof(WCHAR
);
921 UserNameW
.Buffer
= RtlAllocateHeap(RtlGetProcessHeap(), 0, UserNameW
.MaximumLength
);
922 if (UserNameW
.Buffer
== NULL
)
924 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
928 UserNameA
.Buffer
= lpUserName
;
929 UserNameA
.Length
= 0;
930 UserNameA
.MaximumLength
= nMaxUserNameSize
;
933 Ret
= GetNamedPipeHandleStateW(hNamedPipe
,
936 lpMaxCollectionCount
,
937 lpCollectDataTimeout
,
941 if(Ret
&& lpUserName
!= NULL
)
945 RtlInitUnicodeString(&UserNameW
, UserNameW
.Buffer
);
946 Status
= RtlUnicodeStringToAnsiString(&UserNameA
, &UserNameW
, FALSE
);
947 if(!NT_SUCCESS(Status
))
949 SetLastErrorByStatus(Status
);
954 if(UserNameW
.Buffer
!= NULL
)
956 RtlFreeHeap(RtlGetProcessHeap(), 0, UserNameW
.Buffer
);
967 GetNamedPipeInfo(HANDLE hNamedPipe
,
969 LPDWORD lpOutBufferSize
,
970 LPDWORD lpInBufferSize
,
971 LPDWORD lpMaxInstances
)
973 FILE_PIPE_LOCAL_INFORMATION PipeLocalInformation
;
974 IO_STATUS_BLOCK StatusBlock
;
977 Status
= NtQueryInformationFile(hNamedPipe
,
979 &PipeLocalInformation
,
980 sizeof(FILE_PIPE_LOCAL_INFORMATION
),
981 FilePipeLocalInformation
);
982 if (!NT_SUCCESS(Status
))
984 SetLastErrorByStatus(Status
);
990 *lpFlags
= (PipeLocalInformation
.NamedPipeEnd
== FILE_PIPE_SERVER_END
) ? PIPE_SERVER_END
: PIPE_CLIENT_END
;
991 *lpFlags
|= (PipeLocalInformation
.NamedPipeType
== 1) ? PIPE_TYPE_MESSAGE
: PIPE_TYPE_BYTE
;
994 if (lpOutBufferSize
!= NULL
)
995 *lpOutBufferSize
= PipeLocalInformation
.OutboundQuota
;
997 if (lpInBufferSize
!= NULL
)
998 *lpInBufferSize
= PipeLocalInformation
.InboundQuota
;
1000 if (lpMaxInstances
!= NULL
)
1002 if (PipeLocalInformation
.MaximumInstances
>= 255)
1003 *lpMaxInstances
= PIPE_UNLIMITED_INSTANCES
;
1005 *lpMaxInstances
= PipeLocalInformation
.MaximumInstances
;
1016 PeekNamedPipe(HANDLE hNamedPipe
,
1019 LPDWORD lpBytesRead
,
1020 LPDWORD lpTotalBytesAvail
,
1021 LPDWORD lpBytesLeftThisMessage
)
1023 PFILE_PIPE_PEEK_BUFFER Buffer
;
1024 IO_STATUS_BLOCK Iosb
;
1028 /* Calculate the buffer space that we'll need and allocate it */
1029 BufferSize
= nBufferSize
+ FIELD_OFFSET(FILE_PIPE_PEEK_BUFFER
, Data
[0]);
1030 Buffer
= RtlAllocateHeap(RtlGetProcessHeap(), 0, BufferSize
);
1033 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1037 /* Tell the driver to seek */
1038 Status
= NtFsControlFile(hNamedPipe
,
1048 if (Status
== STATUS_PENDING
)
1050 /* Wait for npfs to be done, and update the status */
1051 Status
= NtWaitForSingleObject(hNamedPipe
, FALSE
, NULL
);
1052 if (NT_SUCCESS(Status
)) Status
= Iosb
.Status
;
1055 /* Overflow is success for us */
1056 if (Status
== STATUS_BUFFER_OVERFLOW
) Status
= STATUS_SUCCESS
;
1059 if (!NT_SUCCESS(Status
))
1061 /* Free the buffer and return failure */
1062 RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer
);
1063 SetLastErrorByStatus(Status
);
1067 /* Check if caller requested bytes available */
1068 if (lpTotalBytesAvail
) *lpTotalBytesAvail
= Buffer
->ReadDataAvailable
;
1070 /* Check if caller requested bytes read */
1073 /* Calculate the bytes returned, minus our structure overhead */
1074 *lpBytesRead
= (ULONG
)(Iosb
.Information
-
1075 FIELD_OFFSET(FILE_PIPE_PEEK_BUFFER
, Data
[0]));
1078 /* Check if caller requested bytes left */
1079 if (lpBytesLeftThisMessage
)
1081 /* Calculate total minus what we returned and our structure overhead */
1082 *lpBytesLeftThisMessage
= Buffer
->MessageLength
-
1083 (ULONG
)(Iosb
.Information
-
1084 FIELD_OFFSET(FILE_PIPE_PEEK_BUFFER
, Data
[0]));
1087 /* Check if the caller wanted to see the actual data */
1090 /* Give him what he wants */
1091 RtlCopyMemory(lpBuffer
,
1094 FIELD_OFFSET(FILE_PIPE_PEEK_BUFFER
, Data
[0]));
1097 /* Free the buffer and return success */
1098 RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer
);
1106 TransactNamedPipe(IN HANDLE hNamedPipe
,
1107 IN LPVOID lpInBuffer
,
1108 IN DWORD nInBufferSize
,
1109 OUT LPVOID lpOutBuffer
,
1110 IN DWORD nOutBufferSize
,
1111 OUT LPDWORD lpBytesRead OPTIONAL
,
1112 IN LPOVERLAPPED lpOverlapped OPTIONAL
)
1116 if (lpBytesRead
!= NULL
)
1121 if (lpOverlapped
!= NULL
)
1125 ApcContext
= (((ULONG_PTR
)lpOverlapped
->hEvent
& 0x1) ? NULL
: lpOverlapped
);
1126 lpOverlapped
->Internal
= STATUS_PENDING
;
1128 Status
= NtFsControlFile(hNamedPipe
,
1129 lpOverlapped
->hEvent
,
1132 (PIO_STATUS_BLOCK
)lpOverlapped
,
1133 FSCTL_PIPE_TRANSCEIVE
,
1139 /* return FALSE in case of failure and pending operations! */
1140 if (!NT_SUCCESS(Status
) || Status
== STATUS_PENDING
)
1142 SetLastErrorByStatus(Status
);
1146 if (lpBytesRead
!= NULL
)
1148 *lpBytesRead
= lpOverlapped
->InternalHigh
;
1153 IO_STATUS_BLOCK Iosb
;
1155 Status
= NtFsControlFile(hNamedPipe
,
1160 FSCTL_PIPE_TRANSCEIVE
,
1166 /* wait in case operation is pending */
1167 if (Status
== STATUS_PENDING
)
1169 Status
= NtWaitForSingleObject(hNamedPipe
,
1172 if (NT_SUCCESS(Status
))
1174 Status
= Iosb
.Status
;
1178 if (NT_SUCCESS(Status
))
1180 /* lpNumberOfBytesRead must not be NULL here, in fact Win doesn't
1181 check that case either and crashes (only after the operation
1183 *lpBytesRead
= Iosb
.Information
;
1187 SetLastErrorByStatus(Status
);