ReactOS 0.4.16-dev-927-g467dec4
SHGetComputerDisplayNameW.cpp File Reference
#include "shelltest.h"
#include <lmserver.h>
#include <undocshell.h>
#include <strsafe.h>
#include <versionhelpers.h>
Include dependency graph for SHGetComputerDisplayNameW.cpp:

Go to the source code of this file.

Macros

#define COMPUTER_DESCRIPTIONS_KEY    L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ComputerDescriptions"
 

Typedefs

typedef HRESULT(WINAPIFN_SHGetComputerDisplayNameW) (PWSTR, DWORD, PWSTR, DWORD)
 
typedef NET_API_STATUS(WINAPIFN_NetServerGetInfo) (LPWSTR, DWORD, PBYTE *)
 
typedef NET_API_STATUS(WINAPIFN_NetApiBufferFree) (PVOID)
 

Functions

static PCWSTR SHELL_SkipServerSlashes (_In_ PCWSTR pszPath)
 
static VOID SHELL_CacheComputerDescription (_In_ PCWSTR pszServerName, _In_ PCWSTR pszDesc)
 
static HRESULT SHELL_GetCachedComputerDescription (_Out_writes_z_(cchDescMax) PWSTR pszDesc, _In_ DWORD cchDescMax, _In_ PCWSTR pszServerName)
 
static HRESULT SHELL_BuildDisplayMachineName (_Out_writes_z_(cchNameMax) PWSTR pszName, _In_ DWORD cchNameMax, _In_ PCWSTR pszServerName, _In_ PCWSTR pszDescription)
 
static VOID TEST_SHGetComputerDisplayNameW (VOID)
 
 START_TEST (SHGetComputerDisplayNameW)
 

Variables

static FN_SHGetComputerDisplayNameW s_pSHGetComputerDisplayNameW = NULL
 
static FN_NetServerGetInfo s_pNetServerGetInfo = NULL
 
static FN_NetApiBufferFree s_pNetApiBufferFree = NULL
 

Macro Definition Documentation

◆ COMPUTER_DESCRIPTIONS_KEY

#define COMPUTER_DESCRIPTIONS_KEY    L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ComputerDescriptions"

Definition at line 22 of file SHGetComputerDisplayNameW.cpp.

Typedef Documentation

◆ FN_NetApiBufferFree

typedef NET_API_STATUS(WINAPI * FN_NetApiBufferFree) (PVOID)

Definition at line 16 of file SHGetComputerDisplayNameW.cpp.

◆ FN_NetServerGetInfo

typedef NET_API_STATUS(WINAPI * FN_NetServerGetInfo) (LPWSTR, DWORD, PBYTE *)

Definition at line 15 of file SHGetComputerDisplayNameW.cpp.

◆ FN_SHGetComputerDisplayNameW

typedef HRESULT(WINAPI * FN_SHGetComputerDisplayNameW) (PWSTR, DWORD, PWSTR, DWORD)

Definition at line 14 of file SHGetComputerDisplayNameW.cpp.

Function Documentation

◆ SHELL_BuildDisplayMachineName()

static HRESULT SHELL_BuildDisplayMachineName ( _Out_writes_z_(cchNameMax) PWSTR  pszName,
_In_ DWORD  cchNameMax,
_In_ PCWSTR  pszServerName,
_In_ PCWSTR  pszDescription 
)
static

Definition at line 61 of file SHGetComputerDisplayNameW.cpp.

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}
static PCWSTR SHELL_SkipServerSlashes(_In_ PCWSTR pszPath)
char * va_list
Definition: acmsvcex.h:78
#define E_FAIL
Definition: ddrawi.h:102
DWORD WINAPI FormatMessageW(DWORD dwFlags, LPCVOID lpSource, DWORD dwMessageId, DWORD dwLanguageId, LPWSTR lpBuffer, DWORD nSize, __ms_va_list *args)
Definition: format_msg.c:583
#define S_OK
Definition: intsafe.h:52
#define L(x)
Definition: ntvdm.h:50
@ REST_ALLOWCOMMENTTOGGLE
Definition: shlobj.h:1879
DWORD WINAPI SHRestricted(RESTRICTIONS rest)
Definition: shpolicy.c:150
Definition: match.c:390
const uint16_t * PCWSTR
Definition: typedefs.h:57
#define FORMAT_MESSAGE_FROM_STRING
Definition: winbase.h:447
#define FORMAT_MESSAGE_ARGUMENT_ARRAY
Definition: winbase.h:450

Referenced by TEST_SHGetComputerDisplayNameW().

◆ SHELL_CacheComputerDescription()

static VOID SHELL_CacheComputerDescription ( _In_ PCWSTR  pszServerName,
_In_ PCWSTR  pszDesc 
)
static

Definition at line 36 of file SHGetComputerDisplayNameW.cpp.

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}
#define COMPUTER_DESCRIPTIONS_KEY
DWORD WINAPI SHSetValueW(HKEY hKey, LPCWSTR lpszSubKey, LPCWSTR lpszValue, DWORD dwType, LPCVOID pvData, DWORD cbData)
Definition: reg.c:1306
unsigned long DWORD
Definition: ntddk_ex.h:95
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define REG_SZ
Definition: layer.c:22
ULONG_PTR SIZE_T
Definition: typedefs.h:80
#define HKEY_CURRENT_USER
Definition: winreg.h:11
__wchar_t WCHAR
Definition: xmlstorage.h:180

Referenced by TEST_SHGetComputerDisplayNameW().

◆ SHELL_GetCachedComputerDescription()

