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.

199 lines
4.4 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. Pipe.cpp
  5. Abstract:
  6. Implements code that creates and maintains
  7. the named pipe server
  8. Notes:
  9. Unicode only
  10. History:
  11. 05/04/2001 rparsons Created
  12. --*/
  13. #include "precomp.h"
  14. extern APPINFO g_ai;
  15. /*++
  16. Routine Description:
  17. Creates a pipe and waits for a client connection
  18. to occur
  19. Arguments:
  20. *pVoid - Not used
  21. Return Value:
  22. TRUE on success, FALSE otherwise
  23. --*/
  24. UINT
  25. CreatePipeAndWait(
  26. IN VOID* pVoid
  27. )
  28. {
  29. HANDLE hPipe, hThread;
  30. BOOL fConnected = FALSE;
  31. while (g_ai.fMonitor) {
  32. //
  33. // Create the named pipe
  34. //
  35. hPipe = CreateNamedPipe(PIPE_NAME, // pipe name
  36. PIPE_ACCESS_INBOUND, // read access
  37. PIPE_TYPE_MESSAGE | // message type pipe
  38. PIPE_READMODE_MESSAGE | // message-read mode
  39. PIPE_WAIT, // blocking mode
  40. PIPE_UNLIMITED_INSTANCES, // max. instances
  41. 0, // output buffer size
  42. 2048, // input buffer size
  43. 0, // client time-out
  44. NULL); // no security attribute
  45. if (INVALID_HANDLE_VALUE == hPipe) {
  46. return -1;
  47. }
  48. //
  49. // Wait for clients to connect
  50. //
  51. fConnected = ConnectNamedPipe(hPipe, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
  52. if (fConnected && g_ai.fMonitor) {
  53. hThread = (HANDLE) _beginthreadex(NULL,
  54. 0,
  55. &InstanceThread,
  56. (LPVOID)hPipe,
  57. 0,
  58. &g_ai.uInstThreadId);
  59. if (INVALID_HANDLE_VALUE == hThread) {
  60. return -1;
  61. } else {
  62. CloseHandle(hThread);
  63. }
  64. } else {
  65. CloseHandle(hPipe);
  66. }
  67. }
  68. return 0;
  69. }
  70. /*++
  71. Routine Description:
  72. Creates a thread that is responsible
  73. for starting the named pipe server
  74. Arguments:
  75. None
  76. Return Value:
  77. TRUE on success, FALSE otherwise
  78. --*/
  79. BOOL
  80. CreateReceiveThread(
  81. IN VOID
  82. )
  83. {
  84. HANDLE hThread;
  85. hThread = (HANDLE) _beginthreadex(NULL, 0, &CreatePipeAndWait, NULL, 0, &g_ai.uThreadId);
  86. CloseHandle(hThread);
  87. return TRUE;
  88. }
  89. /*++
  90. Routine Description:
  91. Thread callback responsible for
  92. receiving data from the client
  93. Arguments:
  94. *pVoid - A handle to the pipe
  95. Return Value:
  96. TRUE on success, FALSE otherwise
  97. --*/
  98. UINT
  99. InstanceThread(
  100. IN VOID *pVoid
  101. )
  102. {
  103. HANDLE hPipe;
  104. BOOL fSuccess = TRUE;
  105. DWORD cbBytesRead = 0;
  106. WCHAR wszBuffer[2048];
  107. WCHAR *p;
  108. int nCount = 0;
  109. // Get the pipe handle
  110. hPipe = (HANDLE)pVoid;
  111. while (TRUE) {
  112. fSuccess = ReadFile(hPipe,
  113. wszBuffer,
  114. 2048,
  115. &cbBytesRead,
  116. NULL);
  117. if (!fSuccess || cbBytesRead == 0)
  118. break;
  119. // Ensure that the data is NULL terminated
  120. wszBuffer[cbBytesRead / sizeof(WCHAR)] = 0;
  121. // See if this is a new process notification
  122. p = wcsstr(wszBuffer, L"process");
  123. if (p) {
  124. // We got a new process notification.
  125. // See if any items are already in the list
  126. nCount = ListView_GetItemCount(g_ai.hWndList);
  127. if (nCount) {
  128. AddListViewItem(L"");
  129. }
  130. }
  131. // Add the item to the list box in a normal fashion
  132. AddListViewItem(wszBuffer);
  133. }
  134. // Flush the pipe to allow the client to read the pipe's contents
  135. // before disconnecting. Then disconnect the pipe, and close the
  136. // handle to this pipe instance
  137. FlushFileBuffers(hPipe);
  138. DisconnectNamedPipe(hPipe);
  139. CloseHandle(hPipe);
  140. return TRUE;
  141. }