Make acpi compile with Visual C++
[reactos.git] / reactos / 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 lResult = SendMessageW(hDlgCtrl, LVM_INSERTITEM, 0, (LPARAM)&Item);
207 szResult[0] = L'\0';
208 LoadStringW(hInst, IDS_REG_FAIL, szResult, sizeof(szResult)/sizeof(WCHAR));
209 szResult[(sizeof(szResult)/sizeof(WCHAR))-1] = L'\0';
210 Item.iItem = lResult;
211 Item.iSubItem = 1;
212 lResult = SendMessageW(hDlgCtrl, LVM_SETITEM, 0, (LPARAM)&Item);
213 }
214
215 dwIndex = 0;
216 do
217 {
218 dwName = sizeof(szName) / sizeof(WCHAR);
219 result = RegEnumKeyEx(hKey, dwIndex, szName, &dwName, NULL, NULL, NULL, NULL);
220 if (result == ERROR_SUCCESS)
221 {
222 szName[(sizeof(szName)/sizeof(WCHAR))-1] = L'\0';
223
224 ProviderIndex = UINT_MAX;
225 if (GetRegValue(hKey, szName, L"GUID", REG_SZ, szGUID, sizeof(szGUID)))
226 ProviderIndex = FindProviderIndex(szGUID, PreDefProviders);
227
228 if (ProviderIndex == UINT_MAX)
229 {
230 /* a custom service provider was found */
231 lResult = ListView_GetItemCount(hDlgCtrl);
232 Item.iItem = lResult;
233
234 /* FIXME
235 * on Windows Vista we need to use RegLoadMUIString which is not available for older systems
236 */
237 if (!GetRegValue(hKey, szName, L"Friendly Name", REG_SZ, szResult, sizeof(szResult)))
238 if (!GetRegValue(hKey, szName, L"DescriptionW", REG_SZ, szResult, sizeof(szResult)))
239 szResult[0] = L'\0';
240
241 /* insert the new provider */
242 Item.iSubItem = 0;
243 lResult = SendMessageW(hDlgCtrl, LVM_INSERTITEM, 0, (LPARAM)&Item);
244 /* adjust index */
245 ProviderIndex = lResult - ItemCount;
246 }
247
248 szResult[0] = L'\0';
249 /* check if the 'Path' key is available */
250 if (!GetRegValue(hKey, szName, L"Path", REG_SZ, szResult, sizeof(szResult)))
251 {
252 /* retrieve the path by lookup the CLSID */
253 wcscpy(szTemp, L"CLSID\\");
254 wcscpy(&szTemp[6], szGUID);
255 wcscpy(&szTemp[44], L"\\InProcServer32");
256 if (!GetRegValue(HKEY_CLASSES_ROOT, szTemp, NULL, REG_SZ, szResult, sizeof(szResult)))
257 {
258 szResult[0] = L'\0';
259 ProviderIndex = UINT_MAX;
260 }
261 }
262 if (szResult[0])
263 {
264 /* insert path name */
265 Item.iSubItem = 2;
266 Item.iItem = ProviderIndex + ItemCount;
267 SendMessageW(hDlgCtrl, LVM_SETITEM, 0, (LPARAM)&Item);
268 /* retrieve file version */
269 if (!GetFileVersion(szResult, szTemp, sizeof(szTemp)/sizeof(WCHAR)))
270 {
271 szTemp[0] = L'\0';
272 LoadStringW(hInst, IDS_VERSION_UNKNOWN, szTemp, sizeof(szTemp)/sizeof(WCHAR));
273 szTemp[(sizeof(szTemp)/sizeof(WCHAR))-1] = L'\0';
274 }
275 Item.iSubItem = 3;
276 Item.pszText = szTemp;
277 SendMessageW(hDlgCtrl, LVM_SETITEM, 0, (LPARAM)&Item);
278 Item.pszText = szResult;
279 }
280
281 if (ProviderIndex != UINT_MAX)
282 {
283 RegProviders |= (1 << ProviderIndex);
284 szResult[0] = L'\0';
285 LoadStringW(hInst, IDS_REG_SUCCESS, szResult, sizeof(szResult));
286 Item.iSubItem = 1;
287
288 Item.iItem = ProviderIndex + ItemCount;
289 szResult[(sizeof(szResult)/sizeof(WCHAR))-1] = L'\0';
290 SendMessageW(hDlgCtrl, LVM_SETITEM, 0, (LPARAM)&Item);
291 }
292 }
293 dwIndex++;
294 }while(result != ERROR_NO_MORE_ITEMS);
295
296 /* check if all providers have been registered */
297 // if (RegProviders == 15)
298 return TRUE;
299 return FALSE;
300 }
301
302
303
304 static
305 void
306 InitializeDirectPlayDialog(HWND hwndDlg)
307 {
308 HKEY hKey;
309 LONG result;
310 HWND hDlgCtrl;
311
312 /* open DirectPlay8 key */
313 result = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\DirectPlay8\\Service Providers", 0, KEY_READ, &hKey);
314 if (result != ERROR_SUCCESS)
315 return;
316
317 hDlgCtrl = GetDlgItem(hwndDlg, IDC_LIST_PROVIDER);
318 /* initialize list ctrl */
319 InitListViewColumns(hDlgCtrl);
320
321 /* enumerate providers */
322 result = EnumerateServiceProviders(hKey, hDlgCtrl, DirectPlay8SP);
323 RegCloseKey(hKey);
324 if (!result)
325 return;
326
327 /* open DirectPlay key */
328 result = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\DirectPlay\\Service Providers", 0, KEY_READ, &hKey);
329 if (result != ERROR_SUCCESS)
330 return;
331
332 /* enumerate providers */
333 result = EnumerateServiceProviders(hKey, hDlgCtrl, DirectPlaySP);
334 RegCloseKey(hKey);
335 }
336
337 INT_PTR CALLBACK
338 NetworkPageWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
339 {
340 UNREFERENCED_PARAMETER(lParam);
341 UNREFERENCED_PARAMETER(wParam);
342 switch (message) {
343 case WM_INITDIALOG:
344 {
345 SetWindowPos(hDlg, NULL, 10, 32, 0, 0, SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOSIZE | SWP_NOZORDER);
346 InitializeDirectPlayDialog(hDlg);
347 return TRUE;
348 }
349 }
350
351 return FALSE;
352 }