update for HEAD-2003091401
[reactos.git] / lib / kernel32 / file / mailslot.c
1 /* $Id$
2  *
3  * COPYRIGHT:       See COPYING in the top level directory
4  * PROJECT:         ReactOS system libraries
5  * FILE:            lib/kernel32/file/mailslot.c
6  * PURPOSE:         Mailslot functions
7  * PROGRAMMER:      Ariadne ( ariadne@xs4all.nl)
8  * UPDATE HISTORY:
9  */
10
11 /* INCLUDES *****************************************************************/
12
13 #include <k32.h>
14
15 #define NDEBUG
16 #include <kernel32/kernel32.h>
17
18 /* FUNCTIONS ****************************************************************/
19
20 /*
21  * @implemented
22  */
23 HANDLE STDCALL
24 CreateMailslotA(LPCSTR lpName,
25                 DWORD nMaxMessageSize,
26                 DWORD lReadTimeout,
27                 LPSECURITY_ATTRIBUTES lpSecurityAttributes)
28 {
29    HANDLE MailslotHandle;
30    UNICODE_STRING NameU;
31    ANSI_STRING NameA;
32    
33    RtlInitAnsiString(&NameA, (LPSTR)lpName);
34    RtlAnsiStringToUnicodeString(&NameU, &NameA, TRUE);
35    
36    MailslotHandle = CreateMailslotW(NameU.Buffer,
37                                     nMaxMessageSize,
38                                     lReadTimeout,
39                                     lpSecurityAttributes);
40    
41    RtlFreeUnicodeString(&NameU);
42    
43    return(MailslotHandle);
44 }
45
46
47 /*
48  * @implemented
49  */
50 HANDLE STDCALL
51 CreateMailslotW(LPCWSTR lpName,
52                 DWORD nMaxMessageSize,
53                 DWORD lReadTimeout,
54                 LPSECURITY_ATTRIBUTES lpSecurityAttributes)
55 {
56    OBJECT_ATTRIBUTES ObjectAttributes;
57    UNICODE_STRING MailslotName;
58    HANDLE MailslotHandle;
59    NTSTATUS Status;
60    BOOLEAN Result;
61    LARGE_INTEGER DefaultTimeOut;
62    IO_STATUS_BLOCK Iosb;
63    
64    Result = RtlDosPathNameToNtPathName_U((LPWSTR)lpName,
65                                          &MailslotName,
66                                          NULL,
67                                          NULL);
68    if (!Result)
69      {
70         SetLastError(ERROR_PATH_NOT_FOUND);
71         return(INVALID_HANDLE_VALUE);
72      }
73    
74    DPRINT("Mailslot name: %wZ\n", &MailslotName);
75    
76    InitializeObjectAttributes(&ObjectAttributes,
77                               &MailslotName,
78                               OBJ_CASE_INSENSITIVE,
79                               NULL,
80                               NULL);
81    
82    DefaultTimeOut.QuadPart = lReadTimeout * 10000;
83    
84    Status = NtCreateMailslotFile(&MailslotHandle,
85                                  GENERIC_READ | SYNCHRONIZE | WRITE_DAC,
86                                  &ObjectAttributes,
87                                  &Iosb,
88                                  FILE_WRITE_THROUGH,
89                                  0,
90                                  nMaxMessageSize,
91                                  &DefaultTimeOut);
92    
93    RtlFreeUnicodeString(&MailslotName);
94    
95    if (!NT_SUCCESS(Status))
96      {
97         DPRINT("NtCreateMailslot failed (Status %x)!\n", Status);
98         SetLastErrorByStatus (Status);
99         return(INVALID_HANDLE_VALUE);
100      }
101    
102    return(MailslotHandle);
103 }
104
105
106 /*
107  * @implemented
108  */
109 WINBOOL STDCALL
110 GetMailslotInfo(HANDLE hMailslot,
111                 LPDWORD lpMaxMessageSize,
112                 LPDWORD lpNextSize,
113                 LPDWORD lpMessageCount,
114                 LPDWORD lpReadTimeout)
115 {
116    FILE_MAILSLOT_QUERY_INFORMATION Buffer;
117    IO_STATUS_BLOCK Iosb;
118    NTSTATUS Status;
119    
120    Status = NtQueryInformationFile(hMailslot,
121                                    &Iosb,
122                                    &Buffer,
123                                    sizeof(FILE_MAILSLOT_QUERY_INFORMATION),
124                                    FileMailslotQueryInformation);
125    if (!NT_SUCCESS(Status))
126      {
127         DPRINT("NtQueryInformationFile failed (Status %x)!\n", Status);
128         SetLastErrorByStatus (Status);
129         return(FALSE);
130      }
131    
132    if (lpMaxMessageSize != NULL)
133      {
134         *lpMaxMessageSize = Buffer.MaxMessageSize;
135      }
136    if (lpNextSize != NULL)
137      {
138         *lpNextSize = Buffer.NextSize;
139      }
140    if (lpMessageCount != NULL)
141      {
142         *lpMessageCount = Buffer.MessageCount;
143      }
144    if (lpReadTimeout != NULL)
145      {
146         *lpReadTimeout = (DWORD)(Buffer.Timeout.QuadPart / -10000);
147      }
148    
149    return(TRUE);
150 }
151
152
153 /*
154  * @implemented
155  */
156 WINBOOL STDCALL
157 SetMailslotInfo(HANDLE hMailslot,
158                 DWORD lReadTimeout)
159 {
160    FILE_MAILSLOT_SET_INFORMATION Buffer;
161    IO_STATUS_BLOCK Iosb;
162    NTSTATUS Status;
163    
164    Buffer.Timeout.QuadPart = lReadTimeout * -10000;
165    
166    Status = NtSetInformationFile(hMailslot,
167                                  &Iosb,
168                                  &Buffer,
169                                  sizeof(FILE_MAILSLOT_SET_INFORMATION),
170                                  FileMailslotSetInformation);
171    if (!NT_SUCCESS(Status))
172      {
173         DPRINT("NtSetInformationFile failed (Status %x)!\n", Status);
174         SetLastErrorByStatus (Status);
175         return(FALSE);
176      }
177    
178    return(TRUE);
179 }
180
181 /* EOF */