~zulcss/samba/server-dailies-3.4

« back to all changes in this revision

Viewing changes to testprogs/win32/testmailslot/testmailslot.c

  • Committer: Chuck Short
  • Date: 2010-09-28 20:38:39 UTC
  • Revision ID: zulcss@ubuntu.com-20100928203839-pgjulytsi9ue63x1
Initial version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Very simple test application for mailslots
 
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
int read_slot(const char *mailslotname)
 
11
{
 
12
        HANDLE h;
 
13
        DWORD nr;
 
14
        char data[30000];
 
15
        DWORD nextsize, nummsg = 0;
 
16
 
 
17
        if (strncmp(mailslotname, "\\\\.\\mailslot\\", 13) && strncmp(mailslotname, "\\\\*\\mailslot\\", 13)) {
 
18
                printf("Must specify local mailslot name (starting with \\\\.\\mailslot\\)\n");
 
19
                return 1;
 
20
        }
 
21
 
 
22
        h = CreateMailslot(mailslotname, 0, MAILSLOT_WAIT_FOREVER, NULL);
 
23
 
 
24
        if (h == INVALID_HANDLE_VALUE) {
 
25
                printf("Unable to create mailslot %s: %d\n", mailslotname, GetLastError());
 
26
                return 1;
 
27
        }
 
28
 
 
29
        if (!ReadFile(h, data, sizeof(data)-1, &nr, NULL)) {
 
30
                printf("Error reading: %d\n", GetLastError());
 
31
                return 1;
 
32
        }
 
33
 
 
34
        data[nr] = '\0';
 
35
 
 
36
        printf("%s\n", data);
 
37
 
 
38
        CloseHandle(h);
 
39
}
 
40
 
 
41
int write_slot(const char *mailslotname)
 
42
{
 
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);
 
47
 
 
48
        if (h == INVALID_HANDLE_VALUE) {
 
49
                printf("Unable to open file: %d\n", GetLastError());
 
50
                return 1;
 
51
        }
 
52
 
 
53
        gets(data);
 
54
 
 
55
        if (!WriteFile(h, data, strlen(data), &nw, NULL)) {
 
56
                printf("Error writing file: %d\n", GetLastError());
 
57
                return 1;
 
58
        }
 
59
 
 
60
        CloseHandle(h); 
 
61
}
 
62
 
 
63
int main(int argc, char **argv)
 
64
{
 
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;
 
69
        }
 
70
 
 
71
        if (!strcmp(argv[1], "read")) {
 
72
                return read_slot(argv[2]);
 
73
        }
 
74
 
 
75
        if (!strcmp(argv[1], "write")) {
 
76
                return write_slot(argv[2]);
 
77
        }
 
78
 
 
79
        return 0;
 
80
}