r10577: Fix error in ReadFile()
[samba.git] / testprogs / win32 / npecho / npecho_client.c
1 /*
2  * Simple Named Pipe Client
3  * (C) 2005 Jelmer Vernooij <jelmer@samba.org>
4  * Published to the public domain
5  */
6
7 #include <windows.h>
8 #include <stdio.h>
9
10 #define ECHODATA "Black Dog"
11
12 int main(int argc, char *argv[])
13 {
14         HANDLE h;
15         DWORD numread = 0;
16         char *outbuffer = malloc(strlen(ECHODATA));
17
18         if (argc == 1) {
19                 printf("Usage: %s pipename\n", argv[0]);
20                 printf("  Where pipename is something like \\\\servername\\NPECHO\n");
21                 return -1;
22         }
23
24         h = CreateFile(argv[1], GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
25         if (h == INVALID_HANDLE_VALUE) {
26                 printf("Error opening: %d\n", GetLastError());
27                 return -1;
28         }
29
30         if (!WriteFile(h, ECHODATA, strlen(ECHODATA), &numread, NULL)) {
31                 printf("Error writing: %d\n", GetLastError());
32                 return -1;
33         }
34
35         if (!ReadFile(h, outbuffer, strlen(ECHODATA), &numread, NULL)) {
36                 printf("Error reading: %d\n", GetLastError());
37                 return -1;
38         }
39
40         printf("Read: %s\n", outbuffer);
41         
42         if (!TransactNamedPipe(h, ECHODATA, strlen(ECHODATA), outbuffer, strlen(ECHODATA), &numread, NULL)) {
43                 printf("TransactNamedPipe failed: %d!\n", GetLastError());
44                 return -1;
45         }
46
47         printf("Result: %s\n", outbuffer);
48
49         return 0;
50 }