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.

295 lines
7.4 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows/NT **/
  3. /** Copyright(c) Microsoft Corporation, 1995 - 1999 **/
  4. /**********************************************************************/
  5. /*
  6. FILE HISTORY:
  7. */
  8. #include "stdafx.h"
  9. #include <stdlib.h>
  10. #include <memory.h>
  11. #include <ctype.h>
  12. #include "tfschar.h"
  13. #include "tregkey.h"
  14. #ifdef _DEBUG
  15. #undef THIS_FILE
  16. static char BASED_CODE THIS_FILE[] = __FILE__;
  17. #endif
  18. #define new DEBUG_NEW
  19. #if _WIN32_WINNT < 0x0500
  20. //
  21. // CODEWORK This was taken from winbase.h. MFC requires _WIN32_WINNT=0x4000 whereas
  22. // winbase.h only includes this for _WIN32_WINNT=0x5000. JonN 1/14/99
  23. //
  24. extern "C" {
  25. typedef enum _COMPUTER_NAME_FORMAT {
  26. ComputerNameNetBIOS,
  27. ComputerNameDnsHostname,
  28. ComputerNameDnsDomain,
  29. ComputerNameDnsFullyQualified,
  30. ComputerNamePhysicalNetBIOS,
  31. ComputerNamePhysicalDnsHostname,
  32. ComputerNamePhysicalDnsDomain,
  33. ComputerNamePhysicalDnsFullyQualified,
  34. ComputerNameMax
  35. } COMPUTER_NAME_FORMAT ;
  36. WINBASEAPI
  37. BOOL
  38. WINAPI
  39. GetComputerNameExA (
  40. IN COMPUTER_NAME_FORMAT NameType,
  41. OUT LPSTR lpBuffer,
  42. IN OUT LPDWORD nSize
  43. );
  44. WINBASEAPI
  45. BOOL
  46. WINAPI
  47. GetComputerNameExW (
  48. IN COMPUTER_NAME_FORMAT NameType,
  49. OUT LPWSTR lpBuffer,
  50. IN OUT LPDWORD nSize
  51. );
  52. #ifdef UNICODE
  53. #define GetComputerNameEx GetComputerNameExW
  54. #else
  55. #define GetComputerNameEx GetComputerNameExA
  56. #endif // !UNICODE
  57. } // extern "C"
  58. #endif
  59. /*!--------------------------------------------------------------------------
  60. IsLocalMachine
  61. Returns TRUE if the machine name passed in is the local machine,
  62. or if pszMachineName is NULL.
  63. This compares the NetBIOS name and the DNS (fully-qualified) name.
  64. Returns FALSE otherwise.
  65. Author: KennT
  66. ---------------------------------------------------------------------------*/
  67. BOOL IsLocalMachine(LPCTSTR pszMachineName)
  68. {
  69. static TCHAR s_szLocalMachineName[MAX_PATH*2+1] = _T("");
  70. static TCHAR s_szDnsLocalMachineName[MAX_PATH*2+1] = _T("");
  71. if ((pszMachineName == NULL) || (*pszMachineName == 0))
  72. return TRUE;
  73. // Bypass the beginning slashes
  74. if ((pszMachineName[0] == _T('\\')) && (pszMachineName[1] == _T('\\')))
  75. pszMachineName += 2;
  76. // Check again (for degenerate case of "\\")
  77. if (*pszMachineName == 0)
  78. return TRUE;
  79. if (s_szLocalMachineName[0] == 0)
  80. {
  81. DWORD dwSize = MAX_PATH*2;
  82. GetComputerName(s_szLocalMachineName, &dwSize);
  83. s_szLocalMachineName[MAX_PATH*2] = 0;
  84. }
  85. if (s_szDnsLocalMachineName[0] == 0)
  86. {
  87. DWORD dwSize = MAX_PATH*2;
  88. GetComputerNameEx(ComputerNameDnsFullyQualified,
  89. s_szDnsLocalMachineName,
  90. &dwSize);
  91. s_szDnsLocalMachineName[MAX_PATH*2] = 0;
  92. }
  93. return (StriCmp(pszMachineName, s_szLocalMachineName) == 0) ||
  94. (StriCmp(pszMachineName, s_szDnsLocalMachineName) == 0);
  95. }
  96. /*!--------------------------------------------------------------------------
  97. FUseTaskpadsByDefault
  98. See comments in header file.
  99. Author: KennT
  100. ---------------------------------------------------------------------------*/
  101. BOOL FUseTaskpadsByDefault(LPCTSTR pszMachineName)
  102. {
  103. static DWORD s_dwStopTheInsanity = 42;
  104. RegKey regkeyMMC;
  105. DWORD dwErr;
  106. if (s_dwStopTheInsanity == 42)
  107. {
  108. // Set the default to TRUE (i.e. do not use taskpads by default)
  109. // ------------------------------------------------------------
  110. s_dwStopTheInsanity = 1;
  111. dwErr = regkeyMMC.Open(HKEY_LOCAL_MACHINE,
  112. _T("Software\\Microsoft\\MMC"),
  113. KEY_READ, pszMachineName);
  114. if (dwErr == ERROR_SUCCESS)
  115. {
  116. dwErr = regkeyMMC.QueryValue(_T("TFSCore_StopTheInsanity"), s_dwStopTheInsanity);
  117. if (dwErr != ERROR_SUCCESS)
  118. s_dwStopTheInsanity = 1;
  119. }
  120. }
  121. return !s_dwStopTheInsanity;
  122. }
  123. UINT CalculateStringWidth(HWND hWndParent, LPCTSTR pszString)
  124. {
  125. // Create a dummy list control, set this text width and use
  126. // that to determine the width of the string as used by MMC.
  127. // Create a dummy list control (that will be attached to the window)
  128. CListCtrl listctrl;
  129. CRect rect(0,0,0,0);
  130. UINT nSize;
  131. HWND hWnd;
  132. CString s_szHiddenWndClass = AfxRegisterWndClass(
  133. 0x0, //UINT nClassStyle,
  134. NULL, //HCURSOR hCursor,
  135. NULL, //HBRUSH hbrBackground,
  136. NULL //HICON hIcon
  137. );
  138. hWnd = ::CreateWindowEx(
  139. 0x0, //DWORD dwExStyle,
  140. s_szHiddenWndClass, //LPCTSTR lpszClassName,
  141. NULL, //LPCTSTR lpszWindowName,
  142. 0x0, //DWORD dwStyle,
  143. 0, //int x,
  144. 0, //int y,
  145. 0, //int nWidth,
  146. 0, //int nHeight,
  147. NULL, //HWND hwndParent,
  148. NULL, //HMENU nIDorHMenu,
  149. AfxGetInstanceHandle(),
  150. NULL //LPVOID lpParam = NULL
  151. );
  152. listctrl.Create(0, rect, CWnd::FromHandle(hWnd), 0);
  153. nSize = listctrl.GetStringWidth(pszString);
  154. // Now destroy the window that we created
  155. listctrl.DestroyWindow();
  156. SendMessage(hWnd, WM_CLOSE, 0, 0);
  157. return nSize;
  158. }
  159. /*!--------------------------------------------------------------------------
  160. SearchChildNodesForGuid
  161. -
  162. Author: KennT
  163. ---------------------------------------------------------------------------*/
  164. HRESULT SearchChildNodesForGuid(ITFSNode *pParent, const GUID *pGuid, ITFSNode **ppChild)
  165. {
  166. HRESULT hr = hrFalse;
  167. SPITFSNodeEnum spNodeEnum;
  168. SPITFSNode spNode;
  169. // Enumerate through all of the child nodes and return the
  170. // first node that matches the GUID.
  171. CORg( pParent->GetEnum(&spNodeEnum) );
  172. while ( spNodeEnum->Next(1, &spNode, NULL) == hrOK )
  173. {
  174. if (*(spNode->GetNodeType()) == *pGuid)
  175. break;
  176. spNode.Release();
  177. }
  178. if (spNode)
  179. {
  180. if (ppChild)
  181. *ppChild = spNode.Transfer();
  182. hr = hrOK;
  183. }
  184. Error:
  185. return hr;
  186. }
  187. /*!--------------------------------------------------------------------------
  188. CheckIPAddressAndMask
  189. -
  190. Author: KennT
  191. ---------------------------------------------------------------------------*/
  192. UINT CheckIPAddressAndMask(DWORD ipAddress, DWORD ipMask, DWORD dwFlags)
  193. {
  194. if (dwFlags & IPADDRESS_TEST_NONCONTIGUOUS_MASK)
  195. {
  196. DWORD dwNewMask;
  197. // Hmm... how to do this?
  198. dwNewMask = 0;
  199. for (int i = 0; i < sizeof(ipMask)*8; i++)
  200. {
  201. dwNewMask |= 1 << i;
  202. if (dwNewMask & ipMask)
  203. {
  204. dwNewMask &= ~(1 << i);
  205. break;
  206. }
  207. }
  208. // At this point dwNewMask is 000..0111
  209. // ~dwNewMask is 111..1000
  210. // ~ipMask is 001..0111 (if noncontiguous)
  211. // ~dwNewMask & ~ipMask is 001..0000
  212. // So if this is non-zero then the mask is noncontiguous
  213. if (~dwNewMask & ~ipMask)
  214. {
  215. return IDS_COMMON_ERR_IPADDRESS_NONCONTIGUOUS_MASK;
  216. }
  217. }
  218. if (dwFlags & IPADDRESS_TEST_TOO_SPECIFIC)
  219. {
  220. if (ipAddress != (ipAddress & ipMask))
  221. return IDS_COMMON_ERR_IPADDRESS_TOO_SPECIFIC;
  222. }
  223. if (dwFlags & IPADDRESS_TEST_NORMAL_RANGE)
  224. {
  225. if ((ipAddress < MAKEIPADDRESS(1,0,0,0)) ||
  226. (ipAddress >= MAKEIPADDRESS(224,0,0,0)))
  227. return IDS_COMMON_ERR_IPADDRESS_NORMAL_RANGE;
  228. }
  229. if (dwFlags & IPADDRESS_TEST_NOT_127)
  230. {
  231. if ((ipAddress & 0xFF000000) == MAKEIPADDRESS(127,0,0,0))
  232. return IDS_COMMON_ERR_IPADDRESS_127;
  233. }
  234. if (dwFlags & IPADDRESS_TEST_ADDR_NOT_EQ_MASK)
  235. {
  236. if (ipAddress == ipMask)
  237. return IDS_COMMON_ERR_IPADDRESS_NOT_EQ_MASK;
  238. }
  239. return 0;
  240. }