4 * Copyright 1998 Alexandre Julliard
10 #include "wine/unicode.h"
14 /***********************************************************************
15 * CreateSemaphoreA (KERNEL32.174)
17 HANDLE WINAPI
CreateSemaphoreA( SECURITY_ATTRIBUTES
*sa
, LONG initial
, LONG max
, LPCSTR name
)
20 DWORD len
= name
? MultiByteToWideChar( CP_ACP
, 0, name
, strlen(name
), NULL
, 0 ) : 0;
22 /* Check parameters */
24 if ((max
<= 0) || (initial
< 0) || (initial
> max
))
26 SetLastError( ERROR_INVALID_PARAMETER
);
31 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
37 struct create_semaphore_request
*req
= server_alloc_req( sizeof(*req
),
38 len
* sizeof(WCHAR
) );
40 req
->initial
= (unsigned int)initial
;
41 req
->max
= (unsigned int)max
;
42 req
->inherit
= (sa
&& (sa
->nLength
>=sizeof(*sa
)) && sa
->bInheritHandle
);
43 if (len
) MultiByteToWideChar( CP_ACP
, 0, name
, strlen(name
), server_data_ptr(req
), len
);
45 server_call( REQ_CREATE_SEMAPHORE
);
49 if (ret
== -1) ret
= 0; /* must return 0 on failure, not -1 */
54 /***********************************************************************
55 * CreateSemaphoreW (KERNEL32.175)
57 HANDLE WINAPI
CreateSemaphoreW( SECURITY_ATTRIBUTES
*sa
, LONG initial
,
58 LONG max
, LPCWSTR name
)
61 DWORD len
= name
? strlenW(name
) : 0;
63 /* Check parameters */
65 if ((max
<= 0) || (initial
< 0) || (initial
> max
))
67 SetLastError( ERROR_INVALID_PARAMETER
);
72 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
78 struct create_semaphore_request
*req
= server_alloc_req( sizeof(*req
),
79 len
* sizeof(WCHAR
) );
81 req
->initial
= (unsigned int)initial
;
82 req
->max
= (unsigned int)max
;
83 req
->inherit
= (sa
&& (sa
->nLength
>=sizeof(*sa
)) && sa
->bInheritHandle
);
84 memcpy( server_data_ptr(req
), name
, len
* sizeof(WCHAR
) );
86 server_call( REQ_CREATE_SEMAPHORE
);
90 if (ret
== -1) ret
= 0; /* must return 0 on failure, not -1 */
95 /***********************************************************************
96 * OpenSemaphoreA (KERNEL32.545)
98 HANDLE WINAPI
OpenSemaphoreA( DWORD access
, BOOL inherit
, LPCSTR name
)
101 DWORD len
= name
? MultiByteToWideChar( CP_ACP
, 0, name
, strlen(name
), NULL
, 0 ) : 0;
104 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
109 struct open_semaphore_request
*req
= server_alloc_req( sizeof(*req
),
110 len
* sizeof(WCHAR
) );
111 req
->access
= access
;
112 req
->inherit
= inherit
;
113 if (len
) MultiByteToWideChar( CP_ACP
, 0, name
, strlen(name
), server_data_ptr(req
), len
);
114 server_call( REQ_OPEN_SEMAPHORE
);
118 if (ret
== -1) ret
= 0; /* must return 0 on failure, not -1 */
123 /***********************************************************************
124 * OpenSemaphoreW (KERNEL32.546)
126 HANDLE WINAPI
OpenSemaphoreW( DWORD access
, BOOL inherit
, LPCWSTR name
)
129 DWORD len
= name
? strlenW(name
) : 0;
132 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
137 struct open_semaphore_request
*req
= server_alloc_req( sizeof(*req
), len
* sizeof(WCHAR
) );
138 req
->access
= access
;
139 req
->inherit
= inherit
;
140 memcpy( server_data_ptr(req
), name
, len
* sizeof(WCHAR
) );
141 server_call( REQ_OPEN_SEMAPHORE
);
145 if (ret
== -1) ret
= 0; /* must return 0 on failure, not -1 */
150 /***********************************************************************
151 * ReleaseSemaphore (KERNEL32.583)
153 BOOL WINAPI
ReleaseSemaphore( HANDLE handle
, LONG count
, LONG
*previous
)
155 NTSTATUS status
= NtReleaseSemaphore( handle
, count
, previous
);
156 if (status
) SetLastError( RtlNtStatusToDosError(status
) );