Leaked source code of windows server 2003
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.

411 lines
13 KiB

  1. #pragma once
  2. #include <windows.h>
  3. #include <oleauto.h>
  4. #include <limits.h>
  5. #include <tchar.h>
  6. #include <string>
  7. #include <sstream>
  8. #include <vector>
  9. using namespace std;
  10. #ifdef _UNICODE
  11. #define tstring wstring
  12. #define tstringstream wstringstream
  13. #else // #ifdef _UNICODE
  14. #define tstring string
  15. #define tstringstream stringstream
  16. #endif // #ifdef _UNICODE
  17. //
  18. // useful debug macros
  19. //
  20. //
  21. // pragma message macro
  22. // usage:
  23. // #pragma UDDIMSG(Fix this later)
  24. // outputs during compile:
  25. // C:\...\Foo.c(25) : Fix this later
  26. //
  27. #define UDDISTR2(x) #x
  28. #define UDDISTR(x) UDDISTR2(x)
  29. #define UDDIMSG(x) message(__FILE__ "(" UDDISTR(__LINE__) ") : " #x)
  30. //
  31. // Break macro - break in debugger
  32. //
  33. #ifdef _X86_
  34. #define UDDIBREAK() _asm { int 3 }
  35. #endif
  36. //
  37. // Assertion macro - trace a message and break if assertion failes
  38. //
  39. #if defined( _DEBUG ) || defined( DBG )
  40. //
  41. // Print the location and expression of the assertion and halt
  42. //
  43. #define UDDIASSERT(exp) \
  44. if( !(exp) ) \
  45. { \
  46. char psz[256]; \
  47. ::_snprintf(psz, 256, "%s(%d) : Assertion Failed! - %s\n", \
  48. __FILE__, __LINE__, #exp); \
  49. OutputDebugStringA(psz); \
  50. UDDIBREAK(); \
  51. }
  52. #else
  53. #define UDDIASSERT(exp)
  54. #endif
  55. //
  56. // Verify macro - like UDDIASSERT but remains in all builds
  57. //
  58. #define UDDIVERIFYST(exp, id, hinst) \
  59. if( !(exp) ) \
  60. { \
  61. _TCHAR szLocalizedString[ 512 ]; \
  62. ::LoadString( hinst, id, szLocalizedString, 512 ); \
  63. OutputDebugString( szLocalizedString ); \
  64. throw CUDDIException((HRESULT)E_FAIL, \
  65. szLocalizedString, _T(__FILE__), \
  66. __LINE__, _T(__TIMESTAMP__), _T(__FUNCTION__) ); \
  67. }
  68. #define UDDIVERIFY(exp, str) \
  69. if( !(exp) ) \
  70. { \
  71. OutputDebugString( str ); \
  72. throw CUDDIException((HRESULT)E_FAIL, \
  73. str, _T(__FILE__), \
  74. __LINE__, _T(__TIMESTAMP__), _T(__FUNCTION__) ); \
  75. }
  76. //
  77. // Verify an hresult - trace description of message if bad HRESULT
  78. //
  79. #define UDDIVERIFYHR(hr) \
  80. if( !SUCCEEDED(hr) ) \
  81. { \
  82. LPWSTR lpMsg; \
  83. int n = ::FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | \
  84. FORMAT_MESSAGE_FROM_SYSTEM | \
  85. FORMAT_MESSAGE_IGNORE_INSERTS, \
  86. NULL, (hr), 0, \
  87. (LPWSTR)&lpMsg, 0, NULL); \
  88. if( n != 0 ) { \
  89. lpMsg[::wcslen(lpMsg) - 2] = 0; \
  90. } else { \
  91. lpMsg = L"Unknown"; \
  92. } \
  93. wstring strMsg(lpMsg); \
  94. ::LocalFree(lpMsg); \
  95. throw CUDDIException((HRESULT)(hr), strMsg, _T(__FILE__), \
  96. __LINE__, _T(__TIMESTAMP__), _T(__FUNCTION__)); \
  97. }
  98. //
  99. // Verify an API call - trace description of message if GetLastError fails
  100. //
  101. #define UDDIVERIFYAPI() \
  102. if( ::GetLastError() != ERROR_SUCCESS ) \
  103. { \
  104. DWORD dwErr = ::GetLastError(); \
  105. LPWSTR lpMsg; \
  106. int n = ::FormatMessageW( FORMAT_MESSAGE_ALLOCATE_BUFFER | \
  107. FORMAT_MESSAGE_FROM_SYSTEM | \
  108. FORMAT_MESSAGE_IGNORE_INSERTS, \
  109. NULL, dwErr, 0, \
  110. (LPWSTR)&lpMsg, 0, NULL); \
  111. if( n != 0 ) { \
  112. lpMsg[ ::wcslen(lpMsg) - 2 ] = 0; \
  113. } else { \
  114. lpMsg = L"Unknown"; \
  115. } \
  116. wstring strMsg( lpMsg ); \
  117. ::LocalFree(lpMsg); \
  118. throw CUDDIException((dwErr), strMsg.c_str(), _T(__FILE__), \
  119. __LINE__, _T(__TIMESTAMP__), _T(__FUNCTION__) ); \
  120. }
  121. //
  122. // main tracing function
  123. //
  124. void UDDITRACE( const char* pszFile, int nLine,
  125. int nSev, int nCat,
  126. const wchar_t* pszContext,
  127. const wchar_t* pszFormat, ... );
  128. //
  129. // Severity codes
  130. // use when calling UDDITRACE to pull in file/line number
  131. //
  132. #define UDDI_SEVERITY_ERR __FILE__, __LINE__, 0x01 // EVENTLOG_ERROR_TYPE These are in WINNT.H
  133. #define UDDI_SEVERITY_WARN __FILE__, __LINE__, 0x02 // EVENTLOG_WARNING_TYPE
  134. #define UDDI_SEVERITY_INFO __FILE__, __LINE__, 0x04 // EVENTLOG_INFORMATION_TYPE
  135. #define UDDI_SEVERITY_PASS __FILE__, __LINE__, 0x08 // EVENTLOG_AUDIT_SUCCESS
  136. #define UDDI_SEVERITY_FAIL __FILE__, __LINE__, 0x10 // EVENTLOG_AUDIT_FAILURE
  137. #define UDDI_SEVERITY_VERB __FILE__, __LINE__, 0x20 // most detailed output
  138. //
  139. // Category codes
  140. //
  141. #define UDDI_CATEGORY_NONE 0x00
  142. #define UDDI_CATEGORY_UI 0x01
  143. #define UDDI_CATEGORY_MMC 0x02
  144. #define UDDI_CATEGORY_INSTALL 0x03
  145. //
  146. // Class CUDDIException
  147. // General exception class used for exception handling throughout.
  148. //
  149. class CUDDIException
  150. {
  151. public:
  152. //
  153. // Default constructor for CUDDIException
  154. // Simply sets default parameters.
  155. //
  156. CUDDIException()
  157. : m_dwErr( -1 )
  158. , m_hrErr( E_FAIL )
  159. , m_sErr( _T( "Unknown error" ) )
  160. , m_iLine( -1 ) {}
  161. //
  162. // Copy constructor for CUDDIException
  163. // Deep copy from _copy.
  164. // Params:
  165. // _copy - the source object from which to copy.
  166. //
  167. CUDDIException( const CUDDIException& _copy )
  168. : m_dwErr( _copy.m_dwErr )
  169. , m_hrErr( _copy.m_hrErr )
  170. , m_sErr( _copy.m_sErr )
  171. , m_sBuildTimestamp( _copy.m_sBuildTimestamp )
  172. , m_sFile( _copy.m_sFile )
  173. , m_iLine( _copy.m_iLine )
  174. , m_sFunction( _copy.m_sFunction ) {}
  175. //
  176. // Constructor for CUDDIException
  177. // Generates error info from a DWORD, meaning that it assumes
  178. // the DWORD was generated from GetLastError() and uses the
  179. // system FormatMessage() function to get the error text.
  180. // Params:
  181. // _err - a value returned from GetLastError().
  182. // _file - the source file name where the error occurred.
  183. // _line - the line number in the source where the error
  184. // occurred.
  185. // _timestamp - the build timestamp of the file where the
  186. // error occurred.
  187. //
  188. CUDDIException( DWORD _err, const tstring& _file = _T( "" ), int _line = -1,
  189. const tstring& _timestamp = _T( "" ), const tstring& _function = _T("") )
  190. : m_dwErr( _err )
  191. , m_hrErr( E_FAIL )
  192. , m_sBuildTimestamp( _timestamp )
  193. , m_sFile( _file )
  194. , m_iLine( _line )
  195. , m_sFunction( _function )
  196. {
  197. LPVOID lpMsgBuf;
  198. FormatMessage( // Gets the error text from the OS
  199. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  200. FORMAT_MESSAGE_FROM_SYSTEM |
  201. FORMAT_MESSAGE_IGNORE_INSERTS,
  202. NULL,
  203. _err,
  204. MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
  205. ( LPTSTR )&lpMsgBuf,
  206. 0,
  207. NULL
  208. );
  209. m_sErr = ( LPTSTR )lpMsgBuf;
  210. LocalFree( lpMsgBuf );
  211. }
  212. //
  213. // Constructor for CUDDIException
  214. // Stores error info from a DWORD and passed-in string.
  215. // Params:
  216. // _err - a DWORD error value.
  217. // _str - error text.
  218. // _file - the source file name where the error occurred.
  219. // _line - the line number in the source where the error
  220. // occurred.
  221. // _timestamp - the build timestamp of the file where the
  222. // error occurred.
  223. //
  224. CUDDIException( DWORD _err, const tstring& _str, const tstring& _file = _T( "" ),
  225. int _line = -1, const tstring& _timestamp = _T( "" ), const tstring& _function = _T("") )
  226. : m_dwErr( _err )
  227. , m_sErr( _str )
  228. , m_hrErr( E_FAIL )
  229. , m_sBuildTimestamp( _timestamp )
  230. , m_sFile( _file )
  231. , m_iLine( _line )
  232. , m_sFunction(_function) {}
  233. //
  234. // Constructor for CUDDIException
  235. // Stores error info from an HRESULT and an error string.
  236. // Params:
  237. // _hr - an HRESULT.
  238. // _str - an error string.
  239. // _file - the source file name where the error occurred.
  240. // _line - the line number in the source where the error
  241. // occurred.
  242. // _timestamp - the build timestamp of the file where the
  243. // error occurred.
  244. //
  245. CUDDIException( HRESULT _hr, const tstring& _str, const tstring& _file = _T( "" ),
  246. int _line = -1, const tstring& _timestamp = _T( "" ), const tstring& _function = _T("") )
  247. : m_dwErr( -1 )
  248. , m_sErr( _str )
  249. , m_hrErr( _hr )
  250. , m_sBuildTimestamp( _timestamp )
  251. , m_sFile( _file )
  252. , m_iLine( _line )
  253. , m_sFunction( _function ) {}
  254. //
  255. // Constructor for CUDDIException
  256. // Stores error info from an error string.
  257. // Params:
  258. // _str - an error string.
  259. // _file - the source file name where the error occurred.
  260. // _line - the line number in the source where the error
  261. // occurred.
  262. // _timestamp - the build timestamp of the file where the
  263. // error occurred.
  264. //
  265. CUDDIException( const tstring& _str, const tstring& _file = _T( "" ),
  266. int _line = -1, const tstring& _timestamp = _T( "" ), const tstring& _function = _T("") )
  267. : m_dwErr( -1 )
  268. , m_sErr( _str )
  269. , m_hrErr( E_FAIL )
  270. , m_sBuildTimestamp( _timestamp )
  271. , m_sFile( _file )
  272. , m_iLine( _line )
  273. , m_sFunction( _function ) {}
  274. //
  275. // Assignment operator for CUDDIException
  276. // Deep copy from _copy.
  277. // Params:
  278. // _copy - the source object from which to copy.
  279. //
  280. CUDDIException& operator=( const CUDDIException& _copy )
  281. {
  282. m_dwErr = _copy.m_dwErr;
  283. m_hrErr = _copy.m_hrErr;
  284. m_sErr = _copy.m_sErr;
  285. m_sBuildTimestamp = _copy.m_sBuildTimestamp;
  286. m_sFile = _copy.m_sFile;
  287. m_iLine = _copy.m_iLine;
  288. m_sFunction = _copy.m_sFunction;
  289. }
  290. //
  291. // Cast operators
  292. // We use cast operators to return the various error values
  293. // that can be stored in the error object. Thus, the following
  294. // code is possible:
  295. // CUDDIException _err( GetLastError() );
  296. // DWORD dwErr = _err; // this will be GetLastError()
  297. // HRESULT hrErr = _err; // this will be E_FAIL
  298. // Tstring strErr = _err; // this will be the text description
  299. // // of GetLastError
  300. //
  301. operator DWORD() const { return m_dwErr; }
  302. operator HRESULT() const { return m_hrErr; }
  303. operator const tstring&() const { return m_sErr; }
  304. operator LPCTSTR() const { return m_sErr.c_str(); }
  305. const tstring& GetTimeStamp() const { return m_sBuildTimestamp; }
  306. const tstring& GetFile() const { return m_sFile; }
  307. int GetLine() const { return m_iLine; }
  308. const tstring& GetFunction() const { return m_sFunction; }
  309. const tstring GetEntireError() const
  310. {
  311. tstringstream strm;
  312. strm << _T("Error: ") << m_sErr
  313. << _T("\nCode: 0x") << hex << m_hrErr;
  314. #if defined(_DEBUG) || defined(DBG)
  315. strm << _T("\nFile: ") << m_sFile
  316. << _T("\nFunction: ") << m_sFunction
  317. << _T("\nLine: ") << m_iLine;
  318. #endif
  319. return strm.str();
  320. }
  321. private:
  322. DWORD m_dwErr;
  323. HRESULT m_hrErr;
  324. tstring m_sErr;
  325. tstring m_sBuildTimestamp;
  326. tstring m_sFile;
  327. tstring m_sFunction;
  328. int m_iLine;
  329. };
  330. #define THROW_UDDIEXCEPTION_ST( _hr_, _id_, _hinst_ ) \
  331. _TCHAR szLocalizedString[ 1024 ]; \
  332. ::LoadString( _hinst_, _id_, szLocalizedString, 1024 ); \
  333. throw CUDDIException((HRESULT)_hr_, \
  334. szLocalizedString, _T(__FILE__), \
  335. __LINE__, _T(__TIMESTAMP__), _T(__FUNCTION__) ); \
  336. #define THROW_UDDIEXCEPTION( _hr_, _str_ ) \
  337. throw CUDDIException( (HRESULT)(_hr_), _str_, _T( __FILE__ ), __LINE__, _T( __TIMESTAMP__ ), _T( __FUNCTION__) );
  338. #define THROW_UDDIEXCEPTION_RC( _rc_, _str_ ) \
  339. throw CUDDIException( (DWORD)(_rc_), _str_, _T( __FILE__ ), __LINE__, _T( __TIMESTAMP__ ), _T( __FUNCTION__) );
  340. //
  341. //
  342. //
  343. typedef vector<tstring> StringVector;
  344. class CUDDIRegistryKey
  345. {
  346. public:
  347. CUDDIRegistryKey( const tstring& szRoot, REGSAM access = KEY_ALL_ACCESS, const tstring& szComputer=_T("") );
  348. CUDDIRegistryKey( HKEY hHive, const tstring& szRoot, REGSAM access = KEY_ALL_ACCESS, const tstring& szComputer=_T("") );
  349. ~CUDDIRegistryKey();
  350. void Close();
  351. DWORD GetDWORD( const LPCTSTR szName, DWORD dwDefault );
  352. DWORD GetDWORD( const LPCTSTR szName );
  353. tstring GetString( const LPCTSTR szName, const LPCTSTR szDefault );
  354. tstring GetString( const LPCTSTR szName );
  355. void GetMultiString( const LPCTSTR szName, StringVector& strs );
  356. void SetValue( const LPCTSTR szName, DWORD dwValue );
  357. void SetValue( const LPCTSTR szName, LPCTSTR szValue );
  358. void DeleteValue( const tstring& szValue );
  359. static void Create( HKEY hHive, const tstring& szPath, const tstring& szComputer=_T("") );
  360. static void DeleteKey( HKEY hHive, const tstring& szPath, const tstring& szComputer=_T("") );
  361. static BOOL KeyExists( HKEY hHive, const tstring& szPath, const tstring& szComputer=_T("") );
  362. //
  363. // implements the "get" property for the registry key handle
  364. //
  365. HKEY GetCurrentHandle() { return m_hkey; }
  366. private:
  367. HKEY m_hHive;
  368. HKEY m_hkey;
  369. tstring m_szRoot;
  370. };
  371. void UDDIMsgBox( HWND hwndParent, int idMsg, int idTitle, UINT nType, LPCTSTR szDetail = NULL );
  372. void UDDIMsgBox( HWND hwndParent, LPCTSTR szMsg, int idTitle, UINT nType, LPCTSTR szDetail = NULL );
  373. wstring LocalizedDate( const wstring& str );
  374. wstring LocalizedDateTime( const wstring& str );