diskinfo.c

来自「一本已经绝版的好书」· C语言 代码 · 共 258 行

C
258
字号
/************************************************************
Module name: DiskInfo.C
Notices: Copyright (c) 1995-1997 Jeffrey Richter
************************************************************/


#include "..\CmnHdr.H"                  /* See Appendix C. */
#include <windows.h>
#include <windowsx.h>

#include <tchar.h>
#include <stdio.h>             // For sprintf
#include <string.h>            // For strchr
#include "Resource.H"


/////////////////////////////////////////////////////////////


void Dlg_FillDriveInfo (HWND hwnd, LPTSTR lpszRootPathName) {
   // Variables for processing the drive type information.
   int nDriveType;
   LPCTSTR p;

   // Variables for processing the volume information
   TCHAR szBuf[200];
   TCHAR lpVolumeNameBuffer[200];
   DWORD dwVolumeSerialNumber, dwMaximumComponentLength;
   DWORD dwFileSystemFlags;
   TCHAR lpFileSystemNameBuffer[50];

   // Variables for processing the disk space information
   DWORD dwSectorsPerCluster, dwBytesPerSector;
   DWORD dwFreeClusters, dwClusters;


   // Get the drive type information.
   nDriveType = GetDriveType(lpszRootPathName);
   switch (nDriveType) {
      case DRIVE_UNKNOWN:
         p = __TEXT("Cannot be determined.");
         break;

      case DRIVE_NO_ROOT_DIR:
         p = __TEXT("Path does not exist.");
         break;

      case DRIVE_REMOVABLE:
         p = __TEXT("Removable");
         break;

      case DRIVE_FIXED:
         p = __TEXT("Fixed");
         break;

      case DRIVE_REMOTE:
         p = __TEXT("Remote");
         break;

      case DRIVE_CDROM:
         p = __TEXT("CD-ROM");
         break;

      case DRIVE_RAMDISK:
         p = __TEXT("RAM disk");
         break;

      default:
         p = __TEXT("Unknown");
         break;
   }

   SetWindowText(GetDlgItem(hwnd, IDC_DRIVETYPE), p);

   // Get the volume information.
   if (GetVolumeInformation(lpszRootPathName,
      lpVolumeNameBuffer,
      chDIMOF(lpVolumeNameBuffer), &dwVolumeSerialNumber,
      &dwMaximumComponentLength, &dwFileSystemFlags,
      lpFileSystemNameBuffer,
      chDIMOF(lpFileSystemNameBuffer))) {

      _stprintf(szBuf, __TEXT("%s\n%u\n%u\n"),
         lpVolumeNameBuffer, dwVolumeSerialNumber,
         dwMaximumComponentLength);

      if (dwFileSystemFlags & FS_CASE_IS_PRESERVED)
         _tcscat(szBuf, __TEXT("FS_CASE_IS_PRESERVED"));
      _tcscat(szBuf, __TEXT("\n"));

      if (dwFileSystemFlags & FS_CASE_SENSITIVE)
         _tcscat(szBuf, __TEXT("FS_CASE_SENSITIVE"));
      _tcscat(szBuf, __TEXT("\n"));

      if (dwFileSystemFlags & FS_UNICODE_STORED_ON_DISK)
         _tcscat(szBuf, __TEXT("FS_UNICODE_STORED_ON_DISK"));
      _tcscat(szBuf, __TEXT("\n"));

      if (dwFileSystemFlags & FS_PERSISTENT_ACLS)
         _tcscat(szBuf, __TEXT("FS_PERSISTENT_ACLS"));
      _tcscat(szBuf, __TEXT("\n"));

      if (dwFileSystemFlags & FS_FILE_COMPRESSION)
         _tcscat(szBuf, __TEXT("FS_FILE_COMPRESSION"));
      _tcscat(szBuf, __TEXT("\n"));

      if (dwFileSystemFlags & FS_VOL_IS_COMPRESSED)
         _tcscat(szBuf, __TEXT("FS_VOL_IS_COMPRESSED"));
      _tcscat(szBuf, __TEXT("\n"));

      _tcscat(szBuf, lpFileSystemNameBuffer);
   } else {
      _tcscpy(szBuf, __TEXT("NO VOLUME INFO"));
   }
   SetWindowText(GetDlgItem(hwnd, IDC_VOLINFO), szBuf);


   // Get the disk space information.
   if (GetDiskFreeSpace(lpszRootPathName,
      &dwSectorsPerCluster, &dwBytesPerSector,
      &dwFreeClusters, &dwClusters)) {

      _stprintf(szBuf, __TEXT("%u\n%u\n%u\n%u"),
         dwSectorsPerCluster, dwBytesPerSector,
         dwFreeClusters, dwClusters);
   } else {
      _tcscpy(szBuf, __TEXT("NO\nDISK\nSPACE\nINFO"));
   }
   SetWindowText(GetDlgItem(hwnd, IDC_DISKINFO), szBuf);
}


