summaryrefslogtreecommitdiff
path: root/testprogs/win32/npecho/npecho_client.c
blob: 97d31c4318444362c8d40ec93fa3ff928a0033c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
 * Simple Named Pipe Client
 * (C) 2005 Jelmer Vernooij <jelmer@samba.org>
 * Published to the public domain
 */

#include <windows.h>
#include <stdio.h>

#define ECHODATA "Black Dog"

int main(int argc, char *argv[])
{
	HANDLE h;
	DWORD numread = 0;
	char *outbuffer = malloc(strlen(ECHODATA));

	if (argc == 1) {
		printf("Usage: %s pipename\n", argv[0]);
		printf("  Where pipename is something like \\\\servername\\NPECHO\n");
		return -1;
	}

	h = CreateFile(argv[1], GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
	if (h == INVALID_HANDLE_VALUE) {
		printf("Error opening: %d\n", GetLastError());
		return -1;
	}

	if (!WriteFile(h, ECHODATA, strlen(ECHODATA), &numread, NULL)) {
		printf("Error writing: %d\n", GetLastError());
		return -1;
	}

	if (!ReadFile(h, outbuffer, strlen(ECHODATA), &numread, NULL)) {
		printf("Error reading: %d\n", GetLastError());
		return -1;
	}

	printf("Read: %s\n", outbuffer);
	
	if (!TransactNamedPipe(h, ECHODATA, strlen(ECHODATA), outbuffer, strlen(ECHODATA), &numread, NULL)) {
		printf("TransactNamedPipe failed: %d!\n", GetLastError());
		return -1;
	}

	printf("Result: %s\n", outbuffer);

	return 0;
}