ReactOS 0.4.17-dev-357-ga8f14ff
thread.c
Go to the documentation of this file.
1/*
2 * SHLWAPI thread and MT synchronisation functions
3 *
4 * Copyright 2002 Juergen Schmied
5 * Copyright 2002 Jon Griffiths
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21#include <stdarg.h>
22#include <string.h>
23
24#define COBJMACROS
25
26#include "windef.h"
27#include "winbase.h"
28#include "winnls.h"
29#include "winuser.h"
30#define NO_SHLWAPI_REG
31#define NO_SHLWAPI_PATH
32#define NO_SHLWAPI_GDI
33#define NO_SHLWAPI_STREAM
34#define NO_SHLWAPI_USER
35#include "shlwapi.h"
36#include "shlobj.h"
37#include "wine/debug.h"
38
40
41extern DWORD SHLWAPI_ThreadRef_index; /* Initialised in shlwapi_main.c */
42
44
45/**************************************************************************
46 * CreateAllAccessSecurityAttributes [SHLWAPI.356]
47 *
48 * Initialise security attributes from a security descriptor.
49 *
50 * PARAMS
51 * lpAttr [O] Security attributes
52 * lpSec [I] Security descriptor
53 *
54 * RETURNS
55 * Success: lpAttr, initialised using lpSec.
56 * Failure: NULL, if any parameters are invalid.
57 *
58 * NOTES
59 * This function always returns NULL if the underlying OS version
60 * Wine is impersonating does not use security descriptors (i.e. anything
61 * before Windows NT).
62 */
66 DWORD p3)
67{
68 /* This function is used within SHLWAPI only to create security attributes
69 * for shell semaphores. */
70
71 TRACE("(%p,%p,%08lx)\n", lpAttr, lpSec, p3);
72
73 if (!(GetVersion() & 0x80000000)) /* NT */
74 {
75 if (!lpSec || !lpAttr)
76 return NULL;
77
78 if (InitializeSecurityDescriptor(lpSec, 1))
79 {
81 {
82 lpAttr->nLength = sizeof(SECURITY_ATTRIBUTES);
83 lpAttr->lpSecurityDescriptor = lpSec;
84 lpAttr->bInheritHandle = FALSE;
85 return lpAttr;
86 }
87 }
88 }
89 return NULL;
90}
91
92/*************************************************************************
93 * _SHGetInstanceExplorer [SHLWAPI.@]
94 *
95 * Get an interface to the shell explorer.
96 *
97 * PARAMS
98 * lppUnknown [O] Destination for explorers IUnknown interface.
99 *
100 * RETURNS
101 * Success: S_OK. lppUnknown contains the explorer interface.
102 * Failure: An HRESULT error code.
103 */
105{
106 /* This function is used within SHLWAPI only to hold the IE reference
107 * for threads created with the CTF_PROCESS_REF flag set. */
108 return SHGetInstanceExplorer(lppUnknown);
109}
110
111/*************************************************************************
112 * SHGlobalCounterGetValue [SHLWAPI.223]
113 *
114 * Get the current count of a semaphore.
115 *
116 * PARAMS
117 * hSem [I] Semaphore handle
118 *
119 * RETURNS
120 * The current count of the semaphore.
121 */
123{
124 LONG dwOldCount = 0;
125
126 TRACE("(%p)\n", hSem);
127 ReleaseSemaphore(hSem, 1, &dwOldCount); /* +1 */
128 WaitForSingleObject(hSem, 0); /* -1 */
129 return dwOldCount;
130}
131
132/*************************************************************************
133 * SHGlobalCounterIncrement [SHLWAPI.224]
134 *
135 * Claim a semaphore.
136 *
137 * PARAMS
138 * hSem [I] Semaphore handle
139 *
140 * RETURNS
141 * The new count of the semaphore.
142 */
144{
145 LONG dwOldCount = 0;
146
147 TRACE("(%p)\n", hSem);
148 ReleaseSemaphore(hSem, 1, &dwOldCount);
149 return dwOldCount + 1;
150}
151
152/*************************************************************************
153 * SHGlobalCounterDecrement [SHLWAPI.424]
154 *
155 * Release a semaphore.
156 *
157 * PARAMS
158 * hSem [I] Semaphore handle
159 *
160 * RETURNS
161 * The new count of the semaphore.
162 */
164{
165 DWORD dwOldCount = 0;
166
167 TRACE("(%p)\n", hSem);
168
169 dwOldCount = SHGlobalCounterGetValue(hSem);
170 WaitForSingleObject(hSem, 0);
171 return dwOldCount - 1;
172}
173
174/*************************************************************************
175 * SHGlobalCounterCreateNamedW [SHLWAPI.423]
176 *
177 * Unicode version of SHGlobalCounterCreateNamedA.
178 */
180{
181 static const WCHAR szPrefix[] = { 's', 'h', 'e', 'l', 'l', '.', '\0' };
182 const int iPrefixLen = 6;
183 WCHAR szBuff[MAX_PATH];
185 SECURITY_ATTRIBUTES sAttr, *pSecAttr;
186 HANDLE hRet;
187
188 TRACE("(%s,%ld)\n", debugstr_w(lpszName), iInitial);
189
190 /* Create Semaphore name */
191 memcpy(szBuff, szPrefix, (iPrefixLen + 1) * sizeof(WCHAR));
192 if (lpszName)
193 StrCpyNW(szBuff + iPrefixLen, lpszName, ARRAY_SIZE(szBuff) - iPrefixLen);
194
195 /* Initialise security attributes */
196 pSecAttr = CreateAllAccessSecurityAttributes(&sAttr, &sd, 0);
197
198 if (!(hRet = CreateSemaphoreW(pSecAttr , iInitial, MAXLONG, szBuff)))
200 return hRet;
201}
202
203/*************************************************************************
204 * SHGlobalCounterCreateNamedA [SHLWAPI.422]
205 *
206 * Create a semaphore.
207 *
208 * PARAMS
209 * lpszName [I] Name of semaphore
210 * iInitial [I] Initial count for semaphore
211 *
212 * RETURNS
213 * A new semaphore handle.
214 */
216{
217 WCHAR szBuff[MAX_PATH];
218
219 TRACE("(%s,%ld)\n", debugstr_a(lpszName), iInitial);
220
221 if (lpszName)
222 MultiByteToWideChar(CP_ACP, 0, lpszName, -1, szBuff, MAX_PATH);
223 return SHGlobalCounterCreateNamedW(lpszName ? szBuff : NULL, iInitial);
224}
225
226/*************************************************************************
227 * SHGlobalCounterCreate [SHLWAPI.222]
228 *
229 * Create a semaphore using the name of a GUID.
230 *
231 * PARAMS
232 * guid [I] GUID to use as semaphore name
233 *
234 * RETURNS
235 * A handle to the semaphore.
236 *
237 * NOTES
238 * The initial count of the semaphore is set to 0.
239 */
241{
242 char szName[40];
243
244 TRACE("(%s)\n", debugstr_guid(guid));
245
246 /* Create a named semaphore using the GUID string */
247 SHStringFromGUIDA(guid, szName, sizeof(szName) - 1);
249}
#define WINE_DEFAULT_DEBUG_CHANNEL(t)
Definition: precomp.h:23
void shell(int argc, const char *argv[])
Definition: cmds.c:1231
#define ARRAY_SIZE(A)
Definition: main.h:20
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
BOOL WINAPI InitializeSecurityDescriptor(PSECURITY_DESCRIPTOR pSecurityDescriptor, DWORD dwRevision)
Definition: security.c:929
struct _SECURITY_ATTRIBUTES SECURITY_ATTRIBUTES
#define CP_ACP
Definition: compat.h:109
#define MAX_PATH
Definition: compat.h:34
#define MultiByteToWideChar
Definition: compat.h:110
WCHAR *WINAPI StrCpyNW(WCHAR *dst, const WCHAR *src, int count)
Definition: string.c:470
GUID guid
Definition: version.c:147
DWORD WINAPI GetVersion(void)
Definition: version.c:1458
HANDLE WINAPI SHGlobalCounterCreateNamedW(LPCWSTR lpszName, DWORD iInitial)
Definition: thread.c:179
HANDLE WINAPI SHGlobalCounterCreate(REFGUID guid)
Definition: thread.c:240
LPSECURITY_ATTRIBUTES WINAPI CreateAllAccessSecurityAttributes(LPSECURITY_ATTRIBUTES lpAttr, PSECURITY_DESCRIPTOR lpSec, DWORD p3)
Definition: thread.c:63
LONG WINAPI SHGlobalCounterGetValue(HANDLE hSem)
Definition: thread.c:122
LONG WINAPI SHGlobalCounterIncrement(HANDLE hSem)
Definition: thread.c:143
DWORD SHLWAPI_ThreadRef_index
Definition: shlwapi_main.c:34
DWORD WINAPI SHGlobalCounterDecrement(HANDLE hSem)
Definition: thread.c:163
INT WINAPI SHStringFromGUIDA(REFGUID, LPSTR, INT)
Definition: shimtest.c:51
HRESULT WINAPI _SHGetInstanceExplorer(IUnknown **lppUnknown)
Definition: thread.c:104
HANDLE WINAPI SHGlobalCounterCreateNamedA(LPCSTR lpszName, DWORD iInitial)
Definition: thread.c:215
unsigned long DWORD
Definition: ntddk_ex.h:95
#define debugstr_guid
Definition: kernel32.h:35
#define debugstr_a
Definition: kernel32.h:31
#define debugstr_w
Definition: kernel32.h:32
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
static const WCHAR sd[]
Definition: suminfo.c:286
#define SYNCHRONIZE
Definition: nt_native.h:61
short WCHAR
Definition: pedump.c:58
long LONG
Definition: pedump.c:60
static const WCHAR szName[]
Definition: powrprof.c:45
BOOL WINAPI SetSecurityDescriptorDacl(PSECURITY_DESCRIPTOR pSecurityDescriptor, BOOL bDaclPresent, PACL pDacl, BOOL bDaclDefaulted)
Definition: sec.c:262
HRESULT WINAPI SHGetInstanceExplorer(IUnknown **lpUnknown)
Definition: shellord.c:1721
#define TRACE(s)
Definition: solgame.cpp:4
Definition: scsiwmi.h:51
LPVOID lpSecurityDescriptor
Definition: compat.h:193
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
HANDLE WINAPI DECLSPEC_HOTPATCH OpenSemaphoreW(IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN LPCWSTR lpName)
Definition: synch.c:478
HANDLE WINAPI DECLSPEC_HOTPATCH CreateSemaphoreW(IN LPSECURITY_ATTRIBUTES lpSemaphoreAttributes OPTIONAL, IN LONG lInitialCount, IN LONG lMaximumCount, IN LPCWSTR lpName OPTIONAL)
Definition: synch.c:406
BOOL WINAPI DECLSPEC_HOTPATCH ReleaseSemaphore(IN HANDLE hSemaphore, IN LONG lReleaseCount, IN LPLONG lpPreviousCount)
Definition: synch.c:491
const char * LPCSTR
Definition: typedefs.h:52
const uint16_t * LPCWSTR
Definition: typedefs.h:57
char * LPSTR
Definition: typedefs.h:51
int32_t INT
Definition: typedefs.h:58
#define MAXLONG
Definition: umtypes.h:116
#define SEMAPHORE_MODIFY_STATE
Definition: winbase.h:164
#define WINAPI
Definition: msvc.h:6