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 /* Initialize the object attributes */
357 DPRINT("Opening: %wZ\n", &DevicePath
);
358 InitializeObjectAttributes(&ObjectAttributes
,
360 OBJ_CASE_INSENSITIVE
,
365 Status
= NtOpenFile(&FileHandle
,
366 FILE_READ_ATTRIBUTES
| SYNCHRONIZE
,
369 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
370 FILE_SYNCHRONOUS_IO_NONALERT
);
371 if (!NT_SUCCESS(Status
))
373 /* Fail; couldn't open */
374 DPRINT1("Status: %lx\n", Status
);
375 SetLastErrorByStatus(Status
);
376 RtlFreeUnicodeString(&NamedPipeName
);
380 /* Now calculate the total length of the structure and allocate it */
381 WaitPipeInfoSize
= FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER
, Name
[0]) +
383 WaitPipeInfo
= RtlAllocateHeap(RtlGetProcessHeap(), 0, WaitPipeInfoSize
);
385 /* Check what timeout we got */
386 if (nTimeOut
== NMPWAIT_USE_DEFAULT_WAIT
)
388 /* Don't use a timeout */
389 WaitPipeInfo
->TimeoutSpecified
= FALSE
;
393 /* Check if we should wait forever */
394 if (nTimeOut
== NMPWAIT_WAIT_FOREVER
)
397 WaitPipeInfo
->Timeout
.LowPart
= 0;
398 WaitPipeInfo
->Timeout
.HighPart
= 0x80000000;
402 /* Convert to NT format */
403 WaitPipeInfo
->Timeout
.QuadPart
= UInt32x32To64(-10000, nTimeOut
);
406 /* In both cases, we do have a timeout */
407 WaitPipeInfo
->TimeoutSpecified
= FALSE
;
410 /* Set the length and copy the name */
411 WaitPipeInfo
->NameLength
= NewName
.Length
;
412 RtlCopyMemory(WaitPipeInfo
->Name
, NewName
.Buffer
, NewName
.Length
);
414 /* Get rid of the full name */
415 RtlFreeUnicodeString(&NamedPipeName
);
417 /* Let NPFS know of our request */
418 Status
= NtFsControlFile(FileHandle
,
429 /* Free our pipe info data and close the handle */
430 RtlFreeHeap(RtlGetProcessHeap(), 0, WaitPipeInfo
);
433 /* Check the status */
434 if (!NT_SUCCESS(Status
))
436 /* Failure to wait on the pipe */
437 DPRINT1("Status: %lx\n", Status
);
438 SetLastErrorByStatus (Status
);
450 WaitNamedPipeW(LPCWSTR lpNamedPipeName
,
453 UNICODE_STRING NamedPipeName
;
456 OBJECT_ATTRIBUTES ObjectAttributes
;
457 FILE_PIPE_WAIT_FOR_BUFFER WaitPipe
;
459 IO_STATUS_BLOCK Iosb
;
461 r
= RtlDosPathNameToNtPathName_U((LPWSTR
)lpNamedPipeName
,
470 InitializeObjectAttributes(&ObjectAttributes
,
472 OBJ_CASE_INSENSITIVE
,
475 Status
= NtOpenFile(&FileHandle
,
476 FILE_READ_ATTRIBUTES
| SYNCHRONIZE
,
479 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
480 FILE_SYNCHRONOUS_IO_NONALERT
);
481 if (!NT_SUCCESS(Status
))
483 SetLastErrorByStatus (Status
);
487 WaitPipe
.Timeout
.QuadPart
= nTimeOut
* -10000LL;
489 Status
= NtFsControlFile(FileHandle
,
500 if (!NT_SUCCESS(Status
))
502 SetLastErrorByStatus (Status
);
514 ConnectNamedPipe(IN HANDLE hNamedPipe
,
515 IN LPOVERLAPPED lpOverlapped
)
519 if (lpOverlapped
!= NULL
)
523 lpOverlapped
->Internal
= STATUS_PENDING
;
524 ApcContext
= (((ULONG_PTR
)lpOverlapped
->hEvent
& 0x1) ? NULL
: lpOverlapped
);
526 Status
= NtFsControlFile(hNamedPipe
,
527 lpOverlapped
->hEvent
,
530 (PIO_STATUS_BLOCK
)lpOverlapped
,
537 /* return FALSE in case of failure and pending operations! */
538 if (!NT_SUCCESS(Status
) || Status
== STATUS_PENDING
)
540 SetLastErrorByStatus(Status
);
546 IO_STATUS_BLOCK Iosb
;
548 Status
= NtFsControlFile(hNamedPipe
,
559 /* wait in case operation is pending */
560 if (Status
== STATUS_PENDING
)
562 Status
= NtWaitForSingleObject(hNamedPipe
,
565 if (NT_SUCCESS(Status
))
567 Status
= Iosb
.Status
;
571 if (!NT_SUCCESS(Status
))
573 SetLastErrorByStatus(Status
);
586 SetNamedPipeHandleState(HANDLE hNamedPipe
,
588 LPDWORD lpMaxCollectionCount
,
589 LPDWORD lpCollectDataTimeout
)
591 IO_STATUS_BLOCK Iosb
;
594 /* Check if the Mode is being changed */
597 FILE_PIPE_INFORMATION Settings
;
599 /* Set the Completion Mode */
600 Settings
.CompletionMode
= (*lpMode
& PIPE_NOWAIT
) ?
601 FILE_PIPE_COMPLETE_OPERATION
: FILE_PIPE_QUEUE_OPERATION
;
603 /* Set the Read Mode */
604 Settings
.ReadMode
= (*lpMode
& PIPE_READMODE_MESSAGE
) ?
605 FILE_PIPE_MESSAGE_MODE
: FILE_PIPE_BYTE_STREAM_MODE
;
607 /* Send the changes to the Driver */
608 Status
= NtSetInformationFile(hNamedPipe
,
611 sizeof(FILE_PIPE_INFORMATION
),
612 FilePipeInformation
);
613 if (!NT_SUCCESS(Status
))
615 SetLastErrorByStatus(Status
);
620 /* Check if the Collection count or Timeout are being changed */
621 if (lpMaxCollectionCount
|| lpCollectDataTimeout
)
623 FILE_PIPE_REMOTE_INFORMATION RemoteSettings
;
625 /* Setting one without the other would delete it, so we read old one */
626 if (!lpMaxCollectionCount
|| !lpCollectDataTimeout
)
628 Status
= NtQueryInformationFile(hNamedPipe
,
631 sizeof(FILE_PIPE_REMOTE_INFORMATION
),
632 FilePipeRemoteInformation
);
634 if (!NT_SUCCESS(Status
))
636 SetLastErrorByStatus(Status
);
641 /* Now set the new settings */
642 RemoteSettings
.MaximumCollectionCount
= (lpMaxCollectionCount
) ?
643 *lpMaxCollectionCount
:
644 RemoteSettings
.MaximumCollectionCount
;
645 if (lpCollectDataTimeout
)
647 /* Convert it to Quad */
648 RemoteSettings
.CollectDataTime
.QuadPart
= -(LONGLONG
)
650 *lpCollectDataTimeout
);
653 /* Tell the driver to change them */
654 Status
= NtSetInformationFile(hNamedPipe
,
657 sizeof(FILE_PIPE_REMOTE_INFORMATION
),
658 FilePipeRemoteInformation
);
660 if (!NT_SUCCESS(Status
))
662 SetLastErrorByStatus(Status
);
676 CallNamedPipeA(LPCSTR lpNamedPipeName
,
680 DWORD nOutBufferSize
,
684 PUNICODE_STRING PipeName
= &NtCurrentTeb()->StaticUnicodeString
;
685 ANSI_STRING AnsiPipe
;
687 /* Initialize the string as ANSI_STRING and convert to Unicode */
688 RtlInitAnsiString(&AnsiPipe
, (LPSTR
)lpNamedPipeName
);
689 RtlAnsiStringToUnicodeString(PipeName
, &AnsiPipe
, FALSE
);
691 /* Call the Unicode function */
692 return CallNamedPipeW(PipeName
->Buffer
,
706 CallNamedPipeW(LPCWSTR lpNamedPipeName
,
710 DWORD nOutBufferSize
,
721 /* Try creating it */
722 hPipe
= CreateFileW(lpNamedPipeName
,
723 GENERIC_READ
| GENERIC_WRITE
,
724 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
727 FILE_ATTRIBUTE_NORMAL
,
730 /* Success, break out */
731 if (hPipe
!= INVALID_HANDLE_VALUE
) break;
733 /* Already tried twice, give up */
734 if (bRetry
== FALSE
) return FALSE
;
737 WaitNamedPipeW(lpNamedPipeName
, nTimeOut
);
739 /* Get ready to try again */
743 /* Set the pipe mode */
744 dwPipeMode
= PIPE_READMODE_MESSAGE
| PIPE_WAIT
;
745 bError
= SetNamedPipeHandleState(hPipe
, &dwPipeMode
, NULL
, NULL
);
748 /* Couldn't change state, fail */
753 /* Do the transact */
754 bError
= TransactNamedPipe(hPipe
,
762 /* Close the handle and return */
772 DisconnectNamedPipe(HANDLE hNamedPipe
)
774 IO_STATUS_BLOCK Iosb
;
777 /* Send the FSCTL to the driver */
778 Status
= NtFsControlFile(hNamedPipe
,
783 FSCTL_PIPE_DISCONNECT
,
788 if (Status
== STATUS_PENDING
)
790 /* Wait on NPFS to finish and get updated status */
791 Status
= NtWaitForSingleObject(hNamedPipe
, FALSE
, NULL
);
792 if (NT_SUCCESS(Status
)) Status
= Iosb
.Status
;
795 /* Check for error */
796 if (!NT_SUCCESS(Status
))
799 SetLastErrorByStatus(Status
);
810 GetNamedPipeHandleStateW(HANDLE hNamedPipe
,
812 LPDWORD lpCurInstances
,
813 LPDWORD lpMaxCollectionCount
,
814 LPDWORD lpCollectDataTimeout
,
816 DWORD nMaxUserNameSize
)
818 IO_STATUS_BLOCK StatusBlock
;
823 FILE_PIPE_INFORMATION PipeInfo
;
825 Status
= NtQueryInformationFile(hNamedPipe
,
828 sizeof(FILE_PIPE_INFORMATION
),
829 FilePipeInformation
);
830 if (!NT_SUCCESS(Status
))
832 SetLastErrorByStatus(Status
);
836 *lpState
= ((PipeInfo
.CompletionMode
!= FILE_PIPE_QUEUE_OPERATION
) ? PIPE_NOWAIT
: PIPE_WAIT
);
837 *lpState
|= ((PipeInfo
.ReadMode
!= FILE_PIPE_BYTE_STREAM_MODE
) ? PIPE_READMODE_MESSAGE
: PIPE_READMODE_BYTE
);
840 if(lpCurInstances
!= NULL
)
842 FILE_PIPE_LOCAL_INFORMATION LocalInfo
;
844 Status
= NtQueryInformationFile(hNamedPipe
,
847 sizeof(FILE_PIPE_LOCAL_INFORMATION
),
848 FilePipeLocalInformation
);
849 if(!NT_SUCCESS(Status
))
851 SetLastErrorByStatus(Status
);
855 *lpCurInstances
= min(LocalInfo
.CurrentInstances
, PIPE_UNLIMITED_INSTANCES
);
858 if(lpMaxCollectionCount
!= NULL
|| lpCollectDataTimeout
!= NULL
)
860 FILE_PIPE_REMOTE_INFORMATION RemoteInfo
;
862 Status
= NtQueryInformationFile(hNamedPipe
,
865 sizeof(FILE_PIPE_REMOTE_INFORMATION
),
866 FilePipeRemoteInformation
);
867 if(!NT_SUCCESS(Status
))
869 SetLastErrorByStatus(Status
);
873 if(lpMaxCollectionCount
!= NULL
)
875 *lpMaxCollectionCount
= RemoteInfo
.MaximumCollectionCount
;
878 if(lpCollectDataTimeout
!= NULL
)
881 *lpCollectDataTimeout
= 0;
885 if(lpUserName
!= NULL
)
887 /* FIXME - open the thread token, call ImpersonateNamedPipeClient() and
888 retreive the user name with GetUserName(), revert the impersonation
889 and finally restore the thread token */
900 GetNamedPipeHandleStateA(HANDLE hNamedPipe
,
902 LPDWORD lpCurInstances
,
903 LPDWORD lpMaxCollectionCount
,
904 LPDWORD lpCollectDataTimeout
,
906 DWORD nMaxUserNameSize
)
908 UNICODE_STRING UserNameW
;
909 ANSI_STRING UserNameA
;
912 if(lpUserName
!= NULL
)
914 UserNameW
.Length
= 0;
915 UserNameW
.MaximumLength
= nMaxUserNameSize
* sizeof(WCHAR
);
916 UserNameW
.Buffer
= HeapAlloc(GetCurrentProcess(), 0, UserNameW
.MaximumLength
);
918 UserNameA
.Buffer
= lpUserName
;
919 UserNameA
.Length
= 0;
920 UserNameA
.MaximumLength
= nMaxUserNameSize
;
923 Ret
= GetNamedPipeHandleStateW(hNamedPipe
,
926 lpMaxCollectionCount
,
927 lpCollectDataTimeout
,
931 if(Ret
&& lpUserName
!= NULL
)
933 NTSTATUS Status
= RtlUnicodeStringToAnsiString(&UserNameA
, &UserNameW
, FALSE
);
934 if(!NT_SUCCESS(Status
))
936 SetLastErrorByStatus(Status
);
941 if(UserNameW
.Buffer
!= NULL
)
943 HeapFree(GetCurrentProcess(), 0, UserNameW
.Buffer
);
954 GetNamedPipeInfo(HANDLE hNamedPipe
,
956 LPDWORD lpOutBufferSize
,
957 LPDWORD lpInBufferSize
,
958 LPDWORD lpMaxInstances
)
960 FILE_PIPE_LOCAL_INFORMATION PipeLocalInformation
;
961 IO_STATUS_BLOCK StatusBlock
;
964 Status
= NtQueryInformationFile(hNamedPipe
,
966 &PipeLocalInformation
,
967 sizeof(FILE_PIPE_LOCAL_INFORMATION
),
968 FilePipeLocalInformation
);
969 if (!NT_SUCCESS(Status
))
971 SetLastErrorByStatus(Status
);
977 *lpFlags
= (PipeLocalInformation
.NamedPipeEnd
== FILE_PIPE_SERVER_END
) ? PIPE_SERVER_END
: PIPE_CLIENT_END
;
978 *lpFlags
|= (PipeLocalInformation
.NamedPipeType
== 1) ? PIPE_TYPE_MESSAGE
: PIPE_TYPE_BYTE
;
981 if (lpOutBufferSize
!= NULL
)
982 *lpOutBufferSize
= PipeLocalInformation
.OutboundQuota
;
984 if (lpInBufferSize
!= NULL
)
985 *lpInBufferSize
= PipeLocalInformation
.InboundQuota
;
987 if (lpMaxInstances
!= NULL
)
989 if (PipeLocalInformation
.MaximumInstances
>= 255)
990 *lpMaxInstances
= PIPE_UNLIMITED_INSTANCES
;
992 *lpMaxInstances
= PipeLocalInformation
.MaximumInstances
;
1003 PeekNamedPipe(HANDLE hNamedPipe
,
1006 LPDWORD lpBytesRead
,
1007 LPDWORD lpTotalBytesAvail
,
1008 LPDWORD lpBytesLeftThisMessage
)
1010 PFILE_PIPE_PEEK_BUFFER Buffer
;
1011 IO_STATUS_BLOCK Iosb
;
1015 /* Calculate the buffer space that we'll need and allocate it */
1016 BufferSize
= nBufferSize
+ FIELD_OFFSET(FILE_PIPE_PEEK_BUFFER
, Data
[0]);
1017 Buffer
= RtlAllocateHeap(RtlGetProcessHeap(), 0, BufferSize
);
1019 /* Tell the driver to seek */
1020 Status
= NtFsControlFile(hNamedPipe
,
1030 if (Status
== STATUS_PENDING
)
1032 /* Wait for npfs to be done, and update the status */
1033 Status
= NtWaitForSingleObject(hNamedPipe
, FALSE
, NULL
);
1034 if (NT_SUCCESS(Status
)) Status
= Iosb
.Status
;
1037 /* Overflow is success for us */
1038 if (Status
== STATUS_BUFFER_OVERFLOW
) Status
= STATUS_SUCCESS
;
1041 if (!NT_SUCCESS(Status
))
1043 /* Free the buffer and return failure */
1044 RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer
);
1045 SetLastErrorByStatus(Status
);
1049 /* Check if caller requested bytes available */
1050 if (lpTotalBytesAvail
) *lpTotalBytesAvail
= Buffer
->ReadDataAvailable
;
1052 /* Check if caller requested bytes read */
1055 /* Calculate the bytes returned, minus our structure overhead */
1056 *lpBytesRead
= (ULONG
)(Iosb
.Information
-
1057 FIELD_OFFSET(FILE_PIPE_PEEK_BUFFER
, Data
[0]));
1060 /* Check if caller requested bytes left */
1061 if (lpBytesLeftThisMessage
)
1063 /* Calculate total minus what we returned and our structure overhead */
1064 *lpBytesLeftThisMessage
= Buffer
->MessageLength
-
1065 (ULONG
)(Iosb
.Information
-
1066 FIELD_OFFSET(FILE_PIPE_PEEK_BUFFER
, Data
[0]));
1069 /* Check if the caller wanted to see the actual data */
1072 /* Give him what he wants */
1073 RtlCopyMemory(lpBuffer
,
1076 FIELD_OFFSET(FILE_PIPE_PEEK_BUFFER
, Data
[0]));
1079 /* Free the buffer and return success */
1080 RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer
);
1088 TransactNamedPipe(IN HANDLE hNamedPipe
,
1089 IN LPVOID lpInBuffer
,
1090 IN DWORD nInBufferSize
,
1091 OUT LPVOID lpOutBuffer
,
1092 IN DWORD nOutBufferSize
,
1093 OUT LPDWORD lpBytesRead OPTIONAL
,
1094 IN LPOVERLAPPED lpOverlapped OPTIONAL
)
1098 if (lpBytesRead
!= NULL
)
1103 if (lpOverlapped
!= NULL
)
1107 ApcContext
= (((ULONG_PTR
)lpOverlapped
->hEvent
& 0x1) ? NULL
: lpOverlapped
);
1108 lpOverlapped
->Internal
= STATUS_PENDING
;
1110 Status
= NtFsControlFile(hNamedPipe
,
1111 lpOverlapped
->hEvent
,
1114 (PIO_STATUS_BLOCK
)lpOverlapped
,
1115 FSCTL_PIPE_TRANSCEIVE
,
1121 /* return FALSE in case of failure and pending operations! */
1122 if (!NT_SUCCESS(Status
) || Status
== STATUS_PENDING
)
1124 SetLastErrorByStatus(Status
);
1128 if (lpBytesRead
!= NULL
)
1130 *lpBytesRead
= lpOverlapped
->InternalHigh
;
1135 IO_STATUS_BLOCK Iosb
;
1137 Status
= NtFsControlFile(hNamedPipe
,
1142 FSCTL_PIPE_TRANSCEIVE
,
1148 /* wait in case operation is pending */
1149 if (Status
== STATUS_PENDING
)
1151 Status
= NtWaitForSingleObject(hNamedPipe
,
1154 if (NT_SUCCESS(Status
))
1156 Status
= Iosb
.Status
;
1160 if (NT_SUCCESS(Status
))
1162 /* lpNumberOfBytesRead must not be NULL here, in fact Win doesn't
1163 check that case either and crashes (only after the operation
1165 *lpBytesRead
= Iosb
.Information
;
1169 SetLastErrorByStatus(Status
);