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
,
211 RtlFreeUnicodeString(&NamedPipeName
);
214 if (!NT_SUCCESS(Status
))
216 /* Failed to create it */
217 DPRINT1("NtCreateNamedPipe failed (Status %x)!\n", Status
);
218 SetLastErrorByStatus (Status
);
219 return INVALID_HANDLE_VALUE
;
222 /* Return the handle */
231 WaitNamedPipeA(LPCSTR lpNamedPipeName
,
235 UNICODE_STRING NameU
;
237 /* Convert the name to Unicode */
238 Basep8BitStringToLiveUnicodeString(&NameU
, lpNamedPipeName
);
240 /* Call the Unicode API */
241 r
= WaitNamedPipeW(NameU
.Buffer
, nTimeOut
);
243 /* Free the Unicode string */
244 RtlFreeUnicodeString(&NameU
);
251 * When NPFS will work properly, use this code instead. It is compatible with
252 * Microsoft's NPFS.SYS. The main difference is that:
253 * - This code actually respects the timeout instead of ignoring it!
254 * - This code validates and creates the proper names for both UNC and local pipes
255 * - On NT, you open the *root* pipe directory (either \DosDevices\Pipe or
256 * \DosDevices\Unc\Server\Pipe) and then send the pipe to wait on in the
257 * FILE_PIPE_WAIT_FOR_BUFFER structure.
259 #ifdef USING_PROPER_NPFS_WAIT_SEMANTICS
265 WaitNamedPipeW(LPCWSTR lpNamedPipeName
,
268 UNICODE_STRING NamedPipeName
, NewName
, DevicePath
, PipePrefix
;
273 OBJECT_ATTRIBUTES ObjectAttributes
;
276 IO_STATUS_BLOCK IoStatusBlock
;
277 ULONG WaitPipeInfoSize
;
278 PFILE_PIPE_WAIT_FOR_BUFFER WaitPipeInfo
;
280 /* Start by making a unicode string of the name */
281 DPRINT("Sent path: %S\n", lpNamedPipeName
);
282 RtlCreateUnicodeString(&NamedPipeName
, lpNamedPipeName
);
283 NameLength
= NamedPipeName
.Length
/ sizeof(WCHAR
);
285 /* All slashes must become backslashes */
286 for (i
= 0; i
< NameLength
; i
++)
288 /* Check and convert */
289 if (NamedPipeName
.Buffer
[i
] == L
'/') NamedPipeName
.Buffer
[i
] = L
'\\';
292 /* Find the path type of the name we were given */
293 NewName
= NamedPipeName
;
294 Type
= RtlDetermineDosPathNameType_U(lpNamedPipeName
);
296 /* Check if this was a device path, ie : "\\.\pipe\name" */
297 if (Type
== DEVICE_PATH
)
299 /* Make sure it's a valid prefix */
300 RtlInitUnicodeString(&PipePrefix
, L
"\\\\.\\pipe\\");
301 RtlPrefixString((PANSI_STRING
)&PipePrefix
, (PANSI_STRING
)&NewName
, TRUE
);
305 NewName
.Length
-= 9 * sizeof(WCHAR
);
307 /* Initialize the Dos Devices name */
308 DPRINT("NewName: %wZ\n", &NewName
);
309 RtlInitUnicodeString(&DevicePath
, L
"\\DosDevices\\pipe\\");
311 else if (Type
== UNC_PATH
)
313 /* The path is \\server\\pipe\name; find the pipename itself */
314 p
= &NewName
.Buffer
[2];
316 /* First loop to get past the server name */
319 /* Check if this is a backslash */
320 if (*p
== L
'\\') break;
326 /* Now make sure the full name contains "pipe\" */
327 if ((*p
) && !(_wcsnicmp(p
+ 1, L
"pipe\\", sizeof("pipe\\"))))
329 /* Get to the pipe name itself now */
330 p
+= sizeof("pipe\\") - 1;
334 /* The name is invalid */
335 DPRINT1("Invalid name!\n");
336 SetLastErrorByStatus(STATUS_OBJECT_PATH_SYNTAX_BAD
);
340 /* FIXME: Open \DosDevices\Unc\Server\Pipe\Name */
344 DPRINT1("Invalid path type\n");
345 SetLastErrorByStatus(STATUS_OBJECT_PATH_SYNTAX_BAD
);
349 /* Initialize the object attributes */
350 DPRINT("Opening: %wZ\n", &DevicePath
);
351 InitializeObjectAttributes(&ObjectAttributes
,
353 OBJ_CASE_INSENSITIVE
,
358 Status
= NtOpenFile(&FileHandle
,
359 FILE_READ_ATTRIBUTES
| SYNCHRONIZE
,
362 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
363 FILE_SYNCHRONOUS_IO_NONALERT
);
364 if (!NT_SUCCESS(Status
))
366 /* Fail; couldn't open */
367 DPRINT1("Status: %lx\n", Status
);
368 SetLastErrorByStatus(Status
);
369 RtlFreeUnicodeString(&NamedPipeName
);
373 /* Now calculate the total length of the structure and allocate it */
374 WaitPipeInfoSize
= FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER
, Name
[0]) +
376 WaitPipeInfo
= RtlAllocateHeap(RtlGetProcessHeap(), 0, WaitPipeInfoSize
);
378 /* Check what timeout we got */
379 if (nTimeOut
== NMPWAIT_USE_DEFAULT_WAIT
)
381 /* Don't use a timeout */
382 WaitPipeInfo
->TimeoutSpecified
= FALSE
;
386 /* Check if we should wait forever */
387 if (nTimeOut
== NMPWAIT_WAIT_FOREVER
)
390 WaitPipeInfo
->Timeout
.LowPart
= 0;
391 WaitPipeInfo
->Timeout
.HighPart
= 0x80000000;
395 /* Convert to NT format */
396 WaitPipeInfo
->Timeout
.QuadPart
= UInt32x32To64(-10000, nTimeOut
);
399 /* In both cases, we do have a timeout */
400 WaitPipeInfo
->TimeoutSpecified
= FALSE
;
403 /* Set the length and copy the name */
404 WaitPipeInfo
->NameLength
= NewName
.Length
;
405 RtlCopyMemory(WaitPipeInfo
->Name
, NewName
.Buffer
, NewName
.Length
);
407 /* Get rid of the full name */
408 RtlFreeUnicodeString(&NamedPipeName
);
410 /* Let NPFS know of our request */
411 Status
= NtFsControlFile(FileHandle
,
422 /* Free our pipe info data and close the handle */
423 RtlFreeHeap(RtlGetProcessHeap(), 0, WaitPipeInfo
);
426 /* Check the status */
427 if (!NT_SUCCESS(Status
))
429 /* Failure to wait on the pipe */
430 DPRINT1("Status: %lx\n", Status
);
431 SetLastErrorByStatus (Status
);
443 WaitNamedPipeW(LPCWSTR lpNamedPipeName
,
446 UNICODE_STRING NamedPipeName
;
449 OBJECT_ATTRIBUTES ObjectAttributes
;
450 FILE_PIPE_WAIT_FOR_BUFFER WaitPipe
;
452 IO_STATUS_BLOCK Iosb
;
454 r
= RtlDosPathNameToNtPathName_U((LPWSTR
)lpNamedPipeName
,
463 InitializeObjectAttributes(&ObjectAttributes
,
465 OBJ_CASE_INSENSITIVE
,
468 Status
= NtOpenFile(&FileHandle
,
469 FILE_READ_ATTRIBUTES
| SYNCHRONIZE
,
472 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
473 FILE_SYNCHRONOUS_IO_NONALERT
);
474 if (!NT_SUCCESS(Status
))
476 SetLastErrorByStatus (Status
);
480 WaitPipe
.Timeout
.QuadPart
= nTimeOut
* -10000LL;
482 Status
= NtFsControlFile(FileHandle
,
493 if (!NT_SUCCESS(Status
))
495 SetLastErrorByStatus (Status
);
507 ConnectNamedPipe(IN HANDLE hNamedPipe
,
508 IN LPOVERLAPPED lpOverlapped
)
512 if (lpOverlapped
!= NULL
)
516 lpOverlapped
->Internal
= STATUS_PENDING
;
517 ApcContext
= (((ULONG_PTR
)lpOverlapped
->hEvent
& 0x1) ? NULL
: lpOverlapped
);
519 Status
= NtFsControlFile(hNamedPipe
,
520 lpOverlapped
->hEvent
,
523 (PIO_STATUS_BLOCK
)lpOverlapped
,
530 /* return FALSE in case of failure and pending operations! */
531 if (!NT_SUCCESS(Status
) || Status
== STATUS_PENDING
)
533 SetLastErrorByStatus(Status
);
539 IO_STATUS_BLOCK Iosb
;
541 Status
= NtFsControlFile(hNamedPipe
,
552 /* wait in case operation is pending */
553 if (Status
== STATUS_PENDING
)
555 Status
= NtWaitForSingleObject(hNamedPipe
,
558 if (NT_SUCCESS(Status
))
560 Status
= Iosb
.Status
;
564 if (!NT_SUCCESS(Status
))
566 SetLastErrorByStatus(Status
);
579 SetNamedPipeHandleState(HANDLE hNamedPipe
,
581 LPDWORD lpMaxCollectionCount
,
582 LPDWORD lpCollectDataTimeout
)
584 IO_STATUS_BLOCK Iosb
;
587 /* Check if the Mode is being changed */
590 FILE_PIPE_INFORMATION Settings
;
592 /* Set the Completion Mode */
593 Settings
.CompletionMode
= (*lpMode
& PIPE_NOWAIT
) ?
594 FILE_PIPE_COMPLETE_OPERATION
: FILE_PIPE_QUEUE_OPERATION
;
596 /* Set the Read Mode */
597 Settings
.ReadMode
= (*lpMode
& PIPE_READMODE_MESSAGE
) ?
598 FILE_PIPE_MESSAGE_MODE
: FILE_PIPE_BYTE_STREAM_MODE
;
600 /* Send the changes to the Driver */
601 Status
= NtSetInformationFile(hNamedPipe
,
604 sizeof(FILE_PIPE_INFORMATION
),
605 FilePipeInformation
);
606 if (!NT_SUCCESS(Status
))
608 SetLastErrorByStatus(Status
);
613 /* Check if the Collection count or Timeout are being changed */
614 if (lpMaxCollectionCount
|| lpCollectDataTimeout
)
616 FILE_PIPE_REMOTE_INFORMATION RemoteSettings
;
618 /* Setting one without the other would delete it, so we read old one */
619 if (!lpMaxCollectionCount
|| !lpCollectDataTimeout
)
621 Status
= NtQueryInformationFile(hNamedPipe
,
624 sizeof(FILE_PIPE_REMOTE_INFORMATION
),
625 FilePipeRemoteInformation
);
627 if (!NT_SUCCESS(Status
))
629 SetLastErrorByStatus(Status
);
634 /* Now set the new settings */
635 RemoteSettings
.MaximumCollectionCount
= (lpMaxCollectionCount
) ?
636 *lpMaxCollectionCount
:
637 RemoteSettings
.MaximumCollectionCount
;
638 if (lpCollectDataTimeout
)
640 /* Convert it to Quad */
641 RemoteSettings
.CollectDataTime
.QuadPart
= -(LONGLONG
)
643 *lpCollectDataTimeout
);
646 /* Tell the driver to change them */
647 Status
= NtSetInformationFile(hNamedPipe
,
650 sizeof(FILE_PIPE_REMOTE_INFORMATION
),
651 FilePipeRemoteInformation
);
653 if (!NT_SUCCESS(Status
))
655 SetLastErrorByStatus(Status
);
669 CallNamedPipeA(LPCSTR lpNamedPipeName
,
673 DWORD nOutBufferSize
,
677 PUNICODE_STRING PipeName
= &NtCurrentTeb()->StaticUnicodeString
;
678 ANSI_STRING AnsiPipe
;
680 /* Initialize the string as ANSI_STRING and convert to Unicode */
681 RtlInitAnsiString(&AnsiPipe
, (LPSTR
)lpNamedPipeName
);
682 RtlAnsiStringToUnicodeString(PipeName
, &AnsiPipe
, FALSE
);
684 /* Call the Unicode function */
685 return CallNamedPipeW(PipeName
->Buffer
,
699 CallNamedPipeW(LPCWSTR lpNamedPipeName
,
703 DWORD nOutBufferSize
,
714 /* Try creating it */
715 hPipe
= CreateFileW(lpNamedPipeName
,
716 GENERIC_READ
| GENERIC_WRITE
,
717 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
720 FILE_ATTRIBUTE_NORMAL
,
723 /* Success, break out */
724 if (hPipe
!= INVALID_HANDLE_VALUE
) break;
726 /* Already tried twice, give up */
727 if (bRetry
== FALSE
) return FALSE
;
730 WaitNamedPipeW(lpNamedPipeName
, nTimeOut
);
732 /* Get ready to try again */
736 /* Set the pipe mode */
737 dwPipeMode
= PIPE_READMODE_MESSAGE
| PIPE_WAIT
;
738 bError
= SetNamedPipeHandleState(hPipe
, &dwPipeMode
, NULL
, NULL
);
741 /* Couldn't change state, fail */
746 /* Do the transact */
747 bError
= TransactNamedPipe(hPipe
,
755 /* Close the handle and return */
764 DisconnectNamedPipe(HANDLE hNamedPipe
)
766 IO_STATUS_BLOCK Iosb
;
769 Status
= NtFsControlFile(hNamedPipe
,
774 FSCTL_PIPE_DISCONNECT
,
779 if (Status
== STATUS_PENDING
)
781 Status
= NtWaitForSingleObject(hNamedPipe
,
784 if (!NT_SUCCESS(Status
))
786 SetLastErrorByStatus(Status
);
791 if (!NT_SUCCESS(Status
))
793 SetLastErrorByStatus(Status
);
804 GetNamedPipeHandleStateW(HANDLE hNamedPipe
,
806 LPDWORD lpCurInstances
,
807 LPDWORD lpMaxCollectionCount
,
808 LPDWORD lpCollectDataTimeout
,
810 DWORD nMaxUserNameSize
)
812 IO_STATUS_BLOCK StatusBlock
;
817 FILE_PIPE_INFORMATION PipeInfo
;
819 Status
= NtQueryInformationFile(hNamedPipe
,
822 sizeof(FILE_PIPE_INFORMATION
),
823 FilePipeInformation
);
824 if (!NT_SUCCESS(Status
))
826 SetLastErrorByStatus(Status
);
830 *lpState
= ((PipeInfo
.CompletionMode
!= FILE_PIPE_QUEUE_OPERATION
) ? PIPE_NOWAIT
: PIPE_WAIT
);
831 *lpState
|= ((PipeInfo
.ReadMode
!= FILE_PIPE_BYTE_STREAM_MODE
) ? PIPE_READMODE_MESSAGE
: PIPE_READMODE_BYTE
);
834 if(lpCurInstances
!= NULL
)
836 FILE_PIPE_LOCAL_INFORMATION LocalInfo
;
838 Status
= NtQueryInformationFile(hNamedPipe
,
841 sizeof(FILE_PIPE_LOCAL_INFORMATION
),
842 FilePipeLocalInformation
);
843 if(!NT_SUCCESS(Status
))
845 SetLastErrorByStatus(Status
);
849 *lpCurInstances
= min(LocalInfo
.CurrentInstances
, PIPE_UNLIMITED_INSTANCES
);
852 if(lpMaxCollectionCount
!= NULL
|| lpCollectDataTimeout
!= NULL
)
854 FILE_PIPE_REMOTE_INFORMATION RemoteInfo
;
856 Status
= NtQueryInformationFile(hNamedPipe
,
859 sizeof(FILE_PIPE_REMOTE_INFORMATION
),
860 FilePipeRemoteInformation
);
861 if(!NT_SUCCESS(Status
))
863 SetLastErrorByStatus(Status
);
867 if(lpMaxCollectionCount
!= NULL
)
869 *lpMaxCollectionCount
= RemoteInfo
.MaximumCollectionCount
;
872 if(lpCollectDataTimeout
!= NULL
)
875 *lpCollectDataTimeout
= 0;
879 if(lpUserName
!= NULL
)
881 /* FIXME - open the thread token, call ImpersonateNamedPipeClient() and
882 retreive the user name with GetUserName(), revert the impersonation
883 and finally restore the thread token */
894 GetNamedPipeHandleStateA(HANDLE hNamedPipe
,
896 LPDWORD lpCurInstances
,
897 LPDWORD lpMaxCollectionCount
,
898 LPDWORD lpCollectDataTimeout
,
900 DWORD nMaxUserNameSize
)
902 UNICODE_STRING UserNameW
;
903 ANSI_STRING UserNameA
;
906 if(lpUserName
!= NULL
)
908 UserNameW
.Length
= 0;
909 UserNameW
.MaximumLength
= nMaxUserNameSize
* sizeof(WCHAR
);
910 UserNameW
.Buffer
= HeapAlloc(GetCurrentProcess(), 0, UserNameW
.MaximumLength
);
912 UserNameA
.Buffer
= lpUserName
;
913 UserNameA
.Length
= 0;
914 UserNameA
.MaximumLength
= nMaxUserNameSize
;
917 Ret
= GetNamedPipeHandleStateW(hNamedPipe
,
920 lpMaxCollectionCount
,
921 lpCollectDataTimeout
,
925 if(Ret
&& lpUserName
!= NULL
)
927 NTSTATUS Status
= RtlUnicodeStringToAnsiString(&UserNameA
, &UserNameW
, FALSE
);
928 if(!NT_SUCCESS(Status
))
930 SetLastErrorByStatus(Status
);
935 if(UserNameW
.Buffer
!= NULL
)
937 HeapFree(GetCurrentProcess(), 0, UserNameW
.Buffer
);
948 GetNamedPipeInfo(HANDLE hNamedPipe
,
950 LPDWORD lpOutBufferSize
,
951 LPDWORD lpInBufferSize
,
952 LPDWORD lpMaxInstances
)
954 FILE_PIPE_LOCAL_INFORMATION PipeLocalInformation
;
955 IO_STATUS_BLOCK StatusBlock
;
958 Status
= NtQueryInformationFile(hNamedPipe
,
960 &PipeLocalInformation
,
961 sizeof(FILE_PIPE_LOCAL_INFORMATION
),
962 FilePipeLocalInformation
);
963 if (!NT_SUCCESS(Status
))
965 SetLastErrorByStatus(Status
);
971 *lpFlags
= (PipeLocalInformation
.NamedPipeEnd
== FILE_PIPE_SERVER_END
) ? PIPE_SERVER_END
: PIPE_CLIENT_END
;
972 *lpFlags
|= (PipeLocalInformation
.NamedPipeType
== 1) ? PIPE_TYPE_MESSAGE
: PIPE_TYPE_BYTE
;
975 if (lpOutBufferSize
!= NULL
)
976 *lpOutBufferSize
= PipeLocalInformation
.OutboundQuota
;
978 if (lpInBufferSize
!= NULL
)
979 *lpInBufferSize
= PipeLocalInformation
.InboundQuota
;
981 if (lpMaxInstances
!= NULL
)
983 if (PipeLocalInformation
.MaximumInstances
>= 255)
984 *lpMaxInstances
= PIPE_UNLIMITED_INSTANCES
;
986 *lpMaxInstances
= PipeLocalInformation
.MaximumInstances
;
997 PeekNamedPipe(HANDLE hNamedPipe
,
1000 LPDWORD lpBytesRead
,
1001 LPDWORD lpTotalBytesAvail
,
1002 LPDWORD lpBytesLeftThisMessage
)
1004 PFILE_PIPE_PEEK_BUFFER Buffer
;
1005 IO_STATUS_BLOCK Iosb
;
1009 BufferSize
= nBufferSize
+ sizeof(FILE_PIPE_PEEK_BUFFER
);
1010 Buffer
= RtlAllocateHeap(RtlGetProcessHeap(),
1014 Status
= NtFsControlFile(hNamedPipe
,
1024 if (Status
== STATUS_PENDING
)
1026 Status
= NtWaitForSingleObject(hNamedPipe
,
1029 if (NT_SUCCESS(Status
))
1030 Status
= Iosb
.Status
;
1033 if (Status
== STATUS_BUFFER_OVERFLOW
)
1035 Status
= STATUS_SUCCESS
;
1038 if (!NT_SUCCESS(Status
))
1040 RtlFreeHeap(RtlGetProcessHeap(),
1043 SetLastErrorByStatus(Status
);
1047 if (lpTotalBytesAvail
!= NULL
)
1049 *lpTotalBytesAvail
= Buffer
->ReadDataAvailable
;
1052 if (lpBytesRead
!= NULL
)
1054 *lpBytesRead
= Iosb
.Information
- sizeof(FILE_PIPE_PEEK_BUFFER
);
1057 if (lpBytesLeftThisMessage
!= NULL
)
1059 *lpBytesLeftThisMessage
= Buffer
->MessageLength
-
1060 (Iosb
.Information
- sizeof(FILE_PIPE_PEEK_BUFFER
));
1063 if (lpBuffer
!= NULL
)
1065 memcpy(lpBuffer
, Buffer
->Data
,
1066 min(nBufferSize
, Iosb
.Information
- sizeof(FILE_PIPE_PEEK_BUFFER
)));
1069 RtlFreeHeap(RtlGetProcessHeap(),
1081 TransactNamedPipe(IN HANDLE hNamedPipe
,
1082 IN LPVOID lpInBuffer
,
1083 IN DWORD nInBufferSize
,
1084 OUT LPVOID lpOutBuffer
,
1085 IN DWORD nOutBufferSize
,
1086 OUT LPDWORD lpBytesRead OPTIONAL
,
1087 IN LPOVERLAPPED lpOverlapped OPTIONAL
)
1091 if (lpBytesRead
!= NULL
)
1096 if (lpOverlapped
!= NULL
)
1100 ApcContext
= (((ULONG_PTR
)lpOverlapped
->hEvent
& 0x1) ? NULL
: lpOverlapped
);
1101 lpOverlapped
->Internal
= STATUS_PENDING
;
1103 Status
= NtFsControlFile(hNamedPipe
,
1104 lpOverlapped
->hEvent
,
1107 (PIO_STATUS_BLOCK
)lpOverlapped
,
1108 FSCTL_PIPE_TRANSCEIVE
,
1114 /* return FALSE in case of failure and pending operations! */
1115 if (!NT_SUCCESS(Status
) || Status
== STATUS_PENDING
)
1117 SetLastErrorByStatus(Status
);
1121 if (lpBytesRead
!= NULL
)
1123 *lpBytesRead
= lpOverlapped
->InternalHigh
;
1128 IO_STATUS_BLOCK Iosb
;
1130 Status
= NtFsControlFile(hNamedPipe
,
1135 FSCTL_PIPE_TRANSCEIVE
,
1141 /* wait in case operation is pending */
1142 if (Status
== STATUS_PENDING
)
1144 Status
= NtWaitForSingleObject(hNamedPipe
,
1147 if (NT_SUCCESS(Status
))
1149 Status
= Iosb
.Status
;
1153 if (NT_SUCCESS(Status
))
1155 /* lpNumberOfBytesRead must not be NULL here, in fact Win doesn't
1156 check that case either and crashes (only after the operation
1158 *lpBytesRead
= Iosb
.Information
;
1162 SetLastErrorByStatus(Status
);