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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "wine/debug.h"
26 #define NO_SHLWAPI_REG
27 #define NO_SHLWAPI_PATH
28 #define NO_SHLWAPI_GDI
29 #define NO_SHLWAPI_STREAM
30 #define NO_SHLWAPI_USER
33 WINE_DEFAULT_DEBUG_CHANNEL(shell);
35 /* Get a function pointer from a DLL handle */
36 #define GET_FUNC(func, module, name, fail) \
39 if (!SHLWAPI_h##module && !(SHLWAPI_h##module = LoadLibraryA(#module ".dll"))) return fail; \
40 if (!(func = (void*)GetProcAddress(SHLWAPI_h##module, name))) return fail; \
44 /* DLL handles for late bound calls */
45 extern HMODULE SHLWAPI_hshell32;
47 /* Function pointers for GET_FUNC macro; these need to be global because of gcc bug */
48 static HRESULT (WINAPI *pSHGetInstanceExplorer)(IUnknown**);
50 extern DWORD SHLWAPI_ThreadRef_index; /* Initialised in shlwapi_main.c */
52 DWORD WINAPI SHLWAPI_23(REFGUID,LPSTR,INT);
54 /**************************************************************************
57 * Initialise security attributes from a security descriptor.
60 * lpAttr [O] Security attributes
61 * lpSec [I] Security descriptor
64 * Success: lpAttr, initialised using lpSec.
65 * Failure: NULL, if any parameters are invalid.
68 * This function always returns returns NULL if the underlying OS version
69 * Wine is impersonating does not use security descriptors (i.e. anything
73 WINAPI SHLWAPI_356(LPSECURITY_ATTRIBUTES lpAttr, PSECURITY_DESCRIPTOR lpSec)
75 /* This function is used within SHLWAPI only to create security attributes
76 * for shell semaphores. */
78 TRACE("(%p,%p)\n", lpAttr, lpSec);
80 if (!(GetVersion() & 0x80000000)) /* NT */
82 if (!lpSec || !lpAttr)
85 if (InitializeSecurityDescriptor(lpSec, 1))
87 if (SetSecurityDescriptorDacl(lpSec, TRUE, NULL, FALSE))
89 lpAttr->nLength = sizeof(SECURITY_ATTRIBUTES);
90 lpAttr->lpSecurityDescriptor = lpSec;
91 lpAttr->bInheritHandle = FALSE;
99 /*************************************************************************
100 * _SHGetInstanceExplorer [SHLWAPI.@]
102 * Get an interface to the shell explorer.
105 * lpUnknown [O] pointer to recieve interface.
108 * Success: S_OK. lppUnknown contains the explorer interface.
109 * Failure: An HRESULT error code.
111 HRESULT WINAPI _SHGetInstanceExplorer(IUnknown **lppUnknown)
113 /* This function is used within SHLWAPI only to hold the IE reference
114 * for threads created with the CTF_PROCESS_REF flag set. */
116 GET_FUNC(pSHGetInstanceExplorer, shell32, "SHGetInstanceExplorer", E_FAIL);
117 return pSHGetInstanceExplorer(lppUnknown);
120 /* Internal thread information structure */
121 typedef struct tagSHLWAPI_THREAD_INFO
123 LPTHREAD_START_ROUTINE pfnThreadProc; /* Thread start */
124 LPTHREAD_START_ROUTINE pfnCallback; /* Thread initialisation */
125 PVOID pData; /* Application specific data */
126 BOOL bInitCom; /* Initialise COM for the thread? */
127 HANDLE hEvent; /* Signal for creator to continue */
128 IUnknown *refThread; /* Reference to thread creator */
129 IUnknown *refIE; /* Reference to the IE process */
130 } SHLWAPI_THREAD_INFO, *LPSHLWAPI_THREAD_INFO;
133 /*************************************************************************
134 * SHGetThreadRef [SHLWAPI.@]
136 * Get a per-thread object reference set by SHSetThreadRef.
139 * lppUnknown [O] Destination to receive object reference
142 * Success: S_OK. lppUnk is set to the object reference.
143 * Failure: E_NOINTERFACE, if an error occurs or lppUnk is invalid.
145 HRESULT WINAPI SHGetThreadRef(IUnknown **lppUnknown)
147 TRACE("(%p)\n", lppUnknown);
149 if (!lppUnknown || SHLWAPI_ThreadRef_index == -1u)
150 return E_NOINTERFACE;
152 *lppUnknown = (IUnknown*)TlsGetValue(SHLWAPI_ThreadRef_index);
154 return E_NOINTERFACE;
156 /* Add a reference. Caller will Release() us when finished */
157 IUnknown_AddRef(*lppUnknown);
161 /*************************************************************************
162 * SHSetThreadRef [SHLWAPI.@]
164 * Store a per-thread object reference.
167 * lpUnk [I] Object reference to store
170 * Success: S_OK. lpUnk is stored and can be retrieved by SHGetThreadRef()
171 * Failure: E_NOINTERFACE, if an error occurs or lpUnk is invalid.
173 HRESULT WINAPI SHSetThreadRef(IUnknown *lpUnknown)
175 TRACE("(%p)\n", lpUnknown);
177 if (!lpUnknown || SHLWAPI_ThreadRef_index == 0xffffffff)
178 return E_NOINTERFACE;
180 TlsSetValue(SHLWAPI_ThreadRef_index, lpUnknown);
184 /*************************************************************************
185 * SHReleaseThreadRef [SHLWAPI.@]
187 * Release a per-thread object reference.
193 * Success: S_OK. The threads obbject reference is released.
194 * Failure: An HRESULT error code.
196 HRESULT WINAPI SHReleaseThreadRef()
198 FIXME("() - stub!\n");
202 /*************************************************************************
203 * SHLWAPI_ThreadWrapper
205 * Internal wrapper for executing user thread functions from SHCreateThread.
207 static DWORD WINAPI SHLWAPI_ThreadWrapper(PVOID pTi)
209 SHLWAPI_THREAD_INFO ti;
210 HRESULT hCom = E_FAIL;
215 /* We are now executing in the context of the newly created thread.
216 * So we copy the data passed to us (it is on the stack of the function
217 * that called us, which is waiting for us to signal an event before
219 memcpy(&ti, pTi, sizeof(SHLWAPI_THREAD_INFO));
221 /* Initialise COM for the thread, if desired */
224 hCom = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED|COINIT_DISABLE_OLE1DDE);
227 hCom = CoInitializeEx(NULL, COINIT_DISABLE_OLE1DDE);
230 /* Execute the callback function before returning */
232 ti.pfnCallback(ti.pData);
234 /* Signal the thread that created us; it can return now */
237 /* Execute the callers start code */
238 dwRet = ti.pfnThreadProc(ti.pData);
240 /* Release references to the caller and IE process, if held */
242 IUnknown_Release(ti.refThread);
245 IUnknown_Release(ti.refIE);
250 /* Return the users thread return value */
254 /*************************************************************************
259 * Create a new thread.
262 * pfnThreadProc [I] Function to execute in new thread
263 * pData [I] Application specific data passed to pfnThreadProc
264 * dwFlags [I] Initialisation to perform in the new thread
265 * pfnCallback [I] Function to execute before pfnThreadProc
268 * Success: TRUE. pfnThreadProc was executed.
269 * Failure: FALSE. pfnThreadProc was not executed.
272 * If the thread cannot be created, pfnCallback is NULL, and dwFlags
273 * has bit CTF_INSIST set, pfnThreadProc will be executed synchronously.
275 BOOL WINAPI SHCreateThread(LPTHREAD_START_ROUTINE pfnThreadProc, VOID *pData,
276 DWORD dwFlags, LPTHREAD_START_ROUTINE pfnCallback)
278 SHLWAPI_THREAD_INFO ti;
279 BOOL bCalled = FALSE;
281 TRACE("(%p,%p,0x%lX,%p)\n", pfnThreadProc, pData, dwFlags, pfnCallback);
283 /* Set up data to pass to the new thread (On our stack) */
284 ti.pfnThreadProc = pfnThreadProc;
285 ti.pfnCallback = pfnCallback;
287 ti.bInitCom = dwFlags & CTF_COINIT ? TRUE : FALSE;
288 ti.hEvent = CreateEventA(NULL,FALSE,FALSE,NULL);
290 /* Hold references to the current thread and IE process, if desired */
291 if(dwFlags & CTF_THREAD_REF)
292 SHGetThreadRef(&ti.refThread);
296 if(dwFlags & CTF_PROCESS_REF)
297 _SHGetInstanceExplorer(&ti.refIE);
301 /* Create the thread */
307 hThread = CreateThread(NULL, 0, SHLWAPI_ThreadWrapper, &ti, 0, &dwRetVal);
311 /* Wait for the thread to signal us to continue */
312 WaitForSingleObject(ti.hEvent, -1);
313 CloseHandle(hThread);
316 CloseHandle(ti.hEvent);
321 if (!ti.pfnCallback && dwFlags & CTF_INSIST)
323 /* Couldn't call, call synchronously */
324 pfnThreadProc(pData);
329 /* Free references, since thread hasn't run to do so */
331 IUnknown_Release(ti.refThread);
334 IUnknown_Release(ti.refIE);
340 /*************************************************************************
343 * Get the current count of a semaphore.
346 * hSem [I] Semaphore handle
349 * The current count of the semaphore.
351 DWORD WINAPI SHLWAPI_223(HANDLE hSem)
353 DWORD dwOldCount = 0;
355 TRACE("(%p)\n", hSem);
356 ReleaseSemaphore(hSem, 1, &dwOldCount); /* +1 */
357 WaitForSingleObject(hSem, 0); /* -1 */
361 /*************************************************************************
367 * hSem [I] Semaphore handle
370 * The new count of the semaphore.
372 DWORD WINAPI SHLWAPI_224(HANDLE hSem)
374 DWORD dwOldCount = 0;
376 TRACE("(%p)\n", hSem);
377 ReleaseSemaphore(hSem, 1, &dwOldCount);
378 return dwOldCount + 1;
381 /*************************************************************************
384 * Release a semaphore.
387 * hSem [I] Semaphore handle
390 * The new count of the semaphore.
392 DWORD WINAPI SHLWAPI_424(HANDLE hSem)
394 DWORD dwOldCount = 0;
396 TRACE("(%p)\n", hSem);
398 dwOldCount = SHLWAPI_223(hSem);
399 WaitForSingleObject(hSem, 0);
400 return dwOldCount - 1;
403 /*************************************************************************
406 * Unicode version of SHLWAPI_422.
408 HANDLE WINAPI SHLWAPI_423(LPCWSTR lpszName, DWORD iInitial)
410 static const WCHAR szPrefix[] = { 's', 'h', 'e', 'l', 'l', '.', '\0' };
411 const int iPrefixLen = 6;
412 WCHAR szBuff[MAX_PATH];
413 const int iBuffLen = sizeof(szBuff)/sizeof(WCHAR);
414 SECURITY_DESCRIPTOR sd;
415 SECURITY_ATTRIBUTES sAttr, *pSecAttr;
418 TRACE("(%s,%ld)\n", debugstr_w(lpszName), iInitial);
420 /* Create Semaphore name */
421 memcpy(szBuff, szPrefix, (iPrefixLen + 1) * sizeof(WCHAR));
423 StrCpyNW(szBuff + iPrefixLen, lpszName, iBuffLen - iPrefixLen);
425 /* Initialise security attributes */
426 pSecAttr = SHLWAPI_356(&sAttr, &sd);
428 if (!(hRet = CreateSemaphoreW(pSecAttr , iInitial, MAXLONG, szBuff)))
429 hRet = OpenSemaphoreW(SYNCHRONIZE|SEMAPHORE_MODIFY_STATE, 0, szBuff);
433 /*************************************************************************
436 * Create a semaphore.
439 * lpszName [I] Name of semaphore
440 * iInitial [I] Initial count for semaphore
443 * A new semaphore handle.
445 HANDLE WINAPI SHLWAPI_422(LPCSTR lpszName, DWORD iInitial)
447 WCHAR szBuff[MAX_PATH];
449 TRACE("(%s,%ld)\n", debugstr_a(lpszName), iInitial);
452 MultiByteToWideChar(0, 0, lpszName, -1, szBuff, MAX_PATH);
453 return SHLWAPI_423(lpszName ? szBuff : NULL, iInitial);
456 /*************************************************************************
459 * Create a semaphore using the name of a GUID.
462 * guid [I] GUID to use as semaphore name
465 * A handle to the semaphore.
468 * The initial count of the semaphore is set to 0.
470 HANDLE WINAPI SHLWAPI_222(REFGUID guid)
474 TRACE("(%s)\n", debugstr_guid(guid));
476 /* Create a named semaphore using the GUID string */
477 SHLWAPI_23(guid, szName, sizeof(szName) - 1);
478 return SHLWAPI_422(szName, 0);