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