static HRESULT SHELL_GetCachedComputerDescription ( _Out_writes_z_(cchDescMax) PWSTR  pszDesc,
_In_ DWORD  cchDescMax,
_In_ PCWSTR  pszServerName 
)
static

Definition at line 49 of file SHGetComputerDisplayNameW.cpp.

53{
54 cchDescMax *= sizeof(WCHAR);
56 SHELL_SkipServerSlashes(pszServerName), NULL, pszDesc, &cchDescMax);
58}
#define NULL
Definition: types.h:112
DWORD WINAPI SHGetValueW(HKEY hKey, LPCWSTR lpszSubKey, LPCWSTR lpszValue, LPDWORD pwType, LPVOID pvData, LPDWORD pcbData)
Definition: reg.c:1236
#define error(str)
Definition: mkdosfs.c:1605
#define HRESULT_FROM_WIN32(x)
Definition: winerror.h:92

Referenced by TEST_SHGetComputerDisplayNameW().

◆ SHELL_SkipServerSlashes()

static PCWSTR SHELL_SkipServerSlashes ( _In_ PCWSTR  pszPath)
static

Definition at line 26 of file SHGetComputerDisplayNameW.cpp.

28{
29 PCWSTR pch;
30 for (pch = pszPath; *pch == L'\\'; ++pch)
31 ;
32 return pch;
33}
#define pch(ap)
Definition: match.c:418

Referenced by SHELL_BuildDisplayMachineName(), SHELL_CacheComputerDescription(), and SHELL_GetCachedComputerDescription().

◆ START_TEST()

START_TEST ( SHGetComputerDisplayNameW  )

Definition at line 123 of file SHGetComputerDisplayNameW.cpp.

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 FN_SHGetComputerDisplayNameW s_pSHGetComputerDisplayNameW
static FN_NetServerGetInfo s_pNetServerGetInfo
HRESULT(WINAPI * FN_SHGetComputerDisplayNameW)(PWSTR, DWORD, PWSTR, DWORD)
NET_API_STATUS(WINAPI * FN_NetApiBufferFree)(PVOID)
static FN_NetApiBufferFree s_pNetApiBufferFree
NET_API_STATUS(WINAPI * FN_NetServerGetInfo)(LPWSTR, DWORD, PBYTE *)
#define skip(...)
Definition: atltest.h:64
#define GetProcAddress(x, y)
Definition: compat.h:753
#define FreeLibrary(x)
Definition: compat.h:748
#define LoadLibraryW(x)
Definition: compat.h:747
HMODULE WINAPI GetModuleHandleW(LPCWSTR lpModuleName)
Definition: loader.c:838
static HMODULE hShell32
Definition: string.c:34
VERSIONHELPERAPI IsWindowsVistaOrGreater()
#define MAKEINTRESOURCEA(i)
Definition: winuser.h:581

◆ TEST_SHGetComputerDisplayNameW()

static VOID TEST_SHGetComputerDisplayNameW ( VOID  )
static

Definition at line 77 of file SHGetComputerDisplayNameW.cpp.

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}
static HRESULT SHELL_BuildDisplayMachineName(_Out_writes_z_(cchNameMax) PWSTR pszName, _In_ DWORD cchNameMax, _In_ PCWSTR pszServerName, _In_ PCWSTR pszDescription)
static VOID SHELL_CacheComputerDescription(_In_ PCWSTR pszServerName, _In_ PCWSTR pszDesc)
static HRESULT SHELL_GetCachedComputerDescription(_Out_writes_z_(cchDescMax) PWSTR pszDesc, _In_ DWORD cchDescMax, _In_ PCWSTR pszServerName)
#define ok_hex(expression, result)
Definition: atltest.h:94
#define trace
Definition: atltest.h:70
#define ok_wstr(x, y)
Definition: atltest.h:130
#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 ERROR_SUCCESS
Definition: deptool.c:10
static LSTATUS(WINAPI *pRegDeleteTreeW)(HKEY
#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 MAX_PATH
Definition: compat.h:34
unsigned int BOOL
Definition: ntddk_ex.h:94
FxAutoRegKey hKey
#define FAILED(hr)
Definition: intsafe.h:51
#define wine_dbgstr_w
Definition: kernel32.h:34
#define KEY_WRITE
Definition: nt_native.h:1031
#define UNICODE_NULL
static const WCHAR szName[]
Definition: powrprof.c:45
HRESULT hr
Definition: shlfolder.c:183
#define _countof(array)
Definition: sndvol32.h:70
STRSAFEAPI StringCchCopyW(STRSAFE_LPWSTR pszDest, size_t cchDest, STRSAFE_LPCWSTR pszSrc)
Definition: strsafe.h:149
#define SHGCDN_NOCACHE
Definition: undocshell.h:969
int ret
#define MAX_COMPUTERNAME_LENGTH
Definition: winbase.h:269

Referenced by START_TEST().

Variable Documentation

◆ s_pNetApiBufferFree

FN_NetApiBufferFree s_pNetApiBufferFree = NULL
static

Definition at line 20 of file SHGetComputerDisplayNameW.cpp.

Referenced by START_TEST().

◆ s_pNetServerGetInfo

FN_NetServerGetInfo s_pNetServerGetInfo = NULL
static

Definition at line 19 of file SHGetComputerDisplayNameW.cpp.

Referenced by START_TEST().

◆ s_pSHGetComputerDisplayNameW

FN_SHGetComputerDisplayNameW s_pSHGetComputerDisplayNameW = NULL
static

Definition at line 18 of file SHGetComputerDisplayNameW.cpp.

Referenced by START_TEST(), and TEST_SHGetComputerDisplayNameW().