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
);
58 CreateNamedPipeW(LPCWSTR lpName
,
64 DWORD nDefaultTimeOut
,
65 LPSECURITY_ATTRIBUTES lpSecurityAttributes
)
67 UNICODE_STRING NamedPipeName
;
70 OBJECT_ATTRIBUTES ObjectAttributes
;
72 ACCESS_MASK DesiredAccess
;
73 ULONG CreateOptions
= 0;
74 ULONG WriteModeMessage
;
75 ULONG ReadModeMessage
;
78 ULONG ShareAccess
= 0, Attributes
;
79 LARGE_INTEGER DefaultTimeOut
;
80 PSECURITY_DESCRIPTOR SecurityDescriptor
= NULL
;
82 /* Check for valid instances */
83 if (nMaxInstances
== 0 || nMaxInstances
> PIPE_UNLIMITED_INSTANCES
)
86 SetLastError(ERROR_INVALID_PARAMETER
);
87 return INVALID_HANDLE_VALUE
;
90 /* Convert to NT syntax */
91 if (nMaxInstances
== PIPE_UNLIMITED_INSTANCES
)
94 /* Convert the name */
95 Result
= RtlDosPathNameToNtPathName_U(lpName
,
101 /* Conversion failed */
102 SetLastError(ERROR_PATH_NOT_FOUND
);
103 return INVALID_HANDLE_VALUE
;
106 DPRINT("Pipe name: %wZ\n", &NamedPipeName
);
107 DPRINT("Pipe name: %S\n", NamedPipeName
.Buffer
);
109 /* Always case insensitive, check if we got extra attributes */
110 Attributes
= OBJ_CASE_INSENSITIVE
;
111 if(lpSecurityAttributes
)
113 /* We did; get the security descriptor */
114 SecurityDescriptor
= lpSecurityAttributes
->lpSecurityDescriptor
;
116 /* And check if this is pipe's handle will beinheritable */
117 if (lpSecurityAttributes
->bInheritHandle
)
118 Attributes
|= OBJ_INHERIT
;
121 /* Now we can initialize the object attributes */
122 InitializeObjectAttributes(&ObjectAttributes
,
128 /* Setup the default Desired Access */
129 DesiredAccess
= SYNCHRONIZE
| (dwOpenMode
& (WRITE_DAC
|
131 ACCESS_SYSTEM_SECURITY
));
133 /* Convert to NT Create Flags */
134 if (dwOpenMode
& FILE_FLAG_WRITE_THROUGH
)
136 CreateOptions
|= FILE_WRITE_THROUGH
;
139 if (!(dwOpenMode
& FILE_FLAG_OVERLAPPED
))
141 CreateOptions
|= FILE_SYNCHRONOUS_IO_NONALERT
;
144 /* Handle all open modes */
145 if (dwOpenMode
& PIPE_ACCESS_OUTBOUND
)
147 ShareAccess
|= FILE_SHARE_READ
;
148 DesiredAccess
|= GENERIC_WRITE
;
151 if (dwOpenMode
& PIPE_ACCESS_INBOUND
)
153 ShareAccess
|= FILE_SHARE_WRITE
;
154 DesiredAccess
|= GENERIC_READ
;
157 /* Handle the type flags */
158 if (dwPipeMode
& PIPE_TYPE_MESSAGE
)
160 WriteModeMessage
= FILE_PIPE_MESSAGE_TYPE
;
164 WriteModeMessage
= FILE_PIPE_BYTE_STREAM_TYPE
;
167 /* Handle the mode flags */
168 if (dwPipeMode
& PIPE_READMODE_MESSAGE
)
170 ReadModeMessage
= FILE_PIPE_MESSAGE_MODE
;
174 ReadModeMessage
= FILE_PIPE_BYTE_STREAM_MODE
;
177 /* Handle the blocking mode */
178 if (dwPipeMode
& PIPE_NOWAIT
)
180 NonBlocking
= FILE_PIPE_COMPLETE_OPERATION
;
184 NonBlocking
= FILE_PIPE_QUEUE_OPERATION
;
187 /* Check if we have a timeout */
190 /* Convert the time to NT format */
191 DefaultTimeOut
.QuadPart
= UInt32x32To64(nDefaultTimeOut
, -10000);
195 /* Use default timeout of 50 ms */
196 DefaultTimeOut
.QuadPart
= -500000;
199 /* Now create the pipe */
200 Status
= NtCreateNamedPipeFile(&PipeHandle
,
215 /* Normalize special error codes */
216 if ((Status
== STATUS_INVALID_DEVICE_REQUEST
) ||
217 (Status
== STATUS_NOT_SUPPORTED
))
219 Status
= STATUS_OBJECT_NAME_INVALID
;
223 RtlFreeHeap(RtlGetProcessHeap(),
225 NamedPipeName
.Buffer
);
228 if (!NT_SUCCESS(Status
))
230 /* Failed to create it */
231 DPRINT1("NtCreateNamedPipe failed (Status %x)!\n", Status
);
232 SetLastErrorByStatus (Status
);
233 return INVALID_HANDLE_VALUE
;
236 /* Return the handle */
246 WaitNamedPipeA(LPCSTR lpNamedPipeName
,
250 UNICODE_STRING NameU
;
252 /* Convert the name to Unicode */
253 Basep8BitStringToLiveUnicodeString(&NameU
, lpNamedPipeName
);
255 /* Call the Unicode API */
256 r
= WaitNamedPipeW(NameU
.Buffer
, nTimeOut
);
258 /* Free the Unicode string */
259 RtlFreeUnicodeString(&NameU
);
267 * When NPFS will work properly, use this code instead. It is compatible with
268 * Microsoft's NPFS.SYS. The main difference is that:
269 * - This code actually respects the timeout instead of ignoring it!
270 * - This code validates and creates the proper names for both UNC and local pipes
271 * - On NT, you open the *root* pipe directory (either \DosDevices\Pipe or
272 * \DosDevices\Unc\Server\Pipe) and then send the pipe to wait on in the
273 * FILE_PIPE_WAIT_FOR_BUFFER structure.
275 #ifdef USING_PROPER_NPFS_WAIT_SEMANTICS
281 WaitNamedPipeW(LPCWSTR lpNamedPipeName
,
284 UNICODE_STRING NamedPipeName
, NewName
, DevicePath
, PipePrefix
;
289 OBJECT_ATTRIBUTES ObjectAttributes
;
292 IO_STATUS_BLOCK IoStatusBlock
;
293 ULONG WaitPipeInfoSize
;
294 PFILE_PIPE_WAIT_FOR_BUFFER WaitPipeInfo
;
296 /* Start by making a unicode string of the name */
297 DPRINT("Sent path: %S\n", lpNamedPipeName
);
298 RtlCreateUnicodeString(&NamedPipeName
, lpNamedPipeName
);
299 NameLength
= NamedPipeName
.Length
/ sizeof(WCHAR
);
301 /* All slashes must become backslashes */
302 for (i
= 0; i
< NameLength
; i
++)
304 /* Check and convert */
305 if (NamedPipeName
.Buffer
[i
] == L
'/') NamedPipeName
.Buffer
[i
] = L
'\\';
308 /* Find the path type of the name we were given */
309 NewName
= NamedPipeName
;
310 Type
= RtlDetermineDosPathNameType_U(lpNamedPipeName
);
312 /* Check if this was a device path, ie : "\\.\pipe\name" */
313 if (Type
== RtlPathTypeLocalDevice
)
315 /* Make sure it's a valid prefix */
316 RtlInitUnicodeString(&PipePrefix
, L
"\\\\.\\pipe\\");
317 RtlPrefixString((PANSI_STRING
)&PipePrefix
, (PANSI_STRING
)&NewName
, TRUE
);
321 NewName
.Length
-= 9 * sizeof(WCHAR
);
323 /* Initialize the Dos Devices name */
324 DPRINT("NewName: %wZ\n", &NewName
);
325 RtlInitUnicodeString(&DevicePath
, L
"\\DosDevices\\pipe\\");
327 else if (Type
== RtlPathTypeRootLocalDevice
)
329 /* The path is \\server\\pipe\name; find the pipename itself */
330 p
= &NewName
.Buffer
[2];
332 /* First loop to get past the server name */
335 /* Check if this is a backslash */
336 if (*p
== L
'\\') break;
342 /* Now make sure the full name contains "pipe\" */
343 if ((*p
) && !(_wcsnicmp(p
+ 1, L
"pipe\\", sizeof("pipe\\"))))
345 /* Get to the pipe name itself now */
346 p
+= sizeof("pipe\\") - 1;
350 /* The name is invalid */
351 DPRINT1("Invalid name!\n");
352 SetLastErrorByStatus(STATUS_OBJECT_PATH_SYNTAX_BAD
);
356 /* FIXME: Open \DosDevices\Unc\Server\Pipe\Name */
360 DPRINT1("Invalid path type\n");
361 SetLastErrorByStatus(STATUS_OBJECT_PATH_SYNTAX_BAD
);
365 /* Now calculate the total length of the structure and allocate it */
366 WaitPipeInfoSize
= FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER
, Name
[0]) +
368 WaitPipeInfo
= RtlAllocateHeap(RtlGetProcessHeap(), 0, WaitPipeInfoSize
);
369 if (WaitPipeInfo
== NULL
)
371 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
375 /* Initialize the object attributes */
376 DPRINT("Opening: %wZ\n", &DevicePath
);
377 InitializeObjectAttributes(&ObjectAttributes
,
379 OBJ_CASE_INSENSITIVE
,
384 Status
= NtOpenFile(&FileHandle
,
385 FILE_READ_ATTRIBUTES
| SYNCHRONIZE
,
388 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
389 FILE_SYNCHRONOUS_IO_NONALERT
);
390 if (!NT_SUCCESS(Status
))
392 /* Fail; couldn't open */
393 DPRINT1("Status: %lx\n", Status
);
394 SetLastErrorByStatus(Status
);
395 RtlFreeUnicodeString(&NamedPipeName
);
396 RtlFreeHeap(RtlGetProcessHeap(), 0, WaitPipeInfo
);
400 /* Check what timeout we got */
401 if (nTimeOut
== NMPWAIT_USE_DEFAULT_WAIT
)
403 /* Don't use a timeout */
404 WaitPipeInfo
->TimeoutSpecified
= FALSE
;
408 /* Check if we should wait forever */
409 if (nTimeOut
== NMPWAIT_WAIT_FOREVER
)
412 WaitPipeInfo
->Timeout
.LowPart
= 0;
413 WaitPipeInfo
->Timeout
.HighPart
= 0x80000000;
417 /* Convert to NT format */
418 WaitPipeInfo
->Timeout
.QuadPart
= UInt32x32To64(-10000, nTimeOut
);
421 /* In both cases, we do have a timeout */
422 WaitPipeInfo
->TimeoutSpecified
= FALSE
;
425 /* Set the length and copy the name */
426 WaitPipeInfo
->NameLength
= NewName
.Length
;
427 RtlCopyMemory(WaitPipeInfo
->Name
, NewName
.Buffer
, NewName
.Length
);
429 /* Get rid of the full name */
430 RtlFreeUnicodeString(&NamedPipeName
);
432 /* Let NPFS know of our request */
433 Status
= NtFsControlFile(FileHandle
,
444 /* Free our pipe info data and close the handle */
445 RtlFreeHeap(RtlGetProcessHeap(), 0, WaitPipeInfo
);
448 /* Check the status */
449 if (!NT_SUCCESS(Status
))
451 /* Failure to wait on the pipe */
452 DPRINT1("Status: %lx\n", Status
);
453 SetLastErrorByStatus (Status
);
466 WaitNamedPipeW(LPCWSTR lpNamedPipeName
,
469 UNICODE_STRING NamedPipeName
;
471 OBJECT_ATTRIBUTES ObjectAttributes
;
472 FILE_PIPE_WAIT_FOR_BUFFER WaitPipe
;
474 IO_STATUS_BLOCK Iosb
;
476 if (RtlDosPathNameToNtPathName_U(lpNamedPipeName
,
484 InitializeObjectAttributes(&ObjectAttributes
,
486 OBJ_CASE_INSENSITIVE
,
489 Status
= NtOpenFile(&FileHandle
,
490 FILE_READ_ATTRIBUTES
| SYNCHRONIZE
,
493 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
494 FILE_SYNCHRONOUS_IO_NONALERT
);
495 if (!NT_SUCCESS(Status
))
497 SetLastErrorByStatus(Status
);
501 WaitPipe
.Timeout
.QuadPart
= nTimeOut
* -10000LL;
503 Status
= NtFsControlFile(FileHandle
,
514 if (!NT_SUCCESS(Status
))
516 SetLastErrorByStatus(Status
);
530 ConnectNamedPipe(IN HANDLE hNamedPipe
,
531 IN LPOVERLAPPED lpOverlapped
)
535 if (lpOverlapped
!= NULL
)
539 lpOverlapped
->Internal
= STATUS_PENDING
;
540 ApcContext
= (((ULONG_PTR
)lpOverlapped
->hEvent
& 0x1) ? NULL
: lpOverlapped
);
542 Status
= NtFsControlFile(hNamedPipe
,
543 lpOverlapped
->hEvent
,
546 (PIO_STATUS_BLOCK
)lpOverlapped
,
553 /* return FALSE in case of failure and pending operations! */
554 if (!NT_SUCCESS(Status
) || Status
== STATUS_PENDING
)
556 SetLastErrorByStatus(Status
);
562 IO_STATUS_BLOCK Iosb
;
564 Status
= NtFsControlFile(hNamedPipe
,
575 /* wait in case operation is pending */
576 if (Status
== STATUS_PENDING
)
578 Status
= NtWaitForSingleObject(hNamedPipe
,
581 if (NT_SUCCESS(Status
))
583 Status
= Iosb
.Status
;
587 if (!NT_SUCCESS(Status
))
589 SetLastErrorByStatus(Status
);
603 SetNamedPipeHandleState(HANDLE hNamedPipe
,
605 LPDWORD lpMaxCollectionCount
,
606 LPDWORD lpCollectDataTimeout
)
608 IO_STATUS_BLOCK Iosb
;
611 /* Check if the Mode is being changed */
614 FILE_PIPE_INFORMATION Settings
;
616 /* Set the Completion Mode */
617 Settings
.CompletionMode
= (*lpMode
& PIPE_NOWAIT
) ?
618 FILE_PIPE_COMPLETE_OPERATION
: FILE_PIPE_QUEUE_OPERATION
;
620 /* Set the Read Mode */
621 Settings
.ReadMode
= (*lpMode
& PIPE_READMODE_MESSAGE
) ?
622 FILE_PIPE_MESSAGE_MODE
: FILE_PIPE_BYTE_STREAM_MODE
;
624 /* Send the changes to the Driver */
625 Status
= NtSetInformationFile(hNamedPipe
,
628 sizeof(FILE_PIPE_INFORMATION
),
629 FilePipeInformation
);
630 if (!NT_SUCCESS(Status
))
632 SetLastErrorByStatus(Status
);
637 /* Check if the Collection count or Timeout are being changed */
638 if (lpMaxCollectionCount
|| lpCollectDataTimeout
)
640 FILE_PIPE_REMOTE_INFORMATION RemoteSettings
;
642 /* Setting one without the other would delete it, so we read old one */
643 if (!lpMaxCollectionCount
|| !lpCollectDataTimeout
)
645 Status
= NtQueryInformationFile(hNamedPipe
,
648 sizeof(FILE_PIPE_REMOTE_INFORMATION
),
649 FilePipeRemoteInformation
);
650 if (!NT_SUCCESS(Status
))
652 SetLastErrorByStatus(Status
);
657 /* Now set the new settings */
658 RemoteSettings
.MaximumCollectionCount
= (lpMaxCollectionCount
) ?
659 *lpMaxCollectionCount
:
660 RemoteSettings
.MaximumCollectionCount
;
661 if (lpCollectDataTimeout
)
663 /* Convert it to Quad */
664 RemoteSettings
.CollectDataTime
.QuadPart
=
665 -(LONGLONG
)UInt32x32To64(10000, *lpCollectDataTimeout
);
668 /* Tell the driver to change them */
669 Status
= NtSetInformationFile(hNamedPipe
,
672 sizeof(FILE_PIPE_REMOTE_INFORMATION
),
673 FilePipeRemoteInformation
);
674 if (!NT_SUCCESS(Status
))
676 SetLastErrorByStatus(Status
);
690 CallNamedPipeA(LPCSTR lpNamedPipeName
,
694 DWORD nOutBufferSize
,
698 PUNICODE_STRING PipeName
= &NtCurrentTeb()->StaticUnicodeString
;
699 ANSI_STRING AnsiPipe
;
701 /* Initialize the string as ANSI_STRING and convert to Unicode */
702 RtlInitAnsiString(&AnsiPipe
, (LPSTR
)lpNamedPipeName
);
703 RtlAnsiStringToUnicodeString(PipeName
, &AnsiPipe
, FALSE
);
705 /* Call the Unicode function */
706 return CallNamedPipeW(PipeName
->Buffer
,
721 CallNamedPipeW(LPCWSTR lpNamedPipeName
,
725 DWORD nOutBufferSize
,
736 /* Try creating it */
737 hPipe
= CreateFileW(lpNamedPipeName
,
738 GENERIC_READ
| GENERIC_WRITE
,
739 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
742 FILE_ATTRIBUTE_NORMAL
,
745 /* Success, break out */
746 if (hPipe
!= INVALID_HANDLE_VALUE
)
749 /* Already tried twice, give up */
754 WaitNamedPipeW(lpNamedPipeName
, nTimeOut
);
756 /* Get ready to try again */
760 /* Set the pipe mode */
761 dwPipeMode
= PIPE_READMODE_MESSAGE
| PIPE_WAIT
;
762 bError
= SetNamedPipeHandleState(hPipe
, &dwPipeMode
, NULL
, NULL
);
765 /* Couldn't change state, fail */
770 /* Do the transact */
771 bError
= TransactNamedPipe(hPipe
,
779 /* Close the handle */
791 DisconnectNamedPipe(HANDLE hNamedPipe
)
793 IO_STATUS_BLOCK Iosb
;
796 /* Send the FSCTL to the driver */
797 Status
= NtFsControlFile(hNamedPipe
,
802 FSCTL_PIPE_DISCONNECT
,
807 if (Status
== STATUS_PENDING
)
809 /* Wait on NPFS to finish and get updated status */
810 Status
= NtWaitForSingleObject(hNamedPipe
, FALSE
, NULL
);
811 if (NT_SUCCESS(Status
))
812 Status
= Iosb
.Status
;
815 /* Check for error */
816 if (!NT_SUCCESS(Status
))
819 SetLastErrorByStatus(Status
);
832 GetNamedPipeHandleStateW(HANDLE hNamedPipe
,
834 LPDWORD lpCurInstances
,
835 LPDWORD lpMaxCollectionCount
,
836 LPDWORD lpCollectDataTimeout
,
838 DWORD nMaxUserNameSize
)
840 IO_STATUS_BLOCK StatusBlock
;
845 FILE_PIPE_INFORMATION PipeInfo
;
847 Status
= NtQueryInformationFile(hNamedPipe
,
850 sizeof(FILE_PIPE_INFORMATION
),
851 FilePipeInformation
);
852 if (!NT_SUCCESS(Status
))
854 SetLastErrorByStatus(Status
);
858 *lpState
= ((PipeInfo
.CompletionMode
!= FILE_PIPE_QUEUE_OPERATION
) ? PIPE_NOWAIT
: PIPE_WAIT
);
859 *lpState
|= ((PipeInfo
.ReadMode
!= FILE_PIPE_BYTE_STREAM_MODE
) ? PIPE_READMODE_MESSAGE
: PIPE_READMODE_BYTE
);
862 if(lpCurInstances
!= NULL
)
864 FILE_PIPE_LOCAL_INFORMATION LocalInfo
;
866 Status
= NtQueryInformationFile(hNamedPipe
,
869 sizeof(FILE_PIPE_LOCAL_INFORMATION
),
870 FilePipeLocalInformation
);
871 if (!NT_SUCCESS(Status
))
873 SetLastErrorByStatus(Status
);
877 *lpCurInstances
= min(LocalInfo
.CurrentInstances
, PIPE_UNLIMITED_INSTANCES
);
880 if (lpMaxCollectionCount
!= NULL
|| lpCollectDataTimeout
!= NULL
)
882 FILE_PIPE_REMOTE_INFORMATION RemoteInfo
;
884 Status
= NtQueryInformationFile(hNamedPipe
,
887 sizeof(FILE_PIPE_REMOTE_INFORMATION
),
888 FilePipeRemoteInformation
);
889 if (!NT_SUCCESS(Status
))
891 SetLastErrorByStatus(Status
);
895 if (lpMaxCollectionCount
!= NULL
)
897 *lpMaxCollectionCount
= RemoteInfo
.MaximumCollectionCount
;
900 if(lpCollectDataTimeout
!= NULL
)
903 *lpCollectDataTimeout
= 0;
907 if (lpUserName
!= NULL
)
909 /* FIXME - open the thread token, call ImpersonateNamedPipeClient() and
910 retreive the user name with GetUserName(), revert the impersonation
911 and finally restore the thread token */
923 GetNamedPipeHandleStateA(HANDLE hNamedPipe
,
925 LPDWORD lpCurInstances
,
926 LPDWORD lpMaxCollectionCount
,
927 LPDWORD lpCollectDataTimeout
,
929 DWORD nMaxUserNameSize
)
931 UNICODE_STRING UserNameW
= {0};
932 ANSI_STRING UserNameA
;
935 if(lpUserName
!= NULL
)
937 UserNameW
.MaximumLength
= (USHORT
)nMaxUserNameSize
* sizeof(WCHAR
);
938 UserNameW
.Buffer
= RtlAllocateHeap(RtlGetProcessHeap(), 0, UserNameW
.MaximumLength
);
939 if (UserNameW
.Buffer
== NULL
)
941 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
945 UserNameA
.Buffer
= lpUserName
;
946 UserNameA
.Length
= 0;
947 UserNameA
.MaximumLength
= (USHORT
)nMaxUserNameSize
;
950 Ret
= GetNamedPipeHandleStateW(hNamedPipe
,
953 lpMaxCollectionCount
,
954 lpCollectDataTimeout
,
957 if (Ret
&& lpUserName
!= NULL
)
961 RtlInitUnicodeString(&UserNameW
, UserNameW
.Buffer
);
962 Status
= RtlUnicodeStringToAnsiString(&UserNameA
, &UserNameW
, FALSE
);
963 if (!NT_SUCCESS(Status
))
965 SetLastErrorByStatus(Status
);
970 if (UserNameW
.Buffer
!= NULL
)
972 RtlFreeHeap(RtlGetProcessHeap(), 0, UserNameW
.Buffer
);
984 GetNamedPipeInfo(HANDLE hNamedPipe
,
986 LPDWORD lpOutBufferSize
,
987 LPDWORD lpInBufferSize
,
988 LPDWORD lpMaxInstances
)
990 FILE_PIPE_LOCAL_INFORMATION PipeLocalInformation
;
991 IO_STATUS_BLOCK StatusBlock
;
994 Status
= NtQueryInformationFile(hNamedPipe
,
996 &PipeLocalInformation
,
997 sizeof(FILE_PIPE_LOCAL_INFORMATION
),
998 FilePipeLocalInformation
);
999 if (!NT_SUCCESS(Status
))
1001 SetLastErrorByStatus(Status
);
1005 if (lpFlags
!= NULL
)
1007 *lpFlags
= (PipeLocalInformation
.NamedPipeEnd
== FILE_PIPE_SERVER_END
) ? PIPE_SERVER_END
: PIPE_CLIENT_END
;
1008 *lpFlags
|= (PipeLocalInformation
.NamedPipeType
== 1) ? PIPE_TYPE_MESSAGE
: PIPE_TYPE_BYTE
;
1011 if (lpOutBufferSize
!= NULL
)
1012 *lpOutBufferSize
= PipeLocalInformation
.OutboundQuota
;
1014 if (lpInBufferSize
!= NULL
)
1015 *lpInBufferSize
= PipeLocalInformation
.InboundQuota
;
1017 if (lpMaxInstances
!= NULL
)
1019 if (PipeLocalInformation
.MaximumInstances
>= 255)
1020 *lpMaxInstances
= PIPE_UNLIMITED_INSTANCES
;
1022 *lpMaxInstances
= PipeLocalInformation
.MaximumInstances
;
1034 PeekNamedPipe(HANDLE hNamedPipe
,
1037 LPDWORD lpBytesRead
,
1038 LPDWORD lpTotalBytesAvail
,
1039 LPDWORD lpBytesLeftThisMessage
)
1041 PFILE_PIPE_PEEK_BUFFER Buffer
;
1042 IO_STATUS_BLOCK Iosb
;
1047 /* Calculate the buffer space that we'll need and allocate it */
1048 BufferSize
= nBufferSize
+ FIELD_OFFSET(FILE_PIPE_PEEK_BUFFER
, Data
[0]);
1049 Buffer
= RtlAllocateHeap(RtlGetProcessHeap(), 0, BufferSize
);
1052 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1056 /* Tell the driver to seek */
1057 Status
= NtFsControlFile(hNamedPipe
,
1067 if (Status
== STATUS_PENDING
)
1069 /* Wait for npfs to be done, and update the status */
1070 Status
= NtWaitForSingleObject(hNamedPipe
, FALSE
, NULL
);
1071 if (NT_SUCCESS(Status
))
1072 Status
= Iosb
.Status
;
1075 /* Overflow is success for us */
1076 if (Status
== STATUS_BUFFER_OVERFLOW
)
1077 Status
= STATUS_SUCCESS
;
1080 if (!NT_SUCCESS(Status
))
1082 /* Free the buffer and return failure */
1083 RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer
);
1084 SetLastErrorByStatus(Status
);
1088 /* Check if caller requested bytes available */
1089 if (lpTotalBytesAvail
)
1090 *lpTotalBytesAvail
= Buffer
->ReadDataAvailable
;
1092 /* Calculate the bytes returned, minus our structure overhead */
1093 BytesRead
= (ULONG
)(Iosb
.Information
-
1094 FIELD_OFFSET(FILE_PIPE_PEEK_BUFFER
, Data
[0]));
1096 /* Check if caller requested bytes read */
1099 /* Return the bytes read */
1100 *lpBytesRead
= BytesRead
;
1103 /* Check if caller requested bytes left */
1104 if (lpBytesLeftThisMessage
)
1106 /* Calculate total minus what we returned and our structure overhead */
1107 *lpBytesLeftThisMessage
= Buffer
->MessageLength
- BytesRead
;
1110 /* Check if the caller wanted to see the actual data */
1113 /* Give him what he wants */
1114 RtlCopyMemory(lpBuffer
,
1119 /* Free the buffer */
1120 RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer
);
1131 TransactNamedPipe(IN HANDLE hNamedPipe
,
1132 IN LPVOID lpInBuffer
,
1133 IN DWORD nInBufferSize
,
1134 OUT LPVOID lpOutBuffer
,
1135 IN DWORD nOutBufferSize
,
1136 OUT LPDWORD lpBytesRead OPTIONAL
,
1137 IN LPOVERLAPPED lpOverlapped OPTIONAL
)
1141 if (lpBytesRead
!= NULL
)
1146 if (lpOverlapped
!= NULL
)
1150 ApcContext
= (((ULONG_PTR
)lpOverlapped
->hEvent
& 0x1) ? NULL
: lpOverlapped
);
1151 lpOverlapped
->Internal
= STATUS_PENDING
;
1153 Status
= NtFsControlFile(hNamedPipe
,
1154 lpOverlapped
->hEvent
,
1157 (PIO_STATUS_BLOCK
)lpOverlapped
,
1158 FSCTL_PIPE_TRANSCEIVE
,
1163 if (!NT_SUCCESS(Status
) || Status
== STATUS_PENDING
)
1165 SetLastErrorByStatus(Status
);
1169 if (lpBytesRead
!= NULL
)
1171 *lpBytesRead
= lpOverlapped
->InternalHigh
;
1176 #if 0 /* We don't implement FSCTL_PIPE_TRANSCEIVE yet */
1177 IO_STATUS_BLOCK Iosb
;
1179 Status
= NtFsControlFile(hNamedPipe
,
1184 FSCTL_PIPE_TRANSCEIVE
,
1189 if (Status
== STATUS_PENDING
)
1191 Status
= NtWaitForSingleObject(hNamedPipe
,
1194 if (NT_SUCCESS(Status
))
1195 Status
= Iosb
.Status
;
1198 if (NT_SUCCESS(Status
))
1200 /* lpNumberOfBytesRead must not be NULL here, in fact Win doesn't
1201 check that case either and crashes (only after the operation
1203 *lpBytesRead
= Iosb
.Information
;
1207 SetLastErrorByStatus(Status
);
1210 #else /* Workaround while FSCTL_PIPE_TRANSCEIVE not available */
1213 while (0 != nInBufferSize
&&
1214 WriteFile(hNamedPipe
, lpInBuffer
, nInBufferSize
, &nActualBytes
,
1217 lpInBuffer
= (LPVOID
)((char *) lpInBuffer
+ nActualBytes
);
1218 nInBufferSize
-= nActualBytes
;
1221 if (0 != nInBufferSize
)
1223 /* Must have dropped out of the while 'cause WriteFile failed */
1227 if (!ReadFile(hNamedPipe
, lpOutBuffer
, nOutBufferSize
, &nActualBytes
,
1233 if (NULL
!= lpBytesRead
)
1235 *lpBytesRead
= nActualBytes
;