ReactOS  0.4.14-dev-323-g6fe6a88
npclient.c File Reference
#include <windows.h>
Include dependency graph for npclient.c:

Go to the source code of this file.

Functions

VOID MyErrExit (LPTSTR Message)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int argc  ,
char argv[] 
)

hosttype.c Copyright (C) 2002 by Brian Palmer brian.nosp@m.p@sg.nosp@m.inet..nosp@m.com

Definition at line 10 of file npclient.c.

11 {
12  HANDLE hPipe;
13  LPVOID lpvMessage;
14  CHAR chBuf[512];
15  BOOL fSuccess;
16  DWORD cbRead, cbWritten, dwMode;
17  LPTSTR lpszPipename = "\\\\.\\pipe\\mynamedpipe";
18 
19 // Try to open a named pipe; wait for it, if necessary.
20 
21  while (1)
22  {
23  hPipe = CreateFile(
24  lpszPipename, // pipe name
25  GENERIC_READ | // read and write access
27  0, // no sharing
28  NULL, // no security attributes
29  OPEN_EXISTING, // opens existing pipe
30  0, // default attributes
31  NULL); // no template file
32 
33  // Break if the pipe handle is valid.
34 
35  if (hPipe != INVALID_HANDLE_VALUE)
36  break;
37 
38  // Exit if an error other than ERROR_PIPE_BUSY occurs.
39 
41  MyErrExit("Could not open pipe");
42 
43  // All pipe instances are busy, so wait for 20 seconds.
44 
45  if (! WaitNamedPipe(lpszPipename, 20000) )
46  MyErrExit("Could not open pipe");
47  }
48 
49 // The pipe connected; change to message-read mode.
50 
51  dwMode = PIPE_READMODE_MESSAGE;
52  fSuccess = SetNamedPipeHandleState(
53  hPipe, // pipe handle
54  &dwMode, // new pipe mode
55  NULL, // don't set maximum bytes
56  NULL); // don't set maximum time
57  if (!fSuccess)
58  MyErrExit("SetNamedPipeHandleState");
59 
60 // Send a message to the pipe server.
61 
62  lpvMessage = (argc > 1) ? argv[1] : "default message";
63 
64  fSuccess = WriteFile(
65  hPipe, // pipe handle
66  lpvMessage, // message
67  strlen(lpvMessage) + 1, // message length
68  &cbWritten, // bytes written
69  NULL); // not overlapped
70  if (! fSuccess)
71  MyErrExit("WriteFile");
72 
73  do
74  {
75  // Read from the pipe.
76 
77  fSuccess = ReadFile(
78  hPipe, // pipe handle
79  chBuf, // buffer to receive reply
80  512, // size of buffer
81  &cbRead, // number of bytes read
82  NULL); // not overlapped
83 
84  if (! fSuccess && GetLastError() != ERROR_MORE_DATA)
85  break;
86 
87  // Reply from the pipe is written to STDOUT.
88 
90  chBuf, cbRead, &cbWritten, NULL))
91  {
92  break;
93  }
94 
95  } while (! fSuccess); // repeat loop if ERROR_MORE_DATA
96 
97  CloseHandle(hPipe);
98 
99  return 0;
100 }
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
static int argc
Definition: ServiceArgs.c:12
#define CloseHandle
Definition: compat.h:398
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
char CHAR
Definition: xmlstorage.h:175
#define INVALID_HANDLE_VALUE
Definition: compat.h:391
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
HANDLE WINAPI GetStdHandle(IN DWORD nStdHandle)
Definition: console.c:152
#define argv
Definition: mplay32.c:18
CHAR * LPTSTR
Definition: xmlstorage.h:192
unsigned int BOOL
Definition: ntddk_ex.h:94
#define GENERIC_WRITE
Definition: nt_native.h:90
smooth NULL
Definition: ftsmooth.c:416
#define OPEN_EXISTING
Definition: compat.h:426
VOID MyErrExit(LPTSTR Message)
Definition: npclient.c:3
BOOL WINAPI SetNamedPipeHandleState(HANDLE hNamedPipe, LPDWORD lpMode, LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout)
Definition: npipe.c:774
unsigned long DWORD
Definition: ntddk_ex.h:95
#define ERROR_PIPE_BUSY
Definition: winerror.h:283
#define WaitNamedPipe
Definition: winbase.h:3745
#define GENERIC_READ
Definition: compat.h:124
#define STD_OUTPUT_HANDLE
Definition: winbase.h:265
#define ERROR_MORE_DATA
Definition: dderror.h:13
#define PIPE_READMODE_MESSAGE
Definition: winbase.h:170
IN OUT PVCB OUT PDIRENT OUT PBCB IN BOOLEAN CreateFile
Definition: fatprocs.h:904
BOOL WINAPI ReadFile(IN HANDLE hFile, IN LPVOID lpBuffer, IN DWORD nNumberOfBytesToRead, OUT LPDWORD lpNumberOfBytesRead OPTIONAL, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:123

◆ MyErrExit()

VOID MyErrExit ( LPTSTR  Message)

Definition at line 3 of file npclient.c.

4 {
5 // MessageBox(NULL, Message, NULL, MB_OK);
6  puts(Message);
7  ExitProcess(0);
8 }
VOID WINAPI ExitProcess(IN UINT uExitCode)
Definition: proc.c:1487
CHAR Message[80]
Definition: alive.c:5
int puts(const char *string)
Definition: crtsupp.c:23

Referenced by main().