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 *****************************************************************/
13 #include <wine/debug.h>
15 WINE_DEFAULT_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
);
268 * When NPFS will work properly, use this code instead. It is compatible with
269 * Microsoft's NPFS.SYS. The main difference is that:
270 * - This code actually respects the timeout instead of ignoring it!
271 * - This code validates and creates the proper names for both UNC and local pipes
272 * - On NT, you open the *root* pipe directory (either \DosDevices\Pipe or
273 * \DosDevices\Unc\Server\Pipe) and then send the pipe to wait on in the
274 * FILE_PIPE_WAIT_FOR_BUFFER structure.
276 #ifdef USING_PROPER_NPFS_WAIT_SEMANTICS
282 WaitNamedPipeW(LPCWSTR lpNamedPipeName
,
285 UNICODE_STRING NamedPipeName
, NewName
, DevicePath
, PipePrefix
;
290 OBJECT_ATTRIBUTES ObjectAttributes
;
293 IO_STATUS_BLOCK IoStatusBlock
;
294 ULONG WaitPipeInfoSize
;
295 PFILE_PIPE_WAIT_FOR_BUFFER WaitPipeInfo
;
297 /* Start by making a unicode string of the name */
298 TRACE("Sent path: %S\n", lpNamedPipeName
);
299 RtlCreateUnicodeString(&NamedPipeName
, lpNamedPipeName
);
300 NameLength
= NamedPipeName
.Length
/ sizeof(WCHAR
);
302 /* All slashes must become backslashes */
303 for (i
= 0; i
< NameLength
; i
++)
305 /* Check and convert */
306 if (NamedPipeName
.Buffer
[i
] == L
'/') NamedPipeName
.Buffer
[i
] = L
'\\';
309 /* Find the path type of the name we were given */
310 NewName
= NamedPipeName
;
311 Type
= RtlDetermineDosPathNameType_U(lpNamedPipeName
);
313 /* Check if this was a device path, ie : "\\.\pipe\name" */
314 if (Type
== RtlPathTypeLocalDevice
)
316 /* Make sure it's a valid prefix */
317 RtlInitUnicodeString(&PipePrefix
, L
"\\\\.\\pipe\\");
318 RtlPrefixString((PANSI_STRING
)&PipePrefix
, (PANSI_STRING
)&NewName
, TRUE
);
322 NewName
.Length
-= 9 * sizeof(WCHAR
);
324 /* Initialize the Dos Devices name */
325 TRACE("NewName: %wZ\n", &NewName
);
326 RtlInitUnicodeString(&DevicePath
, L
"\\DosDevices\\pipe\\");
328 else if (Type
== RtlPathTypeRootLocalDevice
)
330 /* The path is \\server\\pipe\name; find the pipename itself */
331 p
= &NewName
.Buffer
[2];
333 /* First loop to get past the server name */
336 /* Check if this is a backslash */
337 if (*p
== L
'\\') break;
343 /* Now make sure the full name contains "pipe\" */
344 if ((*p
) && !(_wcsnicmp(p
+ 1, L
"pipe\\", sizeof("pipe\\"))))
346 /* Get to the pipe name itself now */
347 p
+= sizeof("pipe\\") - 1;
351 /* The name is invalid */
352 WARN("Invalid name!\n");
353 SetLastErrorByStatus(STATUS_OBJECT_PATH_SYNTAX_BAD
);
357 /* FIXME: Open \DosDevices\Unc\Server\Pipe\Name */
361 WARN("Invalid path type\n");
362 SetLastErrorByStatus(STATUS_OBJECT_PATH_SYNTAX_BAD
);
366 /* Now calculate the total length of the structure and allocate it */
367 WaitPipeInfoSize
= FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER
, Name
[0]) +
369 WaitPipeInfo
= RtlAllocateHeap(RtlGetProcessHeap(), 0, WaitPipeInfoSize
);
370 if (WaitPipeInfo
== NULL
)
372 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
376 /* Initialize the object attributes */
377 TRACE("Opening: %wZ\n", &DevicePath
);
378 InitializeObjectAttributes(&ObjectAttributes
,
380 OBJ_CASE_INSENSITIVE
,
385 Status
= NtOpenFile(&FileHandle
,
386 FILE_READ_ATTRIBUTES
| SYNCHRONIZE
,
389 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
390 FILE_SYNCHRONOUS_IO_NONALERT
);
391 if (!NT_SUCCESS(Status
))
393 /* Fail; couldn't open */
394 WARN("Status: %lx\n", Status
);
395 SetLastErrorByStatus(Status
);
396 RtlFreeUnicodeString(&NamedPipeName
);
397 RtlFreeHeap(RtlGetProcessHeap(), 0, WaitPipeInfo
);
401 /* Check what timeout we got */
402 if (nTimeOut
== NMPWAIT_USE_DEFAULT_WAIT
)
404 /* Don't use a timeout */
405 WaitPipeInfo
->TimeoutSpecified
= FALSE
;
409 /* Check if we should wait forever */
410 if (nTimeOut
== NMPWAIT_WAIT_FOREVER
)
413 WaitPipeInfo
->Timeout
.LowPart
= 0;
414 WaitPipeInfo
->Timeout
.HighPart
= 0x80000000;
418 /* Convert to NT format */
419 WaitPipeInfo
->Timeout
.QuadPart
= UInt32x32To64(-10000, nTimeOut
);
422 /* In both cases, we do have a timeout */
423 WaitPipeInfo
->TimeoutSpecified
= TRUE
;
426 /* Set the length and copy the name */
427 WaitPipeInfo
->NameLength
= NewName
.Length
;
428 RtlCopyMemory(WaitPipeInfo
->Name
, NewName
.Buffer
, NewName
.Length
);
430 /* Get rid of the full name */
431 RtlFreeUnicodeString(&NamedPipeName
);
433 /* Let NPFS know of our request */
434 Status
= NtFsControlFile(FileHandle
,
445 /* Free our pipe info data and close the handle */
446 RtlFreeHeap(RtlGetProcessHeap(), 0, WaitPipeInfo
);
449 /* Check the status */
450 if (!NT_SUCCESS(Status
))
452 /* Failure to wait on the pipe */
453 WARN("Status: %lx\n", Status
);
454 SetLastErrorByStatus (Status
);
467 WaitNamedPipeW(LPCWSTR lpNamedPipeName
,
470 UNICODE_STRING NamedPipeName
;
472 OBJECT_ATTRIBUTES ObjectAttributes
;
473 FILE_PIPE_WAIT_FOR_BUFFER WaitPipe
;
475 IO_STATUS_BLOCK Iosb
;
477 if (RtlDosPathNameToNtPathName_U(lpNamedPipeName
,
485 InitializeObjectAttributes(&ObjectAttributes
,
487 OBJ_CASE_INSENSITIVE
,
490 Status
= NtOpenFile(&FileHandle
,
491 FILE_READ_ATTRIBUTES
| SYNCHRONIZE
,
494 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
495 FILE_SYNCHRONOUS_IO_NONALERT
);
496 if (!NT_SUCCESS(Status
))
498 SetLastErrorByStatus(Status
);
502 /* Check what timeout we got */
503 if (nTimeOut
== NMPWAIT_USE_DEFAULT_WAIT
)
505 /* Don't use a timeout */
506 WaitPipe
.TimeoutSpecified
= FALSE
;
510 /* Check if we should wait forever */
511 if (nTimeOut
== NMPWAIT_WAIT_FOREVER
)
514 WaitPipe
.Timeout
.LowPart
= 0;
515 WaitPipe
.Timeout
.HighPart
= 0x80000000;
519 /* Convert to NT format */
520 WaitPipe
.Timeout
.QuadPart
= UInt32x32To64(-10000, nTimeOut
);
523 /* In both cases, we do have a timeout */
524 WaitPipe
.TimeoutSpecified
= TRUE
;
527 Status
= NtFsControlFile(FileHandle
,
538 if (!NT_SUCCESS(Status
))
540 SetLastErrorByStatus(Status
);
554 ConnectNamedPipe(IN HANDLE hNamedPipe
,
555 IN LPOVERLAPPED lpOverlapped
)
559 if (lpOverlapped
!= NULL
)
563 lpOverlapped
->Internal
= STATUS_PENDING
;
564 ApcContext
= (((ULONG_PTR
)lpOverlapped
->hEvent
& 0x1) ? NULL
: lpOverlapped
);
566 Status
= NtFsControlFile(hNamedPipe
,
567 lpOverlapped
->hEvent
,
570 (PIO_STATUS_BLOCK
)lpOverlapped
,
577 /* return FALSE in case of failure and pending operations! */
578 if (!NT_SUCCESS(Status
) || Status
== STATUS_PENDING
)
580 SetLastErrorByStatus(Status
);
586 IO_STATUS_BLOCK Iosb
;
588 Status
= NtFsControlFile(hNamedPipe
,
599 /* wait in case operation is pending */
600 if (Status
== STATUS_PENDING
)
602 Status
= NtWaitForSingleObject(hNamedPipe
,
605 if (NT_SUCCESS(Status
))
607 Status
= Iosb
.Status
;
611 if (!NT_SUCCESS(Status
))
613 SetLastErrorByStatus(Status
);
627 SetNamedPipeHandleState(HANDLE hNamedPipe
,
629 LPDWORD lpMaxCollectionCount
,
630 LPDWORD lpCollectDataTimeout
)
632 IO_STATUS_BLOCK Iosb
;
635 /* Check if the Mode is being changed */
638 FILE_PIPE_INFORMATION Settings
;
640 /* Set the Completion Mode */
641 Settings
.CompletionMode
= (*lpMode
& PIPE_NOWAIT
) ?
642 FILE_PIPE_COMPLETE_OPERATION
: FILE_PIPE_QUEUE_OPERATION
;
644 /* Set the Read Mode */
645 Settings
.ReadMode
= (*lpMode
& PIPE_READMODE_MESSAGE
) ?
646 FILE_PIPE_MESSAGE_MODE
: FILE_PIPE_BYTE_STREAM_MODE
;
648 /* Send the changes to the Driver */
649 Status
= NtSetInformationFile(hNamedPipe
,
652 sizeof(FILE_PIPE_INFORMATION
),
653 FilePipeInformation
);
654 if (!NT_SUCCESS(Status
))
656 SetLastErrorByStatus(Status
);
661 /* Check if the Collection count or Timeout are being changed */
662 if (lpMaxCollectionCount
|| lpCollectDataTimeout
)
664 FILE_PIPE_REMOTE_INFORMATION RemoteSettings
;
666 /* Setting one without the other would delete it, so we read old one */
667 if (!lpMaxCollectionCount
|| !lpCollectDataTimeout
)
669 Status
= NtQueryInformationFile(hNamedPipe
,
672 sizeof(FILE_PIPE_REMOTE_INFORMATION
),
673 FilePipeRemoteInformation
);
674 if (!NT_SUCCESS(Status
))
676 SetLastErrorByStatus(Status
);
681 /* Now set the new settings */
682 RemoteSettings
.MaximumCollectionCount
= (lpMaxCollectionCount
) ?
683 *lpMaxCollectionCount
:
684 RemoteSettings
.MaximumCollectionCount
;
685 if (lpCollectDataTimeout
)
687 /* Convert it to Quad */
688 RemoteSettings
.CollectDataTime
.QuadPart
=
689 -(LONGLONG
)UInt32x32To64(10000, *lpCollectDataTimeout
);
692 /* Tell the driver to change them */
693 Status
= NtSetInformationFile(hNamedPipe
,
696 sizeof(FILE_PIPE_REMOTE_INFORMATION
),
697 FilePipeRemoteInformation
);
698 if (!NT_SUCCESS(Status
))
700 SetLastErrorByStatus(Status
);
714 CallNamedPipeA(LPCSTR lpNamedPipeName
,
718 DWORD nOutBufferSize
,
722 PUNICODE_STRING PipeName
= &NtCurrentTeb()->StaticUnicodeString
;
723 ANSI_STRING AnsiPipe
;
725 /* Initialize the string as ANSI_STRING and convert to Unicode */
726 RtlInitAnsiString(&AnsiPipe
, (LPSTR
)lpNamedPipeName
);
727 RtlAnsiStringToUnicodeString(PipeName
, &AnsiPipe
, FALSE
);
729 /* Call the Unicode function */
730 return CallNamedPipeW(PipeName
->Buffer
,
745 CallNamedPipeW(LPCWSTR lpNamedPipeName
,
749 DWORD nOutBufferSize
,
760 /* Try creating it */
761 hPipe
= CreateFileW(lpNamedPipeName
,
762 GENERIC_READ
| GENERIC_WRITE
,
763 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
766 FILE_ATTRIBUTE_NORMAL
,
769 /* Success, break out */
770 if (hPipe
!= INVALID_HANDLE_VALUE
)
773 /* Already tried twice, give up */
778 WaitNamedPipeW(lpNamedPipeName
, nTimeOut
);
780 /* Get ready to try again */
784 /* Set the pipe mode */
785 dwPipeMode
= PIPE_READMODE_MESSAGE
| PIPE_WAIT
;
786 bError
= SetNamedPipeHandleState(hPipe
, &dwPipeMode
, NULL
, NULL
);
789 /* Couldn't change state, fail */
794 /* Do the transact */
795 bError
= TransactNamedPipe(hPipe
,
803 /* Close the handle */
815 DisconnectNamedPipe(HANDLE hNamedPipe
)
817 IO_STATUS_BLOCK Iosb
;
820 /* Send the FSCTL to the driver */
821 Status
= NtFsControlFile(hNamedPipe
,
826 FSCTL_PIPE_DISCONNECT
,
831 if (Status
== STATUS_PENDING
)
833 /* Wait on NPFS to finish and get updated status */
834 Status
= NtWaitForSingleObject(hNamedPipe
, FALSE
, NULL
);
835 if (NT_SUCCESS(Status
))
836 Status
= Iosb
.Status
;
839 /* Check for error */
840 if (!NT_SUCCESS(Status
))
843 SetLastErrorByStatus(Status
);
856 GetNamedPipeHandleStateW(HANDLE hNamedPipe
,
858 LPDWORD lpCurInstances
,
859 LPDWORD lpMaxCollectionCount
,
860 LPDWORD lpCollectDataTimeout
,
862 DWORD nMaxUserNameSize
)
864 IO_STATUS_BLOCK StatusBlock
;
869 FILE_PIPE_INFORMATION PipeInfo
;
871 Status
= NtQueryInformationFile(hNamedPipe
,
874 sizeof(FILE_PIPE_INFORMATION
),
875 FilePipeInformation
);
876 if (!NT_SUCCESS(Status
))
878 SetLastErrorByStatus(Status
);
882 *lpState
= ((PipeInfo
.CompletionMode
!= FILE_PIPE_QUEUE_OPERATION
) ? PIPE_NOWAIT
: PIPE_WAIT
);
883 *lpState
|= ((PipeInfo
.ReadMode
!= FILE_PIPE_BYTE_STREAM_MODE
) ? PIPE_READMODE_MESSAGE
: PIPE_READMODE_BYTE
);
886 if(lpCurInstances
!= NULL
)
888 FILE_PIPE_LOCAL_INFORMATION LocalInfo
;
890 Status
= NtQueryInformationFile(hNamedPipe
,
893 sizeof(FILE_PIPE_LOCAL_INFORMATION
),
894 FilePipeLocalInformation
);
895 if (!NT_SUCCESS(Status
))
897 SetLastErrorByStatus(Status
);
901 *lpCurInstances
= min(LocalInfo
.CurrentInstances
, PIPE_UNLIMITED_INSTANCES
);
904 if (lpMaxCollectionCount
!= NULL
|| lpCollectDataTimeout
!= NULL
)
906 FILE_PIPE_REMOTE_INFORMATION RemoteInfo
;
908 Status
= NtQueryInformationFile(hNamedPipe
,
911 sizeof(FILE_PIPE_REMOTE_INFORMATION
),
912 FilePipeRemoteInformation
);
913 if (!NT_SUCCESS(Status
))
915 SetLastErrorByStatus(Status
);
919 if (lpMaxCollectionCount
!= NULL
)
921 *lpMaxCollectionCount
= RemoteInfo
.MaximumCollectionCount
;
924 if(lpCollectDataTimeout
!= NULL
)
927 *lpCollectDataTimeout
= 0;
931 if (lpUserName
!= NULL
)
933 /* FIXME - open the thread token, call ImpersonateNamedPipeClient() and
934 retreive the user name with GetUserName(), revert the impersonation
935 and finally restore the thread token */
947 GetNamedPipeHandleStateA(HANDLE hNamedPipe
,
949 LPDWORD lpCurInstances
,
950 LPDWORD lpMaxCollectionCount
,
951 LPDWORD lpCollectDataTimeout
,
953 DWORD nMaxUserNameSize
)
955 UNICODE_STRING UserNameW
= { 0, 0, NULL
};
956 ANSI_STRING UserNameA
;
959 if(lpUserName
!= NULL
)
961 UserNameW
.MaximumLength
= (USHORT
)nMaxUserNameSize
* sizeof(WCHAR
);
962 UserNameW
.Buffer
= RtlAllocateHeap(RtlGetProcessHeap(), 0, UserNameW
.MaximumLength
);
963 if (UserNameW
.Buffer
== NULL
)
965 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
969 UserNameA
.Buffer
= lpUserName
;
970 UserNameA
.Length
= 0;
971 UserNameA
.MaximumLength
= (USHORT
)nMaxUserNameSize
;
974 Ret
= GetNamedPipeHandleStateW(hNamedPipe
,
977 lpMaxCollectionCount
,
978 lpCollectDataTimeout
,
981 if (Ret
&& lpUserName
!= NULL
)
985 RtlInitUnicodeString(&UserNameW
, UserNameW
.Buffer
);
986 Status
= RtlUnicodeStringToAnsiString(&UserNameA
, &UserNameW
, FALSE
);
987 if (!NT_SUCCESS(Status
))
989 SetLastErrorByStatus(Status
);
994 if (UserNameW
.Buffer
!= NULL
)
996 RtlFreeHeap(RtlGetProcessHeap(), 0, UserNameW
.Buffer
);
1008 GetNamedPipeInfo(HANDLE hNamedPipe
,
1010 LPDWORD lpOutBufferSize
,
1011 LPDWORD lpInBufferSize
,
1012 LPDWORD lpMaxInstances
)
1014 FILE_PIPE_LOCAL_INFORMATION PipeLocalInformation
;
1015 IO_STATUS_BLOCK StatusBlock
;
1018 Status
= NtQueryInformationFile(hNamedPipe
,
1020 &PipeLocalInformation
,
1021 sizeof(FILE_PIPE_LOCAL_INFORMATION
),
1022 FilePipeLocalInformation
);
1023 if (!NT_SUCCESS(Status
))
1025 SetLastErrorByStatus(Status
);
1029 if (lpFlags
!= NULL
)
1031 *lpFlags
= (PipeLocalInformation
.NamedPipeEnd
== FILE_PIPE_SERVER_END
) ? PIPE_SERVER_END
: PIPE_CLIENT_END
;
1032 *lpFlags
|= (PipeLocalInformation
.NamedPipeType
== 1) ? PIPE_TYPE_MESSAGE
: PIPE_TYPE_BYTE
;
1035 if (lpOutBufferSize
!= NULL
)
1036 *lpOutBufferSize
= PipeLocalInformation
.OutboundQuota
;
1038 if (lpInBufferSize
!= NULL
)
1039 *lpInBufferSize
= PipeLocalInformation
.InboundQuota
;
1041 if (lpMaxInstances
!= NULL
)
1043 if (PipeLocalInformation
.MaximumInstances
>= 255)
1044 *lpMaxInstances
= PIPE_UNLIMITED_INSTANCES
;
1046 *lpMaxInstances
= PipeLocalInformation
.MaximumInstances
;
1058 PeekNamedPipe(HANDLE hNamedPipe
,
1061 LPDWORD lpBytesRead
,
1062 LPDWORD lpTotalBytesAvail
,
1063 LPDWORD lpBytesLeftThisMessage
)
1065 PFILE_PIPE_PEEK_BUFFER Buffer
;
1066 IO_STATUS_BLOCK Iosb
;
1071 /* Calculate the buffer space that we'll need and allocate it */
1072 BufferSize
= nBufferSize
+ sizeof(FILE_PIPE_PEEK_BUFFER
);
1073 Buffer
= RtlAllocateHeap(RtlGetProcessHeap(), 0, BufferSize
);
1076 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1080 /* Tell the driver to seek */
1081 Status
= NtFsControlFile(hNamedPipe
,
1091 if (Status
== STATUS_PENDING
)
1093 /* Wait for npfs to be done, and update the status */
1094 Status
= NtWaitForSingleObject(hNamedPipe
, FALSE
, NULL
);
1095 if (NT_SUCCESS(Status
))
1096 Status
= Iosb
.Status
;
1099 /* Overflow is success for us */
1100 if (Status
== STATUS_BUFFER_OVERFLOW
)
1101 Status
= STATUS_SUCCESS
;
1104 if (!NT_SUCCESS(Status
))
1106 /* Free the buffer and return failure */
1107 RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer
);
1108 SetLastErrorByStatus(Status
);
1112 /* Check if caller requested bytes available */
1113 if (lpTotalBytesAvail
)
1114 *lpTotalBytesAvail
= Buffer
->ReadDataAvailable
;
1116 /* Calculate the bytes returned, minus our structure overhead */
1117 BytesRead
= (ULONG
)(Iosb
.Information
-
1118 FIELD_OFFSET(FILE_PIPE_PEEK_BUFFER
, Data
[0]));
1120 /* Check if caller requested bytes read */
1123 /* Return the bytes read */
1124 *lpBytesRead
= BytesRead
;
1127 /* Check if caller requested bytes left */
1128 if (lpBytesLeftThisMessage
)
1130 /* Calculate total minus what we returned and our structure overhead */
1131 *lpBytesLeftThisMessage
= Buffer
->MessageLength
- BytesRead
;
1134 /* Check if the caller wanted to see the actual data */
1137 /* Give him what he wants */
1138 RtlCopyMemory(lpBuffer
,
1143 /* Free the buffer */
1144 RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer
);
1155 TransactNamedPipe(IN HANDLE hNamedPipe
,
1156 IN LPVOID lpInBuffer
,
1157 IN DWORD nInBufferSize
,
1158 OUT LPVOID lpOutBuffer
,
1159 IN DWORD nOutBufferSize
,
1160 OUT LPDWORD lpBytesRead OPTIONAL
,
1161 IN LPOVERLAPPED lpOverlapped OPTIONAL
)
1165 if (lpBytesRead
!= NULL
)
1170 if (lpOverlapped
!= NULL
)
1174 ApcContext
= (((ULONG_PTR
)lpOverlapped
->hEvent
& 0x1) ? NULL
: lpOverlapped
);
1175 lpOverlapped
->Internal
= STATUS_PENDING
;
1177 Status
= NtFsControlFile(hNamedPipe
,
1178 lpOverlapped
->hEvent
,
1181 (PIO_STATUS_BLOCK
)lpOverlapped
,
1182 FSCTL_PIPE_TRANSCEIVE
,
1187 if (!NT_SUCCESS(Status
) || Status
== STATUS_PENDING
)
1189 SetLastErrorByStatus(Status
);
1193 if (lpBytesRead
!= NULL
)
1195 *lpBytesRead
= lpOverlapped
->InternalHigh
;
1200 #if 0 /* We don't implement FSCTL_PIPE_TRANSCEIVE yet */
1201 IO_STATUS_BLOCK Iosb
;
1203 Status
= NtFsControlFile(hNamedPipe
,
1208 FSCTL_PIPE_TRANSCEIVE
,
1213 if (Status
== STATUS_PENDING
)
1215 Status
= NtWaitForSingleObject(hNamedPipe
,
1218 if (NT_SUCCESS(Status
))
1219 Status
= Iosb
.Status
;
1222 if (NT_SUCCESS(Status
))
1224 /* lpNumberOfBytesRead must not be NULL here, in fact Win doesn't
1225 check that case either and crashes (only after the operation
1227 *lpBytesRead
= Iosb
.Information
;
1231 SetLastErrorByStatus(Status
);
1234 #else /* Workaround while FSCTL_PIPE_TRANSCEIVE not available */
1237 while (0 != nInBufferSize
&&
1238 WriteFile(hNamedPipe
, lpInBuffer
, nInBufferSize
, &nActualBytes
,
1241 lpInBuffer
= (LPVOID
)((char *) lpInBuffer
+ nActualBytes
);
1242 nInBufferSize
-= nActualBytes
;
1245 if (0 != nInBufferSize
)
1247 /* Must have dropped out of the while 'cause WriteFile failed */
1251 if (!ReadFile(hNamedPipe
, lpOutBuffer
, nOutBufferSize
, &nActualBytes
,
1257 if (NULL
!= lpBytesRead
)
1259 *lpBytesRead
= nActualBytes
;