/////////////////////////////////////////////////////////////


BOOL Dlg_OnInitDialog (HWND hwnd, HWND hwndFocus,
   LPARAM lParam) {

   DWORD  dwNumBytesForDriveStrings;
   HANDLE hHeap;
   LPTSTR pszAllDrives, pszDrive;
   TCHAR  szLogDrive[100];
   HWND   hwndCtl = GetDlgItem(hwnd, IDC_LOGDRIVES);
   int    nNumDrives = 0, nDriveNum;

   // Associate an icon with the dialog box.
   chSETDLGICONS(hwnd, IDI_DISKINFO, IDI_DISKINFO);

   // Get the number of bytes needed to hold all
   // the logical drive strings.
   dwNumBytesForDriveStrings =
      GetLogicalDriveStrings(0, NULL) * sizeof(TCHAR);

   // The GetLogicalDriveStrings function is 
   // supported on this platform.

   // Allocate memory from the heap for the drive
   // string names.
   hHeap = GetProcessHeap();
   pszAllDrives = (LPTSTR) HeapAlloc(hHeap, 
      HEAP_ZERO_MEMORY, dwNumBytesForDriveStrings);

   // Get the drive string names in our buffer.
   GetLogicalDriveStrings(
      dwNumBytesForDriveStrings / sizeof(TCHAR), 
      pszAllDrives);

   // Parse the memory block, and fill the combo box.
   pszDrive = pszAllDrives;
   while (pszDrive[0] != 0) {
      ComboBox_AddString(hwndCtl, pszDrive);
      nNumDrives++;
      pszDrive = _tcschr(pszDrive, 0) + 1;// Go to next string.
   }

   HeapFree(hHeap, 0, pszAllDrives);

   // Initialize the volume information for the first fixed
   // drive so that we don't try to read volume
   // information from a drive that doesn't contain a
   // diskette.
   for (nDriveNum = 0; nDriveNum < nNumDrives; nDriveNum++) {
      ComboBox_GetLBText(hwndCtl, nDriveNum, szLogDrive);
      if (GetDriveType(szLogDrive) == DRIVE_FIXED)
         break;
   }

   if (nDriveNum == nNumDrives) {
      // There are no fixed drives--just use the
      // first drive.
      ComboBox_GetLBText(hwndCtl, nDriveNum = 0, szLogDrive);
   }

   // Select the first fixed drive, or select the first
   // drive if no fixed drives exist.
   ComboBox_SetCurSel(hwndCtl, nDriveNum);

   Dlg_FillDriveInfo(hwnd, szLogDrive);

   return(TRUE);
}


/////////////////////////////////////////////////////////////


void Dlg_OnCommand (HWND hwnd, int id, HWND hwndCtl,
   UINT codeNotify) {

   TCHAR szLogDrive[100];

   switch (id) {
      case IDC_LOGDRIVES:
         if (codeNotify != CBN_SELCHANGE)
            break;

         ComboBox_GetText(hwndCtl, szLogDrive,
            chDIMOF(szLogDrive));
         Dlg_FillDriveInfo(hwnd, szLogDrive);
         break;

      case IDCANCEL:
         EndDialog(hwnd, id);
         break;
   }
}


/////////////////////////////////////////////////////////////


BOOL CALLBACK Dlg_Proc (HWND hwnd, UINT uMsg,
   WPARAM wParam, LPARAM lParam) {

   switch (uMsg) {
      chHANDLE_DLGMSG(hwnd, WM_INITDIALOG, Dlg_OnInitDialog);
      chHANDLE_DLGMSG(hwnd, WM_COMMAND, Dlg_OnCommand);
   }
   return(FALSE);
}


/////////////////////////////////////////////////////////////


int WINAPI _tWinMain (HINSTANCE hinstExe,
   HINSTANCE hinstPrev, LPTSTR pszCmdLine, int nCmdShow) {

   chWARNIFUNICODEUNDERWIN95();
   DialogBox(hinstExe, MAKEINTRESOURCE(IDD_DISKINFO),
      NULL, Dlg_Proc);

   return(0);
}


//////////////////////// End Of File ////////////////////////

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?