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.

1143 lines
33 KiB

  1. //////////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Win32Unicode.h
  4. //
  5. // Copyright (C) 1998, 1999 Microsoft Corporation. All rights reserved.
  6. //
  7. // Abstract :
  8. //
  9. // History :
  10. //
  11. // 05/06/1999 luish Created
  12. //
  13. //////////////////////////////////////////////////////////////////////////////////////////////
  14. #include "Win32API.h"
  15. #include "AppManDebug.h"
  16. #include "Global.h"
  17. #include "AppMan.h"
  18. #include "Resource.h"
  19. //To flag as DBG_WIN32
  20. #ifdef DBG_MODULE
  21. #undef DBG_MODULE
  22. #endif
  23. #define DBG_MODULE DBG_WIN32
  24. //////////////////////////////////////////////////////////////////////////////////////////////
  25. //
  26. //////////////////////////////////////////////////////////////////////////////////////////////
  27. CWin32API::CWin32API(void)
  28. {
  29. FUNCTION("CWin32API::CWin32API (void)");
  30. OSVERSIONINFO sVersionInfo;
  31. //
  32. // Default down to OS_VERSION_WIN32S in case the call to GetVersionEx() fails
  33. //
  34. m_dwOSVersion = OS_VERSION_WIN32S;
  35. sVersionInfo.dwOSVersionInfoSize = sizeof(sVersionInfo);
  36. if (GetVersionEx(&sVersionInfo))
  37. {
  38. switch (sVersionInfo.dwPlatformId)
  39. {
  40. case VER_PLATFORM_WIN32_NT
  41. : m_dwOSVersion = OS_VERSION_WINNT;
  42. break;
  43. case VER_PLATFORM_WIN32_WINDOWS
  44. : if ((sVersionInfo.dwMajorVersion > 4)||((sVersionInfo.dwMajorVersion == 4)&&(sVersionInfo.dwMinorVersion > 0)))
  45. {
  46. m_dwOSVersion = OS_VERSION_WIN98;
  47. }
  48. else
  49. {
  50. if (1111 <= sVersionInfo.dwBuildNumber)
  51. {
  52. m_dwOSVersion = OS_VERSION_WIN95_OSR2;
  53. }
  54. else
  55. {
  56. m_dwOSVersion = OS_VERSION_WIN95;
  57. }
  58. }
  59. break;
  60. case VER_PLATFORM_WIN32s
  61. : m_dwOSVersion = OS_VERSION_WIN32S;
  62. break;
  63. }
  64. }
  65. }
  66. //////////////////////////////////////////////////////////////////////////////////////////////
  67. //
  68. //////////////////////////////////////////////////////////////////////////////////////////////
  69. CWin32API::~CWin32API(void)
  70. {
  71. FUNCTION("CWin32API::~CWin32API (void)");
  72. }
  73. //////////////////////////////////////////////////////////////////////////////////////////////
  74. //
  75. //////////////////////////////////////////////////////////////////////////////////////////////
  76. DWORD CWin32API::WideCharToMultiByte(LPCWSTR wszSourceString, const DWORD dwSourceLen, LPSTR szDestinationString, const DWORD dwDestinationLen)
  77. {
  78. FUNCTION("CWin32API::WideCharToMultiByte ()");
  79. DWORD dwCount;
  80. dwCount = 0;
  81. //
  82. // Check to make sure that the basic incoming parameters are valid
  83. //
  84. if ((NULL == wszSourceString)||(0 == dwSourceLen))
  85. {
  86. return 0;
  87. }
  88. //
  89. // If the dwDestinationLen parameter is set to 0, then the user is just fishing for the strlen of wszSourceString.
  90. // Otherwise the user wants to copy some stuff.
  91. //
  92. if (0 == dwDestinationLen)
  93. {
  94. while ((dwCount < dwSourceLen)&&(0 != wszSourceString[dwCount]))
  95. {
  96. dwCount++;
  97. }
  98. }
  99. else
  100. {
  101. //
  102. // Check some additional parameters for validity
  103. //
  104. if (NULL == szDestinationString)
  105. {
  106. return 0;
  107. }
  108. while ((dwCount < dwSourceLen)&&(0 != wszSourceString[dwCount]))
  109. {
  110. szDestinationString[dwCount] = (CHAR) wszSourceString[dwCount];
  111. dwCount++;
  112. }
  113. szDestinationString[dwCount] = 0;
  114. //dwCount = ::WideCharToMultiByte(CP_ACP, 0, wszSourceString, dwSourceLen, szDestinationString, dwDestinationLen, NULL, NULL);
  115. }
  116. return dwCount;
  117. }
  118. //////////////////////////////////////////////////////////////////////////////////////////////
  119. //
  120. //////////////////////////////////////////////////////////////////////////////////////////////
  121. DWORD CWin32API::MultiByteToWideChar(LPCSTR szSourceString, const DWORD dwSourceLen, LPWSTR wszDestinationString, const DWORD dwDestinationLen)
  122. {
  123. FUNCTION("CWin32API::MultiByteToWideChar()");
  124. DWORD dwCount = 0;
  125. //
  126. // Check to make sure that the basic incoming parameters are valid
  127. //
  128. if ((NULL == szSourceString)||(0 == dwSourceLen))
  129. {
  130. return 0;
  131. }
  132. //
  133. // If the dwDestinationLen parameter is set to 0, then the user is just fishing for the strlen of wszSourceString.
  134. // Otherwise the user wants to copy some stuff.
  135. //
  136. if (0 == dwDestinationLen)
  137. {
  138. while ((dwCount < dwSourceLen)&&(0 != szSourceString[dwCount]))
  139. {
  140. dwCount++;
  141. }
  142. }
  143. else
  144. {
  145. //
  146. // Check some additional parameters for validity
  147. //
  148. if (NULL == wszDestinationString)
  149. {
  150. return 0;
  151. }
  152. while ((dwCount < dwSourceLen)&&(0 != szSourceString[dwCount]))
  153. {
  154. wszDestinationString[dwCount] = (WCHAR) szSourceString[dwCount];
  155. dwCount++;
  156. }
  157. wszDestinationString[dwCount] = 0;
  158. //dwCount = ::MultiByteToWideChar(CP_ACP, 0, szSourceString, dwSourceLen, wszDestinationString, dwDestinationLen);
  159. }
  160. return dwCount;
  161. }
  162. //////////////////////////////////////////////////////////////////////////////////////////////
  163. //
  164. //////////////////////////////////////////////////////////////////////////////////////////////
  165. DWORD CWin32API::GetOSVersion(void)
  166. {
  167. FUNCTION("CWin32API::GetOSVersion ()");
  168. return m_dwOSVersion;
  169. }
  170. //////////////////////////////////////////////////////////////////////////////////////////////
  171. //
  172. //////////////////////////////////////////////////////////////////////////////////////////////
  173. DWORD CWin32API::GetDriveType(LPCSTR lpRootPathName)
  174. {
  175. FUNCTION("CWin32API::GetDriveType ()");
  176. return ::GetDriveType(lpRootPathName);
  177. }
  178. //////////////////////////////////////////////////////////////////////////////////////////////
  179. //
  180. //////////////////////////////////////////////////////////////////////////////////////////////
  181. DWORD CWin32API::GetDriveType(LPCWSTR lpRootPathName)
  182. {
  183. FUNCTION("CWin32API::GetDriveType ()");
  184. CHAR szString[MAX_PATH_CHARCOUNT];
  185. ZeroMemory(szString, sizeof(szString));
  186. if (WideCharToMultiByte(lpRootPathName, MAX_PATH_CHARCOUNT, szString, MAX_PATH_CHARCOUNT))
  187. {
  188. return GetDriveType(szString);
  189. }
  190. return DRIVE_NO_ROOT_DIR;
  191. }
  192. //////////////////////////////////////////////////////////////////////////////////////////////
  193. //
  194. //////////////////////////////////////////////////////////////////////////////////////////////
  195. BOOL CWin32API::IsDriveFormatted(LPCSTR lpRootPathName)
  196. {
  197. FUNCTION("CWin32API::IsDriveFormatted()");
  198. BOOL fReturnValue = FALSE;
  199. DWORD dwVolumeSerial;
  200. if (GetVolumeInformation(lpRootPathName, NULL, 0, &(dwVolumeSerial)))
  201. {
  202. fReturnValue = TRUE;
  203. }
  204. return fReturnValue;
  205. }
  206. //////////////////////////////////////////////////////////////////////////////////////////////
  207. //
  208. //////////////////////////////////////////////////////////////////////////////////////////////
  209. BOOL CWin32API::IsDriveFormatted(LPCWSTR lpRootPathName)
  210. {
  211. FUNCTION("CWin32API::IsDriveFormatted()");
  212. CHAR szString[MAX_PATH_CHARCOUNT];
  213. ZeroMemory(szString, sizeof(szString));
  214. if (WideCharToMultiByte(lpRootPathName, MAX_PATH_CHARCOUNT, szString, MAX_PATH_CHARCOUNT))
  215. {
  216. return IsDriveFormatted(szString);
  217. }
  218. return FALSE;
  219. }
  220. //////////////////////////////////////////////////////////////////////////////////////////////
  221. //
  222. // We want to make sure to create all the directories in the lpPathName tree
  223. //
  224. //////////////////////////////////////////////////////////////////////////////////////////////
  225. BOOL CWin32API::CreateDirectory(LPCSTR lpPathName, const BOOL fInitAppManRoot)
  226. {
  227. FUNCTION("CWin32API::CreateDirectory ()");
  228. CHAR szString[MAX_PATH_CHARCOUNT+1];
  229. DWORD dwIndex;
  230. BOOL fSuccess = TRUE;
  231. //
  232. // Does the directory already exist
  233. //
  234. if (!FileExists(lpPathName))
  235. {
  236. //
  237. // Make sure the Application Manager root paths are build if required
  238. //
  239. if (fInitAppManRoot)
  240. {
  241. WCHAR wszAppManRoot[MAX_PATH_CHARCOUNT+1];
  242. WCHAR wszAppManSetup[MAX_PATH_CHARCOUNT+1];
  243. WCHAR wszString[MAX_PATH_CHARCOUNT+1];
  244. DWORD dwDeviceIndex;
  245. //
  246. // Create the root AppMan directory on the device if it doesn't already exist BUG BUG
  247. //
  248. dwDeviceIndex = (DWORD) lpPathName[0];
  249. (OS_VERSION_9x & GetOSVersion()) ? GetResourceStringW(IDS_APPMAN9x, wszAppManRoot, MAX_PATH_CHARCOUNT): GetResourceStringW(IDS_APPMANNT, wszAppManRoot, MAX_PATH_CHARCOUNT);
  250. swprintf(wszString, L"%c:\\%s", dwDeviceIndex, wszAppManRoot);
  251. if (FALSE == FileExists(wszString))
  252. {
  253. CreateDirectory(wszString, FALSE);
  254. SetFileAttributes(wszString, FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_READONLY);
  255. }
  256. //
  257. // Now create the root AppMan system directory on the device if it doesn't already exist
  258. //
  259. GetResourceStringW(IDS_APPMAN, wszAppManSetup, MAX_PATH_CHARCOUNT);
  260. swprintf(wszString, L"%c:\\%s\\%s", dwDeviceIndex, wszAppManRoot, wszAppManSetup);
  261. if (FALSE == FileExists(wszString))
  262. {
  263. CreateDirectory(wszString, FALSE);
  264. SetFileAttributes(wszString, FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_READONLY);
  265. }
  266. }
  267. //
  268. // Copy the first three bytes from lpPathName to szString (i.e. "C:\x")
  269. //
  270. dwIndex = 4;
  271. memcpy(szString, lpPathName, dwIndex);
  272. szString[dwIndex] = 0;
  273. //
  274. // Continue copying the lpPathName string to szString and everytime the '\' character is
  275. // detected, try to create that directory
  276. //
  277. while ((MAX_PATH_CHARCOUNT > dwIndex)&&(fSuccess)&&(0 != lpPathName[dwIndex]))
  278. {
  279. if (('\\' == lpPathName[dwIndex])&&(!FileExists(szString)))
  280. {
  281. fSuccess = ::CreateDirectoryA(szString, NULL);
  282. }
  283. szString[dwIndex] = lpPathName[dwIndex];
  284. szString[dwIndex+1] = 0;
  285. dwIndex++;
  286. }
  287. fSuccess = ::CreateDirectoryA(szString, NULL);
  288. }
  289. return fSuccess;
  290. }
  291. //////////////////////////////////////////////////////////////////////////////////////////////
  292. //
  293. //////////////////////////////////////////////////////////////////////////////////////////////
  294. BOOL CWin32API::CreateDirectory(LPCWSTR lpPathName, const BOOL fInitAppManRoot)
  295. {
  296. FUNCTION("CWin32API::CreateDirectory ()");
  297. CHAR szString[MAX_PATH_CHARCOUNT];
  298. ZeroMemory(szString, sizeof(szString));
  299. if (WideCharToMultiByte(lpPathName, MAX_PATH_CHARCOUNT, szString, MAX_PATH_CHARCOUNT))
  300. {
  301. return CreateDirectory(szString, fInitAppManRoot);
  302. }
  303. return FALSE;
  304. }
  305. //////////////////////////////////////////////////////////////////////////////////////////////
  306. //
  307. //////////////////////////////////////////////////////////////////////////////////////////////
  308. BOOL CWin32API::RemoveDirectory(LPCSTR lpszPathName)
  309. {
  310. FUNCTION("CWin32API::RemoveDirectory ()");
  311. WIN32_FIND_DATA FindData;
  312. HANDLE hFindFile;
  313. BOOL fContinue;
  314. char szFileMask[MAX_PATH_CHARCOUNT], szFilename[MAX_PATH_CHARCOUNT];
  315. //
  316. // Define the file mask
  317. //
  318. wsprintfA(szFileMask, "%s\\*.*", lpszPathName);
  319. //
  320. // For each file in the file mask, delete it if it has the archive bit on
  321. //
  322. (INVALID_HANDLE_VALUE == (hFindFile = FindFirstFile(szFileMask, &FindData))) ? fContinue = FALSE : fContinue = TRUE;
  323. while (TRUE == fContinue)
  324. {
  325. //
  326. // Check to see whether we need to step into a directory
  327. //
  328. if (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  329. {
  330. //
  331. // This is a directory, so lets delete it
  332. //
  333. if (FindData.cFileName[0] != '.')
  334. {
  335. wsprintfA(szFilename, "%s\\%s", lpszPathName, FindData.cFileName);
  336. RemoveDirectory(szFilename);
  337. ::RemoveDirectory(szFilename);
  338. }
  339. }
  340. else
  341. {
  342. wsprintfA(szFilename, "%s\\%s", lpszPathName, FindData.cFileName);
  343. SetFileAttributes(szFilename, FILE_ATTRIBUTE_NORMAL);
  344. DeleteFile(szFilename);
  345. }
  346. fContinue = FindNextFile(hFindFile, &FindData);
  347. }
  348. FindClose(hFindFile);
  349. return ::RemoveDirectory(lpszPathName);
  350. }
  351. //////////////////////////////////////////////////////////////////////////////////////////////
  352. //
  353. //////////////////////////////////////////////////////////////////////////////////////////////
  354. BOOL CWin32API::RemoveDirectory(LPCWSTR lpPathName)
  355. {
  356. FUNCTION("CWin32API::RemoveDirectory ()");
  357. CHAR szString[MAX_PATH_CHARCOUNT];
  358. ZeroMemory(szString, sizeof(szString));
  359. if (WideCharToMultiByte(lpPathName, MAX_PATH_CHARCOUNT, szString, MAX_PATH_CHARCOUNT))
  360. {
  361. return RemoveDirectory(szString);
  362. }
  363. return FALSE;
  364. }
  365. //////////////////////////////////////////////////////////////////////////////////////////////
  366. //
  367. //////////////////////////////////////////////////////////////////////////////////////////////
  368. DWORD CWin32API::GetDirectorySize(LPCSTR lpPathName)
  369. {
  370. FUNCTION("CWin32API::GetDirectorySize ()");
  371. WIN32_FIND_DATA FindData;
  372. HANDLE hFindFile;
  373. BOOL fContinue;
  374. DWORD dwKilobytes;
  375. char szFileMask[MAX_PATH_CHARCOUNT], szFilename[MAX_PATH_CHARCOUNT];
  376. //
  377. // Start at 0
  378. //
  379. dwKilobytes = 0;
  380. //
  381. // Define the file mask
  382. //
  383. wsprintfA(szFileMask, "%s\\*.*", lpPathName);
  384. //
  385. // For each file in the file mask, delete it if it has the archive bit on
  386. //
  387. (INVALID_HANDLE_VALUE == (hFindFile = FindFirstFile(szFileMask, &FindData))) ? fContinue = FALSE : fContinue = TRUE;
  388. while (TRUE == fContinue)
  389. {
  390. //
  391. // Check to see whether we need to step into a directory
  392. //
  393. if (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  394. {
  395. if (FindData.cFileName[0] != '.')
  396. {
  397. wsprintfA(szFilename, "%s\\%s", lpPathName, FindData.cFileName);
  398. dwKilobytes += GetDirectorySize(szFilename);
  399. }
  400. }
  401. else
  402. {
  403. dwKilobytes += FindData.nFileSizeLow >> 10;
  404. dwKilobytes += FindData.nFileSizeHigh << 22;
  405. }
  406. fContinue = FindNextFile(hFindFile, &FindData);
  407. }
  408. FindClose(hFindFile);
  409. return dwKilobytes;
  410. }
  411. //////////////////////////////////////////////////////////////////////////////////////////////
  412. //
  413. //////////////////////////////////////////////////////////////////////////////////////////////
  414. DWORD CWin32API::GetDirectorySize(LPCWSTR lpPathName)
  415. {
  416. FUNCTION("CWin32API::GetDirectorySize ()");
  417. CHAR szString[MAX_PATH_CHARCOUNT];
  418. ZeroMemory(szString, sizeof(szString));
  419. if (WideCharToMultiByte(lpPathName, MAX_PATH_CHARCOUNT, szString, MAX_PATH_CHARCOUNT))
  420. {
  421. return GetDirectorySize(szString);
  422. }
  423. return 0;
  424. }
  425. //////////////////////////////////////////////////////////////////////////////////////////////
  426. //
  427. //////////////////////////////////////////////////////////////////////////////////////////////
  428. BOOL CWin32API::CreateProcess(LPSTR lpCommandLine, PROCESS_INFORMATION * lpProcessInfo)
  429. {
  430. FUNCTION("CWin32API::CreateProcess ()");
  431. STARTUPINFO sStartupInfo;
  432. ZeroMemory(&sStartupInfo, sizeof(sStartupInfo));
  433. sStartupInfo.cb = sizeof(sStartupInfo);
  434. ZeroMemory(lpProcessInfo, sizeof(PROCESS_INFORMATION));
  435. return ::CreateProcess(NULL, lpCommandLine, NULL, NULL, FALSE, 0, NULL, NULL, &sStartupInfo, lpProcessInfo);
  436. }
  437. //////////////////////////////////////////////////////////////////////////////////////////////
  438. //
  439. //////////////////////////////////////////////////////////////////////////////////////////////
  440. BOOL CWin32API::CreateProcess(LPWSTR lpCommandLine, PROCESS_INFORMATION * lpProcessInfo)
  441. {
  442. FUNCTION("CWin32API::CreateProcess ()");
  443. CHAR szString[MAX_PATH_CHARCOUNT];
  444. ZeroMemory(szString, sizeof(szString));
  445. if (WideCharToMultiByte(lpCommandLine, MAX_PATH_CHARCOUNT, szString, MAX_PATH_CHARCOUNT))
  446. {
  447. return CreateProcess(szString, lpProcessInfo);
  448. }
  449. return FALSE;
  450. }
  451. //////////////////////////////////////////////////////////////////////////////////////////////
  452. //
  453. //////////////////////////////////////////////////////////////////////////////////////////////
  454. BOOL CWin32API::CreateProcess(LPSTR lpApplication, LPSTR lpCommandLine, PROCESS_INFORMATION * lpProcessInfo)
  455. {
  456. FUNCTION("CWin32API::CreateProcess ()");
  457. STARTUPINFO sStartupInfo;
  458. ZeroMemory(&sStartupInfo, sizeof(sStartupInfo));
  459. sStartupInfo.cb = sizeof(sStartupInfo);
  460. ZeroMemory(lpProcessInfo, sizeof(PROCESS_INFORMATION));
  461. return ::CreateProcess(lpApplication, lpCommandLine, NULL, NULL, FALSE, 0, NULL, NULL, &sStartupInfo, lpProcessInfo);
  462. }
  463. //////////////////////////////////////////////////////////////////////////////////////////////
  464. //
  465. //////////////////////////////////////////////////////////////////////////////////////////////
  466. BOOL CWin32API::CreateProcess(LPWSTR /*lpApplication*/, LPWSTR lpCommandLine, PROCESS_INFORMATION * lpProcessInfo) // Get rid of /W4 warning.
  467. {
  468. FUNCTION("CWin32API::CreateProcess ()");
  469. CHAR szApplicationString[MAX_PATH_CHARCOUNT];
  470. CHAR szCommandLineString[MAX_PATH_CHARCOUNT];
  471. ZeroMemory(szCommandLineString, sizeof(szCommandLineString));
  472. if (WideCharToMultiByte(lpCommandLine, MAX_PATH_CHARCOUNT, szCommandLineString, MAX_PATH_CHARCOUNT))
  473. {
  474. ZeroMemory(szApplicationString, sizeof(szApplicationString));
  475. if (WideCharToMultiByte(lpCommandLine, MAX_PATH_CHARCOUNT, szApplicationString, MAX_PATH_CHARCOUNT))
  476. {
  477. return CreateProcess(szApplicationString, szCommandLineString, lpProcessInfo);
  478. }
  479. }
  480. return FALSE;
  481. }
  482. //////////////////////////////////////////////////////////////////////////////////////////////
  483. //
  484. //////////////////////////////////////////////////////////////////////////////////////////////
  485. BOOL CWin32API::IsValidFilename(LPCSTR lpFilename)
  486. {
  487. FUNCTION("CWin32API::IsValidFilename ()");
  488. BOOL fValid = TRUE;
  489. DWORD dwStringLen, dwIndex;
  490. dwStringLen = StrLenA(lpFilename);
  491. dwIndex = strcspn(lpFilename, "\\/<>:|*?\"\x0\x1\x2\x3\x4\x5\x6\x7\x8\x9\xa\xb\xc\xd\xe\xf\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e") + 1;
  492. if (255 < dwStringLen)
  493. {
  494. fValid = FALSE;
  495. }
  496. else if (1 == dwStringLen)
  497. {
  498. fValid = FALSE;
  499. }
  500. else if ((0 == _strnicmp(lpFilename, ".", 1))||(0 == _strnicmp(lpFilename, "..", 2)))
  501. {
  502. fValid = FALSE;
  503. }
  504. else if ((dwStringLen > dwIndex)&&(0 < dwIndex))
  505. {
  506. fValid = FALSE;
  507. }
  508. return fValid;
  509. }
  510. //////////////////////////////////////////////////////////////////////////////////////////////
  511. //
  512. //////////////////////////////////////////////////////////////////////////////////////////////
  513. BOOL CWin32API::IsValidFilename(LPCWSTR lpFilename)
  514. {
  515. FUNCTION("CWin32API::IsValidFilename ()");
  516. CHAR szString[MAX_PATH_CHARCOUNT];
  517. ZeroMemory(szString, sizeof(szString));
  518. if (WideCharToMultiByte(lpFilename, MAX_PATH_CHARCOUNT, szString, MAX_PATH_CHARCOUNT))
  519. {
  520. return IsValidFilename(szString);
  521. }
  522. return FALSE;
  523. }
  524. //////////////////////////////////////////////////////////////////////////////////////////////
  525. //
  526. //////////////////////////////////////////////////////////////////////////////////////////////
  527. BOOL CWin32API::FileExists(LPCSTR lpFilename)
  528. {
  529. FUNCTION("CWin32API::FileExists ()");
  530. WIN32_FIND_DATA sFindFileInfo;
  531. HANDLE hFindFileHandle;
  532. hFindFileHandle = FindFirstFile(lpFilename, &sFindFileInfo);
  533. if (INVALID_HANDLE_VALUE == hFindFileHandle)
  534. {
  535. return FALSE;
  536. }
  537. FindClose(hFindFileHandle);
  538. return TRUE;
  539. }
  540. //////////////////////////////////////////////////////////////////////////////////////////////
  541. //
  542. //////////////////////////////////////////////////////////////////////////////////////////////
  543. BOOL CWin32API::FileExists(LPCWSTR lpFilename)
  544. {
  545. FUNCTION("CWin32API::FileExists ()");
  546. CHAR szString[MAX_PATH_CHARCOUNT];
  547. ZeroMemory(szString, sizeof(szString));
  548. if (WideCharToMultiByte(lpFilename, MAX_PATH_CHARCOUNT, szString, MAX_PATH_CHARCOUNT))
  549. {
  550. return FileExists(szString);
  551. }
  552. return FALSE;
  553. }
  554. //////////////////////////////////////////////////////////////////////////////////////////////
  555. //
  556. //////////////////////////////////////////////////////////////////////////////////////////////
  557. DWORD CWin32API::FileAttributes(LPCSTR lpFilename)
  558. {
  559. FUNCTION("CWin32API::FileAttributes()");
  560. WIN32_FIND_DATA sFindFileInfo;
  561. HANDLE hFindFileHandle;
  562. hFindFileHandle = FindFirstFile(lpFilename, &sFindFileInfo);
  563. if (INVALID_HANDLE_VALUE == hFindFileHandle)
  564. {
  565. return 0;
  566. }
  567. FindClose(hFindFileHandle);
  568. return sFindFileInfo.dwFileAttributes;
  569. }
  570. //////////////////////////////////////////////////////////////////////////////////////////////
  571. //
  572. //////////////////////////////////////////////////////////////////////////////////////////////
  573. DWORD CWin32API::FileAttributes(LPCWSTR lpFilename)
  574. {
  575. FUNCTION("CWin32API::FileAttributes()");
  576. CHAR szString[MAX_PATH_CHARCOUNT];
  577. ZeroMemory(szString, sizeof(szString));
  578. if (WideCharToMultiByte(lpFilename, MAX_PATH_CHARCOUNT, szString, MAX_PATH_CHARCOUNT))
  579. {
  580. return FileAttributes(szString);
  581. }
  582. return 0;
  583. }
  584. //////////////////////////////////////////////////////////////////////////////////////////////
  585. //
  586. //////////////////////////////////////////////////////////////////////////////////////////////
  587. DWORD CWin32API::GetFileSize(LPCSTR lpFilename)
  588. {
  589. FUNCTION("CWin32API::GetFileSize()");
  590. HANDLE hFileHandle;
  591. DWORD dwFileSize;
  592. dwFileSize = 0;
  593. hFileHandle = CreateFile(lpFilename, GENERIC_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL);
  594. if (INVALID_HANDLE_VALUE != hFileHandle)
  595. {
  596. dwFileSize = (::GetFileSize(hFileHandle, NULL)) / 1024;
  597. CloseHandle(hFileHandle);
  598. }
  599. return dwFileSize;
  600. }
  601. //////////////////////////////////////////////////////////////////////////////////////////////
  602. //
  603. //////////////////////////////////////////////////////////////////////////////////////////////
  604. DWORD CWin32API::GetFileSize(LPCWSTR lpFilename)
  605. {
  606. FUNCTION("CWin32API::GetFileSize()");
  607. CHAR szString[MAX_PATH_CHARCOUNT];
  608. ZeroMemory(szString, sizeof(szString));
  609. if (WideCharToMultiByte(lpFilename, MAX_PATH_CHARCOUNT, szString, MAX_PATH_CHARCOUNT))
  610. {
  611. return GetFileSize(szString);
  612. }
  613. return 0;
  614. }
  615. //////////////////////////////////////////////////////////////////////////////////////////////
  616. //
  617. //////////////////////////////////////////////////////////////////////////////////////////////
  618. HANDLE CWin32API::CreateFile(LPCSTR lpFilename, DWORD dwDesiredAccess, DWORD dwShareMode, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes)
  619. {
  620. FUNCTION("CWin32API::CreateFile ()");
  621. return ::CreateFile(lpFilename, dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, dwFlagsAndAttributes, NULL);
  622. }
  623. //////////////////////////////////////////////////////////////////////////////////////////////
  624. //
  625. //////////////////////////////////////////////////////////////////////////////////////////////
  626. HANDLE CWin32API::CreateFile(LPCWSTR lpFilename, DWORD dwDesiredAccess, DWORD dwShareMode, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes)
  627. {
  628. FUNCTION("CWin32API::CreateFile ()");
  629. CHAR szString[MAX_PATH_CHARCOUNT];
  630. ZeroMemory(szString, sizeof(szString));
  631. if (WideCharToMultiByte(lpFilename, MAX_PATH_CHARCOUNT, szString, MAX_PATH_CHARCOUNT))
  632. {
  633. return CreateFile(szString, dwDesiredAccess, dwShareMode, dwCreationDisposition, dwFlagsAndAttributes);
  634. }
  635. return INVALID_HANDLE_VALUE;
  636. }
  637. //////////////////////////////////////////////////////////////////////////////////////////////
  638. //
  639. //////////////////////////////////////////////////////////////////////////////////////////////
  640. BOOL CWin32API::DeleteFile(LPCSTR lpFilename)
  641. {
  642. FUNCTION("CWin32API::DeleteFile ()");
  643. return ::DeleteFile(lpFilename);
  644. }
  645. //////////////////////////////////////////////////////////////////////////////////////////////
  646. //
  647. //////////////////////////////////////////////////////////////////////////////////////////////
  648. BOOL CWin32API::DeleteFile(LPCWSTR lpFilename)
  649. {
  650. FUNCTION("CWin32API::DeleteFile ()");
  651. CHAR szString[MAX_PATH_CHARCOUNT];
  652. ZeroMemory(szString, sizeof(szString));
  653. if (WideCharToMultiByte(lpFilename, MAX_PATH_CHARCOUNT, szString, MAX_PATH_CHARCOUNT))
  654. {
  655. return DeleteFile(szString);
  656. }
  657. return FALSE;
  658. }
  659. //////////////////////////////////////////////////////////////////////////////////////////////
  660. //
  661. //////////////////////////////////////////////////////////////////////////////////////////////
  662. HANDLE CWin32API::CreateFileMapping(HANDLE hFile, DWORD flProtect, DWORD dwMaximumSizeHigh, DWORD dwMaximumSizeLow, LPCSTR lpName)
  663. {
  664. FUNCTION("CWin32API::CreateFileMapping ()");
  665. return ::CreateFileMapping(hFile, NULL, flProtect, dwMaximumSizeHigh, dwMaximumSizeLow, lpName);
  666. }
  667. //////////////////////////////////////////////////////////////////////////////////////////////
  668. //
  669. //////////////////////////////////////////////////////////////////////////////////////////////
  670. HANDLE CWin32API::CreateFileMapping(HANDLE hFile, DWORD flProtect, DWORD dwMaximumSizeHigh, DWORD dwMaximumSizeLow, LPCWSTR lpName)
  671. {
  672. FUNCTION("CWin32API::CreateFileMapping ()");
  673. CHAR szString[MAX_PATH_CHARCOUNT];
  674. ZeroMemory(szString, sizeof(szString));
  675. if (WideCharToMultiByte(lpName, MAX_PATH_CHARCOUNT, szString, MAX_PATH_CHARCOUNT))
  676. {
  677. return CreateFileMapping(hFile, flProtect, dwMaximumSizeHigh, dwMaximumSizeLow, szString);
  678. }
  679. return NULL;
  680. }
  681. //////////////////////////////////////////////////////////////////////////////////////////////
  682. //
  683. //////////////////////////////////////////////////////////////////////////////////////////////
  684. BOOL CWin32API::CopyFile(LPCSTR lpSourceFileName, LPCSTR lpDestinationFileName, BOOL bFailIfExists)
  685. {
  686. FUNCTION("CWin32API::CopyFile ()");
  687. return ::CopyFile(lpSourceFileName, lpDestinationFileName, bFailIfExists);
  688. }
  689. //////////////////////////////////////////////////////////////////////////////////////////////
  690. //
  691. //////////////////////////////////////////////////////////////////////////////////////////////
  692. BOOL CWin32API::CopyFile(LPCWSTR lpSourceFileName, LPCWSTR lpDestinationFileName, BOOL bFailIfExists)
  693. {
  694. FUNCTION("CWin32API::CopyFile ()");
  695. CHAR szSource[MAX_PATH_CHARCOUNT];
  696. CHAR szDestination[MAX_PATH_CHARCOUNT];
  697. ZeroMemory(szSource, sizeof(szSource));
  698. if (WideCharToMultiByte(lpSourceFileName, MAX_PATH_CHARCOUNT, szSource, MAX_PATH_CHARCOUNT))
  699. {
  700. ZeroMemory(szDestination, sizeof(szDestination));
  701. if (WideCharToMultiByte(lpDestinationFileName, MAX_PATH_CHARCOUNT, szDestination, MAX_PATH_CHARCOUNT))
  702. {
  703. return CopyFile(szSource, szDestination, bFailIfExists);
  704. }
  705. }
  706. return FALSE;
  707. }
  708. //////////////////////////////////////////////////////////////////////////////////////////////
  709. //
  710. //////////////////////////////////////////////////////////////////////////////////////////////
  711. DWORD CWin32API::GetDriveSize(LPCSTR lpRootPathName)
  712. {
  713. FUNCTION("CWin32API::GetDriveSize ()");
  714. ULARGE_INTEGER lFreeBytesAvailableToCaller, lTotalSize, lTotalFreeBytes;
  715. DWORD dwTotalNumberOfClusters, dwSectorsPerCluster, dwBytesPerSector, dwNumberOfFreeClusters;
  716. DWORD dwReturnValue = 0;
  717. if (OS_VERSION_WIN95_OSR2 <= GetOSVersion())
  718. {
  719. //
  720. // Excellent, we can use the better GetDiskFreeSpaceEx function
  721. //
  722. if (GetDiskFreeSpaceEx(lpRootPathName, &lFreeBytesAvailableToCaller, &lTotalSize, &lTotalFreeBytes))
  723. {
  724. dwReturnValue = lTotalSize.LowPart >> 10;
  725. dwReturnValue += lTotalSize.HighPart << 22;
  726. }
  727. }
  728. else
  729. {
  730. //
  731. // Bummer we must make use of the GetDiskFreeSpace function
  732. //
  733. if (GetDiskFreeSpace(lpRootPathName, &dwSectorsPerCluster, &dwBytesPerSector, &dwNumberOfFreeClusters, &dwTotalNumberOfClusters))
  734. {
  735. dwReturnValue = ((dwTotalNumberOfClusters * dwSectorsPerCluster * dwBytesPerSector) / 1024);
  736. }
  737. }
  738. return dwReturnValue;
  739. }
  740. //////////////////////////////////////////////////////////////////////////////////////////////
  741. //
  742. //////////////////////////////////////////////////////////////////////////////////////////////
  743. DWORD CWin32API::GetDriveSize(LPCWSTR lpRootPathName)
  744. {
  745. FUNCTION("CWin32API::GetDriveSize ()");
  746. CHAR szString[MAX_PATH_CHARCOUNT];
  747. ZeroMemory(szString, sizeof(szString));
  748. if (WideCharToMultiByte(lpRootPathName, MAX_PATH_CHARCOUNT, szString, MAX_PATH_CHARCOUNT))
  749. {
  750. return GetDriveSize(szString);
  751. }
  752. return 0;
  753. }
  754. //////////////////////////////////////////////////////////////////////////////////////////////
  755. //
  756. //////////////////////////////////////////////////////////////////////////////////////////////
  757. DWORD CWin32API::GetDriveFreeSpace(LPCSTR lpRootPathName)
  758. {
  759. FUNCTION("CWin32API::GetDriveFreeSpace ()");
  760. ULARGE_INTEGER lFreeBytesAvailableToCaller, lTotalSize, lTotalFreeBytes;
  761. DWORD dwTotalNumberOfClusters, dwSectorsPerCluster, dwBytesPerSector, dwNumberOfFreeClusters;
  762. DWORD dwReturnValue = 0;
  763. if (OS_VERSION_WIN95_OSR2 <= GetOSVersion())
  764. {
  765. //
  766. // Excellent, we can use the better GetDiskFreeSpaceEx function
  767. //
  768. if (GetDiskFreeSpaceEx(lpRootPathName, &lFreeBytesAvailableToCaller, &lTotalSize, &lTotalFreeBytes))
  769. {
  770. dwReturnValue = lTotalFreeBytes.LowPart >> 10;
  771. dwReturnValue += lTotalFreeBytes.HighPart << 22;
  772. }
  773. }
  774. else
  775. {
  776. //
  777. // Bummer we must make use of the GetDiskFreeSpace function
  778. //
  779. if (GetDiskFreeSpace(lpRootPathName, &dwSectorsPerCluster, &dwBytesPerSector, &dwNumberOfFreeClusters, &dwTotalNumberOfClusters))
  780. {
  781. dwReturnValue = ((dwNumberOfFreeClusters * dwSectorsPerCluster * dwBytesPerSector) / 1024);
  782. }
  783. }
  784. return dwReturnValue;
  785. }
  786. //////////////////////////////////////////////////////////////////////////////////////////////
  787. //
  788. //////////////////////////////////////////////////////////////////////////////////////////////
  789. DWORD CWin32API::GetDriveFreeSpace(LPCWSTR lpRootPathName)
  790. {
  791. FUNCTION("CWin32API::GetDriveFreeSpace ()");
  792. CHAR szString[MAX_PATH_CHARCOUNT];
  793. ZeroMemory(szString, sizeof(szString));
  794. if (WideCharToMultiByte(lpRootPathName, MAX_PATH_CHARCOUNT, szString, MAX_PATH_CHARCOUNT))
  795. {
  796. return GetDriveFreeSpace(szString);
  797. }
  798. return 0;
  799. }
  800. //////////////////////////////////////////////////////////////////////////////////////////////
  801. //
  802. //////////////////////////////////////////////////////////////////////////////////////////////
  803. DWORD CWin32API::GetDriveUserFreeSpace(LPCSTR lpRootPathName)
  804. {
  805. FUNCTION("CWin32API::GetDriveUserFreeSpace ()");
  806. ULARGE_INTEGER lFreeBytesAvailableToCaller, lTotalSize, lTotalFreeBytes;
  807. DWORD dwTotalNumberOfClusters, dwSectorsPerCluster, dwBytesPerSector, dwNumberOfFreeClusters;
  808. DWORD dwReturnValue = 0;
  809. if (OS_VERSION_WIN95_OSR2 <= GetOSVersion())
  810. {
  811. //
  812. // Excellent, we can use the better GetDiskFreeSpaceEx function
  813. //
  814. if (GetDiskFreeSpaceEx(lpRootPathName, &lFreeBytesAvailableToCaller, &lTotalSize, &lTotalFreeBytes))
  815. {
  816. dwReturnValue = lFreeBytesAvailableToCaller.LowPart >> 10;
  817. dwReturnValue += lFreeBytesAvailableToCaller.HighPart << 22;
  818. }
  819. }
  820. else
  821. {
  822. //
  823. // Bummer we must make use of the GetDiskFreeSpace function
  824. //
  825. if (GetDiskFreeSpace(lpRootPathName, &dwSectorsPerCluster, &dwBytesPerSector, &dwNumberOfFreeClusters, &dwTotalNumberOfClusters))
  826. {
  827. dwReturnValue = ((dwNumberOfFreeClusters * dwSectorsPerCluster * dwBytesPerSector) / 1024);
  828. }
  829. }
  830. return dwReturnValue;
  831. }
  832. //////////////////////////////////////////////////////////////////////////////////////////////
  833. //
  834. //////////////////////////////////////////////////////////////////////////////////////////////
  835. DWORD CWin32API::GetDriveUserFreeSpace(LPCWSTR lpRootPathName)
  836. {
  837. FUNCTION("CWin32API::GetDriveUserFreeSpace ()");
  838. CHAR szString[MAX_PATH_CHARCOUNT];
  839. ZeroMemory(szString, sizeof(szString));
  840. if (WideCharToMultiByte(lpRootPathName, MAX_PATH_CHARCOUNT, szString, MAX_PATH_CHARCOUNT))
  841. {
  842. return GetDriveUserFreeSpace(szString);
  843. }
  844. return 0;
  845. }
  846. //////////////////////////////////////////////////////////////////////////////////////////////
  847. //
  848. //////////////////////////////////////////////////////////////////////////////////////////////
  849. BOOL CWin32API::GetVolumeInformation(LPCSTR lpRootPathName, LPSTR lpVolumeLabel, const DWORD dwVolumeLabelSize, LPDWORD lpdwVolumeSerialNumber)
  850. {
  851. FUNCTION("CWin32API::GetVolumeInformation ()");
  852. return ::GetVolumeInformation(lpRootPathName, lpVolumeLabel, dwVolumeLabelSize, lpdwVolumeSerialNumber, NULL, NULL, NULL, 0);
  853. }
  854. //////////////////////////////////////////////////////////////////////////////////////////////
  855. //
  856. //////////////////////////////////////////////////////////////////////////////////////////////
  857. BOOL CWin32API::GetVolumeInformation(LPCWSTR lpRootPathName, LPSTR lpVolumeLabel, const DWORD dwVolumeLabelSize, LPDWORD lpdwVolumeSerialNumber)
  858. {
  859. FUNCTION("CWin32API::GetVolumeInformation ()");
  860. CHAR szString[MAX_PATH_CHARCOUNT];
  861. ZeroMemory(szString, sizeof(szString));
  862. if (WideCharToMultiByte(lpRootPathName, MAX_PATH_CHARCOUNT, szString, MAX_PATH_CHARCOUNT))
  863. {
  864. return GetVolumeInformation(szString, lpVolumeLabel, dwVolumeLabelSize, lpdwVolumeSerialNumber);
  865. }
  866. return 0;
  867. }
  868. //////////////////////////////////////////////////////////////////////////////////////////////
  869. //
  870. //////////////////////////////////////////////////////////////////////////////////////////////
  871. BOOL CWin32API::SetFileAttributes(LPCSTR lpFilename, const DWORD dwFileAttributes)
  872. {
  873. FUNCTION("CWin32API::SetFileAttributes ()");
  874. return ::SetFileAttributes(lpFilename, dwFileAttributes);
  875. }
  876. //////////////////////////////////////////////////////////////////////////////////////////////
  877. //
  878. //////////////////////////////////////////////////////////////////////////////////////////////
  879. BOOL CWin32API::SetFileAttributes(LPCWSTR lpFilename, const DWORD dwFileAttributes)
  880. {
  881. FUNCTION("CWin32API::SetFileAttributes ()");
  882. CHAR szString[MAX_PATH_CHARCOUNT];
  883. ZeroMemory(szString, sizeof(szString));
  884. if (WideCharToMultiByte(lpFilename, MAX_PATH_CHARCOUNT, szString, MAX_PATH_CHARCOUNT))
  885. {
  886. return SetFileAttributes(szString, dwFileAttributes);
  887. }
  888. return 0;
  889. }