Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
4.5 KiB

  1. /******************************************************************************\
  2. * This is a part of the Microsoft Source Code Samples.
  3. * Copyright 1995 - 1997 Microsoft Corporation.
  4. * All rights reserved.
  5. * This source code is only intended as a supplement to
  6. * Microsoft Development Tools and/or WinHelp documentation.
  7. * See these sources for detailed information regarding the
  8. * Microsoft samples programs.
  9. \******************************************************************************/
  10. /*++
  11. Copyright (c) 1997 Microsoft Corporation
  12. Module Name:
  13. pipeex.c
  14. Abstract:
  15. CreatePipe-like function that lets one or both handles be overlapped
  16. Author:
  17. Dave Hart Summer 1997
  18. Revision History:
  19. --*/
  20. #include <windows.h>
  21. #include <stdio.h>
  22. ULONG PipeSerialNumber;
  23. BOOL
  24. APIENTRY
  25. MyCreatePipeEx(
  26. OUT LPHANDLE lpReadPipe,
  27. OUT LPHANDLE lpWritePipe,
  28. IN LPSECURITY_ATTRIBUTES lpPipeAttributes,
  29. IN DWORD nSize,
  30. DWORD dwReadMode,
  31. DWORD dwWriteMode
  32. )
  33. /*++
  34. Routine Description:
  35. The CreatePipeEx API is used to create an anonymous pipe I/O device.
  36. Unlike CreatePipe FILE_FLAG_OVERLAPPED may be specified for one or
  37. both handles.
  38. Two handles to the device are created. One handle is opened for
  39. reading and the other is opened for writing. These handles may be
  40. used in subsequent calls to ReadFile and WriteFile to transmit data
  41. through the pipe.
  42. Arguments:
  43. lpReadPipe - Returns a handle to the read side of the pipe. Data
  44. may be read from the pipe by specifying this handle value in a
  45. subsequent call to ReadFile.
  46. lpWritePipe - Returns a handle to the write side of the pipe. Data
  47. may be written to the pipe by specifying this handle value in a
  48. subsequent call to WriteFile.
  49. lpPipeAttributes - An optional parameter that may be used to specify
  50. the attributes of the new pipe. If the parameter is not
  51. specified, then the pipe is created without a security
  52. descriptor, and the resulting handles are not inherited on
  53. process creation. Otherwise, the optional security attributes
  54. are used on the pipe, and the inherit handles flag effects both
  55. pipe handles.
  56. nSize - Supplies the requested buffer size for the pipe. This is
  57. only a suggestion and is used by the operating system to
  58. calculate an appropriate buffering mechanism. A value of zero
  59. indicates that the system is to choose the default buffering
  60. scheme.
  61. Return Value:
  62. TRUE - The operation was successful.
  63. FALSE/NULL - The operation failed. Extended error status is available
  64. using GetLastError.
  65. --*/
  66. {
  67. HANDLE ReadPipeHandle, WritePipeHandle;
  68. DWORD dwError;
  69. UCHAR PipeNameBuffer[ MAX_PATH ];
  70. //
  71. // Only one valid OpenMode flag - FILE_FLAG_OVERLAPPED
  72. //
  73. if ((dwReadMode | dwWriteMode) & (~FILE_FLAG_OVERLAPPED)) {
  74. SetLastError(ERROR_INVALID_PARAMETER);
  75. return FALSE;
  76. }
  77. //
  78. // Set the default timeout to 120 seconds
  79. //
  80. if (nSize == 0) {
  81. nSize = 4096;
  82. }
  83. sprintf( PipeNameBuffer,
  84. "\\\\.\\Pipe\\RemoteExeAnon.%08x.%08x",
  85. GetCurrentProcessId(),
  86. PipeSerialNumber++
  87. );
  88. ReadPipeHandle = CreateNamedPipeA(
  89. PipeNameBuffer,
  90. PIPE_ACCESS_INBOUND | dwReadMode,
  91. PIPE_TYPE_BYTE | PIPE_WAIT,
  92. 1, // Number of pipes
  93. nSize, // Out buffer size
  94. nSize, // In buffer size
  95. 120 * 1000, // Timeout in ms
  96. lpPipeAttributes
  97. );
  98. if (! ReadPipeHandle) {
  99. return FALSE;
  100. }
  101. WritePipeHandle = CreateFileA(
  102. PipeNameBuffer,
  103. GENERIC_WRITE,
  104. 0, // No sharing
  105. lpPipeAttributes,
  106. OPEN_EXISTING,
  107. FILE_ATTRIBUTE_NORMAL | dwWriteMode,
  108. NULL // Template file
  109. );
  110. if (INVALID_HANDLE_VALUE == WritePipeHandle) {
  111. dwError = GetLastError();
  112. CloseHandle( ReadPipeHandle );
  113. SetLastError(dwError);
  114. return FALSE;
  115. }
  116. *lpReadPipe = ReadPipeHandle;
  117. *lpWritePipe = WritePipeHandle;
  118. return( TRUE );
  119. }