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.

559 lines
14 KiB

  1. /******************************************************************************
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. srshutil.cpp
  5. Abstract:
  6. This file contains the implementation of common utility functions.
  7. Revision History:
  8. Seong Kook Khang (SKKhang) 06/22/00
  9. created
  10. ******************************************************************************/
  11. #include "stdwin.h"
  12. #include "rstrcore.h"
  13. #include "resource.h"
  14. /****************************************************************************/
  15. LPWSTR IStrDup( LPCWSTR cszSrc )
  16. {
  17. TraceFunctEnter("IStrDup");
  18. int ccLen = 0 ;
  19. LPWSTR szNew = NULL ;
  20. if ( cszSrc == NULL || cszSrc[0] == L'\0' )
  21. goto Exit;
  22. ccLen = ::lstrlen( cszSrc );
  23. szNew = new WCHAR[ccLen+2];
  24. if ( szNew == NULL )
  25. {
  26. //LOGLOG - Insufficient memory!!!
  27. goto Exit;
  28. }
  29. ::lstrcpy( szNew, cszSrc );
  30. Exit:
  31. TraceFunctLeave();
  32. return( szNew );
  33. }
  34. /****************************************************************************/
  35. DWORD StrCpyAlign4( LPBYTE pbDst, LPCWSTR cszSrc )
  36. {
  37. DWORD dwLen = 0 ;
  38. if ( cszSrc != NULL )
  39. dwLen = ::lstrlen( cszSrc ) * sizeof(WCHAR);
  40. if ( cszSrc == NULL || dwLen == 0 )
  41. {
  42. *((LPDWORD)pbDst) = 0;
  43. }
  44. else
  45. {
  46. dwLen = ( dwLen + sizeof(WCHAR) + 3 ) & ~3;
  47. *((LPDWORD)pbDst) = dwLen;
  48. ::lstrcpy( (LPWSTR)(pbDst+sizeof(DWORD)), cszSrc );
  49. }
  50. return( dwLen+sizeof(DWORD) );
  51. }
  52. /****************************************************************************/
  53. BOOL ReadStrAlign4( HANDLE hFile, LPWSTR szStr )
  54. {
  55. TraceFunctEnter("ReadStrAlign4");
  56. BOOL fRet = FALSE;
  57. DWORD dwLen;
  58. DWORD dwRes;
  59. READFILE_AND_VALIDATE( hFile, &dwLen, sizeof(DWORD), dwRes, Exit );
  60. if ( dwLen > MAX_PATH*sizeof(WCHAR)+sizeof(DWORD) )
  61. {
  62. // Broken log file...
  63. goto Exit;
  64. }
  65. if ( dwLen > 0 )
  66. {
  67. READFILE_AND_VALIDATE( hFile, szStr, dwLen, dwRes, Exit );
  68. }
  69. else
  70. szStr[0] = L'\0';
  71. fRet = TRUE;
  72. Exit:
  73. TraceFunctLeave();
  74. return( fRet );
  75. }
  76. /****************************************************************************/
  77. BOOL
  78. SRFormatMessage( LPWSTR szMsg, UINT uFmtId, ... )
  79. {
  80. TraceFunctEnter("SRFormatMessage");
  81. BOOL fRet = FALSE;
  82. va_list marker;
  83. WCHAR szFmt[MAX_STR];
  84. va_start( marker, uFmtId );
  85. ::LoadString( g_hInst, uFmtId, szFmt, MAX_STR );
  86. if ( 0 == ::FormatMessage( FORMAT_MESSAGE_FROM_STRING,
  87. szFmt,
  88. 0,
  89. 0,
  90. szMsg,
  91. MAX_STR,
  92. &marker ) )
  93. {
  94. LPCWSTR cszErr = ::GetSysErrStr();
  95. ErrorTrace(0, "::FormatMessage failed - %ls", cszErr);
  96. goto Exit;
  97. }
  98. va_end( marker );
  99. fRet = TRUE;
  100. Exit:
  101. TraceFunctLeave();
  102. return( fRet );
  103. }
  104. /****************************************************************************/
  105. BOOL ShowSRErrDlg( UINT uMsgId )
  106. {
  107. TraceFunctEnter("ShowSRErrDlg");
  108. BOOL fRet = FALSE;
  109. LPCWSTR cszErr;
  110. WCHAR szTitle[256];
  111. WCHAR szMsg[1024];
  112. if ( ::LoadString( g_hInst, IDS_SYSTEMRESTORE, szTitle,
  113. sizeof(szTitle )/sizeof(WCHAR) ) == 0 )
  114. {
  115. cszErr = ::GetSysErrStr();
  116. ErrorTrace(0, "::LoadString(%u) failed - %ls", IDS_SYSTEMRESTORE, cszErr);
  117. goto Exit;
  118. }
  119. if ( ::LoadString( g_hInst, uMsgId, szMsg,
  120. sizeof(szMsg )/sizeof(WCHAR) ) == 0 )
  121. {
  122. cszErr = ::GetSysErrStr();
  123. ErrorTrace(0, "::LoadString(%u) failed - %ls", uMsgId, cszErr);
  124. goto Exit;
  125. }
  126. ::MessageBox( NULL, szMsg, szTitle, MB_OK );
  127. fRet = TRUE;
  128. Exit:
  129. TraceFunctLeave();
  130. return( fRet );
  131. }
  132. /****************************************************************************/
  133. BOOL SRGetRegDword( HKEY hKey, LPCWSTR cszSubKey, LPCWSTR cszValue, DWORD *pdwData )
  134. {
  135. TraceFunctEnter("SRGetRegDword");
  136. BOOL fRet = FALSE;
  137. DWORD dwType;
  138. DWORD dwRes;
  139. DWORD cbData;
  140. dwType = REG_DWORD;
  141. cbData = sizeof(DWORD);
  142. dwRes = ::SHGetValue( hKey, cszSubKey, cszValue, &dwType, pdwData, &cbData );
  143. if ( dwRes != ERROR_SUCCESS )
  144. {
  145. LPCWSTR cszErr = ::GetSysErrStr( dwRes );
  146. ErrorTrace(0, "::SHGetValue failed - %ls", cszErr);
  147. goto Exit;
  148. }
  149. fRet = TRUE;
  150. Exit:
  151. TraceFunctLeave();
  152. return( fRet );
  153. }
  154. /****************************************************************************/
  155. BOOL SRSetRegDword( HKEY hKey, LPCWSTR cszSubKey, LPCWSTR cszValue, DWORD dwData )
  156. {
  157. TraceFunctEnter("SRSetRegDword");
  158. BOOL fRet = FALSE;
  159. DWORD dwRes;
  160. dwRes = ::SHSetValue( hKey, cszSubKey, cszValue, REG_DWORD, &dwData, sizeof(DWORD) );
  161. if ( dwRes != ERROR_SUCCESS )
  162. {
  163. LPCWSTR cszErr = ::GetSysErrStr( dwRes );
  164. ErrorTrace(0, "::SHSetValue failed - %ls", cszErr);
  165. goto Exit;
  166. }
  167. fRet = TRUE;
  168. Exit:
  169. TraceFunctLeave();
  170. return( fRet );
  171. }
  172. /****************************************************************************/
  173. BOOL SRSetRegStr( HKEY hKey, LPCWSTR cszSubKey, LPCWSTR cszValue, LPCWSTR cszData )
  174. {
  175. TraceFunctEnter("SRSetRegStr");
  176. BOOL fRet = FALSE;
  177. DWORD dwRes;
  178. dwRes = ::SHSetValue( hKey, cszSubKey, cszValue, REG_SZ, cszData, sizeof(WCHAR)*::lstrlen(cszData) );
  179. if ( dwRes != ERROR_SUCCESS )
  180. {
  181. LPCWSTR cszErr = ::GetSysErrStr( dwRes );
  182. ErrorTrace(0, "::SHSetValue failed - %ls", cszErr);
  183. goto Exit;
  184. }
  185. fRet = TRUE;
  186. Exit:
  187. TraceFunctLeave();
  188. return( fRet );
  189. }
  190. /****************************************************************************/
  191. /*
  192. LPWSTR SRGetRegMultiSz( HKEY hkRoot, LPCWSTR cszSubKey, LPCWSTR cszValue, LPDWORD pdwData )
  193. {
  194. TraceFunctEnter("SRGetRegMultiSz");
  195. LPCWSTR cszErr;
  196. DWORD dwRes;
  197. HKEY hKey = NULL;
  198. DWORD dwType;
  199. DWORD cbData;
  200. LPWSTR szBuf = NULL;
  201. dwRes = ::RegOpenKeyEx( hkRoot, cszSubKey, 0, KEY_ALL_ACCESS, &hKey );
  202. if ( dwRes != ERROR_SUCCESS )
  203. {
  204. cszErr = ::GetSysErrStr( dwRes );
  205. ErrorTrace(0, "::RegOpenKey() failed - %ls", cszErr);
  206. goto Exit;
  207. }
  208. dwRes = ::RegQueryValueEx( hKey, cszValue, 0, &dwType, NULL, &cbData );
  209. if ( dwRes != ERROR_SUCCESS )
  210. {
  211. cszErr = ::GetSysErrStr( dwRes );
  212. ErrorTrace(0, "::RegQueryValueEx(len) failed - %ls", cszErr);
  213. goto Exit;
  214. }
  215. if ( dwType != REG_MULTI_SZ )
  216. {
  217. ErrorTrace(0, "Type of '%ls' is %u (not REG_MULTI_SZ)...", cszValue, dwType);
  218. goto Exit;
  219. }
  220. if ( cbData == 0 )
  221. {
  222. ErrorTrace(0, "Value '%ls' is empty...", cszValue);
  223. goto Exit;
  224. }
  225. szBuf = new WCHAR[cbData+2];
  226. dwRes = ::RegQueryValueEx( hKey, cszValue, 0, &dwType, (LPBYTE)szBuf, &cbData );
  227. if ( dwRes != ERROR_SUCCESS )
  228. {
  229. cszErr = ::GetSysErrStr( dwRes );
  230. ErrorTrace(0, "::RegQueryValueEx(data) failed - %ls", cszErr);
  231. delete [] szBuf;
  232. szBuf = NULL;
  233. }
  234. if ( pdwData != NULL )
  235. *pdwData = cbData;
  236. Exit:
  237. if ( hKey != NULL )
  238. ::RegCloseKey( hKey );
  239. TraceFunctLeave();
  240. return( szBuf );
  241. }
  242. */
  243. /****************************************************************************/
  244. /*
  245. BOOL SRSetRegMultiSz( HKEY hkRoot, LPCWSTR cszSubKey, LPCWSTR cszValue, LPCWSTR cszData, DWORD cbData )
  246. {
  247. TraceFunctEnter("SRSetRegMultiSz");
  248. BOOL fRet = FALSE;
  249. LPCWSTR cszErr;
  250. DWORD dwRes;
  251. HKEY hKey = NULL;
  252. dwRes = ::RegOpenKeyEx( hkRoot, cszSubKey, 0, KEY_ALL_ACCESS, &hKey );
  253. if ( dwRes != ERROR_SUCCESS )
  254. {
  255. cszErr = ::GetSysErrStr( dwRes );
  256. ErrorTrace(0, "::RegOpenKey() failed - %ls", cszErr);
  257. goto Exit;
  258. }
  259. dwRes = ::RegSetValueEx( hKey, cszValue, 0, REG_MULTI_SZ, (LPBYTE)cszData, cbData );
  260. if ( dwRes != ERROR_SUCCESS )
  261. {
  262. cszErr = ::GetSysErrStr( dwRes );
  263. ErrorTrace(0, "::RegSetValueEx() failed - %ls", cszErr);
  264. goto Exit;
  265. }
  266. fRet = TRUE;
  267. Exit:
  268. if ( hKey != NULL )
  269. ::RegCloseKey( hKey );
  270. TraceFunctLeave();
  271. return( fRet );
  272. }
  273. */
  274. /****************************************************************************/
  275. /////////////////////////////////////////////////////////////////////////////
  276. //
  277. // CSRStr class
  278. //
  279. /////////////////////////////////////////////////////////////////////////////
  280. CSRStr::CSRStr()
  281. {
  282. TraceFunctEnter("CSRStr::CSRStr()");
  283. m_cch = 0;
  284. m_str = NULL;
  285. TraceFunctLeave();
  286. }
  287. /****************************************************************************/
  288. CSRStr::CSRStr( LPCWSTR cszSrc )
  289. {
  290. TraceFunctEnter("CSRStr::CSRStr(LPCWSTR)");
  291. m_str = NULL;
  292. SetStr( cszSrc );
  293. TraceFunctLeave();
  294. }
  295. /****************************************************************************/
  296. CSRStr::~CSRStr()
  297. {
  298. TraceFunctEnter("CSRStr::~CSRStr");
  299. Empty();
  300. TraceFunctLeave();
  301. }
  302. /****************************************************************************/
  303. int CSRStr::Length()
  304. {
  305. TraceFunctEnter("CSRStr::Length");
  306. TraceFunctLeave();
  307. return( m_cch );
  308. }
  309. /****************************************************************************/
  310. CSRStr::operator LPCWSTR()
  311. {
  312. TraceFunctEnter("CSRStr::operator LPCWSTR");
  313. TraceFunctLeave();
  314. return( m_str );
  315. }
  316. /****************************************************************************/
  317. void CSRStr::Empty()
  318. {
  319. TraceFunctEnter("CSRStr::Empty");
  320. if ( m_str != NULL )
  321. {
  322. delete [] m_str;
  323. m_str = NULL;
  324. m_cch = 0;
  325. }
  326. TraceFunctLeave();
  327. }
  328. /****************************************************************************/
  329. BOOL CSRStr::SetStr( LPCWSTR cszSrc, int cch )
  330. {
  331. TraceFunctEnter("CSRStr::SetStr(LPCWSTR,int)");
  332. BOOL fRet = FALSE;
  333. Empty();
  334. if ( cszSrc == NULL )
  335. goto Exit;
  336. if ( cch == -1 )
  337. cch = ::lstrlen( cszSrc );
  338. if ( cch > 0 )
  339. {
  340. m_str = new WCHAR[cch+2];
  341. if ( m_str == NULL )
  342. {
  343. ErrorTrace(TRACE_ID, "Insufficient memory...");
  344. goto Exit;
  345. }
  346. ::StrCpyN( m_str, cszSrc, cch+1 );
  347. m_str[cch] = L'\0';
  348. m_cch = cch;
  349. }
  350. fRet = TRUE;
  351. Exit:
  352. TraceFunctLeave();
  353. return( fRet );
  354. }
  355. /****************************************************************************/
  356. const CSRStr& CSRStr::operator =( LPCWSTR cszSrc )
  357. {
  358. TraceFunctEnter("CSRStr::operator =(LPCWSTR)");
  359. SetStr( cszSrc );
  360. TraceFunctLeave();
  361. return( *this );
  362. }
  363. /////////////////////////////////////////////////////////////////////////////
  364. //
  365. // CSRLockFile class
  366. //
  367. /////////////////////////////////////////////////////////////////////////////
  368. CSRLockFile::CSRLockFile()
  369. {
  370. TraceFunctEnter("CSRLockFile::CSRLockFile()");
  371. LPCWSTR cszErr;
  372. LPWSTR szList;
  373. DWORD cbData;
  374. LPCWSTR cszPath;
  375. DWORD dwAttr;
  376. HANDLE hLock;
  377. HMODULE hLoad;
  378. szList = ::SRGetRegMultiSz( HKEY_LOCAL_MACHINE, SRREG_PATH_SHELL, SRREG_VAL_LOCKFILELIST, &cbData );
  379. if ( szList != NULL )
  380. {
  381. cszPath = szList;
  382. while ( *cszPath != L'\0' )
  383. {
  384. dwAttr = ::GetFileAttributes( cszPath );
  385. if ( dwAttr != 0xFFFFFFFF )
  386. {
  387. if ( ( dwAttr & FILE_ATTRIBUTE_DIRECTORY ) != 0 )
  388. {
  389. // Lock Directory...
  390. hLock = ::CreateFile( cszPath, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL );
  391. }
  392. else
  393. {
  394. // Lock File...
  395. hLock = ::CreateFile( cszPath, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL );
  396. }
  397. if ( hLock == INVALID_HANDLE_VALUE )
  398. {
  399. cszErr = ::GetSysErrStr();
  400. ErrorTrace(0, "::CreateFile() failed - %ls", cszErr);
  401. ErrorTrace(0, " cszPath='%ls'", cszPath);
  402. }
  403. m_aryLock.AddItem( hLock );
  404. }
  405. else
  406. ErrorTrace(0, "Object does not exist - '%ls'", cszPath);
  407. cszPath += ::lstrlen( cszPath ) + 1;
  408. }
  409. }
  410. delete [] szList;
  411. szList = ::SRGetRegMultiSz( HKEY_LOCAL_MACHINE, SRREG_PATH_SHELL, SRREG_VAL_LOADFILELIST, &cbData );
  412. if ( szList != NULL )
  413. {
  414. cszPath = szList;
  415. while ( *cszPath != L'\0' )
  416. {
  417. dwAttr = ::GetFileAttributes( cszPath );
  418. if ( dwAttr != 0xFFFFFFFF )
  419. {
  420. hLoad = ::LoadLibrary( cszPath );
  421. if ( hLoad == NULL )
  422. {
  423. cszErr = ::GetSysErrStr();
  424. ErrorTrace(0, "::LoadLibrary() failed - %ls", cszErr);
  425. ErrorTrace(0, " cszPath='%ls'", cszPath);
  426. }
  427. m_aryLoad.AddItem( hLoad );
  428. }
  429. else
  430. ErrorTrace(0, "Executable does not exist - '%ls'", cszPath);
  431. cszPath += ::lstrlen( cszPath ) + 1;
  432. }
  433. }
  434. delete [] szList;
  435. TraceFunctLeave();
  436. }
  437. /****************************************************************************/
  438. CSRLockFile::~CSRLockFile()
  439. {
  440. TraceFunctEnter("CSRLockFile::~CSRLockFile");
  441. LPCWSTR cszErr;
  442. int i;
  443. for ( i = m_aryLock.GetUpperBound(); i >= 0; i-- )
  444. if ( m_aryLock[i] != INVALID_HANDLE_VALUE )
  445. if ( !::CloseHandle( m_aryLock[i] ) )
  446. {
  447. cszErr = ::GetSysErrStr();
  448. ErrorTrace(0, "::CloseHandle(m_aryLock[%d]) failed - %ls", i, cszErr);
  449. }
  450. for ( i = m_aryLoad.GetUpperBound(); i >= 0; i-- )
  451. if ( m_aryLoad[i] != NULL )
  452. if ( !::FreeLibrary( m_aryLoad[i] ) )
  453. {
  454. cszErr = ::GetSysErrStr();
  455. ErrorTrace(0, "::CloseHandle(m_aryLoad[%d]) failed - %ls", i, cszErr);
  456. }
  457. TraceFunctLeave();
  458. }
  459. // end of file