2 * SHLWAPI thread and MT synchronisation functions
4 * Copyright 2002 Juergen Schmied
5 * Copyright 2002 Jon Griffiths
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.
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.
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
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
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
41 extern DWORD SHLWAPI_ThreadRef_index
; /* Initialised in shlwapi_main.c */
43 INT WINAPI
SHStringFromGUIDA(REFGUID
,LPSTR
,INT
);
45 /**************************************************************************
46 * CreateAllAccessSecurityAttributes [SHLWAPI.356]
48 * Initialise security attributes from a security descriptor.
51 * lpAttr [O] Security attributes
52 * lpSec [I] Security descriptor
55 * Success: lpAttr, initialised using lpSec.
56 * Failure: NULL, if any parameters are invalid.
59 * This function always returns NULL if the underlying OS version
60 * Wine is impersonating does not use security descriptors (i.e. anything
63 LPSECURITY_ATTRIBUTES WINAPI
CreateAllAccessSecurityAttributes(
64 LPSECURITY_ATTRIBUTES lpAttr
,
65 PSECURITY_DESCRIPTOR lpSec
,
68 /* This function is used within SHLWAPI only to create security attributes
69 * for shell semaphores. */
71 TRACE("(%p,%p,%08x)\n", lpAttr
, lpSec
, p3
);
73 if (!(GetVersion() & 0x80000000)) /* NT */
75 if (!lpSec
|| !lpAttr
)
78 if (InitializeSecurityDescriptor(lpSec
, 1))
80 if (SetSecurityDescriptorDacl(lpSec
, TRUE
, NULL
, FALSE
))
82 lpAttr
->nLength
= sizeof(SECURITY_ATTRIBUTES
);
83 lpAttr
->lpSecurityDescriptor
= lpSec
;
84 lpAttr
->bInheritHandle
= FALSE
;
92 /*************************************************************************
93 * _SHGetInstanceExplorer [SHLWAPI.@]
95 * Get an interface to the shell explorer.
98 * lppUnknown [O] Destination for explorers IUnknown interface.
101 * Success: S_OK. lppUnknown contains the explorer interface.
102 * Failure: An HRESULT error code.
104 HRESULT WINAPI
_SHGetInstanceExplorer(IUnknown
**lppUnknown
)
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
);
111 /*************************************************************************
112 * SHGlobalCounterGetValue [SHLWAPI.223]
114 * Get the current count of a semaphore.
117 * hSem [I] Semaphore handle
120 * The current count of the semaphore.
122 LONG WINAPI
SHGlobalCounterGetValue(HANDLE hSem
)
126 TRACE("(%p)\n", hSem
);
127 ReleaseSemaphore(hSem
, 1, &dwOldCount
); /* +1 */
128 WaitForSingleObject(hSem
, 0); /* -1 */
132 /*************************************************************************
133 * SHGlobalCounterIncrement [SHLWAPI.224]
138 * hSem [I] Semaphore handle
141 * The new count of the semaphore.
143 LONG WINAPI
SHGlobalCounterIncrement(HANDLE hSem
)
147 TRACE("(%p)\n", hSem
);
148 ReleaseSemaphore(hSem
, 1, &dwOldCount
);
149 return dwOldCount
+ 1;
152 /*************************************************************************
153 * SHGlobalCounterDecrement [SHLWAPI.424]
155 * Release a semaphore.
158 * hSem [I] Semaphore handle
161 * The new count of the semaphore.
163 DWORD WINAPI
SHGlobalCounterDecrement(HANDLE hSem
)
165 DWORD dwOldCount
= 0;
167 TRACE("(%p)\n", hSem
);
169 dwOldCount
= SHGlobalCounterGetValue(hSem
);
170 WaitForSingleObject(hSem
, 0);
171 return dwOldCount
- 1;
174 /*************************************************************************
175 * SHGlobalCounterCreateNamedW [SHLWAPI.423]
177 * Unicode version of SHGlobalCounterCreateNamedA.
179 HANDLE WINAPI
SHGlobalCounterCreateNamedW(LPCWSTR lpszName
, DWORD iInitial
)
181 static const WCHAR szPrefix
[] = { 's', 'h', 'e', 'l', 'l', '.', '\0' };
182 const int iPrefixLen
= 6;
183 WCHAR szBuff
[MAX_PATH
];
184 SECURITY_DESCRIPTOR sd
;
185 SECURITY_ATTRIBUTES sAttr
, *pSecAttr
;
188 TRACE("(%s,%d)\n", debugstr_w(lpszName
), iInitial
);
190 /* Create Semaphore name */
191 memcpy(szBuff
, szPrefix
, (iPrefixLen
+ 1) * sizeof(WCHAR
));
193 StrCpyNW(szBuff
+ iPrefixLen
, lpszName
, ARRAY_SIZE(szBuff
) - iPrefixLen
);
195 /* Initialise security attributes */
196 pSecAttr
= CreateAllAccessSecurityAttributes(&sAttr
, &sd
, 0);
198 if (!(hRet
= CreateSemaphoreW(pSecAttr
, iInitial
, MAXLONG
, szBuff
)))
199 hRet
= OpenSemaphoreW(SYNCHRONIZE
|SEMAPHORE_MODIFY_STATE
, 0, szBuff
);
203 /*************************************************************************
204 * SHGlobalCounterCreateNamedA [SHLWAPI.422]
206 * Create a semaphore.
209 * lpszName [I] Name of semaphore
210 * iInitial [I] Initial count for semaphore
213 * A new semaphore handle.
215 HANDLE WINAPI
SHGlobalCounterCreateNamedA(LPCSTR lpszName
, DWORD iInitial
)
217 WCHAR szBuff
[MAX_PATH
];
219 TRACE("(%s,%d)\n", debugstr_a(lpszName
), iInitial
);
222 MultiByteToWideChar(CP_ACP
, 0, lpszName
, -1, szBuff
, MAX_PATH
);
223 return SHGlobalCounterCreateNamedW(lpszName
? szBuff
: NULL
, iInitial
);
226 /*************************************************************************
227 * SHGlobalCounterCreate [SHLWAPI.222]
229 * Create a semaphore using the name of a GUID.
232 * guid [I] GUID to use as semaphore name
235 * A handle to the semaphore.
238 * The initial count of the semaphore is set to 0.
240 HANDLE WINAPI
SHGlobalCounterCreate (REFGUID guid
)
244 TRACE("(%s)\n", debugstr_guid(guid
));
246 /* Create a named semaphore using the GUID string */
247 SHStringFromGUIDA(guid
, szName
, sizeof(szName
) - 1);
248 return SHGlobalCounterCreateNamedA(szName
, 0);