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 DEBUG_CHANNEL(kernel32file
);
17 #define USING_PROPER_NPFS_WAIT_SEMANTICS
19 /* FUNCTIONS ****************************************************************/
26 CreateNamedPipeA(LPCSTR lpName
,
32 DWORD nDefaultTimeOut
,
33 LPSECURITY_ATTRIBUTES lpSecurityAttributes
)
35 PUNICODE_STRING NameU
= &NtCurrentTeb()->StaticUnicodeString
;
38 /* Initialize the string as ANSI_STRING and convert to Unicode */
39 RtlInitAnsiString(&NameA
, (LPSTR
)lpName
);
40 RtlAnsiStringToUnicodeString(NameU
, &NameA
, FALSE
);
42 /* Call the Unicode API */
43 return CreateNamedPipeW(NameU
->Buffer
,
50 lpSecurityAttributes
);
59 CreateNamedPipeW(LPCWSTR lpName
,
65 DWORD nDefaultTimeOut
,
66 LPSECURITY_ATTRIBUTES lpSecurityAttributes
)
68 UNICODE_STRING NamedPipeName
;
71 OBJECT_ATTRIBUTES ObjectAttributes
;
73 ACCESS_MASK DesiredAccess
;
74 ULONG CreateOptions
= 0;
75 ULONG WriteModeMessage
;
76 ULONG ReadModeMessage
;
79 ULONG ShareAccess
= 0, Attributes
;
80 LARGE_INTEGER DefaultTimeOut
;
81 PSECURITY_DESCRIPTOR SecurityDescriptor
= NULL
;
83 /* Check for valid instances */
84 if (nMaxInstances
== 0 || nMaxInstances
> PIPE_UNLIMITED_INSTANCES
)
87 SetLastError(ERROR_INVALID_PARAMETER
);
88 return INVALID_HANDLE_VALUE
;
91 /* Convert to NT syntax */
92 if (nMaxInstances
== PIPE_UNLIMITED_INSTANCES
)
95 /* Convert the name */
96 Result
= RtlDosPathNameToNtPathName_U(lpName
,
102 /* Conversion failed */
103 SetLastError(ERROR_PATH_NOT_FOUND
);
104 return INVALID_HANDLE_VALUE
;
107 TRACE("Pipe name: %wZ\n", &NamedPipeName
);
108 TRACE("Pipe name: %S\n", NamedPipeName
.Buffer
);
110 /* Always case insensitive, check if we got extra attributes */
111 Attributes
= OBJ_CASE_INSENSITIVE
;
112 if(lpSecurityAttributes
)
114 /* We did; get the security descriptor */
115 SecurityDescriptor
= lpSecurityAttributes
->lpSecurityDescriptor
;
117 /* And check if this is pipe's handle will beinheritable */
118 if (lpSecurityAttributes
->bInheritHandle
)
119 Attributes
|= OBJ_INHERIT
;
122 /* Now we can initialize the object attributes */
123 InitializeObjectAttributes(&ObjectAttributes
,
129 /* Setup the default Desired Access */
130 DesiredAccess
= SYNCHRONIZE
| (dwOpenMode
& (WRITE_DAC
|
132 ACCESS_SYSTEM_SECURITY
));
134 /* Convert to NT Create Flags */
135 if (dwOpenMode
& FILE_FLAG_WRITE_THROUGH
)
137 CreateOptions
|= FILE_WRITE_THROUGH
;
140 if (!(dwOpenMode
& FILE_FLAG_OVERLAPPED
))
142 CreateOptions
|= FILE_SYNCHRONOUS_IO_NONALERT
;
145 /* Handle all open modes */
146 if (dwOpenMode
& PIPE_ACCESS_OUTBOUND
)
148 ShareAccess
|= FILE_SHARE_READ
;
149 DesiredAccess
|= GENERIC_WRITE
;
152 if (dwOpenMode
& PIPE_ACCESS_INBOUND
)
154 ShareAccess
|= FILE_SHARE_WRITE
;
155 DesiredAccess
|= GENERIC_READ
;
158 /* Handle the type flags */
159 if (dwPipeMode
& PIPE_TYPE_MESSAGE
)
161 WriteModeMessage
= FILE_PIPE_MESSAGE_TYPE
;
165 WriteModeMessage
= FILE_PIPE_BYTE_STREAM_TYPE
;
168 /* Handle the mode flags */
169 if (dwPipeMode
& PIPE_READMODE_MESSAGE
)
171 ReadModeMessage
= FILE_PIPE_MESSAGE_MODE
;
175 ReadModeMessage
= FILE_PIPE_BYTE_STREAM_MODE
;
178 /* Handle the blocking mode */
179 if (dwPipeMode
& PIPE_NOWAIT
)
181 NonBlocking
= FILE_PIPE_COMPLETE_OPERATION
;
185 NonBlocking
= FILE_PIPE_QUEUE_OPERATION
;
188 /* Check if we have a timeout */
191 /* Convert the time to NT format */
192 DefaultTimeOut
.QuadPart
= UInt32x32To64(nDefaultTimeOut
, -10000);
196 /* Use default timeout of 50 ms */
197 DefaultTimeOut
.QuadPart
= -500000;
200 /* Now create the pipe */
201 Status
= NtCreateNamedPipeFile(&PipeHandle
,
216 /* Normalize special error codes */
217 if ((Status
== STATUS_INVALID_DEVICE_REQUEST
) ||
218 (Status
== STATUS_NOT_SUPPORTED
))
220 Status
= STATUS_OBJECT_NAME_INVALID
;
224 RtlFreeHeap(RtlGetProcessHeap(),
226 NamedPipeName
.Buffer
);
229 if (!NT_SUCCESS(Status
))
231 /* Failed to create it */
232 WARN("NtCreateNamedPipe failed (Status %x)!\n", Status
);
233 SetLastErrorByStatus (Status
);
234 return INVALID_HANDLE_VALUE
;
237 /* Return the handle */
247 WaitNamedPipeA(LPCSTR lpNamedPipeName
,
251 UNICODE_STRING NameU
;
253 /* Convert the name to Unicode */
254 Basep8BitStringToHeapUnicodeString(&NameU
, lpNamedPipeName
);
256 /* Call the Unicode API */
257 r
= WaitNamedPipeW(NameU
.Buffer
, nTimeOut
);
259 /* Free the Unicode string */
260 RtlFreeUnicodeString(&NameU
);
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 TRACE("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
== RtlPathTypeLocalDevice
)
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 TRACE("NewName: %wZ\n", &NewName
);
316 RtlInitUnicodeString(&DevicePath
, L
"\\DosDevices\\pipe\\");
318 else if (Type
== RtlPathTypeRootLocalDevice
)
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 WARN("Invalid name!\n");
343 SetLastErrorByStatus(STATUS_OBJECT_PATH_SYNTAX_BAD
);
347 /* FIXME: Open \DosDevices\Unc\Server\Pipe\Name */
351 WARN("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 TRACE("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 WARN("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
= TRUE
;
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 WARN("Status: %lx\n", Status
);
444 SetLastErrorByStatus (Status
);
458 ConnectNamedPipe(IN HANDLE hNamedPipe
,
459 IN LPOVERLAPPED lpOverlapped
)
463 if (lpOverlapped
!= NULL
)
467 lpOverlapped
->Internal
= STATUS_PENDING
;
468 ApcContext
= (((ULONG_PTR
)lpOverlapped
->hEvent
& 0x1) ? NULL
: lpOverlapped
);
470 Status
= NtFsControlFile(hNamedPipe
,
471 lpOverlapped
->hEvent
,
474 (PIO_STATUS_BLOCK
)lpOverlapped
,
481 /* return FALSE in case of failure and pending operations! */
482 if (!NT_SUCCESS(Status
) || Status
== STATUS_PENDING
)
484 SetLastErrorByStatus(Status
);
490 IO_STATUS_BLOCK Iosb
;
492 Status
= NtFsControlFile(hNamedPipe
,
503 /* wait in case operation is pending */
504 if (Status
== STATUS_PENDING
)
506 Status
= NtWaitForSingleObject(hNamedPipe
,
509 if (NT_SUCCESS(Status
))
511 Status
= Iosb
.Status
;
515 if (!NT_SUCCESS(Status
))
517 SetLastErrorByStatus(Status
);
531 SetNamedPipeHandleState(HANDLE hNamedPipe
,
533 LPDWORD lpMaxCollectionCount
,
534 LPDWORD lpCollectDataTimeout
)
536 IO_STATUS_BLOCK Iosb
;
539 /* Check if the Mode is being changed */
542 FILE_PIPE_INFORMATION Settings
;
544 /* Set the Completion Mode */
545 Settings
.CompletionMode
= (*lpMode
& PIPE_NOWAIT
) ?
546 FILE_PIPE_COMPLETE_OPERATION
: FILE_PIPE_QUEUE_OPERATION
;
548 /* Set the Read Mode */
549 Settings
.ReadMode
= (*lpMode
& PIPE_READMODE_MESSAGE
) ?
550 FILE_PIPE_MESSAGE_MODE
: FILE_PIPE_BYTE_STREAM_MODE
;
552 /* Send the changes to the Driver */
553 Status
= NtSetInformationFile(hNamedPipe
,
556 sizeof(FILE_PIPE_INFORMATION
),
557 FilePipeInformation
);
558 if (!NT_SUCCESS(Status
))
560 SetLastErrorByStatus(Status
);
565 /* Check if the Collection count or Timeout are being changed */
566 if (lpMaxCollectionCount
|| lpCollectDataTimeout
)
568 FILE_PIPE_REMOTE_INFORMATION RemoteSettings
;
570 /* Setting one without the other would delete it, so we read old one */
571 if (!lpMaxCollectionCount
|| !lpCollectDataTimeout
)
573 Status
= NtQueryInformationFile(hNamedPipe
,
576 sizeof(FILE_PIPE_REMOTE_INFORMATION
),
577 FilePipeRemoteInformation
);
578 if (!NT_SUCCESS(Status
))
580 SetLastErrorByStatus(Status
);
585 /* Now set the new settings */
586 RemoteSettings
.MaximumCollectionCount
= (lpMaxCollectionCount
) ?
587 *lpMaxCollectionCount
:
588 RemoteSettings
.MaximumCollectionCount
;
589 if (lpCollectDataTimeout
)
591 /* Convert it to Quad */
592 RemoteSettings
.CollectDataTime
.QuadPart
=
593 -(LONGLONG
)UInt32x32To64(10000, *lpCollectDataTimeout
);
596 /* Tell the driver to change them */
597 Status
= NtSetInformationFile(hNamedPipe
,
600 sizeof(FILE_PIPE_REMOTE_INFORMATION
),
601 FilePipeRemoteInformation
);
602 if (!NT_SUCCESS(Status
))
604 SetLastErrorByStatus(Status
);
618 CallNamedPipeA(LPCSTR lpNamedPipeName
,
622 DWORD nOutBufferSize
,
626 PUNICODE_STRING PipeName
= &NtCurrentTeb()->StaticUnicodeString
;
627 ANSI_STRING AnsiPipe
;
629 /* Initialize the string as ANSI_STRING and convert to Unicode */
630 RtlInitAnsiString(&AnsiPipe
, (LPSTR
)lpNamedPipeName
);
631 RtlAnsiStringToUnicodeString(PipeName
, &AnsiPipe
, FALSE
);
633 /* Call the Unicode function */
634 return CallNamedPipeW(PipeName
->Buffer
,
649 CallNamedPipeW(LPCWSTR lpNamedPipeName
,
653 DWORD nOutBufferSize
,
664 /* Try creating it */
665 hPipe
= CreateFileW(lpNamedPipeName
,
666 GENERIC_READ
| GENERIC_WRITE
,
667 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
670 FILE_ATTRIBUTE_NORMAL
,
673 /* Success, break out */
674 if (hPipe
!= INVALID_HANDLE_VALUE
)
677 /* Already tried twice, give up */
682 WaitNamedPipeW(lpNamedPipeName
, nTimeOut
);
684 /* Get ready to try again */
688 /* Set the pipe mode */
689 dwPipeMode
= PIPE_READMODE_MESSAGE
| PIPE_WAIT
;
690 bError
= SetNamedPipeHandleState(hPipe
, &dwPipeMode
, NULL
, NULL
);
693 /* Couldn't change state, fail */
698 /* Do the transact */
699 bError
= TransactNamedPipe(hPipe
,
707 /* Close the handle */
719 DisconnectNamedPipe(HANDLE hNamedPipe
)
721 IO_STATUS_BLOCK Iosb
;
724 /* Send the FSCTL to the driver */
725 Status
= NtFsControlFile(hNamedPipe
,
730 FSCTL_PIPE_DISCONNECT
,
735 if (Status
== STATUS_PENDING
)
737 /* Wait on NPFS to finish and get updated status */
738 Status
= NtWaitForSingleObject(hNamedPipe
, FALSE
, NULL
);
739 if (NT_SUCCESS(Status
))
740 Status
= Iosb
.Status
;
743 /* Check for error */
744 if (!NT_SUCCESS(Status
))
747 SetLastErrorByStatus(Status
);
760 GetNamedPipeHandleStateW(HANDLE hNamedPipe
,
762 LPDWORD lpCurInstances
,
763 LPDWORD lpMaxCollectionCount
,
764 LPDWORD lpCollectDataTimeout
,
766 DWORD nMaxUserNameSize
)
768 IO_STATUS_BLOCK StatusBlock
;
773 FILE_PIPE_INFORMATION PipeInfo
;
775 Status
= NtQueryInformationFile(hNamedPipe
,
778 sizeof(FILE_PIPE_INFORMATION
),
779 FilePipeInformation
);
780 if (!NT_SUCCESS(Status
))
782 SetLastErrorByStatus(Status
);
786 *lpState
= ((PipeInfo
.CompletionMode
!= FILE_PIPE_QUEUE_OPERATION
) ? PIPE_NOWAIT
: PIPE_WAIT
);
787 *lpState
|= ((PipeInfo
.ReadMode
!= FILE_PIPE_BYTE_STREAM_MODE
) ? PIPE_READMODE_MESSAGE
: PIPE_READMODE_BYTE
);
790 if(lpCurInstances
!= NULL
)
792 FILE_PIPE_LOCAL_INFORMATION LocalInfo
;
794 Status
= NtQueryInformationFile(hNamedPipe
,
797 sizeof(FILE_PIPE_LOCAL_INFORMATION
),
798 FilePipeLocalInformation
);
799 if (!NT_SUCCESS(Status
))
801 SetLastErrorByStatus(Status
);
805 *lpCurInstances
= min(LocalInfo
.CurrentInstances
, PIPE_UNLIMITED_INSTANCES
);
808 if (lpMaxCollectionCount
!= NULL
|| lpCollectDataTimeout
!= NULL
)
810 FILE_PIPE_REMOTE_INFORMATION RemoteInfo
;
812 Status
= NtQueryInformationFile(hNamedPipe
,
815 sizeof(FILE_PIPE_REMOTE_INFORMATION
),
816 FilePipeRemoteInformation
);
817 if (!NT_SUCCESS(Status
))
819 SetLastErrorByStatus(Status
);
823 if (lpMaxCollectionCount
!= NULL
)
825 *lpMaxCollectionCount
= RemoteInfo
.MaximumCollectionCount
;
828 if(lpCollectDataTimeout
!= NULL
)
831 *lpCollectDataTimeout
= 0;
835 if (lpUserName
!= NULL
)
837 /* FIXME - open the thread token, call ImpersonateNamedPipeClient() and
838 retreive the user name with GetUserName(), revert the impersonation
839 and finally restore the thread token */
851 GetNamedPipeHandleStateA(HANDLE hNamedPipe
,
853 LPDWORD lpCurInstances
,
854 LPDWORD lpMaxCollectionCount
,
855 LPDWORD lpCollectDataTimeout
,
857 DWORD nMaxUserNameSize
)
859 UNICODE_STRING UserNameW
= { 0, 0, NULL
};
860 ANSI_STRING UserNameA
;
863 if(lpUserName
!= NULL
)
865 UserNameW
.MaximumLength
= (USHORT
)nMaxUserNameSize
* sizeof(WCHAR
);
866 UserNameW
.Buffer
= RtlAllocateHeap(RtlGetProcessHeap(), 0, UserNameW
.MaximumLength
);
867 if (UserNameW
.Buffer
== NULL
)
869 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
873 UserNameA
.Buffer
= lpUserName
;
874 UserNameA
.Length
= 0;
875 UserNameA
.MaximumLength
= (USHORT
)nMaxUserNameSize
;
878 Ret
= GetNamedPipeHandleStateW(hNamedPipe
,
881 lpMaxCollectionCount
,
882 lpCollectDataTimeout
,
885 if (Ret
&& lpUserName
!= NULL
)
889 RtlInitUnicodeString(&UserNameW
, UserNameW
.Buffer
);
890 Status
= RtlUnicodeStringToAnsiString(&UserNameA
, &UserNameW
, FALSE
);
891 if (!NT_SUCCESS(Status
))
893 SetLastErrorByStatus(Status
);
898 if (UserNameW
.Buffer
!= NULL
)
900 RtlFreeHeap(RtlGetProcessHeap(), 0, UserNameW
.Buffer
);
912 GetNamedPipeInfo(HANDLE hNamedPipe
,
914 LPDWORD lpOutBufferSize
,
915 LPDWORD lpInBufferSize
,
916 LPDWORD lpMaxInstances
)
918 FILE_PIPE_LOCAL_INFORMATION PipeLocalInformation
;
919 IO_STATUS_BLOCK StatusBlock
;
922 Status
= NtQueryInformationFile(hNamedPipe
,
924 &PipeLocalInformation
,
925 sizeof(FILE_PIPE_LOCAL_INFORMATION
),
926 FilePipeLocalInformation
);
927 if (!NT_SUCCESS(Status
))
929 SetLastErrorByStatus(Status
);
935 *lpFlags
= (PipeLocalInformation
.NamedPipeEnd
== FILE_PIPE_SERVER_END
) ? PIPE_SERVER_END
: PIPE_CLIENT_END
;
936 *lpFlags
|= (PipeLocalInformation
.NamedPipeType
== 1) ? PIPE_TYPE_MESSAGE
: PIPE_TYPE_BYTE
;
939 if (lpOutBufferSize
!= NULL
)
940 *lpOutBufferSize
= PipeLocalInformation
.OutboundQuota
;
942 if (lpInBufferSize
!= NULL
)
943 *lpInBufferSize
= PipeLocalInformation
.InboundQuota
;
945 if (lpMaxInstances
!= NULL
)
947 if (PipeLocalInformation
.MaximumInstances
>= 255)
948 *lpMaxInstances
= PIPE_UNLIMITED_INSTANCES
;
950 *lpMaxInstances
= PipeLocalInformation
.MaximumInstances
;
962 PeekNamedPipe(HANDLE hNamedPipe
,
966 LPDWORD lpTotalBytesAvail
,
967 LPDWORD lpBytesLeftThisMessage
)
969 PFILE_PIPE_PEEK_BUFFER Buffer
;
970 IO_STATUS_BLOCK Iosb
;
975 /* Calculate the buffer space that we'll need and allocate it */
976 BufferSize
= nBufferSize
+ sizeof(FILE_PIPE_PEEK_BUFFER
);
977 Buffer
= RtlAllocateHeap(RtlGetProcessHeap(), 0, BufferSize
);
980 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
984 /* Tell the driver to seek */
985 Status
= NtFsControlFile(hNamedPipe
,
995 if (Status
== STATUS_PENDING
)
997 /* Wait for npfs to be done, and update the status */
998 Status
= NtWaitForSingleObject(hNamedPipe
, FALSE
, NULL
);
999 if (NT_SUCCESS(Status
))
1000 Status
= Iosb
.Status
;
1003 /* Overflow is success for us */
1004 if (Status
== STATUS_BUFFER_OVERFLOW
)
1005 Status
= STATUS_SUCCESS
;
1008 if (!NT_SUCCESS(Status
))
1010 /* Free the buffer and return failure */
1011 RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer
);
1012 SetLastErrorByStatus(Status
);
1016 /* Check if caller requested bytes available */
1017 if (lpTotalBytesAvail
)
1018 *lpTotalBytesAvail
= Buffer
->ReadDataAvailable
;
1020 /* Calculate the bytes returned, minus our structure overhead */
1021 BytesRead
= (ULONG
)(Iosb
.Information
-
1022 FIELD_OFFSET(FILE_PIPE_PEEK_BUFFER
, Data
[0]));
1024 /* Check if caller requested bytes read */
1027 /* Return the bytes read */
1028 *lpBytesRead
= BytesRead
;
1031 /* Check if caller requested bytes left */
1032 if (lpBytesLeftThisMessage
)
1034 /* Calculate total minus what we returned and our structure overhead */
1035 *lpBytesLeftThisMessage
= Buffer
->MessageLength
- BytesRead
;
1038 /* Check if the caller wanted to see the actual data */
1041 /* Give him what he wants */
1042 RtlCopyMemory(lpBuffer
,
1047 /* Free the buffer */
1048 RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer
);
1059 TransactNamedPipe(IN HANDLE hNamedPipe
,
1060 IN LPVOID lpInBuffer
,
1061 IN DWORD nInBufferSize
,
1062 OUT LPVOID lpOutBuffer
,
1063 IN DWORD nOutBufferSize
,
1064 OUT LPDWORD lpBytesRead OPTIONAL
,
1065 IN LPOVERLAPPED lpOverlapped OPTIONAL
)
1069 if (lpBytesRead
!= NULL
)
1074 if (lpOverlapped
!= NULL
)
1078 ApcContext
= (((ULONG_PTR
)lpOverlapped
->hEvent
& 0x1) ? NULL
: lpOverlapped
);
1079 lpOverlapped
->Internal
= STATUS_PENDING
;
1081 Status
= NtFsControlFile(hNamedPipe
,
1082 lpOverlapped
->hEvent
,
1085 (PIO_STATUS_BLOCK
)lpOverlapped
,
1086 FSCTL_PIPE_TRANSCEIVE
,
1091 if (!NT_SUCCESS(Status
) || Status
== STATUS_PENDING
)
1093 SetLastErrorByStatus(Status
);
1097 if (lpBytesRead
!= NULL
)
1099 *lpBytesRead
= lpOverlapped
->InternalHigh
;
1104 #if 0 /* We don't implement FSCTL_PIPE_TRANSCEIVE yet */
1105 IO_STATUS_BLOCK Iosb
;
1107 Status
= NtFsControlFile(hNamedPipe
,
1112 FSCTL_PIPE_TRANSCEIVE
,
1117 if (Status
== STATUS_PENDING
)
1119 Status
= NtWaitForSingleObject(hNamedPipe
,
1122 if (NT_SUCCESS(Status
))
1123 Status
= Iosb
.Status
;
1126 if (NT_SUCCESS(Status
))
1128 /* lpNumberOfBytesRead must not be NULL here, in fact Win doesn't
1129 check that case either and crashes (only after the operation
1131 *lpBytesRead
= Iosb
.Information
;
1135 SetLastErrorByStatus(Status
);
1138 #else /* Workaround while FSCTL_PIPE_TRANSCEIVE not available */
1141 while (0 != nInBufferSize
&&
1142 WriteFile(hNamedPipe
, lpInBuffer
, nInBufferSize
, &nActualBytes
,
1145 lpInBuffer
= (LPVOID
)((char *) lpInBuffer
+ nActualBytes
);
1146 nInBufferSize
-= nActualBytes
;
1149 if (0 != nInBufferSize
)
1151 /* Must have dropped out of the while 'cause WriteFile failed */
1155 if (!ReadFile(hNamedPipe
, lpOutBuffer
, nOutBufferSize
, &nActualBytes
,
1161 if (NULL
!= lpBytesRead
)
1163 *lpBytesRead
= nActualBytes
;