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.

245 lines
10 KiB

  1. //=--------------------------------------------------------------------------=
  2. // Util.H
  3. //=--------------------------------------------------------------------------=
  4. // Copyright 1995 Microsoft Corporation. All Rights Reserved.
  5. //
  6. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  7. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  8. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  9. // PARTICULAR PURPOSE.
  10. //=--------------------------------------------------------------------------=
  11. //
  12. // contains utilities that we will find useful.
  13. //
  14. #ifndef _UTIL_H_
  15. #include "Globals.H"
  16. #include "Macros.h"
  17. //=--------------------------------------------------------------------------=
  18. // Structs, Enums, Etc.
  19. //=--------------------------------------------------------------------------=
  20. typedef enum
  21. {
  22. VERSION_LESS_THAN=-1,
  23. VERSION_EQUAL=0,
  24. VERSION_GREATER_THAN=1,
  25. VERSION_NOT_EQUAL=2
  26. } VERSIONRESULT;
  27. //=--------------------------------------------------------------------------=
  28. // Misc Helper Stuff
  29. //=--------------------------------------------------------------------------=
  30. //
  31. HWND GetParkingWindow(void);
  32. HINSTANCE _stdcall GetResourceHandle(LCID lcid = 0); // Optional LCID param
  33. // If not used or zero, we use
  34. // g_lcidLocale.
  35. //=--------------------------------------------------------------------------=
  36. // VERSION.DLL function pointers
  37. //=--------------------------------------------------------------------------=
  38. //
  39. #define DLL_VERSION "VERSION.DLL"
  40. #define FUNC_VERQUERYVALUE "VerQueryValueA"
  41. #define FUNC_GETFILEVERSIONINFO "GetFileVersionInfoA"
  42. #define FUNC_GETFILEVERSIONINFOSIZE "GetFileVersionInfoSizeA"
  43. typedef DWORD (_stdcall * PGETFILEVERSIONINFOSIZE)(LPTSTR lptstrFilename, LPDWORD lpdwHandle);
  44. typedef BOOL (_stdcall * PGETFILEVERSIONINFO)(LPTSTR lpststrFilename, DWORD dwHandle, DWORD dwLen, LPVOID lpData);
  45. typedef BOOL (_stdcall * PVERQUERYVALUE)(const LPVOID pBlock, LPTSTR lpSubBlock, LPVOID *lplpBuffer, PUINT puLen);
  46. BOOL CallGetFileVersionInfoSize(LPTSTR lptstrFilename, LPDWORD lpdwHandle);
  47. BOOL CallGetFileVersionInfo(LPTSTR lpststrFilename, DWORD dwHandle, DWORD dwLen, LPVOID lpData);
  48. BOOL CallVerQueryValue(const LPVOID pBlock, LPTSTR lpSubBlock, LPVOID *lplpBuffer, PUINT puLen);
  49. BOOL _stdcall InitVersionFuncs();
  50. VERSIONRESULT _stdcall CompareDllVersion(const char * pszFilename, BOOL bCompareMajorVerOnly);
  51. BOOL _stdcall GetVerInfo(const char * pszFilename, VS_FIXEDFILEINFO *pffi);
  52. // an array of common OLE automation data types and their sizes [in bytes]
  53. //
  54. extern const BYTE g_rgcbDataTypeSize [];
  55. //=--------------------------------------------------------------------------=
  56. // miscellaneous [useful] numerical constants
  57. //=--------------------------------------------------------------------------=
  58. // the length of a guid once printed out with -'s, leading and trailing bracket,
  59. // plus 1 for NULL
  60. //
  61. // CAUTION: Be careful setting MAX_VERSION too high. Changing this value
  62. // has serious performance implications. This means that for
  63. // every control registered we may call RegOpenKeyEx MAX_VERSION-1 times
  64. // to find out if there is a version dependent ProgId. Since major
  65. // version upgrades of components should be a infrequent occurance
  66. // this should be a reasonable limit for most components.
  67. // PERF: I ran ICECAP for the value below and this lead to RegOpenKeyEx to
  68. // count for 2.2% of unregistration time. I tried 255 and RegOpenKeyEx
  69. // jumps to 44% of unregistration time. This value has no impact on registration time.
  70. //
  71. #define MAX_VERSION 32 // The max number of version dependent ProgIds to look for
  72. #define VERSION_DELTA 10 // Subtract this value from MAX_VERSION to obtain the threshold
  73. // at which we'll throw an assert warning you that the version
  74. // of your component is nearing or exceeding the versions
  75. // (MAX_VERSION) we support
  76. #define GUID_STR_LEN 40
  77. //=--------------------------------------------------------------------------=
  78. // allocates a temporary buffer that will disappear when it goes out of scope
  79. // NOTE: be careful of that -- make sure you use the string in the same or
  80. // nested scope in which you created this buffer. people should not use this
  81. // class directly. use the macro(s) below.
  82. //
  83. class TempBuffer {
  84. public:
  85. TempBuffer(ULONG cBytes) {
  86. m_pBuf = (cBytes <= 120) ? &m_szTmpBuf : CtlHeapAlloc(g_hHeap, 0, cBytes);
  87. m_fHeapAlloc = (cBytes > 120);
  88. if (m_pBuf)
  89. *((unsigned short *)m_pBuf) = 0;
  90. }
  91. ~TempBuffer() {
  92. if (m_pBuf && m_fHeapAlloc) CtlHeapFree(g_hHeap, 0, m_pBuf);
  93. }
  94. void *GetBuffer() {
  95. return m_pBuf;
  96. }
  97. private:
  98. void *m_pBuf;
  99. // we'll use this temp buffer for small cases.
  100. //
  101. char m_szTmpBuf[120];
  102. unsigned m_fHeapAlloc:1;
  103. };
  104. //=--------------------------------------------------------------------------=
  105. // string helpers.
  106. //
  107. // given and ANSI String, copy it into a wide buffer.
  108. // be careful about scoping when using this macro!
  109. //
  110. // how to use the below two macros:
  111. //
  112. // ...
  113. // LPSTR pszA;
  114. // pszA = MyGetAnsiStringRoutine();
  115. // MAKE_WIDEPTR_FROMANSI(pwsz, pszA);
  116. // MyUseWideStringRoutine(pwsz);
  117. // ...
  118. //
  119. // similarily for MAKE_ANSIPTR_FROMWIDE. note that the first param does not
  120. // have to be declared, and no clean up must be done.
  121. //
  122. #define MAKE_WIDEPTR_FROMANSI(ptrname, ansistr) \
  123. long __l##ptrname = (lstrlen(ansistr) + 1) * sizeof(WCHAR); \
  124. TempBuffer __TempBuffer##ptrname(__l##ptrname); \
  125. MultiByteToWideChar(CP_ACP, 0, ansistr, -1, (LPWSTR)__TempBuffer##ptrname.GetBuffer(), __l##ptrname); \
  126. LPWSTR ptrname = (LPWSTR)__TempBuffer##ptrname.GetBuffer()
  127. // * 2 for DBCS handling in below length computation
  128. //
  129. #define MAKE_ANSIPTR_FROMWIDE(ptrname, widestr) \
  130. long __l##ptrname = (lstrlenW(widestr) + 1) * 2 * sizeof(char); \
  131. TempBuffer __TempBuffer##ptrname(__l##ptrname); \
  132. WideCharToMultiByte(CP_ACP, 0, widestr, -1, (LPSTR)__TempBuffer##ptrname.GetBuffer(), __l##ptrname, NULL, NULL); \
  133. LPSTR ptrname = (LPSTR)__TempBuffer##ptrname.GetBuffer()
  134. #define STR_BSTR 0
  135. #define STR_OLESTR 1
  136. #define BSTRFROMANSI(x) (BSTR)MakeWideStrFromAnsi((LPSTR)(x), STR_BSTR)
  137. #define OLESTRFROMANSI(x) (LPOLESTR)MakeWideStrFromAnsi((LPSTR)(x), STR_OLESTR)
  138. #define BSTRFROMRESID(x) (BSTR)MakeWideStrFromResourceId(x, STR_BSTR)
  139. #define OLESTRFROMRESID(x) (LPOLESTR)MakeWideStrFromResourceId(x, STR_OLESTR)
  140. #define COPYOLESTR(x) (LPOLESTR)MakeWideStrFromWide(x, STR_OLESTR)
  141. #define COPYBSTR(x) (BSTR)MakeWideStrFromWide(x, STR_BSTR) // Embedded NULLs not supported
  142. inline BSTR DuplicateBSTR(BSTR bstr) {
  143. return SysAllocStringLen(bstr, SysStringLen(bstr)); }
  144. LPWSTR MakeWideStrFromAnsi(LPSTR, BYTE bType);
  145. LPWSTR MakeWideStrFromResourceId(WORD, BYTE bType);
  146. LPWSTR MakeWideStrFromWide(LPWSTR, BYTE bType);
  147. // takes a GUID, and a pointer to a buffer, and places the string form of the
  148. // GUID in said buffer.
  149. //
  150. int StringFromGuidA(REFIID, LPSTR);
  151. //=--------------------------------------------------------------------------=
  152. // registry helpers.
  153. //
  154. // takes some information about an Automation Object, and places all the
  155. // relevant information about it in the registry.
  156. //
  157. BOOL RegSetMultipleValues(HKEY hkey, ...);
  158. BOOL RegisterUnknownObject(LPCSTR pszObjectName, LPCSTR pszLabelName, REFCLSID riidObject, BOOL fAptThreadSafe);
  159. BOOL RegisterAutomationObject(LPCSTR pszLibName, LPCSTR pszObjectName, LPCSTR pszLabelName, long lObjVer, long lMajorVersion, long lMinorVersion, REFCLSID riidLibrary, REFCLSID riidObject, BOOL fAptThreadSafe);
  160. BOOL RegisterControlObject(LPCSTR pszLibName, LPCSTR pszObjectName, LPCSTR pszLabelName, long lObjMajVer, long lObjMinVer, long lMajorVersion, long lMinorVersion, REFCLSID riidLibrary, REFCLSID riidObject, DWORD dwMiscStatus, WORD wToolboxBitmapId, BOOL fAptThreadSafe, BOOL fControl);
  161. BOOL UnregisterUnknownObject(REFCLSID riidObject, BOOL *pfAllRemoved);
  162. BOOL UnregisterAutomationObject(LPCSTR pszLibName, LPCSTR pszObjectName, long lVersion, REFCLSID riidObject);
  163. #define UnregisterControlObject UnregisterAutomationObject
  164. BOOL UnregisterTypeLibrary(REFCLSID riidLibrary);
  165. // Register/UnregisterUnknownObject helpers to help prevent us from blowing away specific keys
  166. //
  167. BOOL ExistInprocServer(HKEY hkCLSID, char *pszCLSID);
  168. BOOL ExistImplementedCategories(REFCLSID riid);
  169. // Finds out if there are other version dependent ProgIds for our component around
  170. //
  171. BOOL QueryOtherVersionProgIds(LPCSTR pszLibName, LPCSTR pszObjectName, long lVersion, long *plFoundVersion, BOOL *pfFailure);
  172. // Copies the contents of a version dependent ProgId to a version independent ProgId
  173. //
  174. BOOL CopyVersionDependentProgIdToIndependentProgId(LPCSTR pszLibName, LPCSTR pszObjectName, long lVersion);
  175. // Copies a source key (including all sub-keys) to a destination key
  176. //
  177. BOOL CopyRegistrySection(HKEY hkSource, HKEY hkDest);
  178. // deletes a key in the registr and all of it's subkeys
  179. //
  180. BOOL DeleteKeyAndSubKeys(HKEY hk, LPCSTR pszSubKey);
  181. // Path of Windows\Help directory.
  182. //
  183. UINT GetHelpFilePath(char *pszPath, UINT cbPath);
  184. // Helper function for registration
  185. //
  186. void _MakePath(LPSTR pszFull, const char * pszName, LPSTR pszOut);
  187. // TypeLib helper functions
  188. //
  189. HRESULT GetTypeFlagsForGuid(ITypeLib *pTypeLib, REFGUID guidTypeInfo, WORD *pwFlags);
  190. //=--------------------------------------------------------------------------=
  191. // string helpers.
  192. //
  193. char * FileExtension(const char *pszFilename);
  194. //=--------------------------------------------------------------------------=
  195. // conversion helpers.
  196. //
  197. void HiMetricToPixel(const SIZEL *pSizeInHiMetric, SIZEL *pSizeinPixels);
  198. void PixelToHiMetric(const SIZEL *pSizeInPixels, SIZEL *pSizeInHiMetric);
  199. //=--------------------------------------------------------------------------=
  200. // This is a version macro so that versioning can be done in text and binary
  201. // streams.
  202. //
  203. #define VERSION(x,y) MAKELONG(y,x)
  204. #define _UTIL_H_
  205. #endif // _UTIL_H_