Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Getting logical drives of all Physical drives

Getting logical drives of all Physical drives

Scheduled Pinned Locked Moved C / C++ / MFC
helpdata-structures
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    learningvisualc
    wrote on last edited by
    #1

    Hi all, I have 2 hard disk in my system. I want to get Logical drives of both hard drives i.e. Physical drive 1 contains c: and e: partitions and Physical drive 2 contains d: f: and g: partitions. I have used this code to get the hard disk details.... but it is giving wrong result....

    TCHAR szTemp[BUFSIZE];
    szTemp[0] = '\0';

    if (GetLogicalDriveStrings(BUFSIZE-1, szTemp)) 
    {
      TCHAR szName\[MAX\_PATH\];
      TCHAR szDrive\[MAX\_PATH\] = TEXT(" :");
      BOOL bFound = FALSE;
      TCHAR\* p = szTemp;
    
      do 
      {
        // Copy the drive letter to the template string
        \*szDrive = \*p;
    
    
        // Look up each device name
        if (QueryDosDevice(szDrive, szName, BUFSIZE))
        {
    		CString str = szDrive;
    		str = str+szName;
    		//strcat(szDrive , szName);
    		m\_Tree\_Ctrl.InsertItem(str,root,TVI\_LAST);
    		//m\_Tree\_Ctrl.InsertItem("Physical Drive",root,TVI\_LAST);		
        }
    
        // Go to the next NULL character.
        while (\*p++);
      } while (!bFound && \*p); // end of string
    }
    m\_Tree\_Ctrl.Expand(root,TVE\_EXPAND);
    

    It is making a tree like Drives C:\Device\Harddiskvolume1 D:\Device\Harddiskvolume2 E:\Device\Harddiskvolume3 . . . I am not getting what exactly is the problem... Can anybody help me in this... Thanks in advance

    A 1 Reply Last reply
    0
    • L learningvisualc

      Hi all, I have 2 hard disk in my system. I want to get Logical drives of both hard drives i.e. Physical drive 1 contains c: and e: partitions and Physical drive 2 contains d: f: and g: partitions. I have used this code to get the hard disk details.... but it is giving wrong result....

      TCHAR szTemp[BUFSIZE];
      szTemp[0] = '\0';

      if (GetLogicalDriveStrings(BUFSIZE-1, szTemp)) 
      {
        TCHAR szName\[MAX\_PATH\];
        TCHAR szDrive\[MAX\_PATH\] = TEXT(" :");
        BOOL bFound = FALSE;
        TCHAR\* p = szTemp;
      
        do 
        {
          // Copy the drive letter to the template string
          \*szDrive = \*p;
      
      
          // Look up each device name
          if (QueryDosDevice(szDrive, szName, BUFSIZE))
          {
      		CString str = szDrive;
      		str = str+szName;
      		//strcat(szDrive , szName);
      		m\_Tree\_Ctrl.InsertItem(str,root,TVI\_LAST);
      		//m\_Tree\_Ctrl.InsertItem("Physical Drive",root,TVI\_LAST);		
          }
      
          // Go to the next NULL character.
          while (\*p++);
        } while (!bFound && \*p); // end of string
      }
      m\_Tree\_Ctrl.Expand(root,TVE\_EXPAND);
      

      It is making a tree like Drives C:\Device\Harddiskvolume1 D:\Device\Harddiskvolume2 E:\Device\Harddiskvolume3 . . . I am not getting what exactly is the problem... Can anybody help me in this... Thanks in advance

      A Offline
      A Offline
      Andrew Brock
      wrote on last edited by
      #2

      This is a function in 1 of my utilities which does what i thiny you are wanting

      #include <windows.h>
      #include <stdio.h>

      LPCSTR FormatSize(QWORD nSize) {
      static char szSize[16];
      if (nSize < 1024) {
      sprintf(szSize, "%uB", nSize);
      } else {
      char szScales[] = "KMGTPEZY";
      int nScale = 0;
      for (; nScale < sizeof(szScales); ++nScale) {
      if (nSize < 1024 * 1024) {
      sprintf(szSize, "%6.2f%cB", nSize / 1024.0f, szScales[nScale]);
      break;
      }
      nSize = (nSize + 512) / 1024;
      }
      if (nScale == sizeof(szScales)) {
      strcpy(szSize, "Unknown Size");
      }
      }
      return szSize;
      }

      void PrintDrives(char *szDrives) {
      DWORD nDrives = GetLogicalDrives();
      char szDrive[] = "A:\\";
      char szDriveName[MAX_PATH + 1];
      int nPrintedDrives = 0;
      while (nDrives != 0) {
      if (nDrives & 1) {
      szDrives[nPrintedDrives] = szDrive[0];
      GetVolumeInformation(szDrive, szDriveName, sizeof(szDriveName), NULL, NULL, NULL, NULL, 0);
      QWORD nAvaliable;
      QWORD nTotal;
      QWORD nFree;
      if (!GetDiskFreeSpaceEx(szDrive, (PULARGE_INTEGER)&nAvaliable, (PULARGE_INTEGER)&nTotal, (PULARGE_INTEGER)&nFree)) {
      nAvaliable = 0;
      nTotal = 0;
      nFree = 0;
      }
      if (*szDriveName == 0) {
      if (nTotal > 0) {
      printf("%s (%s)\n", szDrive, FormatSize(nTotal));
      } else {
      printf("%s (Unknown Size)\n", szDrive);
      }
      } else {
      if (nTotal > 0) {
      printf("%s (%s) [%s]\n", szDrive, FormatSize(nTotal), szDriveName);
      } else {
      printf("%s (Unknown Size) [%s]\n", szDrive, szDriveName);
      }
      }
      }
      nDrives >>= 1;
      ++szDrive[0];
      }
      }

      int main(int argc, char *argv[]) {
      char szDrives[27];
      szDrives[26] = 0;
      PrintDrives(szDrives);
      return 0;
      }

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups