[CLT2012]
[reactos.git] / base / applications / dxdiag / network.c
1 /*
2 * PROJECT: ReactX Diagnosis Application
3 * LICENSE: LGPL - See COPYING in the top level directory
4 * FILE: base/applications/dxdiag/dxdiag.c
5 * PURPOSE: ReactX diagnosis network page
6 * COPYRIGHT: Copyright 2008 Johannes Anderwald
7 *
8 */
9
10 #include "precomp.h"
11
12 typedef struct
13 {
14 WCHAR Guid[40];
15 UINT ResourceID;
16 }DIRECTPLAY_GUID;
17
18 typedef struct _LANGANDCODEPAGE_
19 {
20 WORD lang;
21 WORD code;
22 } LANGANDCODEPAGE, *LPLANGANDCODEPAGE;
23
24 static DIRECTPLAY_GUID DirectPlay8SP[] =
25 {
26 {
27 L"{6D4A3650-628D-11D2-AE0F-006097B01411}",
28 IDS_DIRECTPLAY8_MODEMSP
29 },
30 {
31 L"{743B5D60-628D-11D2-AE0F-006097B01411}",
32 IDS_DIRECTPLAY8_SERIALSP
33 },
34 {
35 L"{53934290-628D-11D2-AE0F-006097B01411}",
36 IDS_DIRECTPLAY8_IPXSP
37 },
38 {
39 L"{EBFE7BA0-628D-11D2-AE0F-006097B01411}",
40 IDS_DIRECTPLAY8_TCPSP
41 }
42 };
43
44 static DIRECTPLAY_GUID DirectPlaySP[] =
45 {
46 {
47 L"{36E95EE0-8577-11cf-960C-0080C7534E82}",
48 IDS_DIRECTPLAY_TCPCONN
49 },
50 {
51 L"685BC400-9D2C-11cf-A9CD-00AA006886E3",
52 IDS_DIRECTPLAY_IPXCONN
53 },
54 {
55 L"{44EAA760-CB68-11cf-9C4E-00A0C905425E}",
56 IDS_DIRECTPLAY_MODEMCONN
57 },
58 {
59 L"{0F1D6860-88D9-11cf-9C4E-00A0C905425E}",
60 IDS_DIRECTPLAY_SERIALCONN
61 }
62 };
63
64 static
65 VOID
66 InitListViewColumns(HWND hDlgCtrl)
67 {
68 WCHAR szText[256];
69 LVCOLUMNW lvcolumn;
70 INT Index;
71
72 ZeroMemory(&lvcolumn, sizeof(LVCOLUMNW));
73 lvcolumn.pszText = szText;
74 lvcolumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH;
75 lvcolumn.fmt = LVCFMT_LEFT;
76 lvcolumn.cx = 200;
77
78 for(Index = 0; Index < 4; Index++)
79 {
80 szText[0] = L'\0';
81 LoadStringW(hInst, IDS_DIRECTPLAY_COL_NAME1 + Index, szText, sizeof(szText) / sizeof(WCHAR));
82 szText[(sizeof(szText) / sizeof(WCHAR))-1] = L'\0';
83 if (Index)
84 lvcolumn.cx = 98;
85 if (SendMessageW(hDlgCtrl, LVM_INSERTCOLUMNW, Index, (LPARAM)&lvcolumn) == -1)
86 return;
87 }
88 }
89
90 UINT
91 FindProviderIndex(LPCWSTR szGuid, DIRECTPLAY_GUID * PreDefProviders)
92 {
93 UINT Index;
94 for(Index = 0; Index < 4; Index++)
95 {
96 if (!wcsncmp(PreDefProviders[Index].Guid, szGuid, 40))
97 return Index;
98 }
99 return UINT_MAX;
100 }
101
102 BOOL
103 GetFileVersion(LPCWSTR szAppName, WCHAR * szVer, DWORD szVerSize)
104 {
105 UINT VerSize;
106 DWORD DummyHandle;
107 LPVOID pBuf;
108 WORD lang = 0;
109 WORD code = 0;
110 LPLANGANDCODEPAGE lplangcode;
111 WCHAR szBuffer[100];
112 WCHAR * pResult;
113 BOOL bResult = FALSE;
114 BOOL bVer;
115
116 static const WCHAR wFormat[] = L"\\StringFileInfo\\%04x%04x\\FileVersion";
117 static const WCHAR wTranslation[] = L"VarFileInfo\\Translation";
118
119 /* query version info size */
120 VerSize = GetFileVersionInfoSizeW(szAppName, &DummyHandle);
121 if (!VerSize)
122 return FALSE;
123
124
125 /* allocate buffer */
126 pBuf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, VerSize);
127 if (!pBuf)
128 return FALSE;
129
130
131 /* query version info */
132 if(!GetFileVersionInfoW(szAppName, 0, VerSize, pBuf))
133 {
134 HeapFree(GetProcessHeap(), 0, pBuf);
135 return FALSE;
136 }
137
138 /* query lang code */
139 if(VerQueryValueW(pBuf, wTranslation, (LPVOID *)&lplangcode, &VerSize))
140 {
141 /* FIXME find language from current locale / if not available,
142 * default to english
143 * for now default to first available language
144 */
145 lang = lplangcode->lang;
146 code = lplangcode->code;
147 }
148 /* set up format */
149 wsprintfW(szBuffer, wFormat, lang, code);
150 /* query manufacturer */
151 pResult = NULL;
152 bVer = VerQueryValueW(pBuf, szBuffer, (LPVOID *)&pResult, &VerSize);
153
154 if (VerSize < szVerSize && bVer && pResult)
155 {
156 wcscpy(szVer, pResult);
157 pResult = wcschr(szVer, L' ');
158 if (pResult)
159 {
160 /* cut off build info */
161 VerSize = (pResult - szVer);
162 }
163 if (GetLocaleInfoW(MAKELCID(lang, SORT_DEFAULT), LOCALE_SLANGUAGE, &szVer[VerSize], szVerSize-VerSize))
164 {
165 szVer[VerSize-1] = L' ';
166 szVer[szVerSize-1] = L'\0';
167 }
168 bResult = TRUE;
169 }
170
171 HeapFree(GetProcessHeap(), 0, pBuf);
172 return bResult;
173 }
174
175 static
176 BOOL
177 EnumerateServiceProviders(HKEY hKey, HWND hDlgCtrl, DIRECTPLAY_GUID * PreDefProviders)
178 {
179 DWORD dwIndex = 0;
180 LONG result;
181 WCHAR szName[50];
182 WCHAR szGUID[40];
183 WCHAR szTemp[63];
184 WCHAR szResult[MAX_PATH+20] = {0};
185 DWORD RegProviders = 0;
186 DWORD ProviderIndex;
187 DWORD dwName;
188 LVITEMW Item;
189 INT ItemCount;
190 LRESULT lResult;
191
192
193 ItemCount = ListView_GetItemCount(hDlgCtrl);
194 ZeroMemory(&Item, sizeof(LVITEMW));
195 Item.mask = LVIF_TEXT;
196 Item.pszText = szResult;
197 Item.iItem = ItemCount;
198 /* insert all predefined items first */
199 for(dwIndex = 0; dwIndex < 4; dwIndex++)
200 {
201 Item.iItem = ItemCount + dwIndex;
202 Item.iSubItem = 0;
203 szResult[0] = L'\0';
204 LoadStringW(hInst, PreDefProviders[dwIndex].ResourceID, szResult, sizeof(szResult)/sizeof(WCHAR));
205 szResult[(sizeof(szResult)/sizeof(WCHAR))-1] = L'\0';
206 Item.iItem = SendMessageW(hDlgCtrl, LVM_INSERTITEM, 0, (LPARAM)&Item);
207 Item.iSubItem = 1;
208 szResult[0] = L'\0';
209 LoadStringW(hInst, IDS_REG_FAIL, szResult, sizeof(szResult)/sizeof(WCHAR));
210 szResult[(sizeof(szResult)/sizeof(WCHAR))-1] = L'\0';
211 SendMessageW(hDlgCtrl, LVM_SETITEM, 0, (LPARAM)&Item);
212 }
213
214 dwIndex = 0;
215 do
216 {
217 dwName = sizeof(szName) / sizeof(WCHAR);
218 result = RegEnumKeyEx(hKey, dwIndex, szName, &dwName, NULL, NULL, NULL, NULL);
219 if (result == ERROR_SUCCESS)
220 {
221 szName[(sizeof(szName)/sizeof(WCHAR))-1] = L'\0';
222
223 ProviderIndex = UINT_MAX;
224 if (GetRegValue(hKey, szName, L"GUID", REG_SZ, szGUID, sizeof(szGUID)))
225 ProviderIndex = FindProviderIndex(szGUID, PreDefProviders);
226
227 if (ProviderIndex == UINT_MAX)
228 {
229 /* a custom service provider was found */
230 Item.iItem = ListView_GetItemCount(hDlgCtrl);
231
232 /* FIXME
233 * on Windows Vista we need to use RegLoadMUIString which is not available for older systems
234 */
235 if (!GetRegValue(hKey, szName, L"Friendly Name", REG_SZ, szResult, sizeof(szResult)))
236 if (!GetRegValue(hKey, szName, L"DescriptionW", REG_SZ, szResult, sizeof(szResult)))
237 szResult[0] = L'\0';
238
239 /* insert the new provider */
240 Item.iSubItem = 0;
241 lResult = SendMessageW(hDlgCtrl, LVM_INSERTITEM, 0, (LPARAM)&Item);
242 /* adjust index */
243 ProviderIndex = lResult - ItemCount;
244 }
245
246 szResult[0] = L'\0';
247 /* check if the 'Path' key is available */
248 if (!GetRegValue(hKey, szName, L"Path", REG_SZ, szResult, sizeof(szResult)))
249 {
250 /* retrieve the path by lookup the CLSID */
251 wcscpy(szTemp, L"CLSID\\");
252 wcscpy(&szTemp[6], szGUID);
253 wcscpy(&szTemp[44], L"\\InProcServer32");
254 if (!GetRegValue(HKEY_CLASSES_ROOT, szTemp, NULL, REG_SZ, szResult, sizeof(szResult)))
255 {
256 szResult[0] = L'\0';
257 ProviderIndex = UINT_MAX;
258 }
259 }
260 if (szResult[0])
261 {
262 /* insert path name */
263 Item.iSubItem = 2;
264 Item.iItem = ProviderIndex + ItemCount;
265 SendMessageW(hDlgCtrl, LVM_SETITEM, 0, (LPARAM)&Item);
266 /* retrieve file version */
267 if (!GetFileVersion(szResult, szTemp, sizeof(szTemp)/sizeof(WCHAR)))
268 {
269 szTemp[0] = L'\0';
270 LoadStringW(hInst, IDS_VERSION_UNKNOWN, szTemp, sizeof(szTemp)/sizeof(WCHAR));
271 szTemp[(sizeof(szTemp)/sizeof(WCHAR))-1] = L'\0';
272 }
273 Item.iSubItem = 3;
274 Item.pszText = szTemp;
275 SendMessageW(hDlgCtrl, LVM_SETITEM, 0, (LPARAM)&Item);
276 Item.pszText = szResult;
277 }
278
279 if (ProviderIndex != UINT_MAX)
280 {
281 RegProviders |= (1 << ProviderIndex);
282 szResult[0] = L'\0';
283 LoadStringW(hInst, IDS_REG_SUCCESS, szResult, sizeof(szResult));
284 Item.iSubItem = 1;
285
286 Item.iItem = ProviderIndex + ItemCount;
287 szResult[(sizeof(szResult)/sizeof(WCHAR))-1] = L'\0';
288 SendMessageW(hDlgCtrl, LVM_SETITEM, 0, (LPARAM)&Item);
289 }
290 }
291 dwIndex++;
292 }while(result != ERROR_NO_MORE_ITEMS);
293
294 /* check if all providers have been registered */
295 // if (RegProviders == 15)
296 return TRUE;
297 return FALSE;
298 }
299
300
301
302 static
303 void
304 InitializeDirectPlayDialog(HWND hwndDlg)
305 {
306 HKEY hKey;
307 LONG result;
308 HWND hDlgCtrl;
309
310 /* open DirectPlay8 key */
311 result = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\DirectPlay8\\Service Providers", 0, KEY_READ, &hKey);
312 if (result != ERROR_SUCCESS)
313 return;
314
315 hDlgCtrl = GetDlgItem(hwndDlg, IDC_LIST_PROVIDER);
316 /* initialize list ctrl */
317 InitListViewColumns(hDlgCtrl);
318
319 /* enumerate providers */
320 result = EnumerateServiceProviders(hKey, hDlgCtrl, DirectPlay8SP);
321 RegCloseKey(hKey);
322 if (!result)
323 return;
324
325 /* open DirectPlay key */
326 result = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\DirectPlay\\Service Providers", 0, KEY_READ, &hKey);
327 if (result != ERROR_SUCCESS)
328 return;
329
330 /* enumerate providers */
331 EnumerateServiceProviders(hKey, hDlgCtrl, DirectPlaySP);
332 RegCloseKey(hKey);
333 }
334
335 INT_PTR CALLBACK
336 NetworkPageWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
337 {
338 UNREFERENCED_PARAMETER(lParam);
339 UNREFERENCED_PARAMETER(wParam);
340 switch (message) {
341 case WM_INITDIALOG:
342 {
343 SetWindowPos(hDlg, NULL, 10, 32, 0, 0, SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOSIZE | SWP_NOZORDER);
344 InitializeDirectPlayDialog(hDlg);
345 return TRUE;
346 }
347 }
348
349 return FALSE;
350 }