Leaked source code of windows server 2003
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.

662 lines
23 KiB

  1. #include "trayicon.h"
  2. #include "resource.h"
  3. #include "shellapi.h"
  4. #include "windowsx.h"
  5. #include "status.h"
  6. #include "properties.h"
  7. #include "util.h"
  8. #include "stdio.h"
  9. #include "client.h"
  10. #include "InternetGatewayFinder.h"
  11. CICSTrayIcon::CICSTrayIcon()
  12. {
  13. m_pInternetGateway = NULL;
  14. m_hShowIconResult = E_FAIL;
  15. m_pDeviceFinder = NULL;
  16. m_DummySocket = INVALID_SOCKET;
  17. m_bShowingProperties = FALSE;
  18. m_bShowingStatus = FALSE;
  19. m_dwRegisterClassCookie = 0;
  20. m_bFileVersionsAccepted = FALSE;
  21. }
  22. CICSTrayIcon::~CICSTrayIcon()
  23. {
  24. }
  25. LRESULT CICSTrayIcon::OnAddBeacon(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  26. {
  27. IInternetGateway* pInternetGateway = reinterpret_cast<IInternetGateway*>(lParam);
  28. OnRemoveBeacon(WM_APP_REMOVEBEACON, 0, 0, bHandled);
  29. m_pInternetGateway = pInternetGateway;
  30. m_pInternetGateway->AddRef();
  31. m_hShowIconResult = ShowTrayIcon(0);
  32. return 0;
  33. }
  34. LRESULT CICSTrayIcon::OnRemoveBeacon(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  35. {
  36. HRESULT hr = S_OK;
  37. BSTR UniqueDeviceName = reinterpret_cast<BSTR>(lParam);
  38. if(NULL != m_pInternetGateway)
  39. {
  40. if(NULL != UniqueDeviceName)
  41. {
  42. BSTR CurrentUniqueDeviceName;
  43. hr = m_pInternetGateway->GetUniqueDeviceName(&CurrentUniqueDeviceName);
  44. if(SUCCEEDED(hr))
  45. {
  46. if(0 != wcscmp(UniqueDeviceName, CurrentUniqueDeviceName))
  47. {
  48. hr = E_FAIL;
  49. }
  50. SysFreeString(CurrentUniqueDeviceName);
  51. }
  52. }
  53. if(SUCCEEDED(hr))
  54. {
  55. if(SUCCEEDED(m_hShowIconResult))
  56. {
  57. HideTrayIcon(0);
  58. }
  59. m_pInternetGateway->Release();
  60. m_pInternetGateway = NULL;
  61. }
  62. }
  63. return 0;
  64. }
  65. LRESULT CICSTrayIcon::OnGetBeacon(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  66. {
  67. HRESULT hr = S_OK;
  68. IInternetGateway** ppInternetGateway = reinterpret_cast<IInternetGateway**>(lParam);
  69. hr = GetInternetGateway(ppInternetGateway);
  70. return SUCCEEDED(hr) ? 1 : 0;
  71. }
  72. HRESULT CICSTrayIcon::ShowTrayIcon(UINT uID)
  73. {
  74. HRESULT hr = S_OK;
  75. hr = CPropertiesDialog::ShouldShowIcon();
  76. if(SUCCEEDED(hr))
  77. {
  78. int nHeight = GetSystemMetrics(SM_CYSMICON); // ok if these fails, LoadImage has default behavior
  79. int nWidth = GetSystemMetrics(SM_CXSMICON);
  80. NOTIFYICONDATA IconData;
  81. IconData.cbSize = NOTIFYICONDATA_V1_SIZE; // compat with pre IE5
  82. IconData.uID = uID;
  83. IconData.hWnd = m_hWnd;
  84. IconData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
  85. IconData.uCallbackMessage = WM_APP_TRAYMESSAGE;
  86. IconData.hIcon = reinterpret_cast<HICON>(LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDI_TRAYICON), IMAGE_ICON, nWidth, nHeight, LR_DEFAULTCOLOR));
  87. lstrcpyn(IconData.szTip, TEXT("Internet Connection Sharing"), 64);
  88. if(FALSE == Shell_NotifyIcon(NIM_ADD, &IconData))
  89. {
  90. hr = E_FAIL;
  91. }
  92. }
  93. return hr;
  94. }
  95. HRESULT CICSTrayIcon::HideTrayIcon(UINT uID)
  96. {
  97. HRESULT hr = S_OK;
  98. NOTIFYICONDATA IconData;
  99. IconData.cbSize = NOTIFYICONDATA_V1_SIZE; // compat with pre IE5
  100. IconData.hWnd = m_hWnd;
  101. IconData.uID = uID;
  102. IconData.uFlags = 0;
  103. Shell_NotifyIcon(NIM_DELETE, &IconData);
  104. return hr;
  105. }
  106. HRESULT CICSTrayIcon::GetInternetGateway(IInternetGateway** ppInternetGateway)
  107. {
  108. HRESULT hr = S_OK;
  109. *ppInternetGateway = NULL;
  110. if(NULL != m_pInternetGateway)
  111. {
  112. *ppInternetGateway = m_pInternetGateway;
  113. m_pInternetGateway->AddRef();
  114. }
  115. else
  116. {
  117. hr = E_FAIL;
  118. }
  119. return hr;
  120. }
  121. LRESULT CICSTrayIcon::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  122. {
  123. HRESULT hr = S_OK;
  124. // if not IE 5.01 we can't do UPnP, but the window must stay so it can later post error dialogs
  125. hr = EnsureFileVersion("wininet.dll", MakeQword4(5, 0, 2919, 6305));
  126. if(SUCCEEDED(hr))
  127. {
  128. hr = EnsureFileVersion("urlmon.dll", MakeQword4(5, 0, 2919, 6303));
  129. }
  130. if(SUCCEEDED(hr))
  131. {
  132. hr = EnsureFileVersion("msxml.dll", MakeQword4(5, 0, 2919, 6303));
  133. }
  134. if(SUCCEEDED(hr))
  135. {
  136. m_bFileVersionsAccepted = TRUE;
  137. IUPnPDeviceFinder* pDeviceFinder;
  138. hr = CoCreateInstance(CLSID_UPnPDeviceFinder, NULL, CLSCTX_INPROC_SERVER, IID_IUPnPDeviceFinder, reinterpret_cast<void **>(&m_pDeviceFinder));
  139. if(SUCCEEDED(hr))
  140. {
  141. hr = StartSearch();
  142. }
  143. if(SUCCEEDED(hr))
  144. {
  145. m_DummySocket = socket(AF_INET, SOCK_DGRAM, 0);
  146. if(INVALID_SOCKET != m_DummySocket)
  147. {
  148. if(0 == WSAAsyncSelect(m_DummySocket, m_hWnd, WM_APP_SOCKET_NOTIFICATION, FD_ADDRESS_LIST_CHANGE))
  149. {
  150. DWORD dwBytesReturned;
  151. if(SOCKET_ERROR != WSAIoctl(m_DummySocket, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &dwBytesReturned, NULL, NULL) || WSAEWOULDBLOCK != WSAGetLastError())
  152. {
  153. hr = E_FAIL;
  154. }
  155. }
  156. else
  157. {
  158. hr = E_FAIL;
  159. }
  160. }
  161. else
  162. {
  163. hr = E_FAIL;
  164. }
  165. }
  166. if(SUCCEEDED(hr))
  167. {
  168. CComObject<CInternetGatewayFinderClassFactory>* pInternetGatewayFinderClassFactory;
  169. hr = CComObject<CInternetGatewayFinderClassFactory>::CreateInstance(&pInternetGatewayFinderClassFactory);
  170. if(SUCCEEDED(hr))
  171. {
  172. pInternetGatewayFinderClassFactory->AddRef();
  173. hr = pInternetGatewayFinderClassFactory->Initialize(m_hWnd);
  174. if(SUCCEEDED(hr))
  175. {
  176. hr = CoRegisterClassObject(CLSID_CInternetGatewayFinder, pInternetGatewayFinderClassFactory, CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, &m_dwRegisterClassCookie);
  177. }
  178. pInternetGatewayFinderClassFactory->Release();
  179. }
  180. }
  181. }
  182. return 0;
  183. }
  184. LRESULT CICSTrayIcon::OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  185. {
  186. DestroyWindow();
  187. return 0;
  188. }
  189. LRESULT CICSTrayIcon::OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  190. {
  191. OnRemoveBeacon(WM_APP_REMOVEBEACON, 0, 0, bHandled);
  192. if(NULL != m_pDeviceFinder)
  193. {
  194. m_pDeviceFinder->CancelAsyncFind(m_lSearchCookie);
  195. m_pDeviceFinder->Release();
  196. m_pDeviceFinder = NULL;
  197. }
  198. if(INVALID_SOCKET != m_DummySocket)
  199. {
  200. closesocket(m_DummySocket);
  201. }
  202. if(0 != m_dwRegisterClassCookie)
  203. {
  204. CoRevokeClassObject(m_dwRegisterClassCookie);
  205. }
  206. PostQuitMessage(0);
  207. return 0;
  208. }
  209. LRESULT CICSTrayIcon::OnTrayMessage(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  210. {
  211. HRESULT hr = S_OK;
  212. switch(lParam)
  213. {
  214. case WM_RBUTTONDOWN:
  215. {
  216. IInternetGateway* pInternetGateway;
  217. hr = GetInternetGateway(&pInternetGateway);
  218. if(SUCCEEDED(hr))
  219. {
  220. NETCON_MEDIATYPE MediaType;
  221. hr = pInternetGateway->GetMediaType(&MediaType);
  222. if(SUCCEEDED(hr))
  223. {
  224. IUPnPService* pWANConnectionService;
  225. hr = GetWANConnectionService(pInternetGateway, &pWANConnectionService);
  226. if(SUCCEEDED(hr))
  227. {
  228. NETCON_STATUS Status;
  229. hr = GetConnectionStatus(pWANConnectionService, &Status);
  230. if(SUCCEEDED(hr))
  231. {
  232. HMENU hMenu;
  233. LPTSTR pszMenuID = NULL;
  234. if(NCS_CONNECTED == Status)
  235. {
  236. pszMenuID = NCM_SHAREDACCESSHOST_LAN == MediaType ? MAKEINTRESOURCE(IDM_TRAYICON_LAN_CONNECT) : MAKEINTRESOURCE(IDM_TRAYICON_RAS_CONNECT);
  237. }
  238. else if (NCS_DISCONNECTED == Status)
  239. {
  240. pszMenuID = NCM_SHAREDACCESSHOST_LAN == MediaType ? MAKEINTRESOURCE(IDM_TRAYICON_LAN_DISCONNECT) : MAKEINTRESOURCE(IDM_TRAYICON_RAS_DISCONNECT);
  241. }
  242. if(NULL != pszMenuID)
  243. {
  244. hMenu = LoadMenu(_Module.GetResourceInstance(), pszMenuID);
  245. if(NULL != hMenu)
  246. {
  247. HMENU hSubMenu = GetSubMenu(hMenu, 0);
  248. if(NULL != hSubMenu)
  249. {
  250. POINT CursorPosition;
  251. if(GetCursorPos(&CursorPosition))
  252. {
  253. SetForegroundWindow(m_hWnd); // this is to get the menu to go away when it loses focus.
  254. TrackPopupMenu(hSubMenu, TPM_RIGHTBUTTON, CursorPosition.x, CursorPosition.y, 0, m_hWnd, NULL);
  255. }
  256. }
  257. DestroyMenu(hMenu);
  258. }
  259. }
  260. }
  261. pWANConnectionService->Release();
  262. }
  263. }
  264. pInternetGateway->Release();
  265. }
  266. break;
  267. }
  268. case WM_MOUSEMOVE: // REVIEW: is there a better message?
  269. {
  270. IInternetGateway* pInternetGateway;
  271. hr = GetInternetGateway(&pInternetGateway);
  272. if(SUCCEEDED(hr))
  273. {
  274. IUPnPService* pWANConnectionService;
  275. hr = GetWANConnectionService(pInternetGateway, &pWANConnectionService);
  276. if(SUCCEEDED(hr))
  277. {
  278. NETCON_STATUS Status;
  279. hr = GetConnectionStatus(pWANConnectionService, &Status);
  280. if(SUCCEEDED(hr))
  281. {
  282. TCHAR szConnectionStatus[64];
  283. hr = ConnectionStatusToString(Status, szConnectionStatus, sizeof(szConnectionStatus) / sizeof(TCHAR));
  284. if(SUCCEEDED(hr))
  285. {
  286. TCHAR szFormat[64];
  287. if(0 != LoadString(_Module.GetResourceInstance(), IDS_TOOLTIP_FORMAT, szFormat, sizeof(szFormat) / sizeof(TCHAR)))
  288. {
  289. NOTIFYICONDATA NotifyIconData;
  290. ZeroMemory(&NotifyIconData, sizeof(NotifyIconData));
  291. NotifyIconData.cbSize = NOTIFYICONDATA_V1_SIZE;
  292. NotifyIconData.hWnd = m_hWnd;
  293. NotifyIconData.uID = 0;
  294. NotifyIconData.uFlags = NIF_TIP;
  295. _sntprintf(NotifyIconData.szTip, 64, szFormat, szConnectionStatus);
  296. NotifyIconData.szTip[63] = TEXT('\0'); // make sure a maximum length string is null terminated
  297. Shell_NotifyIcon(NIM_MODIFY, &NotifyIconData);
  298. }
  299. }
  300. }
  301. pWANConnectionService->Release();
  302. }
  303. pInternetGateway->Release();
  304. }
  305. break;
  306. }
  307. default:
  308. bHandled = FALSE;
  309. break;
  310. }
  311. return 0;
  312. }
  313. LRESULT CICSTrayIcon::OnStatus(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
  314. {
  315. HRESULT hr = S_OK;
  316. if(FALSE == m_bShowingStatus)
  317. {
  318. m_bShowingStatus = TRUE;
  319. IInternetGateway* pInternetGateway;
  320. hr = GetInternetGateway(&pInternetGateway);
  321. if(SUCCEEDED(hr))
  322. {
  323. LPTSTR pszConnectionName;
  324. hr = GetConnectionName(pInternetGateway, &pszConnectionName);
  325. if(SUCCEEDED(hr))
  326. {
  327. TCHAR szFormat[32];
  328. if(0 != LoadString(_Module.GetResourceInstance(), IDS_STATUS_FORMAT, szFormat, sizeof(szFormat) / sizeof(TCHAR)))
  329. {
  330. LPTSTR pszArguments[] = {pszConnectionName};
  331. LPTSTR pszFormattedString;
  332. if(0 != FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ARGUMENT_ARRAY, szFormat, 0, 0, reinterpret_cast<LPTSTR>(&pszFormattedString), 0, pszArguments))
  333. {
  334. CStatusDialog StatusDialog(pInternetGateway);
  335. HPROPSHEETPAGE hStatusPage = StatusDialog.Create();
  336. if(NULL != hStatusPage)
  337. {
  338. HPROPSHEETPAGE PropertySheetPages[1];
  339. PropertySheetPages[0] = hStatusPage;
  340. PROPSHEETHEADER PropertySheetHeader;
  341. ZeroMemory(&PropertySheetHeader, sizeof(PropertySheetHeader));
  342. PropertySheetHeader.dwSize = PROPSHEETHEADER_V1_SIZE;
  343. PropertySheetHeader.dwFlags = PSH_NOAPPLYNOW | PSH_NOCONTEXTHELP;
  344. PropertySheetHeader.hwndParent = m_hWnd;
  345. PropertySheetHeader.pszCaption = pszFormattedString;
  346. PropertySheetHeader.hInstance = _Module.GetResourceInstance();
  347. PropertySheetHeader.nPages = 1;
  348. PropertySheetHeader.phpage = PropertySheetPages;
  349. PropertySheet(&PropertySheetHeader);
  350. }
  351. LocalFree(pszFormattedString);
  352. }
  353. }
  354. LocalFree(pszConnectionName);
  355. }
  356. pInternetGateway->Release();
  357. }
  358. else // if not beacon was detected
  359. {
  360. TCHAR szTitle[128];
  361. TCHAR szText[1024];
  362. if(NULL != LoadString(_Module.GetResourceInstance(), IDS_APPTITLE, szTitle, sizeof(szTitle) / sizeof(TCHAR)))
  363. {
  364. if(TRUE == m_bFileVersionsAccepted)
  365. {
  366. if(NULL != LoadString(_Module.GetResourceInstance(), IDS_BEACONNOTFOUND, szText, sizeof(szText) / sizeof(TCHAR)))
  367. {
  368. MessageBox(szText, szTitle);
  369. }
  370. }
  371. else
  372. {
  373. if(NULL != LoadString(_Module.GetResourceInstance(), IDS_NEEDNEWERIE, szText, sizeof(szText) / sizeof(TCHAR)))
  374. {
  375. MessageBox(szText, szTitle);
  376. }
  377. }
  378. }
  379. }
  380. m_bShowingStatus = FALSE;
  381. }
  382. return 0;
  383. }
  384. extern HPROPSHEETPAGE GetSharedAccessPropertyPage (IInternetGateway* pInternetGateway, HWND hwndOwner); // in saprop.cpp
  385. LRESULT CICSTrayIcon::OnProperties(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
  386. {
  387. HRESULT hr = S_OK;
  388. if(FALSE == m_bShowingProperties)
  389. {
  390. m_bShowingProperties = TRUE;
  391. IInternetGateway* pInternetGateway;
  392. hr = GetInternetGateway(&pInternetGateway);
  393. if(SUCCEEDED(hr))
  394. {
  395. CPropertiesDialog PropertiesDialog(pInternetGateway);
  396. HPROPSHEETPAGE hStatusPage = PropertiesDialog.Create();
  397. if(NULL != hStatusPage)
  398. {
  399. LPTSTR pszConnectionName;
  400. hr = GetConnectionName(pInternetGateway, &pszConnectionName);
  401. if(SUCCEEDED(hr))
  402. {
  403. TCHAR szFormat[32];
  404. if(0 != LoadString(_Module.GetResourceInstance(), IDS_PROPERTIES_FORMAT, szFormat, sizeof(szFormat) / sizeof(TCHAR)))
  405. {
  406. LPTSTR pszArguments[] = {pszConnectionName};
  407. LPTSTR pszFormattedString;
  408. if(0 != FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ARGUMENT_ARRAY, szFormat, 0, 0, reinterpret_cast<LPTSTR>(&pszFormattedString), 0, pszArguments))
  409. {
  410. HPROPSHEETPAGE PropertySheetPages[2];
  411. PropertySheetPages[0] = hStatusPage;
  412. PropertySheetPages[1] = GetSharedAccessPropertyPage (pInternetGateway, hWndCtl);
  413. PROPSHEETHEADER PropertySheetHeader;
  414. ZeroMemory(&PropertySheetHeader, sizeof(PropertySheetHeader));
  415. PropertySheetHeader.dwSize = PROPSHEETHEADER_V1_SIZE;
  416. PropertySheetHeader.dwFlags = PSH_NOAPPLYNOW | PSH_NOCONTEXTHELP;
  417. PropertySheetHeader.hwndParent = m_hWnd;
  418. PropertySheetHeader.pszCaption = pszFormattedString;
  419. PropertySheetHeader.hInstance = _Module.GetResourceInstance();
  420. PropertySheetHeader.phpage = PropertySheetPages;
  421. PropertySheetHeader.nPages = PropertySheetPages[1] ? 2 : 1;
  422. PropertySheet(&PropertySheetHeader);
  423. HRESULT hShouldShowIcon = CPropertiesDialog::ShouldShowIcon();
  424. if(SUCCEEDED(hShouldShowIcon)) // if we are now showing the icon
  425. {
  426. if(FAILED(m_hShowIconResult)) // and weren't before
  427. {
  428. m_hShowIconResult = ShowTrayIcon(0); // show it
  429. }
  430. }
  431. else // if we are not hiding the icon
  432. {
  433. if(SUCCEEDED(m_hShowIconResult)) // and were showing it before
  434. {
  435. HideTrayIcon(0); // hide it
  436. m_hShowIconResult = E_FAIL;
  437. }
  438. }
  439. LocalFree(pszFormattedString);
  440. }
  441. }
  442. LocalFree(pszConnectionName);
  443. }
  444. }
  445. pInternetGateway->Release();
  446. }
  447. m_bShowingProperties = FALSE;
  448. }
  449. return 0;
  450. }
  451. LRESULT CICSTrayIcon::OnConnect(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
  452. {
  453. HRESULT hr = S_OK;
  454. IInternetGateway* pInternetGateway;
  455. hr = GetInternetGateway(&pInternetGateway);
  456. if(SUCCEEDED(hr))
  457. {
  458. IUPnPService* pWANConnectionService;
  459. hr = GetWANConnectionService(pInternetGateway, &pWANConnectionService);
  460. if(SUCCEEDED(hr))
  461. {
  462. VARIANT OutArgs;
  463. hr = InvokeVoidAction(pWANConnectionService, L"RequestConnection", &OutArgs);
  464. if(SUCCEEDED(hr))
  465. {
  466. VariantClear(&OutArgs);
  467. }
  468. else if(UPNP_ACTION_HRESULT(800) == hr)
  469. {
  470. ShowErrorDialog(m_hWnd, IDS_ACCESSDENIED);
  471. }
  472. else if(UPNP_E_DEVICE_TIMEOUT != hr)
  473. {
  474. ShowErrorDialog(m_hWnd, IDS_CONNECTIONFAILED);
  475. }
  476. pWANConnectionService->Release();
  477. }
  478. pInternetGateway->Release();
  479. }
  480. return 0;
  481. }
  482. LRESULT CICSTrayIcon::OnDisconnect(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
  483. {
  484. HRESULT hr = S_OK;
  485. IInternetGateway* pInternetGateway;
  486. hr = GetInternetGateway(&pInternetGateway);
  487. if(SUCCEEDED(hr))
  488. {
  489. IUPnPService* pWANConnectionService;
  490. hr = GetWANConnectionService(pInternetGateway, &pWANConnectionService);
  491. if(SUCCEEDED(hr))
  492. {
  493. VARIANT OutArgs;
  494. hr = InvokeVoidAction(pWANConnectionService, L"ForceTermination", &OutArgs);
  495. if(SUCCEEDED(hr))
  496. {
  497. VariantClear(&OutArgs);
  498. }
  499. else if(UPNP_ACTION_HRESULT(800) == hr)
  500. {
  501. ShowErrorDialog(m_hWnd, IDS_ACCESSDENIED);
  502. }
  503. pWANConnectionService->Release();
  504. }
  505. pInternetGateway->Release();
  506. }
  507. return 0;
  508. }
  509. LRESULT CICSTrayIcon::OnSocketNotification(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  510. {
  511. HRESULT hr S_OK;
  512. if(FD_ADDRESS_LIST_CHANGE == WSAGETSELECTEVENT(lParam))
  513. {
  514. if(NULL != m_pDeviceFinder)
  515. {
  516. hr = m_pDeviceFinder->CancelAsyncFind(m_lSearchCookie);
  517. hr = StartSearch();
  518. }
  519. DWORD dwBytesReturned;
  520. if(SOCKET_ERROR != WSAIoctl(m_DummySocket, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &dwBytesReturned, NULL, NULL) || WSAEWOULDBLOCK != WSAGetLastError())
  521. {
  522. hr = E_FAIL;
  523. }
  524. }
  525. return 0;
  526. }
  527. HRESULT CICSTrayIcon::StartSearch(void)
  528. {
  529. HRESULT hr = S_OK;
  530. BOOL bHandled;
  531. OnRemoveBeacon(WM_APP_REMOVEBEACON, 0, 0, bHandled); // dump out the cache
  532. CComObject<CBeaconFinder>* pBeaconFinder;
  533. hr = CComObject<CBeaconFinder>::CreateInstance(&pBeaconFinder);
  534. if(SUCCEEDED(hr))
  535. {
  536. pBeaconFinder->AddRef();
  537. hr = pBeaconFinder->Initialize(m_hWnd);
  538. if(SUCCEEDED(hr))
  539. {
  540. BSTR bstrTypeURI;
  541. bstrTypeURI = SysAllocString(L"urn:schemas-upnp-org:device:InternetGatewayDevice:1");
  542. if (NULL != bstrTypeURI)
  543. {
  544. hr = m_pDeviceFinder->CreateAsyncFind(bstrTypeURI, 0, pBeaconFinder, &m_lSearchCookie);
  545. if(SUCCEEDED(hr))
  546. {
  547. hr = m_pDeviceFinder->StartAsyncFind(m_lSearchCookie);
  548. }
  549. if(FAILED(hr)) // we must close this here because later calls don't know if the cookie is valid
  550. {
  551. m_pDeviceFinder->Release();
  552. m_pDeviceFinder = NULL;
  553. }
  554. SysFreeString(bstrTypeURI);
  555. }
  556. else
  557. {
  558. hr = E_OUTOFMEMORY;
  559. }
  560. }
  561. pBeaconFinder->Release();
  562. }
  563. return hr;
  564. }