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.

522 lines
14 KiB

  1. #include "uddi.h"
  2. #include "globals.h"
  3. #include "resource.h"
  4. #include <shlwapi.h>
  5. #define HKEY_CLASSES_ROOT (( HKEY ) (ULONG_PTR)((LONG)0x80000000) )
  6. #define HKEY_CURRENT_USER (( HKEY ) (ULONG_PTR)((LONG)0x80000001) )
  7. #define HKEY_LOCAL_MACHINE (( HKEY ) (ULONG_PTR)((LONG)0x80000002) )
  8. #define HKEY_USERS (( HKEY ) (ULONG_PTR)((LONG)0x80000003) )
  9. CUDDIRegistryKey::CUDDIRegistryKey( HKEY hHive, const tstring& szRoot, REGSAM access, const tstring& szComputer )
  10. : m_szRoot( szRoot )
  11. , m_hHive( NULL )
  12. , m_hkey( NULL )
  13. {
  14. BOOL bResult = FALSE;
  15. LONG lResult = 0;
  16. TCHAR szComputerName[ 256 ];
  17. DWORD dwSize = 256;
  18. szComputerName[ 0 ] = 0;
  19. bResult = GetComputerName( szComputerName, &dwSize );
  20. UDDIASSERT( bResult );
  21. //
  22. // Open a registry key to hHive on the remote or local server
  23. //
  24. if( szComputer == _T("") || 0 == _tcscmp( szComputerName, szComputer.c_str() ) )
  25. {
  26. lResult = RegOpenKeyEx( hHive, NULL, NULL, access, &m_hHive );
  27. UDDIVERIFYST( ERROR_SUCCESS == lResult, IDS_REGISTRY_OPEN_ERROR, g_hinst );
  28. }
  29. else
  30. {
  31. lResult = RegConnectRegistry( szComputer.c_str(), hHive, &m_hHive );
  32. UDDIVERIFYST( ERROR_SUCCESS == lResult, IDS_REGISTRY_OPEN_REMOTE_ERROR, g_hinst );
  33. }
  34. //
  35. // Open the UDDI sub key for READ and WRITE
  36. //
  37. lResult = RegOpenKeyEx( m_hHive, szRoot.c_str(), 0UL, access, &m_hkey );
  38. if( ERROR_SUCCESS != lResult )
  39. {
  40. _TCHAR szUnable[ 128 ];
  41. ::LoadString( g_hinst, IDS_REGISTRY_UNABLE_TO_OPEN_KEY, szUnable, ARRAYLEN( szUnable ) );
  42. tstring str( szUnable );
  43. str += szRoot;
  44. RegCloseKey( m_hHive );
  45. THROW_UDDIEXCEPTION_RC( lResult, str );
  46. }
  47. }
  48. CUDDIRegistryKey::CUDDIRegistryKey( const tstring& szRoot, REGSAM access, const tstring& szComputer )
  49. : m_szRoot( szRoot )
  50. , m_hHive( NULL )
  51. , m_hkey( NULL )
  52. {
  53. BOOL bResult = FALSE;
  54. LONG lResult = 0;
  55. TCHAR szComputerName[ 256 ];
  56. DWORD dwSize = 256;
  57. szComputerName[ 0 ] = 0;
  58. bResult = GetComputerName( szComputerName, &dwSize );
  59. //
  60. // Open a registry key to HKLM on the remote or local server
  61. //
  62. if( szComputer == _T("") || 0 == _tcscmp( szComputerName, szComputer.c_str() ) )
  63. {
  64. lResult = RegOpenKeyEx( HKEY_LOCAL_MACHINE, NULL, NULL, access, &m_hHive );
  65. UDDIVERIFYST( ERROR_SUCCESS == lResult, IDS_REGISTRY_OPEN_ERROR, g_hinst );
  66. }
  67. else
  68. {
  69. lResult = RegConnectRegistry( szComputer.c_str(), HKEY_LOCAL_MACHINE, &m_hHive );
  70. UDDIVERIFYST( ERROR_SUCCESS == lResult, IDS_REGISTRY_OPEN_REMOTE_ERROR, g_hinst );
  71. }
  72. //
  73. // Open the UDDI sub key for READ and WRITE
  74. //
  75. lResult = RegOpenKeyEx( m_hHive, szRoot.c_str(), 0UL, access, &m_hkey );
  76. if( ERROR_SUCCESS != lResult )
  77. {
  78. _TCHAR szUnable[ 128 ];
  79. ::LoadString( g_hinst, IDS_REGISTRY_UNABLE_TO_OPEN_KEY, szUnable, ARRAYLEN( szUnable ) );
  80. tstring str( szUnable );
  81. str += szRoot;
  82. RegCloseKey( m_hHive );
  83. THROW_UDDIEXCEPTION_RC( lResult, str );
  84. }
  85. }
  86. CUDDIRegistryKey::~CUDDIRegistryKey()
  87. {
  88. Close();
  89. }
  90. void
  91. CUDDIRegistryKey::Create( HKEY hHive, const tstring& szPath, const tstring& szComputer )
  92. {
  93. BOOL bResult = FALSE;
  94. LONG lResult = 0;
  95. TCHAR szComputerName[ 256 ];
  96. DWORD dwSize = 256;
  97. szComputerName[ 0 ] = 0;
  98. bResult = GetComputerName( szComputerName, &dwSize );
  99. HKEY hRoot = NULL;
  100. HKEY hkey = NULL;
  101. UDDIASSERT( bResult );
  102. //
  103. // Open a registry key to hHive on the remote or local server
  104. //
  105. if( szComputer == _T("") || 0 == _tcscmp( szComputerName, szComputer.c_str() ) )
  106. {
  107. lResult = RegOpenKeyEx( hHive, NULL, NULL, KEY_ALL_ACCESS, &hRoot );
  108. UDDIVERIFYST( ERROR_SUCCESS == lResult, IDS_REGISTRY_OPEN_ERROR, g_hinst );
  109. }
  110. else
  111. {
  112. lResult = RegConnectRegistry( szComputer.c_str(), hHive, &hRoot );
  113. UDDIVERIFYST( ERROR_SUCCESS == lResult, IDS_REGISTRY_OPEN_REMOTE_ERROR, g_hinst );
  114. }
  115. //
  116. // Open the UDDI sub key for READ and WRITE
  117. //
  118. lResult = RegCreateKey( hHive, szPath.c_str(), &hkey );
  119. if( ERROR_SUCCESS != lResult )
  120. {
  121. _TCHAR szUnable[ 128 ];
  122. ::LoadString( g_hinst, IDS_REGISTRY_UNABLE_TO_OPEN_KEY, szUnable, ARRAYLEN( szUnable ) );
  123. tstring str( szUnable );
  124. str += szPath;
  125. RegCloseKey( hRoot );
  126. THROW_UDDIEXCEPTION_RC( lResult, str );
  127. }
  128. RegCloseKey( hkey );
  129. RegCloseKey( hRoot );
  130. }
  131. void CUDDIRegistryKey::Close()
  132. {
  133. if( m_hkey )
  134. {
  135. ::RegCloseKey( m_hkey );
  136. m_hkey = NULL;
  137. }
  138. if( m_hHive )
  139. {
  140. ::RegCloseKey( m_hHive );
  141. m_hHive = NULL;
  142. }
  143. }
  144. DWORD CUDDIRegistryKey::GetDWORD( const LPCTSTR szName, DWORD dwDefault )
  145. {
  146. DWORD dwValue = dwDefault;
  147. try
  148. {
  149. dwValue = GetDWORD( szName );
  150. }
  151. catch(...)
  152. {
  153. }
  154. return dwValue;
  155. }
  156. DWORD CUDDIRegistryKey::GetDWORD( const LPCTSTR szName )
  157. {
  158. #if defined( _DEBUG ) || defined( DBG )
  159. OutputDebugString( _T("Reading Registry Value: " ) );
  160. OutputDebugString( szName );
  161. OutputDebugString( _T("\n") );
  162. #endif
  163. DWORD dwValue = 0UL;
  164. DWORD dwSize = sizeof( dwValue );
  165. DWORD dwType = REG_DWORD;
  166. LONG lResult = RegQueryValueEx( m_hkey, szName, NULL, &dwType, (LPBYTE) &dwValue, &dwSize );
  167. UDDIVERIFYST( ERROR_SUCCESS == lResult, IDS_REGISTRY_FAILED_TO_READ_VALUE, g_hinst );
  168. return dwValue;
  169. }
  170. void CUDDIRegistryKey::GetMultiString( const LPCTSTR szName, StringVector& strs )
  171. {
  172. #if defined( _DEBUG ) || defined( DBG )
  173. OutputDebugString( _T("Reading Registry Value: " ) );
  174. OutputDebugString( szName );
  175. OutputDebugString( _T("\n") );
  176. #endif
  177. TCHAR szValue[ 1024 ];
  178. DWORD dwSize = sizeof( szValue );
  179. DWORD dwType = REG_MULTI_SZ;
  180. LONG lResult = RegQueryValueEx( m_hkey, szName, NULL, &dwType, (LPBYTE) szValue, &dwSize );
  181. UDDIVERIFYST( ERROR_SUCCESS == lResult, IDS_REGISTRY_FAILED_TO_READ_VALUE, g_hinst );
  182. _TCHAR* psz = szValue;
  183. while( NULL != psz[ 0 ] )
  184. {
  185. strs.push_back( psz );
  186. psz += _tcslen( psz ) + 1;
  187. }
  188. }
  189. tstring CUDDIRegistryKey::GetString( const LPCTSTR szName, const LPCTSTR szDefault )
  190. {
  191. tstring strReturn = szDefault;
  192. try
  193. {
  194. strReturn = GetString( szName );
  195. }
  196. catch(...)
  197. {
  198. #if defined( _DEBUG ) || defined( DBG )
  199. OutputDebugString( _T("Failed using default value:: " ) );
  200. OutputDebugString( szDefault );
  201. OutputDebugString( _T("\n") );
  202. #endif
  203. }
  204. return strReturn;
  205. }
  206. tstring CUDDIRegistryKey::GetString( const LPCTSTR szName )
  207. {
  208. #if defined( _DEBUG ) || defined( DBG )
  209. OutputDebugString( _T("Reading Registry Value: " ) );
  210. OutputDebugString( szName );
  211. OutputDebugString( _T("\n") );
  212. #endif
  213. TCHAR szValue[ 1024 ];
  214. DWORD dwSize = sizeof( szValue );
  215. DWORD dwType = REG_SZ;
  216. LONG lResult = RegQueryValueEx( m_hkey, szName, NULL, &dwType, (LPBYTE) szValue, &dwSize );
  217. UDDIVERIFYST( ERROR_SUCCESS == lResult, IDS_REGISTRY_FAILED_TO_READ_VALUE, g_hinst );
  218. return tstring( szValue );
  219. }
  220. void CUDDIRegistryKey::SetValue( const LPCTSTR szName, DWORD dwValue )
  221. {
  222. DWORD dwSize = sizeof( dwValue );
  223. LONG lResult = RegSetValueEx( m_hkey, szName, NULL, REG_DWORD, (LPBYTE) &dwValue, sizeof( dwValue ) );
  224. UDDIVERIFYST( ERROR_SUCCESS == lResult, IDS_REGISTRY_FAILED_TO_WRITE_VALUE, g_hinst );
  225. }
  226. void CUDDIRegistryKey::SetValue( const LPCTSTR szName, LPCTSTR szValue )
  227. {
  228. DWORD dwSize = (DWORD) ( _tcslen( szValue ) + 1 ) * sizeof( TCHAR );
  229. LONG lResult = RegSetValueEx( m_hkey, szName, NULL, REG_SZ, (LPBYTE) szValue, dwSize );
  230. UDDIVERIFYST( ERROR_SUCCESS == lResult, IDS_REGISTRY_FAILED_TO_WRITE_VALUE, g_hinst );
  231. }
  232. void CUDDIRegistryKey::DeleteValue( const tstring& szValue )
  233. {
  234. LONG lResult = RegDeleteValue( m_hkey, szValue.c_str() );
  235. UDDIVERIFYST( ERROR_SUCCESS == lResult, IDS_REGISTRY_FAILED_TO_READ_VALUE, g_hinst );
  236. }
  237. BOOL CUDDIRegistryKey::KeyExists( HKEY hHive, const tstring& szPath, const tstring& szComputer )
  238. {
  239. BOOL fRet = FALSE;
  240. TCHAR szComputerName[ 256 ];
  241. DWORD dwSize = 256;
  242. szComputerName[ 0 ] = 0;
  243. GetComputerName( szComputerName, &dwSize );
  244. HKEY hKey = NULL;
  245. HKEY hQueriedKey = NULL;
  246. LONG lResult;
  247. //
  248. // Open a registry key to HKLM on the remote or local server
  249. //
  250. if( szComputer == _T("") || 0 == _tcscmp( szComputerName, szComputer.c_str() ) )
  251. {
  252. lResult = RegOpenKeyEx( hHive, NULL, NULL, KEY_READ, &hKey );
  253. }
  254. else
  255. {
  256. lResult = RegConnectRegistry( szComputer.c_str(), hHive, &hKey );
  257. }
  258. if( ERROR_SUCCESS != lResult )
  259. {
  260. return FALSE;
  261. }
  262. lResult = RegOpenKeyEx( hKey, szPath.c_str(), 0, KEY_READ, &hQueriedKey );
  263. if( ERROR_SUCCESS != lResult )
  264. {
  265. fRet = FALSE;
  266. }
  267. else
  268. {
  269. fRet = TRUE;
  270. RegCloseKey( hQueriedKey );
  271. }
  272. RegCloseKey( hKey );
  273. return fRet;
  274. }
  275. void CUDDIRegistryKey::DeleteKey( HKEY hHive, const tstring& szPath, const tstring& szComputer )
  276. {
  277. BOOL bResult = FALSE;
  278. LONG lResult = 0;
  279. TCHAR szComputerName[ 256 ];
  280. DWORD dwSize = 256;
  281. szComputerName[ 0 ] = 0;
  282. bResult = GetComputerName( szComputerName, &dwSize );
  283. HKEY hKey = NULL;
  284. //
  285. // Open a registry key to HKLM on the remote or local server
  286. //
  287. if( szComputer == _T("") || 0 == _tcscmp( szComputerName, szComputer.c_str() ) )
  288. {
  289. lResult = RegOpenKeyEx( hHive, NULL, NULL, KEY_READ, &hKey );
  290. }
  291. else
  292. {
  293. lResult = RegConnectRegistry( szComputer.c_str(), hHive, &hKey );
  294. }
  295. DWORD dwResult = SHDeleteKey( hKey, szPath.c_str() );
  296. UDDIVERIFYST( ERROR_SUCCESS == dwResult, IDS_REGISTRY_FAILED_TO_READ_VALUE, g_hinst );
  297. }
  298. void UDDIMsgBox( HWND hwndParent, int idMsg, int idTitle, UINT nType, LPCTSTR szDetail )
  299. {
  300. _TCHAR szMessage[ 512 ];
  301. _TCHAR szTitle[ 256 ];
  302. int nResult = LoadString( g_hinst, idMsg, szMessage, ARRAYLEN( szMessage ) - 1 );
  303. if( nResult <= 0 )
  304. {
  305. _sntprintf( szMessage, ARRAYLEN(szMessage), _T("Message string missing. ID=%d"), idMsg );
  306. szMessage[ ARRAYLEN(szMessage) - 1 ] = NULL;
  307. }
  308. nResult = LoadString( g_hinst, idTitle, szTitle, ARRAYLEN( szTitle ) - 1 );
  309. if( nResult <= 0 )
  310. {
  311. _sntprintf( szMessage, ARRAYLEN(szTitle), _T("Title string missing. ID=%d"), idTitle );
  312. }
  313. tstring strMessage = szMessage;
  314. #if defined(DBG) || defined(_DEBUG)
  315. if( szDetail )
  316. {
  317. strMessage += _T("\nDetail:\n\n");
  318. strMessage += szDetail;
  319. }
  320. #endif
  321. MessageBox( hwndParent, strMessage.c_str(), szTitle, nType );
  322. }
  323. void UDDIMsgBox( HWND hwndParent, LPCTSTR szMsg, int idTitle, UINT nType, LPCTSTR szDetail )
  324. {
  325. _TCHAR szMessage[ 512 ];
  326. _TCHAR szTitle[ 256 ];
  327. _tcsncpy( szMessage, szMsg, ARRAYLEN( szMessage ) - 1 );
  328. int nResult = LoadString( g_hinst, idTitle, szTitle, ARRAYLEN( szTitle ) - 1 );
  329. if( nResult <= 0 )
  330. {
  331. _sntprintf( szMessage, ARRAYLEN(szTitle), _T("Title string missing. ID=%d"), idTitle );
  332. }
  333. tstring strMessage = szMessage;
  334. #if defined(DBG) || defined(_DEBUG)
  335. if( szDetail )
  336. {
  337. strMessage += _T("\nDetail:\n\n");
  338. strMessage += szDetail;
  339. }
  340. #endif
  341. MessageBox( hwndParent, strMessage.c_str(), szTitle, nType );
  342. }
  343. //
  344. // This function accepts a date expressed in the format
  345. // mm/dd/yyyy or m/d/yyyy and returns the localized
  346. // representation of that date in the long format.
  347. //
  348. wstring LocalizedDate( const wstring& str )
  349. {
  350. try
  351. {
  352. size_t n = str.length();
  353. size_t nposMonth = str.find( L'/' );
  354. size_t nposDay = str.find( L'/', nposMonth + 2 );
  355. if( ( 1 != nposMonth && 2 != nposMonth ) ||
  356. ( ( nposMonth + 2 != nposDay ) &&
  357. ( nposMonth + 3 != nposDay ) ) ||
  358. ( n < 8 || n > 10 ) )
  359. {
  360. throw "Invalid date format. Date must be of the form x/x/xxxx.";
  361. }
  362. SYSTEMTIME st;
  363. memset( &st, 0x00, sizeof( SYSTEMTIME ) );
  364. st.wYear = (WORD) _wtoi( str.substr( nposDay + 1, 4 ).c_str() );
  365. st.wMonth = (WORD) _wtoi( str.substr( 0, nposMonth ).c_str() );
  366. st.wDay = (WORD) _wtoi( str.substr( nposMonth + 1, nposDay - nposMonth ).c_str() );
  367. DWORD dwRC = ERROR_SUCCESS;
  368. wchar_t szDate[ 150 ];
  369. int nLen = GetDateFormatW(
  370. LOCALE_USER_DEFAULT,
  371. DATE_LONGDATE,
  372. &st,
  373. NULL,
  374. szDate,
  375. 150 );
  376. if( 0 == nLen )
  377. {
  378. throw "GetDateFormat failed";
  379. }
  380. return wstring( szDate );
  381. }
  382. catch(...)
  383. {
  384. return wstring(L"");
  385. }
  386. }
  387. wstring LocalizedDateTime( const wstring& str )
  388. {
  389. try
  390. {
  391. const size_t nposMonth = 0;
  392. const size_t nposDay = 3;
  393. const size_t nposYear = 6;
  394. const size_t nposTime = 10;
  395. const size_t nposHour = 11;
  396. const size_t nposMinute = 14;
  397. const size_t nposSecond = 17;
  398. size_t n = str.length();
  399. if( L'/' != str.c_str()[nposDay - 1] ||
  400. L'/' != str.c_str()[nposYear - 1] ||
  401. L' ' != str.c_str()[nposHour - 1] ||
  402. L':' != str.c_str()[nposMinute - 1] ||
  403. L':' != str.c_str()[nposSecond - 1] ||
  404. n != 19 )
  405. {
  406. throw "Invalid datetime format. Date must be of the form MM/dd/yyyy HH:mm:ss";
  407. }
  408. SYSTEMTIME st;
  409. memset( &st, 0x00, sizeof( SYSTEMTIME ) );
  410. st.wYear = (WORD) _wtoi( str.substr( nposYear, 4 ).c_str() );
  411. st.wMonth = (WORD) _wtoi( str.substr( nposMonth, 2 ).c_str() );
  412. st.wDay = (WORD) _wtoi( str.substr( nposDay, 2 ).c_str() );
  413. st.wHour = (WORD) _wtoi( str.substr( nposHour, 2 ).c_str() );
  414. st.wMinute = (WORD) _wtoi( str.substr( nposMinute, 2 ).c_str() );
  415. st.wSecond = (WORD) _wtoi( str.substr( nposSecond, 2 ).c_str() );
  416. DWORD dwRC = ERROR_SUCCESS;
  417. wchar_t szDate[ 150 ];
  418. int nLen = GetDateFormat(
  419. LOCALE_USER_DEFAULT,
  420. DATE_LONGDATE,
  421. &st,
  422. NULL,
  423. szDate,
  424. ARRAYLEN( szDate ) );
  425. if( 0 == nLen )
  426. {
  427. throw "GetDateFormat failed";
  428. }
  429. dwRC = ERROR_SUCCESS;
  430. wchar_t szTime[ 150 ];
  431. nLen = GetTimeFormat(
  432. LOCALE_USER_DEFAULT,
  433. 0,
  434. &st,
  435. NULL,
  436. szTime,
  437. ARRAYLEN( szTime ) );
  438. if( 0 == nLen )
  439. {
  440. throw "GetTimeFormat failed";
  441. }
  442. wstring strDateTime = szDate;
  443. strDateTime += L" ";
  444. strDateTime += szTime;
  445. return strDateTime;
  446. }
  447. catch(...)
  448. {
  449. return wstring(L"");
  450. }
  451. }