The real, definitive, Visual C++ support branch. Accept no substitutes
[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 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 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 RtlFreeHeap(RtlGetProcessHeap(),
113 0,
114 MailslotName.Buffer);
115
116 if (!NT_SUCCESS(Status))
117 {
118 WARN("NtCreateMailslot failed (Status %x)!\n", Status);
119 SetLastErrorByStatus (Status);
120 return(INVALID_HANDLE_VALUE);
121 }
122
123 return(MailslotHandle);
124 }
125
126
127 /*
128 * @implemented
129 */
130 BOOL STDCALL
131 GetMailslotInfo(HANDLE hMailslot,
132 LPDWORD lpMaxMessageSize,
133 LPDWORD lpNextSize,
134 LPDWORD lpMessageCount,
135 LPDWORD lpReadTimeout)
136 {
137 FILE_MAILSLOT_QUERY_INFORMATION Buffer;
138 IO_STATUS_BLOCK Iosb;
139 NTSTATUS Status;
140
141 Status = NtQueryInformationFile(hMailslot,
142 &Iosb,
143 &Buffer,
144 sizeof(FILE_MAILSLOT_QUERY_INFORMATION),
145 FileMailslotQueryInformation);
146 if (!NT_SUCCESS(Status))
147 {
148 WARN("NtQueryInformationFile failed (Status %x)!\n", Status);
149 SetLastErrorByStatus (Status);
150 return(FALSE);
151 }
152
153 if (lpMaxMessageSize != NULL)
154 {
155 *lpMaxMessageSize = Buffer.MaximumMessageSize;
156 }
157 if (lpNextSize != NULL)
158 {
159 *lpNextSize = Buffer.NextMessageSize;
160 }
161 if (lpMessageCount != NULL)
162 {
163 *lpMessageCount = Buffer.MessagesAvailable;
164 }
165 if (lpReadTimeout != NULL)
166 {
167 if (Buffer.ReadTimeout.LowPart == 0 &&
168 Buffer.ReadTimeout.HighPart == 0x80000000)
169 *lpReadTimeout = MAILSLOT_WAIT_FOREVER;
170 else
171 *lpReadTimeout = (DWORD)(Buffer.ReadTimeout.QuadPart / -10000);
172 }
173
174 return(TRUE);
175 }
176
177
178 /*
179 * @implemented
180 */
181 BOOL STDCALL
182 SetMailslotInfo(HANDLE hMailslot,
183 DWORD lReadTimeout)
184 {
185 FILE_MAILSLOT_SET_INFORMATION Buffer;
186 LARGE_INTEGER Timeout;
187 IO_STATUS_BLOCK Iosb;
188 NTSTATUS Status;
189
190 if (lReadTimeout == MAILSLOT_WAIT_FOREVER)
191 {
192 /* Set the max */
193 Timeout.LowPart = 0;
194 Timeout.HighPart = 0x80000000;
195 }
196 else
197 {
198 /* Convert to NT format */
199 Timeout.QuadPart = UInt32x32To64(-10000, lReadTimeout);
200 }
201 Buffer.ReadTimeout = &Timeout;
202
203 Status = NtSetInformationFile(hMailslot,
204 &Iosb,
205 &Buffer,
206 sizeof(FILE_MAILSLOT_SET_INFORMATION),
207 FileMailslotSetInformation);
208 if (!NT_SUCCESS(Status))
209 {
210 WARN("NtSetInformationFile failed (Status %x)!\n", Status);
211 SetLastErrorByStatus (Status);
212 return(FALSE);
213 }
214
215 return(TRUE);
216 }
217
218 /* EOF */