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.

358 lines
8.1 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1997 - 1999
  6. //
  7. // File: SnpUtils.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. #include "stdafx.h"
  11. #include "windowsx.h"
  12. DWORD FormatError(HRESULT hr, TCHAR *pszBuffer, UINT cchBuffer)
  13. {
  14. DWORD dwErr;
  15. // Copy over default message into szBuffer
  16. _tcscpy(pszBuffer, _T("Error"));
  17. // Ok, we can't get the error info, so try to format it
  18. // using the FormatMessage
  19. // Ignore the return message, if this call fails then I don't
  20. // know what to do.
  21. dwErr = FormatMessage(
  22. FORMAT_MESSAGE_FROM_SYSTEM,
  23. NULL,
  24. hr,
  25. 0,
  26. pszBuffer,
  27. cchBuffer,
  28. NULL);
  29. pszBuffer[cchBuffer-1] = 0;
  30. return dwErr;
  31. }
  32. ///////////////////////////////////////////////////////////////////////////////
  33. // GetErrorMessage
  34. // Format the error message based on the HRESULT
  35. //
  36. // Note: The caller should NOT try to modify the string returned by this function
  37. //
  38. LPCTSTR GetErrorMessage(HRESULT hr)
  39. {
  40. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  41. static CString st;
  42. st = _T("");
  43. if (FAILED(hr))
  44. {
  45. TCHAR szBuffer[2048];
  46. CString stErrCode;
  47. FormatError(hr, szBuffer, DimensionOf(szBuffer));
  48. stErrCode.Format(_T("%08lx"), hr);
  49. AfxFormatString2(st, IDS_ERROR_SYSTEM_ERROR_FORMAT,
  50. szBuffer, (LPCTSTR) stErrCode);
  51. }
  52. return (LPCTSTR)st;
  53. }
  54. void ReportError(UINT uMsgId, HRESULT hr)
  55. {
  56. CString strMessage;
  57. CThemeContextActivator activator;
  58. strMessage.FormatMessage (uMsgId, GetErrorMessage(hr));
  59. AfxMessageBox (strMessage);
  60. }
  61. //Allocate the data buffer for a new Wireless Policy. Fills in
  62. //default values and the GUID identifier.
  63. //The caller needs to call FreeWirelessPolicyData if the return is
  64. //S_OK
  65. HRESULT CreateWirelessPolicyDataBuffer(
  66. PWIRELESS_POLICY_DATA * ppPolicy
  67. )
  68. {
  69. ASSERT(ppPolicy);
  70. HRESULT hr = S_OK;
  71. *ppPolicy = NULL;
  72. PWIRELESS_POLICY_DATA pPolicy = NULL;
  73. pPolicy = (PWIRELESS_POLICY_DATA)AllocPolMem(sizeof(*pPolicy));
  74. if (NULL == pPolicy)
  75. {
  76. return E_OUTOFMEMORY;
  77. }
  78. CoCreateGuid(&pPolicy->PolicyIdentifier);
  79. pPolicy->dwPollingInterval = 10800;
  80. pPolicy->dwDisableZeroConf = 0;
  81. pPolicy->dwNumPreferredSettings = 0;
  82. pPolicy->dwNetworkToAccess = WIRELESS_ACCESS_NETWORK_ANY;
  83. pPolicy->dwConnectToNonPreferredNtwks = 0;
  84. if (FAILED(hr))
  85. {
  86. if (pPolicy)
  87. {
  88. FreePolMem(pPolicy);
  89. }
  90. }
  91. else
  92. {
  93. *ppPolicy = pPolicy;
  94. }
  95. return hr;
  96. }
  97. void FreeAndThenDupString(LPWSTR * ppwszDest, LPCWSTR pwszSource)
  98. {
  99. ASSERT(ppwszDest);
  100. if (*ppwszDest)
  101. FreePolStr(*ppwszDest);
  102. *ppwszDest = AllocPolStr(pwszSource);
  103. }
  104. void SSIDDupString(WCHAR *ppwszDest, LPCWSTR pwszSource)
  105. {
  106. wcsncpy(ppwszDest,pwszSource, 32);
  107. }
  108. BOOL
  109. IsDuplicateSSID(
  110. CString &NewSSID,
  111. DWORD dwNetworkType,
  112. PWIRELESS_POLICY_DATA pWirelessPolicyData,
  113. DWORD dwId
  114. )
  115. {
  116. DWORD dwError = 0;
  117. DWORD dwNumPreferredSettings = 0;
  118. PWIRELESS_PS_DATA pWirelessPSData = NULL;
  119. PWIRELESS_PS_DATA *ppWirelessPSData = NULL;
  120. DWORD i = 0;
  121. BOOL bDuplicate = FALSE;
  122. DWORD dwSSIDLen = 0;
  123. DWORD dwStart = 0;
  124. DWORD dwEnd = 0;
  125. WCHAR pszTempSSID[33];
  126. ppWirelessPSData = pWirelessPolicyData->ppWirelessPSData;
  127. dwNumPreferredSettings = pWirelessPolicyData->dwNumPreferredSettings;
  128. if (dwNetworkType == WIRELESS_NETWORK_TYPE_AP) {
  129. dwStart = 0;
  130. dwEnd = pWirelessPolicyData->dwNumAPNetworks;
  131. } else
  132. {
  133. dwStart = pWirelessPolicyData->dwNumAPNetworks;
  134. dwEnd = pWirelessPolicyData->dwNumPreferredSettings;
  135. }
  136. for (i = dwStart; i < dwEnd ; i++) {
  137. if (i != dwId) {
  138. pWirelessPSData = *(ppWirelessPSData + i);
  139. dwSSIDLen = pWirelessPSData->dwWirelessSSIDLen;
  140. // terminate the pszWirelessSSID to correct length or comparision may fail
  141. // ideally WirelessSSID should be a 33 char unicode string with room for a null
  142. // char in the end. Since, we didnt start with that to begin with,
  143. // As a hack, copy the ssid to a new location with null termination.
  144. wcsncpy(pszTempSSID, pWirelessPSData->pszWirelessSSID, 32);
  145. pszTempSSID[dwSSIDLen] = L'\0';
  146. if (0 == NewSSID.Compare(pszTempSSID)) {
  147. bDuplicate = TRUE;
  148. }
  149. }
  150. }
  151. return (bDuplicate);
  152. }
  153. HRESULT DeleteWirelessPolicy(HANDLE hPolicyStore,
  154. PWIRELESS_POLICY_DATA pPolicy)
  155. {
  156. ASSERT(pPolicy);
  157. HRESULT hr = S_OK;
  158. CWRg(WirelessDeletePolicyData(
  159. hPolicyStore,
  160. pPolicy
  161. ));
  162. Error:
  163. return hr;
  164. }
  165. #ifndef PROPSHEETPAGE_LATEST
  166. #ifdef UNICODE
  167. #define PROPSHEETPAGE_LATEST PROPSHEETPAGEW_LATEST
  168. #else
  169. #define PROPSHEETPAGE_LATEST PROPSHEETPAGEA_LATEST
  170. #endif
  171. #endif
  172. HPROPSHEETPAGE MyCreatePropertySheetPage(PROPSHEETPAGE* ppsp)
  173. {
  174. PROPSHEETPAGE_LATEST pspLatest = {0};
  175. CopyMemory (&pspLatest, ppsp, ppsp->dwSize);
  176. pspLatest.dwSize = sizeof(pspLatest);
  177. HPROPSHEETPAGE pProp= ::CreatePropertySheetPage (&pspLatest);
  178. DWORD dwErr = GetLastError();
  179. {
  180. LPVOID lpMsgBuf;
  181. FormatMessage(
  182. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  183. FORMAT_MESSAGE_FROM_SYSTEM |
  184. FORMAT_MESSAGE_IGNORE_INSERTS,
  185. NULL,
  186. dwErr,
  187. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  188. (LPTSTR) &lpMsgBuf,
  189. 0,
  190. NULL
  191. );
  192. // Free the buffer.
  193. LocalFree( lpMsgBuf );
  194. }
  195. return pProp;
  196. }
  197. void
  198. InitFonts(
  199. HWND hDialog,
  200. HFONT& bigBoldFont)
  201. {
  202. ASSERT(::IsWindow(hDialog));
  203. do
  204. {
  205. NONCLIENTMETRICS ncm;
  206. memset(&ncm, 0, sizeof(ncm));
  207. ncm.cbSize = sizeof(ncm);
  208. if ( FALSE == ::SystemParametersInfo(SPI_GETNONCLIENTMETRICS, 0, &ncm, 0))
  209. {
  210. break;
  211. }
  212. LOGFONT bigBoldLogFont = ncm.lfMessageFont;
  213. bigBoldLogFont.lfWeight = FW_BOLD;
  214. //localize
  215. CString fontName;
  216. fontName.LoadString(IDS_BOLD_FONT_NAME);
  217. // ensure null termination 260237
  218. memset(bigBoldLogFont.lfFaceName, 0, LF_FACESIZE * sizeof(TCHAR));
  219. size_t fnLen = fontName.GetLength();
  220. lstrcpyn(
  221. bigBoldLogFont.lfFaceName, // destination buffer
  222. (LPCTSTR) fontName, // string // don't copy over the last null
  223. min(LF_FACESIZE - 1, fnLen) // number of characters to copy
  224. );
  225. //define font size
  226. CString strTemp;
  227. strTemp.LoadString(IDS_BOLD_FONT_SIZE);
  228. unsigned fontSize = _ttoi( (LPCTSTR) strTemp );
  229. HDC hdc = 0;
  230. hdc = ::GetDC(hDialog);
  231. if ( NULL == hdc )
  232. {
  233. break;
  234. }
  235. bigBoldLogFont.lfHeight =
  236. - ::MulDiv(
  237. static_cast<int>(fontSize),
  238. GetDeviceCaps(hdc, LOGPIXELSY),
  239. 72);
  240. bigBoldFont = ::CreateFontIndirect( ( CONST LOGFONT* ) &bigBoldLogFont);
  241. if ( NULL == bigBoldFont )
  242. {
  243. break;
  244. }
  245. ReleaseDC(hDialog, hdc);
  246. }
  247. while (0);
  248. }
  249. void
  250. SetControlFont(HWND parentDialog, int controlID, HFONT font)
  251. {
  252. ASSERT(::IsWindow(parentDialog));
  253. ASSERT(controlID);
  254. ASSERT(font);
  255. HWND control = ::GetDlgItem(parentDialog, controlID);
  256. if (control)
  257. {
  258. SetWindowFont(control, font, TRUE);
  259. }
  260. }
  261. void
  262. SetLargeFont(HWND dialog, int bigBoldResID)
  263. {
  264. ASSERT(::IsWindow(dialog));
  265. ASSERT(bigBoldResID);
  266. static HFONT bigBoldFont = 0;
  267. if (!bigBoldFont)
  268. {
  269. InitFonts(dialog, bigBoldFont);
  270. }
  271. SetControlFont(dialog, bigBoldResID, bigBoldFont);
  272. }