kernel32: Fix compilation warnings in 64-bit mode.
[wine] / dlls / kernel32 / actctx.c
1 /*
2  * Activation contexts
3  *
4  * Copyright 2004 Jon Griffiths
5  * Copyright 2007 Eric Pouech
6  * Copyright 2007 Jacek Caban for CodeWeavers
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <stdarg.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winerror.h"
30 #include "winnls.h"
31 #include "wine/debug.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(actctx);
34
35
36 #define ACTCTX_FLAGS_ALL (\
37  ACTCTX_FLAG_PROCESSOR_ARCHITECTURE_VALID |\
38  ACTCTX_FLAG_LANGID_VALID |\
39  ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID |\
40  ACTCTX_FLAG_RESOURCE_NAME_VALID |\
41  ACTCTX_FLAG_SET_PROCESS_DEFAULT |\
42  ACTCTX_FLAG_APPLICATION_NAME_VALID |\
43  ACTCTX_FLAG_SOURCE_IS_ASSEMBLYREF |\
44  ACTCTX_FLAG_HMODULE_VALID )
45
46 #define ACTCTX_FAKE_HANDLE ((HANDLE) 0xf00baa)
47 #define ACTCTX_FAKE_COOKIE ((ULONG_PTR) 0xf00bad)
48
49 #define ACTCTX_MAGIC       0xC07E3E11
50
51 struct actctx
52 {
53     ULONG               magic;
54     LONG                ref_count;
55 };
56
57 /***********************************************************************
58  * CreateActCtxA (KERNEL32.@)
59  *
60  * Create an activation context.
61  */
62 HANDLE WINAPI CreateActCtxA(PCACTCTXA pActCtx)
63 {
64     ACTCTXW     actw;
65     SIZE_T      len;
66     HANDLE      ret = INVALID_HANDLE_VALUE;
67     LPWSTR      src = NULL, assdir = NULL, resname = NULL, appname = NULL;
68
69     TRACE("%p %08x\n", pActCtx, pActCtx ? pActCtx->dwFlags : 0);
70
71     if (!pActCtx || pActCtx->cbSize != sizeof(*pActCtx) ||
72         (pActCtx->dwFlags & ~ACTCTX_FLAGS_ALL))
73     {
74         SetLastError(ERROR_INVALID_PARAMETER);
75         return INVALID_HANDLE_VALUE;
76     }
77
78     actw.cbSize = sizeof(actw);
79     actw.dwFlags = pActCtx->dwFlags;
80     if (pActCtx->lpSource)
81     {
82         len = MultiByteToWideChar(CP_ACP, 0, pActCtx->lpSource, -1, NULL, 0);
83         src = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
84         if (!src) return INVALID_HANDLE_VALUE;
85         MultiByteToWideChar(CP_ACP, 0, pActCtx->lpSource, -1, src, len);
86     }
87     actw.lpSource = src;
88
89     if (actw.dwFlags & ACTCTX_FLAG_PROCESSOR_ARCHITECTURE_VALID)
90         actw.wProcessorArchitecture = pActCtx->wProcessorArchitecture;
91     if (actw.dwFlags & ACTCTX_FLAG_LANGID_VALID)
92         actw.wLangId = pActCtx->wLangId;
93     if (actw.dwFlags & ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID)
94     {
95         len = MultiByteToWideChar(CP_ACP, 0, pActCtx->lpAssemblyDirectory, -1, NULL, 0);
96         assdir = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
97         if (!assdir) goto done;
98         MultiByteToWideChar(CP_ACP, 0, pActCtx->lpAssemblyDirectory, -1, assdir, len);
99         actw.lpAssemblyDirectory = assdir;
100     }
101     if (actw.dwFlags & ACTCTX_FLAG_RESOURCE_NAME_VALID)
102     {
103         if ((ULONG_PTR)pActCtx->lpResourceName >> 16)
104         {
105             len = MultiByteToWideChar(CP_ACP, 0, pActCtx->lpResourceName, -1, NULL, 0);
106             resname = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
107             if (!resname) goto done;
108             MultiByteToWideChar(CP_ACP, 0, pActCtx->lpResourceName, -1, resname, len);
109             actw.lpResourceName = resname;
110         }
111         else actw.lpResourceName = (LPWSTR)pActCtx->lpResourceName;
112     }
113     if (actw.dwFlags & ACTCTX_FLAG_APPLICATION_NAME_VALID)
114     {
115         len = MultiByteToWideChar(CP_ACP, 0, pActCtx->lpApplicationName, -1, NULL, 0);
116         appname = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
117         if (!appname) goto done;
118         MultiByteToWideChar(CP_ACP, 0, pActCtx->lpApplicationName, -1, appname, len);
119         actw.lpApplicationName = appname;
120     }
121     if (actw.dwFlags & ACTCTX_FLAG_HMODULE_VALID)
122         actw.hModule = pActCtx->hModule;
123
124     ret = CreateActCtxW(&actw);
125
126 done:
127     HeapFree(GetProcessHeap(), 0, src);
128     HeapFree(GetProcessHeap(), 0, assdir);
129     HeapFree(GetProcessHeap(), 0, resname);
130     HeapFree(GetProcessHeap(), 0, appname);
131     return ret;
132 }
133
134 /***********************************************************************
135  * CreateActCtxW (KERNEL32.@)
136  *
137  * Create an activation context.
138  */
139 HANDLE WINAPI CreateActCtxW(PCACTCTXW pActCtx)
140 {
141     struct actctx*      actctx;
142     DWORD               ret = ERROR_SUCCESS;
143
144     TRACE("%p %08x\n", pActCtx, pActCtx ? pActCtx->dwFlags : 0);
145
146     if (!pActCtx || pActCtx->cbSize != sizeof(*pActCtx) ||
147         (pActCtx->dwFlags & ~ACTCTX_FLAGS_ALL))
148     {
149         SetLastError(ERROR_INVALID_PARAMETER);
150         return INVALID_HANDLE_VALUE;
151     }
152     actctx = HeapAlloc(GetProcessHeap(), 0, sizeof(*actctx));
153     if (!actctx) return INVALID_HANDLE_VALUE;
154
155     actctx->magic = ACTCTX_MAGIC;
156     actctx->ref_count = 1;
157
158     if (ret == ERROR_SUCCESS)
159     {
160         return (HANDLE)actctx;
161     }
162
163     ReleaseActCtx((HANDLE)actctx);
164     SetLastError(ret);
165     return INVALID_HANDLE_VALUE;
166 }
167
168 static struct actctx* check_actctx(HANDLE h)
169 {
170     struct actctx*      actctx = (struct actctx*)h;
171
172     switch (actctx->magic)
173     {
174     case ACTCTX_MAGIC: return actctx;
175     default:
176         SetLastError(ERROR_INVALID_HANDLE);
177         return NULL;
178     }
179 }
180
181 /***********************************************************************
182  * ActivateActCtx (KERNEL32.@)
183  *
184  * Activate an activation context.
185  */
186 BOOL WINAPI ActivateActCtx(HANDLE hActCtx, ULONG_PTR *ulCookie)
187 {
188   static BOOL reported = FALSE;
189
190   if (reported)
191     TRACE("%p %p\n", hActCtx, ulCookie);
192   else
193   {
194     FIXME("%p %p\n", hActCtx, ulCookie);
195     reported = TRUE;
196   }
197
198   if (ulCookie)
199     *ulCookie = ACTCTX_FAKE_COOKIE;
200   return TRUE;
201 }
202
203 /***********************************************************************
204  * DeactivateActCtx (KERNEL32.@)
205  *
206  * Deactivate an activation context.
207  */
208 BOOL WINAPI DeactivateActCtx(DWORD dwFlags, ULONG_PTR ulCookie)
209 {
210   static BOOL reported = FALSE;
211
212   if (reported)
213     TRACE("%08x %08lx\n", dwFlags, ulCookie);
214   else
215   {
216     FIXME("%08x %08lx\n", dwFlags, ulCookie);
217     reported = TRUE;
218   }
219
220   if (ulCookie != ACTCTX_FAKE_COOKIE)
221     return FALSE;
222   return TRUE;
223 }
224
225 /***********************************************************************
226  * GetCurrentActCtx (KERNEL32.@)
227  *
228  * Get the current activation context.
229  */
230 BOOL WINAPI GetCurrentActCtx(HANDLE* phActCtx)
231 {
232   FIXME("%p\n", phActCtx);
233   *phActCtx = ACTCTX_FAKE_HANDLE;
234   return TRUE;
235 }
236
237 /***********************************************************************
238  * AddRefActCtx (KERNEL32.@)
239  *
240  * Add a reference to an activation context.
241  */
242 void WINAPI AddRefActCtx(HANDLE hActCtx)
243 {
244     struct actctx*      actctx;
245
246     TRACE("%p\n", hActCtx);
247
248     if ((actctx = check_actctx(hActCtx)))
249         InterlockedIncrement( &actctx->ref_count );
250 }
251
252 /***********************************************************************
253  * ReleaseActCtx (KERNEL32.@)
254  *
255  * Release a reference to an activation context.
256  */
257 void WINAPI ReleaseActCtx(HANDLE hActCtx)
258 {
259     struct actctx*      actctx;
260
261     TRACE("%p\n", hActCtx);
262
263     if ((actctx = check_actctx(hActCtx)))
264     {
265         if (!InterlockedDecrement( &actctx->ref_count ))
266         {
267             actctx->magic = 0;
268             HeapFree(GetProcessHeap(), 0, actctx);
269         }
270     }
271 }
272
273 /***********************************************************************
274  * ZombifyActCtx (KERNEL32.@)
275  *
276  * Release a reference to an activation context.
277  */
278 BOOL WINAPI ZombifyActCtx(HANDLE hActCtx)
279 {
280   FIXME("%p\n", hActCtx);
281   if (hActCtx != ACTCTX_FAKE_HANDLE)
282     return FALSE;
283   return TRUE;
284 }
285
286 /***********************************************************************
287  * FindActCtxSectionStringA (KERNEL32.@)
288  *
289  * Find information about a GUID in an activation context.
290  */
291 BOOL WINAPI FindActCtxSectionStringA(DWORD dwFlags, const GUID* lpExtGuid,
292                                     ULONG ulId, LPCSTR lpSearchStr,
293                                     PACTCTX_SECTION_KEYED_DATA pInfo)
294 {
295   FIXME("%08x %s %u %s %p\n", dwFlags, debugstr_guid(lpExtGuid),
296        ulId, debugstr_a(lpSearchStr), pInfo);
297   SetLastError( ERROR_CALL_NOT_IMPLEMENTED);
298   return FALSE;
299 }
300
301 /***********************************************************************
302  * FindActCtxSectionStringW (KERNEL32.@)
303  *
304  * Find information about a GUID in an activation context.
305  */
306 BOOL WINAPI FindActCtxSectionStringW(DWORD dwFlags, const GUID* lpExtGuid,
307                                     ULONG ulId, LPCWSTR lpSearchStr,
308                                     PACTCTX_SECTION_KEYED_DATA pInfo)
309 {
310   FIXME("%08x %s %u %s %p\n", dwFlags, debugstr_guid(lpExtGuid),
311         ulId, debugstr_w(lpSearchStr), pInfo);
312
313   if (lpExtGuid)
314   {
315     FIXME("expected lpExtGuid == NULL\n");
316     SetLastError(ERROR_INVALID_PARAMETER);
317     return FALSE;
318   }
319
320   if (dwFlags & ~FIND_ACTCTX_SECTION_KEY_RETURN_HACTCTX)
321   {
322     FIXME("unknown dwFlags %08x\n", dwFlags);
323     SetLastError(ERROR_INVALID_PARAMETER);
324     return FALSE;
325   }
326
327   if (!pInfo || pInfo->cbSize < sizeof (ACTCTX_SECTION_KEYED_DATA))
328   {
329     SetLastError(ERROR_INVALID_PARAMETER);
330     return FALSE;
331   }
332
333   pInfo->ulDataFormatVersion = 1;
334   pInfo->lpData = NULL;
335   pInfo->lpSectionGlobalData = NULL;
336   pInfo->ulSectionGlobalDataLength = 0;
337   pInfo->lpSectionBase = NULL;
338   pInfo->ulSectionTotalLength = 0;
339   pInfo->hActCtx = ACTCTX_FAKE_HANDLE;
340   pInfo->ulAssemblyRosterIndex = 0;
341
342   return TRUE;
343 }
344
345 /***********************************************************************
346  * FindActCtxSectionGuid (KERNEL32.@)
347  *
348  * Find information about a GUID in an activation context.
349  */
350 BOOL WINAPI FindActCtxSectionGuid(DWORD dwFlags, const GUID* lpExtGuid,
351                                   ULONG ulId, const GUID* lpSearchGuid,
352                                   PACTCTX_SECTION_KEYED_DATA pInfo)
353 {
354   FIXME("%08x %s %u %s %p\n", dwFlags, debugstr_guid(lpExtGuid),
355        ulId, debugstr_guid(lpSearchGuid), pInfo);
356   SetLastError( ERROR_CALL_NOT_IMPLEMENTED);
357   return FALSE;
358 }
359
360 /***********************************************************************
361  * QueryActCtxW (KERNEL32.@)
362  *
363  * Get information about an activation context.
364  */
365 BOOL WINAPI QueryActCtxW(DWORD dwFlags, HANDLE hActCtx, PVOID pvSubInst,
366                          ULONG ulClass, PVOID pvBuff, SIZE_T cbBuff,
367                          SIZE_T *pcbLen)
368 {
369   FIXME("%08x %p %p %u %p %ld %p\n", dwFlags, hActCtx,
370        pvSubInst, ulClass, pvBuff, cbBuff, pcbLen);
371   /* this makes Adobe Photoshop 7.0 happy */
372   SetLastError( ERROR_CALL_NOT_IMPLEMENTED);
373   return FALSE;
374 }