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.

271 lines
8.4 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // File: util.cpp
  4. //
  5. // Module: CMMGR32.EXE
  6. //
  7. // Synopsis: Utility functions for cmmgr32.exe
  8. //
  9. // Copyright (c) 1998-1999 Microsoft Corporation
  10. //
  11. // Author: quintinb created Header 08/16/99
  12. //
  13. //+----------------------------------------------------------------------------
  14. #include "cmmaster.h"
  15. //+----------------------------------------------------------------------------
  16. //
  17. // Function GetProfileInfo
  18. //
  19. // Synopsis get the service name from cms
  20. //
  21. //
  22. // Arguments pszCmpName the cmp file name. Can be in one of the following
  23. // 3 formats:
  24. //
  25. // 1. relative paths without the extension(e.g. msn, cm\msn)
  26. // 2. relative path with the extension(e.g. msn.cmp, cm\msn.cmp)
  27. // 3. full path(e.g. c:\cm\msn.cmp)
  28. //
  29. // pszServiceName the output buffer for the service name(ServiceName).
  30. // must be at least RAS_MaxEntryName.
  31. //
  32. // Returns BOOL TRUE=success, FALSE=failure
  33. //
  34. //-----------------------------------------------------------------------------
  35. BOOL GetProfileInfo(
  36. LPTSTR pszCmpName,
  37. LPTSTR pszServiceName
  38. )
  39. {
  40. LPTSTR pszTmp;
  41. LPTSTR pszDot;
  42. LPTSTR pszSlash;
  43. LPTSTR pszColon;
  44. TCHAR szFileName[MAX_PATH + 1];
  45. TCHAR szCmsFile[MAX_PATH + 1];
  46. TCHAR szPath[MAX_PATH + 1];
  47. lstrcpynU(szFileName, pszCmpName, sizeof(szFileName)/sizeof(TCHAR)-1);
  48. pszDot = CmStrrchr(szFileName, TEXT('.'));
  49. pszSlash = CmStrrchr(szFileName, TEXT('\\'));
  50. pszColon = CmStrrchr(szFileName, TEXT(':'));
  51. if ((pszSlash >= pszDot) && (pszColon >= pszDot))
  52. {
  53. //
  54. // The argument doesn't have an extension, so we'll include one.
  55. //
  56. lstrcatU(szFileName, TEXT(".cmp"));
  57. }
  58. //
  59. // We need to change our current dir to read the profiles.
  60. // If we found a slash, it's either a UNC path, relative path, or
  61. // a full path. Use it to set the current dir. Otherwise use we
  62. // assume that the profile is local and use the application path.
  63. //
  64. if (pszSlash)
  65. {
  66. *pszSlash = TEXT('\0');
  67. MYVERIFY(SetCurrentDirectoryU(szFileName));
  68. //
  69. // restore the slash
  70. //
  71. *pszSlash = TEXT('\\');
  72. }
  73. else
  74. {
  75. //
  76. // Assumes its local, use app path for current dir
  77. //
  78. TCHAR szCurrent[MAX_PATH];
  79. if (GetModuleFileNameU(NULL, szCurrent, MAX_PATH - 1))
  80. {
  81. pszSlash = CmStrrchr(szCurrent, TEXT('\\'));
  82. MYDBGASSERT(pszSlash);
  83. if (pszSlash)
  84. {
  85. *pszSlash = TEXT('\0');
  86. MYVERIFY(SetCurrentDirectoryU(szCurrent));
  87. }
  88. }
  89. }
  90. //
  91. // test whether this is a valid cmp
  92. //
  93. if (SearchPathU(NULL, szFileName, NULL, MAX_PATH, szPath, &pszTmp))
  94. {
  95. BOOL bReturn = FALSE;
  96. //
  97. // szPath should now be a full path.
  98. //
  99. //
  100. // first get the CMS file path from the cmp file.
  101. //
  102. GetPrivateProfileStringU(c_pszCmSection, c_pszCmEntryCmsFile, TEXT(""), szCmsFile, MAX_PATH, szPath);
  103. //
  104. // construct the cms file path. the cms file path obtained from the cmp file
  105. // is a relative path.
  106. //
  107. pszTmp = CmStrrchr(szPath, TEXT('\\'));
  108. if (NULL != pszTmp)
  109. {
  110. //
  111. // Move past the '\\'
  112. //
  113. pszTmp = CharNextU(pszTmp);
  114. if (NULL != pszTmp)
  115. {
  116. lstrcpyU(pszTmp, szCmsFile);
  117. GetPrivateProfileStringU(c_pszCmSection, c_pszCmEntryServiceName, TEXT(""),
  118. pszServiceName, MAX_PATH, szPath);
  119. //
  120. // If the .cms file doesn't exist or is corrupt
  121. // the value of pszService will be ""
  122. //
  123. if (TEXT('\0') != *pszServiceName)
  124. {
  125. bReturn = TRUE;
  126. }
  127. }
  128. }
  129. return bReturn;
  130. }
  131. else
  132. {
  133. //
  134. // there isn't much we can do here
  135. //
  136. *pszServiceName = TEXT('\0');
  137. return FALSE;
  138. }
  139. }
  140. //+----------------------------------------------------------------------------
  141. //
  142. // Function IsCmpPathAllUser
  143. //
  144. // Synopsis If this function is executed on NT5, then it checks to see if
  145. // the passed in CMP file path has the users APP_DATA directory as
  146. // part of the path. If so, then it considers the profile to be
  147. // single user. Otherwise it returns that the profile is all user.
  148. // If the function encounters an error it returns that the profile
  149. // is all user (that is considered the default case).
  150. //
  151. //
  152. // Arguments pszCmp the cmp file name
  153. //
  154. // Returns BOOL TRUE == All User Profile, FALSE == Single User profile
  155. //
  156. // History quintinb Created 05/12/99
  157. //
  158. //-----------------------------------------------------------------------------
  159. BOOL IsCmpPathAllUser(LPCTSTR pszCmp)
  160. {
  161. BOOL bReturn = TRUE;
  162. //
  163. // If we get an invalid input parameter then just assume that it is
  164. // All User. On the other hand, if the OS isn't NT5, then we are
  165. // All User so there is no need to check the path. If we are on
  166. // NT5 and the beginning of the cmp path matches the users
  167. // Application data dir, then we have a single user profile and
  168. // should return false.
  169. //
  170. if ((NULL != pszCmp) && (TEXT('\0') != pszCmp[0]) && OS_NT5)
  171. {
  172. //
  173. // Load shell32 here so that we can call the shell to find out
  174. // the path to the Application Data directory.
  175. //
  176. typedef HRESULT (WINAPI *pfnSHGetSpecialFolderLocationSpec)(HWND, int, LPITEMIDLIST*);
  177. typedef BOOL (WINAPI *pfnSHGetPathFromIDListSpec)(LPCITEMIDLIST, LPTSTR);
  178. typedef HRESULT (WINAPI *pfnSHGetMallocSpec)(LPMALLOC *);
  179. pfnSHGetSpecialFolderLocationSpec pfnSHGetSpecialFolderLocation;
  180. pfnSHGetMallocSpec pfnSHGetMalloc;
  181. pfnSHGetPathFromIDListSpec pfnSHGetPathFromIDList;
  182. HMODULE hShell32 = LoadLibraryExA("Shell32.dll", NULL, 0);
  183. if (hShell32)
  184. {
  185. pfnSHGetSpecialFolderLocation = (pfnSHGetSpecialFolderLocationSpec)GetProcAddress(hShell32,
  186. "SHGetSpecialFolderLocation");
  187. pfnSHGetMalloc = (pfnSHGetMallocSpec)GetProcAddress(hShell32, "SHGetMalloc");
  188. #ifdef UNICODE
  189. pfnSHGetPathFromIDList = (pfnSHGetPathFromIDListSpec)GetProcAddress(hShell32,
  190. "SHGetPathFromIDListW");
  191. #else
  192. pfnSHGetPathFromIDList = (pfnSHGetPathFromIDListSpec)GetProcAddress(hShell32,
  193. "SHGetPathFromIDListA");
  194. #endif
  195. if (pfnSHGetSpecialFolderLocation && pfnSHGetPathFromIDList && pfnSHGetMalloc)
  196. {
  197. LPITEMIDLIST pidl;
  198. TCHAR szAppDataDir[MAX_PATH+1];
  199. TCHAR szTemp[MAX_PATH+1];
  200. HRESULT hr = pfnSHGetSpecialFolderLocation(NULL,
  201. CSIDL_APPDATA,
  202. &pidl);
  203. if (SUCCEEDED(hr))
  204. {
  205. if (pfnSHGetPathFromIDList(pidl, szAppDataDir))
  206. {
  207. UINT uiLen = lstrlenU(szAppDataDir) + 1;
  208. lstrcpynU(szTemp, pszCmp, uiLen);
  209. if (0 == lstrcmpiU(szAppDataDir, szTemp))
  210. {
  211. bReturn = FALSE;
  212. }
  213. }
  214. LPMALLOC pMalloc;
  215. if (SUCCEEDED(pfnSHGetMalloc(&pMalloc)))
  216. {
  217. pMalloc->Free(pidl);
  218. MYVERIFY(SUCCEEDED(pMalloc->Release()));
  219. }
  220. }
  221. }
  222. FreeLibrary(hShell32);
  223. }
  224. }
  225. //
  226. // Figure out what the user directory of the current user is. We can compare this
  227. // against the directory of the phonebook and see if we have a private user
  228. // profile or an all user profile.
  229. return bReturn;
  230. }