Eliminate lots of __WINE__ conditionals from the headers.
[wine] / dlls / shlwapi / thread.c
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21 #include <string.h>
22
23 #include "windef.h"
24 #include "winnls.h"
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
31 #include "shlwapi.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(shell);
34
35 /* Get a function pointer from a DLL handle */
36 #define GET_FUNC(func, module, name, fail) \
37   do { \
38     if (!func) { \
39       if (!SHLWAPI_h##module && !(SHLWAPI_h##module = LoadLibraryA(#module ".dll"))) return fail; \
40       if (!(func = (void*)GetProcAddress(SHLWAPI_h##module, name))) return fail; \
41     } \
42   } while (0)
43
44 /* DLL handles for late bound calls */
45 extern HMODULE SHLWAPI_hshell32;
46
47 /* Function pointers for GET_FUNC macro; these need to be global because of gcc bug */
48 static HRESULT (WINAPI *pSHGetInstanceExplorer)(IUnknown**);
49
50 extern DWORD SHLWAPI_ThreadRef_index;  /* Initialised in shlwapi_main.c */
51
52 DWORD WINAPI SHLWAPI_23(REFGUID,LPSTR,INT);
53
54 /**************************************************************************
55  *      @       [SHLWAPI.356]
56  *
57  * Initialise security attributes from a security descriptor.
58  *
59  * PARAMS
60  *  lpAttr [O] Security attributes
61  *  lpSec  [I] Security descriptor
62  *
63  * RETURNS
64  *  Success: lpAttr, initialised using lpSec.
65  *  Failure: NULL, if any parameters are invalid.
66  *
67  * NOTES
68  *  This function always returns returns NULL if the underlying OS version
69  *  Wine is impersonating does not use security descriptors (i.e. anything
70  *  before Windows NT).
71  */
72 LPSECURITY_ATTRIBUTES
73 WINAPI SHLWAPI_356(LPSECURITY_ATTRIBUTES lpAttr, PSECURITY_DESCRIPTOR lpSec)
74 {
75   /* This function is used within SHLWAPI only to create security attributes
76    * for shell semaphores. */
77
78   TRACE("(%p,%p)\n", lpAttr, lpSec);
79
80   if (!(GetVersion() & 0x80000000))  /* NT */
81   {
82     if (!lpSec || !lpAttr)
83       return NULL;
84
85     if (InitializeSecurityDescriptor(lpSec, 1))
86     {
87       if (SetSecurityDescriptorDacl(lpSec, TRUE, NULL, FALSE))
88       {
89          lpAttr->nLength = sizeof(SECURITY_ATTRIBUTES);
90          lpAttr->lpSecurityDescriptor = lpSec;
91          lpAttr->bInheritHandle = FALSE;
92          return lpAttr;
93       }
94     }
95   }
96   return NULL;
97 }
98
99 /*************************************************************************
100  *      _SHGetInstanceExplorer  [SHLWAPI.@]
101  *
102  * Get an interface to the shell explorer.
103  *
104  * PARAMS
105  *  lpUnknown [O] pointer to recieve interface.
106  *
107  * RETURNS
108  *  Success: S_OK. lppUnknown contains the explorer interface.
109  *  Failure: An HRESULT error code.
110  */
111 HRESULT WINAPI _SHGetInstanceExplorer(IUnknown **lppUnknown)
112 {
113   /* This function is used within SHLWAPI only to hold the IE reference
114    * for threads created with the CTF_PROCESS_REF flag set. */
115
116   GET_FUNC(pSHGetInstanceExplorer, shell32, "SHGetInstanceExplorer", E_FAIL);
117   return pSHGetInstanceExplorer(lppUnknown);
118 }
119
120 /* Internal thread information structure */
121 typedef struct tagSHLWAPI_THREAD_INFO
122 {
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;
131
132
133 /*************************************************************************
134  * SHGetThreadRef       [SHLWAPI.@]
135  *
136  * Get a per-thread object reference set by SHSetThreadRef.
137  *
138  * PARAMS
139  *   lppUnknown [O] Destination to receive object reference
140  *
141  * RETURNS
142  *   Success: S_OK. lppUnk is set to the object reference.
143  *   Failure: E_NOINTERFACE, if an error occurs or lppUnk is invalid.
144  */
145 HRESULT WINAPI SHGetThreadRef(IUnknown **lppUnknown)
146 {
147   TRACE("(%p)\n", lppUnknown);
148
149   if (!lppUnknown || SHLWAPI_ThreadRef_index == -1u)
150     return E_NOINTERFACE;
151
152   *lppUnknown = (IUnknown*)TlsGetValue(SHLWAPI_ThreadRef_index);
153   if (!*lppUnknown)
154     return E_NOINTERFACE;
155
156   /* Add a reference. Caller will Release() us when finished */
157   IUnknown_AddRef(*lppUnknown);
158   return S_OK;
159 }
160
161 /*************************************************************************
162  * SHSetThreadRef       [SHLWAPI.@]
163  *
164  * Store a per-thread object reference.
165  *
166  * PARAMS
167  *   lpUnk [I] Object reference to store
168  *
169  * RETURNS
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.
172  */
173 HRESULT WINAPI SHSetThreadRef(IUnknown *lpUnknown)
174 {
175   TRACE("(%p)\n", lpUnknown);
176
177   if (!lpUnknown || SHLWAPI_ThreadRef_index  == 0xffffffff)
178     return E_NOINTERFACE;
179
180   TlsSetValue(SHLWAPI_ThreadRef_index, lpUnknown);
181   return S_OK;
182 }
183
184 /*************************************************************************
185  * SHReleaseThreadRef   [SHLWAPI.@]
186  *
187  * Release a per-thread object reference.
188  *
189  * PARAMS
190  *  None.
191  *
192  * RETURNS
193  *   Success: S_OK. The threads obbject reference is released.
194  *   Failure: An HRESULT error code.
195  */
196 HRESULT WINAPI SHReleaseThreadRef()
197 {
198   FIXME("() - stub!\n");
199   return S_OK;
200 }
201
202 /*************************************************************************
203  * SHLWAPI_ThreadWrapper
204  *
205  * Internal wrapper for executing user thread functions from SHCreateThread.
206  */
207 static DWORD WINAPI SHLWAPI_ThreadWrapper(PVOID pTi)
208 {
209   SHLWAPI_THREAD_INFO ti;
210   HRESULT hCom = E_FAIL;
211   DWORD dwRet;
212
213   TRACE("(%p)", pTi);
214
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
218    * returning). */
219   memcpy(&ti, pTi, sizeof(SHLWAPI_THREAD_INFO));
220
221   /* Initialise COM for the thread, if desired */
222   if (ti.bInitCom)
223   {
224     hCom = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED|COINIT_DISABLE_OLE1DDE);
225
226     if (FAILED(hCom))
227       hCom = CoInitializeEx(NULL, COINIT_DISABLE_OLE1DDE);
228   }
229
230   /* Execute the callback function before returning */
231   if (ti.pfnCallback)
232     ti.pfnCallback(ti.pData);
233
234   /* Signal the thread that created us; it can return now */
235   SetEvent(ti.hEvent);
236
237   /* Execute the callers start code */
238   dwRet = ti.pfnThreadProc(ti.pData);
239
240   /* Release references to the caller and IE process, if held */
241   if (ti.refThread)
242     IUnknown_Release(ti.refThread);
243
244   if (ti.refIE)
245     IUnknown_Release(ti.refIE);
246
247   if (SUCCEEDED(hCom))
248     CoUninitialize();
249
250   /* Return the users thread return value */
251   return dwRet;
252 }
253
254 /*************************************************************************
255  *      @       [SHLWAPI.16]
256  *
257  * SHCreateThread
258  *
259  * Create a new thread.
260  *
261  * PARAMS
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
266  *
267  * RETURNS
268  *   Success: TRUE. pfnThreadProc was executed.
269  *   Failure: FALSE. pfnThreadProc was not executed.
270  *
271  * NOTES
272  *   If the thread cannot be created, pfnCallback is NULL, and dwFlags
273  *   has bit CTF_INSIST set, pfnThreadProc will be executed synchronously.
274  */
275 BOOL WINAPI SHCreateThread(LPTHREAD_START_ROUTINE pfnThreadProc, VOID *pData,
276                            DWORD dwFlags, LPTHREAD_START_ROUTINE pfnCallback)
277 {
278   SHLWAPI_THREAD_INFO ti;
279   BOOL bCalled = FALSE;
280
281   TRACE("(%p,%p,0x%lX,%p)\n", pfnThreadProc, pData, dwFlags, pfnCallback);
282
283   /* Set up data to pass to the new thread (On our stack) */
284   ti.pfnThreadProc = pfnThreadProc;
285   ti.pfnCallback = pfnCallback;
286   ti.pData = pData;
287   ti.bInitCom = dwFlags & CTF_COINIT ? TRUE : FALSE;
288   ti.hEvent = CreateEventA(NULL,FALSE,FALSE,NULL);
289
290   /* Hold references to the current thread and IE process, if desired */
291   if(dwFlags & CTF_THREAD_REF)
292     SHGetThreadRef(&ti.refThread);
293   else
294     ti.refThread = NULL;
295
296   if(dwFlags & CTF_PROCESS_REF)
297     _SHGetInstanceExplorer(&ti.refIE);
298   else
299     ti.refIE = NULL;
300
301   /* Create the thread */
302   if(ti.hEvent)
303   {
304     DWORD dwRetVal;
305     HANDLE hThread;
306
307     hThread = CreateThread(NULL, 0, SHLWAPI_ThreadWrapper, &ti, 0, &dwRetVal);
308
309     if(hThread)
310     {
311       /* Wait for the thread to signal us to continue */
312       WaitForSingleObject(ti.hEvent, -1);
313       CloseHandle(hThread);
314       bCalled = TRUE;
315     }
316     CloseHandle(ti.hEvent);
317   }
318
319   if (!bCalled)
320   {
321     if (!ti.pfnCallback && dwFlags & CTF_INSIST)
322     {
323       /* Couldn't call, call synchronously */
324       pfnThreadProc(pData);
325       bCalled = TRUE;
326     }
327     else
328     {
329       /* Free references, since thread hasn't run to do so */
330       if(ti.refThread)
331         IUnknown_Release(ti.refThread);
332
333       if(ti.refIE)
334         IUnknown_Release(ti.refIE);
335     }
336   }
337   return bCalled;
338 }
339
340 /*************************************************************************
341  *      @       [SHLWAPI.223]
342  *
343  * Get the current count of a semaphore.
344  *
345  * PARAMS
346  *  hSem [I] Semaphore handle
347  *
348  * RETURNS
349  *  The current count of the semaphore.
350  */
351 DWORD WINAPI SHLWAPI_223(HANDLE hSem)
352 {
353   DWORD dwOldCount = 0;
354
355   TRACE("(%p)\n", hSem);
356   ReleaseSemaphore(hSem, 1, &dwOldCount); /* +1 */
357   WaitForSingleObject(hSem, 0);           /* -1 */
358   return dwOldCount;
359 }
360
361 /*************************************************************************
362  *      @       [SHLWAPI.224]
363  *
364  * Claim a semaphore.
365  *
366  * PARAMS
367  *  hSem [I] Semaphore handle
368  *
369  * RETURNS
370  *  The new count of the semaphore.
371  */
372 DWORD WINAPI SHLWAPI_224(HANDLE hSem)
373 {
374   DWORD dwOldCount = 0;
375
376   TRACE("(%p)\n", hSem);
377   ReleaseSemaphore(hSem, 1, &dwOldCount);
378   return dwOldCount + 1;
379 }
380
381 /*************************************************************************
382  *      @       [SHLWAPI.424]
383  *
384  * Release a semaphore.
385  *
386  * PARAMS
387  *  hSem [I] Semaphore handle
388  *
389  * RETURNS
390  *  The new count of the semaphore.
391  */
392 DWORD WINAPI SHLWAPI_424(HANDLE hSem)
393 {
394   DWORD dwOldCount = 0;
395
396   TRACE("(%p)\n", hSem);
397
398   dwOldCount = SHLWAPI_223(hSem);
399   WaitForSingleObject(hSem, 0);
400   return dwOldCount - 1;
401 }
402
403 /*************************************************************************
404  *      @       [SHLWAPI.423]
405  *
406  * Unicode version of SHLWAPI_422.
407  */
408 HANDLE WINAPI SHLWAPI_423(LPCWSTR lpszName, DWORD iInitial)
409 {
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;
416   HANDLE hRet;
417
418   TRACE("(%s,%ld)\n", debugstr_w(lpszName), iInitial);
419
420   /* Create Semaphore name */
421   memcpy(szBuff, szPrefix, (iPrefixLen + 1) * sizeof(WCHAR));
422   if (lpszName)
423     StrCpyNW(szBuff + iPrefixLen, lpszName, iBuffLen - iPrefixLen);
424
425   /* Initialise security attributes */
426   pSecAttr = SHLWAPI_356(&sAttr, &sd);
427
428   if (!(hRet = CreateSemaphoreW(pSecAttr , iInitial, MAXLONG, szBuff)))
429     hRet = OpenSemaphoreW(SYNCHRONIZE|SEMAPHORE_MODIFY_STATE, 0, szBuff);
430   return hRet;
431 }
432
433 /*************************************************************************
434  *      @       [SHLWAPI.422]
435  *
436  * Create a semaphore.
437  *
438  * PARAMS
439  *  lpszName [I] Name of semaphore
440  *  iInitial [I] Initial count for semaphore
441  *
442  * RETURNS
443  *  A new semaphore handle.
444  */
445 HANDLE WINAPI SHLWAPI_422(LPCSTR lpszName, DWORD iInitial)
446 {
447   WCHAR szBuff[MAX_PATH];
448
449   TRACE("(%s,%ld)\n", debugstr_a(lpszName), iInitial);
450
451   if (lpszName)
452     MultiByteToWideChar(0, 0, lpszName, -1, szBuff, MAX_PATH);
453   return SHLWAPI_423(lpszName ? szBuff : NULL, iInitial);
454 }
455
456 /*************************************************************************
457  *      @       [SHLWAPI.222]
458  *
459  * Create a semaphore using the name of a GUID.
460  *
461  * PARAMS
462  *  guid [I] GUID to use as semaphore name
463  *
464  * RETURNS
465  *  A handle to the semaphore.
466  *
467  * NOTES
468  *  The initial count of the semaphore is set to 0.
469  */
470 HANDLE WINAPI SHLWAPI_222(REFGUID guid)
471 {
472   char szName[40];
473
474   TRACE("(%s)\n", debugstr_guid(guid));
475
476   /* Create a named semaphore using the GUID string */
477   SHLWAPI_23(guid, szName, sizeof(szName) - 1);
478   return SHLWAPI_422(szName, 0);
479 }