Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

404 lines
8.8 KiB

  1. // ICSTEST.cpp : Defines the entry point for the application.
  2. //
  3. #include "stdafx.h"
  4. #include "resource.h"
  5. #include "..\icshelper\icshelpapi.h"
  6. #include <winuser.h>
  7. #include <mmsystem.h>
  8. #include <io.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <fcntl.h>
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #define MAX_LOADSTRING 100
  15. // Global Variables:
  16. HINSTANCE hInst; // current instance
  17. TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
  18. TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
  19. DWORD hPort=0;
  20. HANDLE hAlertEvent=0;
  21. int iAlerts=0;
  22. WCHAR szAddr[4096];
  23. #ifndef ARRAYSIZE
  24. #define ARRAYSIZE(x) sizeof(x)/sizeof(x[0])
  25. #endif
  26. // Foward declarations of functions included in this code module:
  27. ATOM MyRegisterClass(HINSTANCE hInstance);
  28. BOOL InitInstance(HINSTANCE, int);
  29. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  30. LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
  31. int APIENTRY WinMain(HINSTANCE hInstance,
  32. HINSTANCE hPrevInstance,
  33. LPSTR lpCmdLine,
  34. int nCmdShow)
  35. {
  36. MSG msg;
  37. // HACCEL hAccelTable;
  38. HWND hWnd;
  39. WNDCLASSEX wcex;
  40. // Initialize global strings
  41. LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  42. LoadString(hInstance, IDC_ICSTEST, szWindowClass, MAX_LOADSTRING);
  43. wcex.cbSize = sizeof(WNDCLASSEX);
  44. wcex.style = CS_HREDRAW | CS_VREDRAW;
  45. wcex.lpfnWndProc = (WNDPROC)WndProc;
  46. wcex.cbClsExtra = 0;
  47. wcex.cbWndExtra = DLGWINDOWEXTRA;
  48. wcex.hInstance = hInstance;
  49. wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_ICSTEST);
  50. wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  51. wcex.hbrBackground = GetSysColorBrush(COLOR_BTNFACE);
  52. wcex.lpszMenuName = NULL; //(LPCSTR)IDC_ICSTEST;
  53. wcex.lpszClassName = szWindowClass;
  54. wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
  55. RegisterClassEx(&wcex);
  56. hInst = hInstance; // Store instance handle in our global variable
  57. hWnd = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_MAIN), 0, (DLGPROC)WndProc);
  58. ShowWindow(hWnd, nCmdShow);
  59. // hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_ICSTEST);
  60. // Main message loop:
  61. while (GetMessage(&msg, NULL, 0, 0))
  62. {
  63. // if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  64. {
  65. TranslateMessage(&msg);
  66. DispatchMessage(&msg);
  67. }
  68. }
  69. return msg.wParam;
  70. }
  71. void fileSpew(HANDLE hFile, WCHAR *szMessage)
  72. {
  73. if (hFile)
  74. _write((int)hFile, szMessage, (2*lstrlenW(szMessage)));
  75. }
  76. WCHAR szLogfileName[MAX_PATH];
  77. HANDLE OpenSpewFile(void)
  78. {
  79. HANDLE iDbgFileHandle;
  80. GetSystemWindowsDirectoryW(szLogfileName, sizeof(szLogfileName)/sizeof(szLogfileName[0]));
  81. lstrcatW(szLogfileName, L"\\PCHealthICStest.log");
  82. iDbgFileHandle = (HANDLE)_wopen(szLogfileName, _O_APPEND | _O_BINARY | _O_RDWR, 0);
  83. if (-1 != (int)iDbgFileHandle)
  84. {
  85. OutputDebugStringA("opened debug log file:");
  86. OutputDebugStringW(szLogfileName);
  87. OutputDebugStringA("\r\n");
  88. }
  89. else
  90. {
  91. unsigned char UniCode[2] = {0xff, 0xfe};
  92. // we must create the file
  93. OutputDebugStringA("must create debug log file");
  94. iDbgFileHandle = (HANDLE)_wopen(szLogfileName, _O_BINARY | _O_CREAT | _O_RDWR, _S_IREAD | _S_IWRITE);
  95. if (-1 != (int)iDbgFileHandle)
  96. _write((int)iDbgFileHandle, &UniCode, sizeof(UniCode));
  97. else
  98. {
  99. OutputDebugStringA("ERROR: failed to create debug log file");
  100. iDbgFileHandle = 0;
  101. }
  102. }
  103. return iDbgFileHandle;
  104. }
  105. void CloseSpewFile(HANDLE hSpew)
  106. {
  107. if (hSpew)
  108. _close((int)hSpew);
  109. }
  110. void ExecSpewFile(HANDLE hSpew)
  111. {
  112. if (hSpew)
  113. {
  114. STARTUPINFOW sui;
  115. PROCESS_INFORMATION pi;
  116. ZeroMemory(&sui, sizeof(sui));
  117. sui.cb = sizeof(sui);
  118. OutputDebugStringA("start up:");
  119. OutputDebugStringW(szLogfileName);
  120. OutputDebugStringA("\r\n");
  121. if (CreateProcessW(L"%windir%\notepad.exe", szLogfileName, NULL, NULL, FALSE, 0, NULL, NULL, &sui, &pi))
  122. {
  123. CloseHandle(pi.hProcess);
  124. CloseHandle(pi.hThread);
  125. OutputDebugStringA("started OK\r\n");
  126. }
  127. else
  128. {
  129. char foo[400];
  130. wsprintf(foo, "failed to start [%S], err=0x%x\r\n", szLogfileName, GetLastError());
  131. OutputDebugStringA(foo);
  132. }
  133. }
  134. }
  135. BOOL PrintSettings(HWND hWnd)
  136. {
  137. WCHAR scratch[2000];
  138. ICSSTAT is;
  139. HANDLE hSpew;
  140. hSpew = OpenSpewFile();
  141. fileSpew(hSpew, L"ICS test results:\r\n");
  142. // get current address list
  143. FetchAllAddresses(scratch, ARRAYSIZE(scratch));
  144. fileSpew(hSpew, scratch);
  145. // get ICS status struct
  146. is.dwSize = sizeof(is);
  147. GetIcsStatus(&is);
  148. // print connection types
  149. wcscpy(scratch, L"Connections found: ");
  150. if (is.bModemPresent)
  151. wcscat(scratch, L"Modem connection");
  152. else
  153. wcscat(scratch, L"Network (LAN) connection");
  154. if (is.bVpnPresent)
  155. wcscat(scratch, L" with VPN");
  156. wcscat(scratch, L"\r\n");
  157. fileSpew(hSpew, scratch);
  158. if (is.bIcsFound)
  159. {
  160. if (is.bIcsServer)
  161. {
  162. // this is an ICS server
  163. fileSpew(hSpew, L"Found, server on this machine\r\n");
  164. }
  165. else
  166. {
  167. // must be an ICS client
  168. fileSpew(hSpew, L"Found, server not local\r\n");
  169. }
  170. wsprintfW(scratch, L"local addr=%s\r\npublic addr=%s\r\nDLL=%s", is.wszLocAddr, is.wszPubAddr, is.wszDllName);
  171. fileSpew(hSpew, scratch);
  172. }
  173. else
  174. {
  175. fileSpew(hSpew, L"no ICS found");
  176. }
  177. CloseSpewFile(hSpew);
  178. ExecSpewFile(hSpew);
  179. return TRUE;
  180. }
  181. BOOL DisplaySettings(HWND hWnd)
  182. {
  183. WCHAR scratch[2000];
  184. ICSSTAT is;
  185. FetchAllAddresses(szAddr, ARRAYSIZE(szAddr));
  186. SetDlgItemTextW(hWnd, IDC_ADDRLIST, szAddr);
  187. is.dwSize = sizeof(is);
  188. GetIcsStatus(&is);
  189. scratch[0] = 0;
  190. if (is.bModemPresent)
  191. wcscat(scratch, L"Modem connection");
  192. else
  193. wcscat(scratch, L"Network (LAN) connection");
  194. if (is.bVpnPresent)
  195. wcscat(scratch, L" with VPN");
  196. SetDlgItemTextW(hWnd, IDC_CONN_TYPES, scratch);
  197. if (is.bIcsFound)
  198. {
  199. if (is.bIcsServer)
  200. {
  201. // this is an ICS server
  202. SetDlgItemText(hWnd, IDC_ICS_STAT, "Found, server on this machine");
  203. }
  204. else
  205. {
  206. // must be an ICS client
  207. SetDlgItemText(hWnd, IDC_ICS_STAT, "Found, server not local");
  208. }
  209. // set the addresses
  210. SetDlgItemTextW(hWnd, IDC_LOCAL_ADDR, is.wszLocAddr);
  211. SetDlgItemTextW(hWnd, IDC_PUBLIC_ADDR, is.wszPubAddr);
  212. // and the support DLL name
  213. SetDlgItemTextW(hWnd, IDC_ICS_DLL_NAME, is.wszDllName);
  214. }
  215. else
  216. {
  217. char *szNullIP = "";
  218. SetDlgItemText(hWnd, IDC_ICS_STAT, "none found");
  219. SetDlgItemText(hWnd, IDC_ICS_DLL_NAME, szNullIP);
  220. SetDlgItemText(hWnd, IDC_LOCAL_ADDR, szNullIP);
  221. SetDlgItemText(hWnd, IDC_PUBLIC_ADDR, szNullIP);
  222. }
  223. return TRUE;
  224. }
  225. //
  226. // FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
  227. //
  228. // PURPOSE: Processes messages for the main window.
  229. //
  230. // WM_COMMAND - process the application menu
  231. // WM_PAINT - Paint the main window
  232. // WM_DESTROY - post a quit message and return
  233. //
  234. //
  235. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  236. {
  237. int wmId, wmEvent;
  238. PAINTSTRUCT ps;
  239. TEXTMETRIC tm;
  240. HDC hdc;
  241. int i;
  242. switch (message)
  243. {
  244. case WM_INITDIALOG:
  245. hAlertEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  246. StartICSLib();
  247. SetAlertEvent(hAlertEvent);
  248. Sleep(250); // gives the lib time to start up...
  249. SetTimer(hWnd, 1, 1000, NULL);
  250. DisplaySettings(hWnd);
  251. break;
  252. case WM_TIMER:
  253. if (hAlertEvent)
  254. {
  255. if (WaitForSingleObjectEx(hAlertEvent, 0, FALSE) == WAIT_OBJECT_0)
  256. {
  257. FLASHWINFO fw;
  258. fw.cbSize=sizeof(fw);
  259. fw.hwnd=hWnd;
  260. fw.dwFlags=FLASHW_ALL;
  261. fw.uCount=8;
  262. fw.dwTimeout=0;
  263. DisplaySettings(hWnd);
  264. iAlerts++;
  265. FlashWindowEx(&fw);
  266. PlaySound("AddressChange", NULL, SND_ASYNC);
  267. ResetEvent(hAlertEvent);
  268. }
  269. }
  270. break;
  271. case WM_COMMAND:
  272. wmId = LOWORD(wParam);
  273. wmEvent = HIWORD(wParam);
  274. SetFocus(hWnd);
  275. // Parse the menu selections:
  276. switch (wmId)
  277. {
  278. case IDM_ABOUT:
  279. DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
  280. break;
  281. case IDM_EXIT:
  282. DestroyWindow(hWnd);
  283. break;
  284. case ID_FILE_CLOSEPORT:
  285. if (hPort)
  286. {
  287. ClosePort(hPort);
  288. DisplaySettings(hWnd);
  289. }
  290. hPort = 0;
  291. break;
  292. case ID_FILE_CLOSEALLPORTS:
  293. CloseAllOpenPorts();
  294. DisplaySettings(hWnd);
  295. hPort = 0;
  296. break;
  297. case ID_FILE_REFRESH:
  298. DisplaySettings(hWnd);
  299. break;
  300. case IDC_MYICON:
  301. PrintSettings(hWnd);
  302. break;
  303. case IDM_OPEN:
  304. if (!hPort)
  305. {
  306. hPort = OpenPort(3389);
  307. DisplaySettings(hWnd);
  308. }
  309. break;
  310. default:
  311. return DefWindowProc(hWnd, message, wParam, lParam);
  312. }
  313. break;
  314. case WM_DESTROY:
  315. KillTimer(hWnd, 1);
  316. SetAlertEvent(0);
  317. CloseHandle(hAlertEvent);
  318. hAlertEvent=0;
  319. CloseAllOpenPorts();
  320. StopICSLib();
  321. PostQuitMessage(0);
  322. break;
  323. default:
  324. return DefWindowProc(hWnd, message, wParam, lParam);
  325. }
  326. return 0;
  327. }
  328. // Mesage handler for about box.
  329. LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  330. {
  331. switch (message)
  332. {
  333. case WM_INITDIALOG:
  334. return TRUE;
  335. case WM_COMMAND:
  336. if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  337. {
  338. EndDialog(hDlg, LOWORD(wParam));
  339. return TRUE;
  340. }
  341. break;
  342. }
  343. return FALSE;
  344. }