Leaked source code of windows server 2003
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.

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