script/commit_mark.sh: add the current branch name
[Samba/gebeck_regimport.git] / testprogs / win32 / testmailslot / testmailslot.c
blobb953636fd3eaf111f4ba7ee13078364bd7d60d0e
1 /*
2 * Very simple test application for mailslots
3 * (C) 2005 Jelmer Vernooij <jelmer@samba.org>
4 * Published to the public domain
5 */
7 #include <windows.h>
8 #include <stdio.h>
10 int read_slot(const char *mailslotname)
12 HANDLE h;
13 DWORD nr;
14 char data[30000];
15 DWORD nextsize, nummsg = 0;
17 if (strncmp(mailslotname, "\\\\.\\mailslot\\", 13) && strncmp(mailslotname, "\\\\*\\mailslot\\", 13)) {
18 printf("Must specify local mailslot name (starting with \\\\.\\mailslot\\)\n");
19 return 1;
22 h = CreateMailslot(mailslotname, 0, MAILSLOT_WAIT_FOREVER, NULL);
24 if (h == INVALID_HANDLE_VALUE) {
25 printf("Unable to create mailslot %s: %d\n", mailslotname, GetLastError());
26 return 1;
29 if (!ReadFile(h, data, sizeof(data)-1, &nr, NULL)) {
30 printf("Error reading: %d\n", GetLastError());
31 return 1;
34 data[nr] = '\0';
36 printf("%s\n", data);
38 CloseHandle(h);
41 int write_slot(const char *mailslotname)
43 HANDLE h;
44 DWORD nw;
45 char data[30000];
46 h = CreateFile(mailslotname, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
48 if (h == INVALID_HANDLE_VALUE) {
49 printf("Unable to open file: %d\n", GetLastError());
50 return 1;
53 gets(data);
55 if (!WriteFile(h, data, strlen(data), &nw, NULL)) {
56 printf("Error writing file: %d\n", GetLastError());
57 return 1;
60 CloseHandle(h);
63 int main(int argc, char **argv)
65 if (argc < 3 ||
66 (strcmp(argv[1], "read") && strcmp(argv[1], "write"))) {
67 printf("Usage: %s read|write mailslot\n", argv[0]);
68 return 1;
71 if (!strcmp(argv[1], "read")) {
72 return read_slot(argv[2]);
75 if (!strcmp(argv[1], "write")) {
76 return write_slot(argv[2]);
79 return 0;