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.

214 lines
7.6 KiB

  1. #ifndef __UTILS__
  2. #define __UTILS__
  3. #include <windows.h>
  4. #include <advpub.h>
  5. #include <CleanOC.h>
  6. #include <debug.h>
  7. #include "general.h"
  8. #define LStrNICmp(sz1, sz2, cch) (CompareString(LOCALE_USER_DEFAULT, NORM_IGNORECASE, sz1, cch, sz2, cch) - 2)
  9. #define MODULE_UNKNOWN_OWNER "Unknown Owner"
  10. #define MAX_MESSAGE_LEN 2048
  11. // numeric constants
  12. #define GD_EXTRACTDIR 0
  13. #define GD_CACHEDIR 1
  14. #define GD_CONFLICTDIR 2
  15. #define GD_CONTAINERDIR 3
  16. #define GD_WINDOWSDIR 10
  17. #define GD_SYSTEMDIR 11
  18. #define LENGTH_NAME 200
  19. #define MAX_INF_SECTION_SIZE 1024
  20. #define OLEUI_CCHKEYMAX 256
  21. #define OLEUI_CCHKEYMAX_SIZE (OLEUI_CCHKEYMAX*sizeof(TCHAR))
  22. #define TIMESTAMP_MAXSIZE 64
  23. #define VERSUBBLOCK_SIZE 256
  24. #define MAX_VERSION_SIZE 16
  25. // string constants
  26. #define INPROCSERVER TEXT("InprocServer")
  27. #define LOCALSERVER TEXT("LocalServer")
  28. #define INPROCSERVERX86 TEXT("InProcServerX86")
  29. #define LOCALSERVERX86 TEXT("LocalServerX86")
  30. #define INPROCSERVER32 TEXT("InprocServer32")
  31. #define LOCALSERVER32 TEXT("LocalServer32")
  32. #define INFFILE TEXT("InfFile")
  33. #define UNKNOWNDATA TEXT("n/a")
  34. #define UNKNOWNOWNER TEXT("Unknown Owner")
  35. #define VARTRANSLATION TEXT("\\VarFileInfo\\Translation")
  36. #define FILEVERSION TEXT("\\FileVersion")
  37. #define STRINGFILEINFO TEXT("\\StringFileInfo\\")
  38. #define HKCR_CLSID TEXT("CLSID")
  39. #define HKCR_TYPELIB TEXT("TypeLib")
  40. #define HKCR_INTERFACE TEXT("Interface")
  41. #define VALUE_OWNER TEXT(".Owner")
  42. #define VALUE_ACTIVEXCACHE TEXT("ActiveXCache")
  43. #define VALUE_PATH TEXT("PATH")
  44. #define VALUE_SYSTEM TEXT("SystemComponent")
  45. #define CONTAINER_APP TEXT("IEXPLORE.EXE")
  46. #define DEMO_PAGE TEXT("DemoTmp.html")
  47. #define KEY_HOOK TEXT("Hook")
  48. #define KEY_INFFILE TEXT("InfFile")
  49. #define KEY_INFSECTION TEXT("InfSection")
  50. #define KEY_DEFAULTUNINSTALL TEXT("DefaultUninstall")
  51. #define KEY_UNINSTALL TEXT("UNINSTALL")
  52. #define KEY_SETUPHOOK TEXT("SETUP HOOKS")
  53. #define INF_EXTENSION TEXT(".INF")
  54. #define ENV_PATH TEXT("PATH")
  55. #define KEY_ADDCODE TEXT("Add.Code")
  56. #define DEFAULT_VALUE TEXT("")
  57. #define DEFAULT_CACHE TEXT("\\OCCACHE")
  58. #define DEFAULT_CONFLICT TEXT("\\CONFLICT")
  59. #define DU_INSTALLER_VALUE TEXT("Installer")
  60. #define CDL_INSTALLER TEXT("MSICD")
  61. // registry paths for ModuleUsage
  62. #define REGSTR_PATH_SHAREDDLLS TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\SharedDlls")
  63. #define REGSTR_PATH_MODULE_USAGE TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\ModuleUsage")
  64. #define REGSTR_PATH_IE TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths")
  65. #define REGSTR_PATH_IE_SETTINGS TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings")
  66. //#define REGSTR_PATH_ACTIVEX_CACHE TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ActiveX Cache\\Paths")
  67. #define REGSTR_PATH_ACTIVEX_CACHE TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ActiveX Cache")
  68. #define SOFTWARECLASSES TEXT("SOFTWARE\\CLASSES")
  69. // CLSIDLIST_ITEM declaration
  70. struct tagCLSIDLIST_ITEM;
  71. typedef struct tagCLSIDLIST_ITEM CLSIDLIST_ITEM;
  72. typedef CLSIDLIST_ITEM* LPCLSIDLIST_ITEM;
  73. struct tagCLSIDLIST_ITEM
  74. {
  75. TCHAR szFile[MAX_PATH];
  76. TCHAR szCLSID[MAX_DIST_UNIT_NAME_LEN];
  77. BOOL bIsDistUnit;
  78. LPCLSIDLIST_ITEM pNext;
  79. };
  80. // function prototypes
  81. // void RemoveObsoleteKeys();
  82. void ReverseSlashes(LPTSTR pszStr);
  83. LPTSTR ReverseStrchr(LPCTSTR szString, TCHAR ch);
  84. HRESULT NullLastSlash(LPTSTR szString, UINT uiOffset);
  85. LONG DeleteKeyAndSubKeys(HKEY hkIn, LPCTSTR pszSubKey);
  86. BOOL FileExist(LPCTSTR lpszFileName);
  87. HRESULT LookUpModuleUsage(
  88. LPCTSTR lpszFileName,
  89. LPCTSTR lpszCLSID,
  90. LPTSTR lpszOwner = NULL,
  91. DWORD dwOwnerSize = 0);
  92. DWORD SubtractModuleOwner( LPCTSTR lpszFileName, LPCTSTR lpszGUID );
  93. HRESULT SetSharedDllsCount(
  94. LPCTSTR lpszFileName,
  95. LONG cRef,
  96. LONG *pcRefOld = NULL);
  97. HRESULT GetSizeOfFile(LPCTSTR lpszFile, LPDWORD lpSize);
  98. HRESULT CleanOrphanedRegistry(
  99. LPCTSTR szFileName,
  100. LPCTSTR szClientClsId,
  101. LPCTSTR szTypeLibCLSID);
  102. HRESULT UnregisterOCX(LPCTSTR pszFile);
  103. HRESULT GetDirectory(
  104. UINT nDirType,
  105. LPTSTR szDirBuffer,
  106. int nBufSize,
  107. LPCTSTR szOCXFullName = NULL);
  108. /*
  109. HRESULT GetTypeLibId(
  110. LPCTSTR lpszClientClsId,
  111. LPTSTR lpszTypeLibId,
  112. LONG* pLibIdSize);
  113. */
  114. HRESULT CleanInterfaceEntries(LPCTSTR lpszTypeLibCLSID);
  115. HRESULT ConvertToLongFileName(
  116. LPTSTR lpszShortFileName,
  117. BOOL bToUpper = FALSE);
  118. void RemoveList(LPCLSIDLIST_ITEM lpListHead);
  119. BOOL ReadInfFileNameFromRegistry(LPCTSTR lpszCLSID, LPTSTR lpszInf, LONG nBufLen);
  120. BOOL WriteInfFileNameToRegistry(LPCTSTR lpszCLSID, LPTSTR lpszInf);
  121. HRESULT FindDLLInModuleUsage(
  122. LPTSTR lpszFileName,
  123. LPCTSTR lpszCLSID,
  124. DWORD &iSubKey);
  125. HRESULT
  126. ExpandCommandLine(
  127. LPCSTR szSrc,
  128. LPSTR szBuf,
  129. DWORD cbBuffer,
  130. const char * szVars[],
  131. const char * szValues[]);
  132. BOOL PatternMatch(LPCTSTR szModName, LPTSTR szSectionName);
  133. DWORD OCCGetLongPathName( LPTSTR szLong, LPCTSTR szShort, DWORD cchBuffer );
  134. TCHAR *CatPathStrN( TCHAR *szDst, const TCHAR *szHead, const TCHAR *szTail, int cchDst );
  135. //=--------------------------------------------------------------------------=
  136. // allocates a temporary buffer that will disappear when it goes out of scope
  137. // NOTE: be careful of that -- make sure you use the string in the same or
  138. // nested scope in which you created this buffer. people should not use this
  139. // class directly. use the macro(s) below.
  140. //
  141. class TempBuffer {
  142. public:
  143. TempBuffer(ULONG cBytes) {
  144. m_pBuf = (cBytes <= 120) ? &m_szTmpBuf : CoTaskMemAlloc(cBytes);
  145. m_fHeapAlloc = (cBytes > 120);
  146. }
  147. ~TempBuffer() {
  148. if (m_pBuf && m_fHeapAlloc) CoTaskMemFree(m_pBuf);
  149. }
  150. void *GetBuffer() {
  151. return m_pBuf;
  152. }
  153. private:
  154. void *m_pBuf;
  155. // we'll use this temp buffer for small cases.
  156. //
  157. char m_szTmpBuf[120];
  158. unsigned m_fHeapAlloc:1;
  159. };
  160. //=--------------------------------------------------------------------------=
  161. // string helpers.
  162. //
  163. // given and ANSI String, copy it into a wide buffer.
  164. // be careful about scoping when using this macro!
  165. //
  166. // how to use the below two macros:
  167. //
  168. // ...
  169. // LPSTR pszA;
  170. // pszA = MyGetAnsiStringRoutine();
  171. // MAKE_WIDEPTR_FROMANSI(pwsz, pszA);
  172. // MyUseWideStringRoutine(pwsz);
  173. // ...
  174. //
  175. // similarily for MAKE_ANSIPTR_FROMWIDE. note that the first param does not
  176. // have to be declared, and no clean up must be done.
  177. //
  178. #define MAKE_WIDEPTR_FROMANSI(ptrname, ansistr) \
  179. long __l##ptrname = (lstrlen(ansistr) + 1) * sizeof(WCHAR); \
  180. TempBuffer __TempBuffer##ptrname(__l##ptrname); \
  181. MultiByteToWideChar(CP_ACP, 0, ansistr, -1, (LPWSTR)__TempBuffer##ptrname.GetBuffer(), __l##ptrname); \
  182. LPWSTR ptrname = (LPWSTR)__TempBuffer##ptrname.GetBuffer()
  183. //
  184. // Note: allocate lstrlenW(widestr) * 2 because its possible for a UNICODE
  185. // character to map to 2 ansi characters this is a quick guarantee that enough
  186. // space will be allocated.
  187. //
  188. #define MAKE_ANSIPTR_FROMWIDE(ptrname, widestr) \
  189. long __l##ptrname = (lstrlenW(widestr) + 1) * 2 * sizeof(char); \
  190. TempBuffer __TempBuffer##ptrname(__l##ptrname); \
  191. WideCharToMultiByte(CP_ACP, 0, widestr, -1, (LPSTR)__TempBuffer##ptrname.GetBuffer(), __l##ptrname, NULL, NULL); \
  192. LPSTR ptrname = (LPSTR)__TempBuffer##ptrname.GetBuffer()
  193. #endif