:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[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 <ddk/ntddk.h>
14 #include <ntdll/rtl.h>
15 #include <windows.h>
16
17 #include <kernel32/kernel32.h>
18 #include <kernel32/error.h>
19
20 /* FUNCTIONS ****************************************************************/
21
22 HANDLE STDCALL
23 CreateMailslotA(LPCSTR lpName,
24                 DWORD nMaxMessageSize,
25                 DWORD lReadTimeout,
26                 LPSECURITY_ATTRIBUTES lpSecurityAttributes)
27 {
28    HANDLE MailslotHandle;
29    UNICODE_STRING NameU;
30    ANSI_STRING NameA;
31    
32    RtlInitAnsiString(&NameA, (LPSTR)lpName);
33    RtlAnsiStringToUnicodeString(&NameU, &NameA, TRUE);
34    
35    MailslotHandle = CreateMailslotW(NameU.Buffer,
36                                     nMaxMessageSize,
37                                     lReadTimeout,
38                                     lpSecurityAttributes);
39    
40    RtlFreeUnicodeString(&NameU);
41    
42    return(MailslotHandle);
43 }
44
45
46 HANDLE STDCALL
47 CreateMailslotW(LPCWSTR lpName,
48                 DWORD nMaxMessageSize,
49                 DWORD lReadTimeout,
50                 LPSECURITY_ATTRIBUTES lpSecurityAttributes)
51 {
52    OBJECT_ATTRIBUTES ObjectAttributes;
53    UNICODE_STRING MailslotName;
54    HANDLE MailslotHandle;
55    NTSTATUS Status;
56    BOOLEAN Result;
57    LARGE_INTEGER DefaultTimeOut;
58    IO_STATUS_BLOCK Iosb;
59    
60    Result = RtlDosPathNameToNtPathName_U((LPWSTR)lpName,
61                                          &MailslotName,
62                                          NULL,
63                                          NULL);
64    if (!Result)
65      {
66         SetLastError(ERROR_PATH_NOT_FOUND);
67         return(INVALID_HANDLE_VALUE);
68      }
69    
70    DPRINT("Mailslot name: %wZ\n", &MailslotName);
71    
72    InitializeObjectAttributes(&ObjectAttributes,
73                               &MailslotName,
74                               OBJ_CASE_INSENSITIVE,
75                               NULL,
76                               NULL);
77    
78    DefaultTimeOut.QuadPart = lReadTimeout * 10000;
79    
80    Status = NtCreateMailslotFile(&MailslotHandle,
81                                  GENERIC_READ | SYNCHRONIZE | WRITE_DAC,
82                                  &ObjectAttributes,
83                                  &Iosb,
84                                  FILE_WRITE_THROUGH,
85                                  0,
86                                  nMaxMessageSize,
87                                  &DefaultTimeOut);
88    
89    RtlFreeUnicodeString(&MailslotName);
90    
91    if (!NT_SUCCESS(Status))
92      {
93         DPRINT("NtCreateMailslot failed (Status %x)!\n", Status);
94         SetLastErrorByStatus (Status);
95         return(INVALID_HANDLE_VALUE);
96      }
97    
98    return(MailslotHandle);
99 }
100
101
102 WINBOOL STDCALL
103 GetMailslotInfo(HANDLE hMailslot,
104                 LPDWORD lpMaxMessageSize,
105                 LPDWORD lpNextSize,
106                 LPDWORD lpMessageCount,
107                 LPDWORD lpReadTimeout)
108 {
109    FILE_MAILSLOT_QUERY_INFORMATION Buffer;
110    IO_STATUS_BLOCK Iosb;
111    NTSTATUS Status;
112    
113    Status = NtQueryInformationFile(hMailslot,
114                                    &Iosb,
115                                    &Buffer,
116                                    sizeof(FILE_MAILSLOT_QUERY_INFORMATION),
117                                    FileMailslotQueryInformation);
118    if (!NT_SUCCESS(Status))
119      {
120         DPRINT("NtQueryInformationFile failed (Status %x)!\n", Status);
121         SetLastErrorByStatus (Status);
122         return(FALSE);
123      }
124    
125    if (lpMaxMessageSize != NULL)
126      {
127         *lpMaxMessageSize = Buffer.MaxMessageSize;
128      }
129    if (lpNextSize != NULL)
130      {
131         *lpNextSize = Buffer.NextSize;
132      }
133    if (lpMessageCount != NULL)
134      {
135         *lpMessageCount = Buffer.MessageCount;
136      }
137    if (lpReadTimeout != NULL)
138      {
139         *lpReadTimeout = (DWORD)(Buffer.Timeout.QuadPart / -10000);
140      }
141    
142    return(TRUE);
143 }
144
145
146 WINBOOL STDCALL
147 SetMailslotInfo(HANDLE hMailslot,
148                 DWORD lReadTimeout)
149 {
150    FILE_MAILSLOT_SET_INFORMATION Buffer;
151    IO_STATUS_BLOCK Iosb;
152    NTSTATUS Status;
153    
154    Buffer.Timeout.QuadPart = lReadTimeout * -10000;
155    
156    Status = NtSetInformationFile(hMailslot,
157                                  &Iosb,
158                                  &Buffer,
159                                  sizeof(FILE_MAILSLOT_SET_INFORMATION),
160                                  FileMailslotSetInformation);
161    if (!NT_SUCCESS(Status))
162      {
163         DPRINT("NtSetInformationFile failed (Status %x)!\n", Status);
164         SetLastErrorByStatus (Status);
165         return(FALSE);
166      }
167    
168    return(TRUE);
169 }
170
171 /* EOF */