ReactOS 0.4.16-dev-1056-gbe87e00
SHGetComputerDisplayNameW.cpp
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS API Tests
3 * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4 * PURPOSE: Tests for SHGetComputerDisplayNameW
5 * COPYRIGHT: Copyright 2025 Katayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com>
6 */
7
8#include "shelltest.h"
9#include <lmserver.h>
10#include <undocshell.h>
11#include <strsafe.h>
12#include <versionhelpers.h>
13
17
21
22#define COMPUTER_DESCRIPTIONS_KEY \
23 L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ComputerDescriptions"
24
25static PCWSTR
27 _In_ PCWSTR pszPath)
28{
29 PCWSTR pch;
30 for (pch = pszPath; *pch == L'\\'; ++pch)
31 ;
32 return pch;
33}
34
35static VOID
37 _In_ PCWSTR pszServerName,
38 _In_ PCWSTR pszDesc)
39{
40 if (!pszDesc)
41 return;
42
43 SIZE_T cbDesc = (wcslen(pszDesc) + 1) * sizeof(WCHAR);
45 SHELL_SkipServerSlashes(pszServerName), REG_SZ, pszDesc, (DWORD)cbDesc);
46}
47
48static HRESULT
50 _Out_writes_z_(cchDescMax) PWSTR pszDesc,
51 _In_ DWORD cchDescMax,
52 _In_ PCWSTR pszServerName)
53{
54 cchDescMax *= sizeof(WCHAR);
56 SHELL_SkipServerSlashes(pszServerName), NULL, pszDesc, &cchDescMax);
58}
59
60static HRESULT
62 _Out_writes_z_(cchNameMax) PWSTR pszName,
63 _In_ DWORD cchNameMax,
64 _In_ PCWSTR pszServerName,
65 _In_ PCWSTR pszDescription)
66{
67 if (!pszDescription || !*pszDescription)
68 return E_FAIL;
69
70 PCWSTR pszFormat = (SHRestricted(REST_ALLOWCOMMENTTOGGLE) ? L"%2 (%1)" : L"%1 (%2)");
71 PCWSTR args[] = { pszDescription , SHELL_SkipServerSlashes(pszServerName) };
73 pszFormat, 0, 0, pszName, cchNameMax, (va_list *)args) ? S_OK : E_FAIL);
74}
75
76static VOID
78{
79 WCHAR szCompName[MAX_COMPUTERNAME_LENGTH + 1], szDesc[256], szDisplayName[MAX_PATH];
80 WCHAR szName[MAX_PATH], szServerName[] = L"DummyServerName";
81
82 DWORD cchCompName = _countof(szCompName);
83 BOOL ret = GetComputerNameW(szCompName, &cchCompName);
84 ok_int(ret, TRUE);
85 trace("%s\n", wine_dbgstr_w(szCompName));
86
87 SHELL_CacheComputerDescription(szServerName, L"DummyDescription");
88
89 HRESULT hr = SHELL_GetCachedComputerDescription(szDesc, _countof(szDesc), szServerName);
90 if (FAILED(hr))
91 szDesc[0] = UNICODE_NULL;
92 trace("%s\n", wine_dbgstr_w(szDesc));
93
94 StringCchCopyW(szDisplayName, _countof(szDisplayName), L"@");
95 hr = s_pSHGetComputerDisplayNameW(NULL, SHGCDN_NOCACHE, szDisplayName, _countof(szDisplayName));
96 ok_hex(hr, S_OK);
97 trace("%s\n", wine_dbgstr_w(szDisplayName));
98 ok_wstr(szDisplayName, szCompName);
99
100 StringCchCopyW(szDisplayName, _countof(szDisplayName), L"@");
101 hr = s_pSHGetComputerDisplayNameW(szServerName, 0, szDisplayName, _countof(szDisplayName));
102 ok_hex(hr, S_OK);
103 trace("%s\n", wine_dbgstr_w(szServerName));
104 ok_wstr(szServerName, L"DummyServerName");
105
106 hr = SHELL_BuildDisplayMachineName(szName, _countof(szName), szServerName, szDesc);
107 ok_hex(hr, S_OK);
108
109 trace("%s\n", wine_dbgstr_w(szDisplayName));
110 trace("%s\n", wine_dbgstr_w(szName));
111 ok_wstr(szDisplayName, szName);
112
113 // Delete registry value
114 HKEY hKey;
116 if (error == ERROR_SUCCESS)
117 {
118 RegDeleteValueW(hKey, L"DummyServerName");
120 }
121}
122
124{
126 {
127 skip("Tests on Vista+ will cause exception\n");
128 return;
129 }
130
131 HINSTANCE hShell32 = GetModuleHandleW(L"shell32.dll");
135 {
136 skip("SHGetComputerDisplayNameW not found\n");
137 return;
138 }
139
140 HINSTANCE hNetApi32 = LoadLibraryW(L"netapi32.dll");
141 if (!hNetApi32)
142 {
143 skip("netapi32.dll not found\n");
144 return;
145 }
146
147 s_pNetServerGetInfo = (FN_NetServerGetInfo)GetProcAddress(hNetApi32, "NetServerGetInfo");
148 s_pNetApiBufferFree = (FN_NetApiBufferFree)GetProcAddress(hNetApi32, "NetApiBufferFree");
150 {
151 skip("NetServerGetInfo or NetApiBufferFree not found\n");
152 FreeLibrary(hNetApi32);
153 return;
154 }
155
157
158 FreeLibrary(hNetApi32);
159}
static VOID TEST_SHGetComputerDisplayNameW(VOID)
static HRESULT SHELL_BuildDisplayMachineName(_Out_writes_z_(cchNameMax) PWSTR pszName, _In_ DWORD cchNameMax, _In_ PCWSTR pszServerName, _In_ PCWSTR pszDescription)
static FN_SHGetComputerDisplayNameW s_pSHGetComputerDisplayNameW
static VOID SHELL_CacheComputerDescription(_In_ PCWSTR pszServerName, _In_ PCWSTR pszDesc)
static FN_NetServerGetInfo s_pNetServerGetInfo
HRESULT(WINAPI * FN_SHGetComputerDisplayNameW)(PWSTR, DWORD, PWSTR, DWORD)
NET_API_STATUS(WINAPI * FN_NetApiBufferFree)(PVOID)
static HRESULT SHELL_GetCachedComputerDescription(_Out_writes_z_(cchDescMax) PWSTR pszDesc, _In_ DWORD cchDescMax, _In_ PCWSTR pszServerName)
#define COMPUTER_DESCRIPTIONS_KEY
static FN_NetApiBufferFree s_pNetApiBufferFree
NET_API_STATUS(WINAPI * FN_NetServerGetInfo)(LPWSTR, DWORD, PBYTE *)
static PCWSTR SHELL_SkipServerSlashes(_In_ PCWSTR pszPath)
char * va_list
Definition: acmsvcex.h:78
#define ok_hex(expression, result)
Definition: atltest.h:94
#define trace
Definition: atltest.h:70
#define skip(...)
Definition: atltest.h:64
#define ok_wstr(x, y)
Definition: atltest.h:130
#define START_TEST(x)
Definition: atltest.h:75
#define ok_int(expression, result)
Definition: atltest.h:134
#define RegCloseKey(hKey)
Definition: registry.h:49
BOOL WINAPI GetComputerNameW(LPWSTR lpBuffer, LPDWORD lpnSize)
Definition: compname.c:446
#define E_FAIL
Definition: ddrawi.h:102
#define ERROR_SUCCESS
Definition: deptool.c:10
static LSTATUS(WINAPI *pRegDeleteTreeW)(HKEY
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
LONG WINAPI RegOpenKeyExW(HKEY hKey, LPCWSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult)
Definition: reg.c:3333
LONG WINAPI RegDeleteValueW(HKEY hKey, LPCWSTR lpValueName)
Definition: reg.c:2330
#define GetProcAddress(x, y)
Definition: compat.h:753
#define FreeLibrary(x)
Definition: compat.h:748
#define MAX_PATH
Definition: compat.h:34
#define LoadLibraryW(x)
Definition: compat.h:747
HMODULE WINAPI GetModuleHandleW(LPCWSTR lpModuleName)
Definition: loader.c:838
DWORD WINAPI FormatMessageW(DWORD dwFlags, LPCVOID lpSource, DWORD dwMessageId, DWORD dwLanguageId, LPWSTR lpBuffer, DWORD nSize, __ms_va_list *args)
Definition: format_msg.c:583
EXTERN_C HRESULT WINAPI SHGetComputerDisplayNameW(_In_opt_ PWSTR pszServerName, _In_ DWORD dwFlags, _Out_writes_z_(cchNameMax) PWSTR pszName, _In_ DWORD cchNameMax)
Definition: utils.cpp:1988
DWORD WINAPI SHGetValueW(HKEY hKey, LPCWSTR lpszSubKey, LPCWSTR lpszValue, LPDWORD pwType, LPVOID pvData, LPDWORD pcbData)
Definition: reg.c:1236
DWORD WINAPI SHSetValueW(HKEY hKey, LPCWSTR lpszSubKey, LPCWSTR lpszValue, DWORD dwType, LPCVOID pvData, DWORD cbData)
Definition: reg.c:1306
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
FxAutoRegKey hKey
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define S_OK
Definition: intsafe.h:52
#define FAILED(hr)
Definition: intsafe.h:51
#define wine_dbgstr_w
Definition: kernel32.h:34
#define REG_SZ
Definition: layer.c:22
#define NET_API_STATUS
Definition: lmcons.h:8
#define error(str)
Definition: mkdosfs.c:1605
#define pch(ap)
Definition: match.c:418
static HMODULE hShell32
Definition: string.c:34
#define _Out_writes_z_(s)
Definition: no_sal2.h:180
#define _In_
Definition: no_sal2.h:158
#define KEY_WRITE
Definition: nt_native.h:1031
#define DWORD
Definition: nt_native.h:44
#define UNICODE_NULL
#define L(x)
Definition: ntvdm.h:50
BYTE * PBYTE
Definition: pedump.c:66
static const WCHAR szName[]
Definition: powrprof.c:45
HRESULT hr
Definition: shlfolder.c:183
@ REST_ALLOWCOMMENTTOGGLE
Definition: shlobj.h:1879
DWORD WINAPI SHRestricted(RESTRICTIONS rest)
Definition: shpolicy.c:166
#define _countof(array)
Definition: sndvol32.h:70
STRSAFEAPI StringCchCopyW(STRSAFE_LPWSTR pszDest, size_t cchDest, STRSAFE_LPCWSTR pszSrc)
Definition: strsafe.h:149
Definition: match.c:390
uint16_t * PWSTR
Definition: typedefs.h:56
const uint16_t * PCWSTR
Definition: typedefs.h:57
void * PVOID
Definition: typedefs.h:50
ULONG_PTR SIZE_T
Definition: typedefs.h:80
#define SHGCDN_NOCACHE
Definition: undocshell.h:978
VERSIONHELPERAPI IsWindowsVistaOrGreater()
int ret
#define FORMAT_MESSAGE_FROM_STRING
Definition: winbase.h:447
#define FORMAT_MESSAGE_ARGUMENT_ARRAY
Definition: winbase.h:450
#define MAX_COMPUTERNAME_LENGTH
Definition: winbase.h:269
#define HRESULT
Definition: msvc.h:7
#define WINAPI
Definition: msvc.h:6
#define HRESULT_FROM_WIN32(x)
Definition: winerror.h:92
#define HKEY_CURRENT_USER
Definition: winreg.h:11
#define MAKEINTRESOURCEA(i)
Definition: winuser.h:581
__wchar_t WCHAR
Definition: xmlstorage.h:180
WCHAR * LPWSTR
Definition: xmlstorage.h:184