ReactOS 0.4.17-dev-258-gb1f0478
AssocQueryKey.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS api tests
3 * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4 * PURPOSE: Tests for AssocQueryKey
5 * COPYRIGHT: Copyright 2026 Katayama Hirofumi MZ (katayama.hirofumi.mz@gmail.com)
6 */
7
8#include <apitest.h>
9#include <windef.h>
10#include <shlwapi.h>
11#include <pseh/pseh2.h>
12
14{
20
22{
26
27// ntdll!NtQueryKey can get the key path from HKEY
30
32{
33 HMODULE hNTDLL = GetModuleHandleW(L"ntdll.dll");
34 if (!hNTDLL) return FALSE;
35 g_NtQueryKey = (FN_NtQueryKey)GetProcAddress(hNTDLL, "NtQueryKey");
36 return g_NtQueryKey != NULL;
37}
38
39// Get path of HKEY
41{
42 if (!g_NtQueryKey || !hKey)
43 return NULL;
44
45 ULONG needed = 0;
47
48 if (!needed)
49 return NULL;
50
51 SIZE_T cb = needed + sizeof(WCHAR);
52 PBYTE buf = (PBYTE)_alloca(cb);
54
55 ULONG returned = 0;
57
58 if (status < 0)
59 return NULL;
60
62 return StrDupW(info->Name); // needs LocalFree
63}
64
65// Test ASSOCKEYs
66static void TEST_AssocKeys(void)
67{
68 {
69 HKEY hKey = NULL;
71 ok_hr(hr, S_OK);
72
74 if (hKey)
76
77 ok(path &&
78 (StrStrIW(path, L"\\REGISTRY\\MACHINE\\") || StrStrIW(path, L"\\REGISTRY\\USER\\")) &&
79 StrStrIW(path, L"regfile"),
80 "path was %s\n", wine_dbgstr_w(path));
82 }
83
84 {
85 HKEY hKey = NULL;
87 ok_hr(hr, S_OK);
88
90 if (hKey)
92
94 L"\\REGISTRY\\MACHINE\\SOFTWARE\\Classes\\Applications\\regedit.exe"),
95 "path was %s\n", wine_dbgstr_w(path));
97 }
98
99 {
100 HKEY hKey = NULL;
102 ok_hr(hr, S_OK);
103
105 if (hKey)
107
108 ok(path &&
109 (StrStrIW(path, L"\\REGISTRY\\MACHINE\\") || StrStrIW(path, L"\\REGISTRY\\USER\\")) &&
110 StrStrIW(path, L"Classes\\regfile"),
111 "path was %s\n", wine_dbgstr_w(path));
113 }
114
115 {
116 HKEY hKey = NULL;
118 ok_hr(hr, S_OK);
119
121 if (hKey)
123
124 ok(path && !_wcsicmp(path, L"\\REGISTRY\\MACHINE\\SOFTWARE\\Classes\\exefile"),
125 "path was %s\n", wine_dbgstr_w(path));
127 }
128}
129
130// Test ASSOCF flags
131static void TEST_AssocF_Flags(void)
132{
133 const ASSOCF cases[] =
134 {
148 };
149
150 for (size_t i = 0; i < _countof(cases); ++i)
151 {
152 HKEY hKey = NULL;
153 HRESULT hr = AssocQueryKeyW(cases[i], ASSOCKEY_CLASS, L".reg", NULL, &hKey);
154 ok_hr(hr, S_OK);
155
157 if (hKey)
159
160 ok(path &&
161 (StrStrIW(path, L"\\REGISTRY\\MACHINE\\") || StrStrIW(path, L"\\REGISTRY\\USER\\")),
162 "path was %s\n", wine_dbgstr_w(path));
164 }
165}
166
167// Test pszAssoc
168static void TEST_PszAssoc(void)
169{
170 const wchar_t* exts[] =
171 {
172 L".txt", L".htm", L".reg", L".xml", L".png", L".jpg", L".zip"
173 };
174
175 for (size_t i = 0; i < _countof(exts); ++i)
176 {
177 HKEY hKey = NULL;
179 ok_hr(hr, S_OK);
180
182 if (hKey)
184
185 ok(path &&
186 (StrStrIW(path, L"\\REGISTRY\\MACHINE\\") || StrStrIW(path, L"\\REGISTRY\\USER\\")),
187 "path was %s\n", wine_dbgstr_w(path));
189 }
190
191 // Direct ProgID
192 {
193 HKEY hKey = NULL;
195 ok_hr(hr, S_OK);
196
198 if (hKey)
200
201 ok(path &&
202 (StrStrIW(path, L"\\REGISTRY\\MACHINE\\") || StrStrIW(path, L"\\REGISTRY\\USER\\")) &&
203 StrStrIW(path, L"regfile"),
204 "path was %s\n", wine_dbgstr_w(path));
206 }
207 {
208 HKEY hKey = NULL;
210 ok_hr(hr, S_OK);
211
213 if (hKey)
215
216 ok(path && !_wcsicmp(path, L"\\REGISTRY\\MACHINE\\SOFTWARE\\Classes\\exefile"),
217 "path was %s\n", wine_dbgstr_w(path));
219 }
220
221 // ProgID: InternetShortcut
222 {
223 HKEY hKey = NULL;
224 HRESULT hr = AssocQueryKeyW(ASSOCF_NONE, ASSOCKEY_CLASS, L"InternetShortcut", NULL, &hKey);
225 ok_hr(hr, S_OK);
226 if (hKey)
228 }
229}
230
231// Test invalid arguments
232static void TEST_InvalidArgs(void)
233{
234 // phkeyOut == NULL
235 {
236 BOOL threw = FALSE;
238
240 {
242 }
244 {
245 hr = 0xDEADFACE;
246 threw = TRUE;
247 }
248 _SEH2_END;
249
250 ok_hr(hr, 0xDEADFACE);
251 ok_int(threw, TRUE);
252 }
253
254 // pszAssoc == NULL
255 {
256 BOOL threw = FALSE;
258 HKEY hKey = NULL;
259
261 {
263 }
265 {
266 hr = 0xDEADFACE;
267 threw = TRUE;
268 }
269 _SEH2_END;
270
271 ok(hKey == NULL, "hKey was not NULL\n");
272 if (hKey)
274
276 ok_int(threw, FALSE);
277 }
278
279 // pszAssoc == ""
280 {
281 HKEY hKey = NULL;
283 if (hKey)
286 }
287
288 // Invalid ASSOCKEY value
289 {
290 HKEY hKey = NULL;
291 BOOL threw = FALSE;
293
295 {
296 hr = AssocQueryKeyW(ASSOCF_NONE, (ASSOCKEY)0xFFFF, L".txt", NULL, &hKey);
297 }
299 {
300 hr = 0xDEADFACE;
301 threw = TRUE;
302 }
303 _SEH2_END;
304
305 ok(hKey == NULL, "hKey was not NULL\n");
306 if (hKey)
308
310 ok_int(threw, FALSE);
311 }
312}
313
314static void TEST_ByExeName(void)
315{
316 WCHAR notepadPath[MAX_PATH];
317 GetSystemDirectoryW(notepadPath, MAX_PATH);
318 lstrcatW(notepadPath, L"\\notepad.exe");
319
320 // Full path
321 {
322 HKEY hKey = NULL;
324
326 if (hKey)
328
329 ok_hr(hr, S_OK);
330
331 ok(path &&
332 !_wcsicmp(path, L"\\REGISTRY\\MACHINE\\SOFTWARE\\Classes\\Applications\\notepad.exe"),
333 "path was %s\n", wine_dbgstr_w(path));
335 }
336
337 // Short name
338 {
339 HKEY hKey = NULL;
341 L"regedit.exe", NULL, &hKey);
343 if (hKey)
345 ok_hr(hr, S_OK);
346
347 ok(path &&
348 !_wcsicmp(path, L"\\REGISTRY\\MACHINE\\SOFTWARE\\Classes\\Applications\\regedit.exe"),
349 "path was %s\n", wine_dbgstr_w(path));
351 }
352
353 // Non existent
354 {
355 HKEY hKey = NULL;
357 L"__ghost__.exe", NULL, &hKey);
358 ok(hKey == NULL, "hKey was not NULL\n");
359 if (hKey)
361 ok(FAILED(hr), "hr was 0x%08lX\n", hr);
362 }
363}
364
365// Check ASSOCKEY_SHELLEXECCLASS
366static void TEST_ShellExecClass(void)
367{
368 static const LPCWSTR cases[] = { L".txt", L".htm", L".html", L"txtfile" };
369 for (size_t i = 0; i < _countof(cases); ++i)
370 {
371 HKEY hKey = NULL;
373 ok_hr(hr, S_OK);
374
376 if (hKey)
378
379 ok(path && StrStrIW(path, L"\\REGISTRY\\MACHINE\\SOFTWARE\\Classes\\"),
380 "path was %s\n", wine_dbgstr_w(path));
382 }
383}
384
386{
387 if (!InitNtQueryKey())
388 {
389 skip("NtQueryKey not found\n");
390 return;
391 }
392
393 HRESULT hrCoInit = CoInitialize(NULL);
394
401
402 if (SUCCEEDED(hrCoInit))
404}
#define ok_hr(status, expected)
Definition: ACListISF.cpp:31
static BOOL InitNtQueryKey(VOID)
Definition: AssocQueryKey.c:31
static void TEST_ByExeName(void)
static FN_NtQueryKey g_NtQueryKey
Definition: AssocQueryKey.c:29
static void TEST_ShellExecClass(void)
struct _MY_KEY_NAME_INFORMATION * PMY_KEY_NAME_INFORMATION
struct _MY_KEY_NAME_INFORMATION MY_KEY_NAME_INFORMATION
_MY_KEY_INFORMATION_CLASS
Definition: AssocQueryKey.c:14
@ MyKeyNodeInformation
Definition: AssocQueryKey.c:16
@ MyKeyNameInformation
Definition: AssocQueryKey.c:18
@ MyKeyBasicInformation
Definition: AssocQueryKey.c:15
@ MyKeyFullInformation
Definition: AssocQueryKey.c:17
static void TEST_PszAssoc(void)
static void TEST_AssocKeys(void)
Definition: AssocQueryKey.c:66
static void TEST_AssocF_Flags(void)
enum _MY_KEY_INFORMATION_CLASS MY_KEY_INFORMATION_CLASS
static LPWSTR GetKeyPath(HKEY hKey)
Definition: AssocQueryKey.c:40
NTSTATUS(__stdcall * FN_NtQueryKey)(HANDLE, MY_KEY_INFORMATION_CLASS, PVOID, ULONG, PULONG)
Definition: AssocQueryKey.c:28
static void TEST_InvalidArgs(void)
#define ok(value,...)
Definition: atltest.h:57
#define skip(...)
Definition: atltest.h:64
#define START_TEST(x)
Definition: atltest.h:75
#define ok_int(expression, result)
Definition: atltest.h:134
LONG NTSTATUS
Definition: precomp.h:26
#define RegCloseKey(hKey)
Definition: registry.h:49
HRESULT hr
Definition: delayimp.cpp:582
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define NTSTATUS
Definition: precomp.h:19
void WINAPI DECLSPEC_HOTPATCH CoUninitialize(void)
Definition: combase.c:2842
LPWSTR WINAPI StrStrIW(LPCWSTR lpszStr, LPCWSTR lpszSearch)
Definition: string.c:380
#define ERROR_INVALID_PARAMETER
Definition: compat.h:101
#define GetProcAddress(x, y)
Definition: compat.h:753
#define MAX_PATH
Definition: compat.h:34
HMODULE WINAPI GetModuleHandleW(LPCWSTR lpModuleName)
Definition: loader.c:838
UINT WINAPI GetSystemDirectoryW(OUT LPWSTR lpBuffer, IN UINT uSize)
Definition: path.c:2232
WCHAR *WINAPI StrDupW(const WCHAR *str)
Definition: string.c:313
static MonoProfilerRuntimeShutdownBeginCallback cb
Definition: metahost.c:118
#define __stdcall
Definition: corecrt.h:120
_ACRTIMP int __cdecl _wcsicmp(const wchar_t *, const wchar_t *)
Definition: wcs.c:159
HRESULT WINAPI CoInitialize(LPVOID lpReserved)
Definition: compobj.c:531
HRESULT WINAPI AssocQueryKeyW(ASSOCF cfFlags, ASSOCKEY assockey, LPCWSTR pszAssoc, LPCWSTR pszExtra, HKEY *phkeyOut)
Definition: assoc.c:369
#define L(x)
Definition: resources.c:13
unsigned int BOOL
Definition: ntddk_ex.h:94
FxAutoRegKey hKey
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1594
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:90
#define S_OK
Definition: intsafe.h:52
#define SUCCEEDED(hr)
Definition: intsafe.h:50
#define FAILED(hr)
Definition: intsafe.h:51
#define wine_dbgstr_w
Definition: kernel32.h:34
LPWSTR WINAPI lstrcatW(LPWSTR lpString1, LPCWSTR lpString2)
Definition: lstring.c:274
#define ZeroMemory
Definition: minwinbase.h:31
static const struct encodedExtensions exts[]
Definition: encode.c:2738
BYTE * PBYTE
Definition: pedump.c:66
short WCHAR
Definition: pedump.c:58
@ ASSOCF_REMAPRUNDLL
Definition: shlwapi.h:867
@ ASSOCF_NOFIXUPS
Definition: shlwapi.h:868
@ ASSOCF_INIT_DEFAULTTOSTAR
Definition: shlwapi.h:862
@ ASSOCF_INIT_DEFAULTTOFOLDER
Definition: shlwapi.h:863
@ ASSOCF_IGNOREBASECLASS
Definition: shlwapi.h:869
@ ASSOCF_INIT_NOREMAPCLSID
Definition: shlwapi.h:859
@ ASSOCF_VERIFY
Definition: shlwapi.h:866
@ ASSOCF_INIT_IGNOREUNKNOWN
Definition: shlwapi.h:870
@ ASSOCF_NOUSERSETTINGS
Definition: shlwapi.h:864
@ ASSOCF_OPEN_BYEXENAME
Definition: shlwapi.h:861
@ ASSOCF_NONE
Definition: shlwapi.h:858
@ ASSOCF_NOTRUNCATE
Definition: shlwapi.h:865
@ ASSOCF_INIT_BYEXENAME
Definition: shlwapi.h:860
ASSOCKEY
Definition: shlwapi.h:918
@ ASSOCKEY_CLASS
Definition: shlwapi.h:921
@ ASSOCKEY_SHELLEXECCLASS
Definition: shlwapi.h:919
@ ASSOCKEY_APP
Definition: shlwapi.h:920
#define AssocQueryKey
Definition: shlwapi.h:1016
DWORD ASSOCF
Definition: shlwapi.h:967
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:104
#define _SEH2_END
Definition: pseh2_64.h:194
#define _SEH2_TRY
Definition: pseh2_64.h:93
#define _countof(array)
Definition: sndvol32.h:70
Definition: ps.c:97
uint32_t * PULONG
Definition: typedefs.h:59
const uint16_t * LPCWSTR
Definition: typedefs.h:57
uint16_t * LPWSTR
Definition: typedefs.h:56
void * PVOID
Definition: typedefs.h:50
PVOID HANDLE
Definition: typedefs.h:73
ULONG_PTR SIZE_T
Definition: typedefs.h:80
uint32_t ULONG
Definition: typedefs.h:59
static HRESULT HRESULT_FROM_WIN32(unsigned int x)
Definition: winerror.h:210
#define E_UNEXPECTED
Definition: winerror.h:3528