Sync with trunk head (part 1 of x)
[reactos.git] / dll / win32 / 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 #include <wine/debug.h>
15
16 WINE_DEFAULT_DEBUG_CHANNEL(kernel32file);
17
18 /* FUNCTIONS ****************************************************************/
19
20 /*
21 * @implemented
22 */
23 HANDLE WINAPI
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 WINAPI
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 ULONG Attributes = OBJ_CASE_INSENSITIVE;
64 PSECURITY_DESCRIPTOR SecurityDescriptor = NULL;
65
66 Result = RtlDosPathNameToNtPathName_U(lpName,
67 &MailslotName,
68 NULL,
69 NULL);
70 if (!Result)
71 {
72 SetLastError(ERROR_PATH_NOT_FOUND);
73 return(INVALID_HANDLE_VALUE);
74 }
75
76 TRACE("Mailslot name: %wZ\n", &MailslotName);
77
78 if(lpSecurityAttributes)
79 {
80 SecurityDescriptor = lpSecurityAttributes->lpSecurityDescriptor;
81 if(lpSecurityAttributes->bInheritHandle)
82 Attributes |= OBJ_INHERIT;
83 }
84
85 InitializeObjectAttributes(&ObjectAttributes,
86 &MailslotName,
87 Attributes,
88 NULL,
89 SecurityDescriptor);
90
91 if (lReadTimeout == MAILSLOT_WAIT_FOREVER)
92 {
93 /* Set the max */
94 DefaultTimeOut.LowPart = 0;
95 DefaultTimeOut.HighPart = 0x80000000;
96 }
97 else
98 {
99 /* Convert to NT format */
100 DefaultTimeOut.QuadPart = UInt32x32To64(-10000, lReadTimeout);
101 }
102
103 Status = NtCreateMailslotFile(&MailslotHandle,
104 GENERIC_READ | SYNCHRONIZE | WRITE_DAC,
105 &ObjectAttributes,
106 &Iosb,
107 FILE_WRITE_THROUGH,
108 0,
109 nMaxMessageSize,
110 &DefaultTimeOut);
111
112 if (Status == STATUS_INVALID_DEVICE_REQUEST || Status == STATUS_NOT_SUPPORTED)
113 {
114 Status = STATUS_OBJECT_NAME_INVALID;
115 }
116
117 RtlFreeHeap(RtlGetProcessHeap(),
118 0,
119 MailslotName.Buffer);
120
121 if (!NT_SUCCESS(Status))
122 {
123 WARN("NtCreateMailslot failed (Status %x)!\n", Status);
124 SetLastErrorByStatus (Status);
125 return(INVALID_HANDLE_VALUE);
126 }
127
128 return(MailslotHandle);
129 }
130
131
132 /*
133 * @implemented
134 */
135 BOOL WINAPI
136 GetMailslotInfo(HANDLE hMailslot,
137 LPDWORD lpMaxMessageSize,
138 LPDWORD lpNextSize,
139 LPDWORD lpMessageCount,
140 LPDWORD lpReadTimeout)
141 {
142 FILE_MAILSLOT_QUERY_INFORMATION Buffer;
143 IO_STATUS_BLOCK Iosb;
144 NTSTATUS Status;
145
146 Status = NtQueryInformationFile(hMailslot,
147 &Iosb,
148 &Buffer,
149 sizeof(FILE_MAILSLOT_QUERY_INFORMATION),
150 FileMailslotQueryInformation);
151 if (!NT_SUCCESS(Status))
152 {
153 WARN("NtQueryInformationFile failed (Status %x)!\n", Status);
154 SetLastErrorByStatus (Status);
155 return(FALSE);
156 }
157
158 if (lpMaxMessageSize != NULL)
159 {
160 *lpMaxMessageSize = Buffer.MaximumMessageSize;
161 }
162 if (lpNextSize != NULL)
163 {
164 *lpNextSize = Buffer.NextMessageSize;
165 }
166 if (lpMessageCount != NULL)
167 {
168 *lpMessageCount = Buffer.MessagesAvailable;
169 }
170 if (lpReadTimeout != NULL)
171 {
172 if (Buffer.ReadTimeout.LowPart == 0 &&
173 Buffer.ReadTimeout.HighPart == (LONG)0x80000000)
174 *lpReadTimeout = MAILSLOT_WAIT_FOREVER;
175 else
176 *lpReadTimeout = (DWORD)(Buffer.ReadTimeout.QuadPart / -10000);
177 }
178
179 return(TRUE);
180 }
181
182
183 /*
184 * @implemented
185 */
186 BOOL WINAPI
187 SetMailslotInfo(HANDLE hMailslot,
188 DWORD lReadTimeout)
189 {
190 FILE_MAILSLOT_SET_INFORMATION Buffer;
191 LARGE_INTEGER Timeout;
192 IO_STATUS_BLOCK Iosb;
193 NTSTATUS Status;
194
195 if (lReadTimeout == MAILSLOT_WAIT_FOREVER)
196 {
197 /* Set the max */
198 Timeout.LowPart = 0;
199 Timeout.HighPart = 0x80000000;
200 }
201 else
202 {
203 /* Convert to NT format */
204 Timeout.QuadPart = UInt32x32To64(-10000, lReadTimeout);
205 }
206 Buffer.ReadTimeout = &Timeout;
207
208 Status = NtSetInformationFile(hMailslot,
209 &Iosb,
210 &Buffer,
211 sizeof(FILE_MAILSLOT_SET_INFORMATION),
212 FileMailslotSetInformation);
213 if (!NT_SUCCESS(Status))
214 {
215 WARN("NtSetInformationFile failed (Status %x)!\n", Status);
216 SetLastErrorByStatus (Status);
217 return(FALSE);
218 }
219
220 return(TRUE);
221 }
222
223 /* EOF */