sync with trunk (r46275)
[reactos.git] / dll / win32 / kernel32 / misc / actctx.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: dll/win32/kernel32/misc/actctx.c
5 * PURPOSE: Activation contexts
6 * PROGRAMMERS: Jacek Caban for CodeWeavers
7 * Eric Pouech
8 * Jon Griffiths
9 * Dmitry Chapyshev (dmitry@reactos.org)
10 * Samuel SerapiĆ³n
11 */
12
13 /* synched with wine 1.1.26 */
14
15 #include <k32.h>
16
17 #include "wine/debug.h"
18
19 WINE_DEFAULT_DEBUG_CHANNEL(actctx);
20
21 #define ACTCTX_FAKE_HANDLE ((HANDLE) 0xf00baa)
22
23 /***********************************************************************
24 * CreateActCtxA (KERNEL32.@)
25 *
26 * Create an activation context.
27 */
28 HANDLE WINAPI CreateActCtxA(PCACTCTXA pActCtx)
29 {
30 ACTCTXW actw;
31 SIZE_T len;
32 HANDLE ret = INVALID_HANDLE_VALUE;
33 LPWSTR src = NULL, assdir = NULL, resname = NULL, appname = NULL;
34
35 TRACE("%p %08x\n", pActCtx, pActCtx ? pActCtx->dwFlags : 0);
36
37 if (!pActCtx || pActCtx->cbSize != sizeof(*pActCtx))
38 {
39 SetLastError(ERROR_INVALID_PARAMETER);
40 return INVALID_HANDLE_VALUE;
41 }
42
43 actw.cbSize = sizeof(actw);
44 actw.dwFlags = pActCtx->dwFlags;
45 if (pActCtx->lpSource)
46 {
47 len = MultiByteToWideChar(CP_ACP, 0, pActCtx->lpSource, -1, NULL, 0);
48 src = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
49 if (!src) return INVALID_HANDLE_VALUE;
50 MultiByteToWideChar(CP_ACP, 0, pActCtx->lpSource, -1, src, len);
51 }
52 actw.lpSource = src;
53
54 if (actw.dwFlags & ACTCTX_FLAG_PROCESSOR_ARCHITECTURE_VALID)
55 actw.wProcessorArchitecture = pActCtx->wProcessorArchitecture;
56 if (actw.dwFlags & ACTCTX_FLAG_LANGID_VALID)
57 actw.wLangId = pActCtx->wLangId;
58 if (actw.dwFlags & ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID)
59 {
60 len = MultiByteToWideChar(CP_ACP, 0, pActCtx->lpAssemblyDirectory, -1, NULL, 0);
61 assdir = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
62 if (!assdir) goto done;
63 MultiByteToWideChar(CP_ACP, 0, pActCtx->lpAssemblyDirectory, -1, assdir, len);
64 actw.lpAssemblyDirectory = assdir;
65 }
66 if (actw.dwFlags & ACTCTX_FLAG_RESOURCE_NAME_VALID)
67 {
68 if ((ULONG_PTR)pActCtx->lpResourceName >> 16)
69 {
70 len = MultiByteToWideChar(CP_ACP, 0, pActCtx->lpResourceName, -1, NULL, 0);
71 resname = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
72 if (!resname) goto done;
73 MultiByteToWideChar(CP_ACP, 0, pActCtx->lpResourceName, -1, resname, len);
74 actw.lpResourceName = resname;
75 }
76 else actw.lpResourceName = (LPCWSTR)pActCtx->lpResourceName;
77 }
78 if (actw.dwFlags & ACTCTX_FLAG_APPLICATION_NAME_VALID)
79 {
80 len = MultiByteToWideChar(CP_ACP, 0, pActCtx->lpApplicationName, -1, NULL, 0);
81 appname = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
82 if (!appname) goto done;
83 MultiByteToWideChar(CP_ACP, 0, pActCtx->lpApplicationName, -1, appname, len);
84 actw.lpApplicationName = appname;
85 }
86 if (actw.dwFlags & ACTCTX_FLAG_HMODULE_VALID)
87 actw.hModule = pActCtx->hModule;
88
89 ret = CreateActCtxW(&actw);
90
91 done:
92 HeapFree(GetProcessHeap(), 0, src);
93 HeapFree(GetProcessHeap(), 0, assdir);
94 HeapFree(GetProcessHeap(), 0, resname);
95 HeapFree(GetProcessHeap(), 0, appname);
96 return ret;
97 }
98
99 /***********************************************************************
100 * CreateActCtxW (KERNEL32.@)
101 *
102 * Create an activation context.
103 */
104 HANDLE WINAPI CreateActCtxW(PCACTCTXW pActCtx)
105 {
106 NTSTATUS status;
107 HANDLE hActCtx;
108
109 TRACE("%p %08x\n", pActCtx, pActCtx ? pActCtx->dwFlags : 0);
110
111 if ((status = RtlCreateActivationContext(&hActCtx, (PVOID*)pActCtx)))
112 {
113 SetLastError(RtlNtStatusToDosError(status));
114 return INVALID_HANDLE_VALUE;
115 }
116 return hActCtx;
117 }
118
119 /***********************************************************************
120 * ActivateActCtx (KERNEL32.@)
121 *
122 * Activate an activation context.
123 */
124 BOOL WINAPI ActivateActCtx(HANDLE hActCtx, ULONG_PTR *ulCookie)
125 {
126 NTSTATUS status;
127
128 if ((status = RtlActivateActivationContext( 0, hActCtx, ulCookie )))
129 {
130 SetLastError(RtlNtStatusToDosError(status));
131 return FALSE;
132 }
133 return TRUE;
134 }
135
136 /***********************************************************************
137 * DeactivateActCtx (KERNEL32.@)
138 *
139 * Deactivate an activation context.
140 */
141 BOOL WINAPI DeactivateActCtx(DWORD dwFlags, ULONG_PTR ulCookie)
142 {
143 RtlDeactivateActivationContext( dwFlags, ulCookie );
144 return TRUE;
145 }
146
147 /***********************************************************************
148 * GetCurrentActCtx (KERNEL32.@)
149 *
150 * Get the current activation context.
151 */
152 BOOL WINAPI GetCurrentActCtx(HANDLE* phActCtx)
153 {
154 NTSTATUS status;
155
156 if ((status = RtlGetActiveActivationContext(phActCtx)))
157 {
158 SetLastError(RtlNtStatusToDosError(status));
159 return FALSE;
160 }
161 return TRUE;
162 }
163
164 /***********************************************************************
165 * AddRefActCtx (KERNEL32.@)
166 *
167 * Add a reference to an activation context.
168 */
169 void WINAPI AddRefActCtx(HANDLE hActCtx)
170 {
171 RtlAddRefActivationContext(hActCtx);
172 }
173
174 /***********************************************************************
175 * ReleaseActCtx (KERNEL32.@)
176 *
177 * Release a reference to an activation context.
178 */
179 void WINAPI ReleaseActCtx(HANDLE hActCtx)
180 {
181 RtlReleaseActivationContext(hActCtx);
182 }
183
184 /***********************************************************************
185 * ZombifyActCtx (KERNEL32.@)
186 *
187 * Release a reference to an activation context.
188 */
189 BOOL WINAPI ZombifyActCtx(HANDLE hActCtx)
190 {
191 FIXME("%p\n", hActCtx);
192 if (hActCtx != ACTCTX_FAKE_HANDLE)
193 return FALSE;
194 return TRUE;
195 }
196
197 /***********************************************************************
198 * FindActCtxSectionStringA (KERNEL32.@)
199 *
200 * Find information about a string in an activation context.
201 */
202 BOOL WINAPI FindActCtxSectionStringA(DWORD dwFlags, const GUID* lpExtGuid,
203 ULONG ulId, LPCSTR lpSearchStr,
204 PACTCTX_SECTION_KEYED_DATA pInfo)
205 {
206 LPWSTR search_str;
207 DWORD len;
208 BOOL ret;
209
210 TRACE("%08x %s %u %s %p\n", dwFlags, debugstr_guid(lpExtGuid),
211 ulId, debugstr_a(lpSearchStr), pInfo);
212
213 if (!lpSearchStr)
214 {
215 SetLastError(ERROR_INVALID_PARAMETER);
216 return FALSE;
217 }
218
219 len = MultiByteToWideChar(CP_ACP, 0, lpSearchStr, -1, NULL, 0);
220 search_str = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
221 MultiByteToWideChar(CP_ACP, 0, lpSearchStr, -1, search_str, len);
222
223 ret = FindActCtxSectionStringW(dwFlags, lpExtGuid, ulId, search_str, pInfo);
224
225 HeapFree(GetProcessHeap(), 0, search_str);
226 return ret;
227 }
228
229 /***********************************************************************
230 * FindActCtxSectionStringW (KERNEL32.@)
231 *
232 * Find information about a string in an activation context.
233 */
234 BOOL WINAPI FindActCtxSectionStringW(DWORD dwFlags, const GUID* lpExtGuid,
235 ULONG ulId, LPCWSTR lpSearchStr,
236 PACTCTX_SECTION_KEYED_DATA pInfo)
237 {
238 UNICODE_STRING us;
239 NTSTATUS status;
240
241 RtlInitUnicodeString(&us, lpSearchStr);
242 if ((status = RtlFindActivationContextSectionString(dwFlags, lpExtGuid, ulId, &us, pInfo)))
243 {
244 SetLastError(RtlNtStatusToDosError(status));
245 return FALSE;
246 }
247 return TRUE;
248 }
249
250 /***********************************************************************
251 * FindActCtxSectionGuid (KERNEL32.@)
252 *
253 * Find information about a GUID in an activation context.
254 */
255 BOOL WINAPI FindActCtxSectionGuid(DWORD dwFlags, const GUID* lpExtGuid,
256 ULONG ulId, const GUID* lpSearchGuid,
257 PACTCTX_SECTION_KEYED_DATA pInfo)
258 {
259 FIXME("%08x %s %u %s %p\n", dwFlags, debugstr_guid(lpExtGuid),
260 ulId, debugstr_guid(lpSearchGuid), pInfo);
261 SetLastError( ERROR_CALL_NOT_IMPLEMENTED);
262 return FALSE;
263 }
264
265 /***********************************************************************
266 * QueryActCtxW (KERNEL32.@)
267 *
268 * Get information about an activation context.
269 */
270 BOOL WINAPI QueryActCtxW(DWORD dwFlags, HANDLE hActCtx, PVOID pvSubInst,
271 ULONG ulClass, PVOID pvBuff, SIZE_T cbBuff,
272 SIZE_T *pcbLen)
273 {
274 NTSTATUS status;
275
276 if ((status = RtlQueryInformationActivationContext( dwFlags, hActCtx, pvSubInst, ulClass,
277 pvBuff, cbBuff, pcbLen )))
278 {
279 SetLastError(RtlNtStatusToDosError(status));
280 return FALSE;
281 }
282 return TRUE;
283 }