How can I know the hard disk of my computer?

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
idhan
Experienced Solver
Experienced Solver
Posts: 88
Joined: Tue Feb 28, 2006 9:03 pm

How can I know the hard disk of my computer?

Post by idhan » Thu Mar 20, 2008 5:03 pm

Hi,

I need to know all the hark disks of my computer and also any removable device available. Is for a 3D interface, I can't use any the standard dialogs.

Any helps is more than welcome.

lester
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 211
Joined: Sat Sep 02, 2006 7:24 pm
Location: Ukraine

Post by lester » Thu Mar 20, 2008 5:19 pm

OS?

idhan
Experienced Solver
Experienced Solver
Posts: 88
Joined: Tue Feb 28, 2006 9:03 pm

Post by idhan » Thu Mar 20, 2008 5:20 pm

windows and linux

lester
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 211
Joined: Sat Sep 02, 2006 7:24 pm
Location: Ukraine

Post by lester » Thu Mar 20, 2008 5:34 pm

For windows You can use

#define DRIVE_UNKNOWN 0
#define DRIVE_NO_ROOT_DIR 1
#define DRIVE_REMOVABLE 2
#define DRIVE_FIXED 3
#define DRIVE_REMOTE 4
#define DRIVE_CDROM 5
#define DRIVE_RAMDISK 6

WINBASEAPI
DWORD
WINAPI
GetLogicalDrives(
VOID
);

WINBASEAPI
UINT
WINAPI
GetDriveTypeA(
__in_opt LPCSTR lpRootPathName
);

WINBASEAPI
UINT
WINAPI
GetDriveTypeW(
__in_opt LPCWSTR lpRootPathName
);
#ifdef UNICODE
#define GetDriveType GetDriveTypeW
#else
#define GetDriveType GetDriveTypeA
#endif // !UNICODE

You must use it like:
for( char ch = 'A' ; ch <= 'Z' ; ++ch )
...







For linux maybe you must manually read /etc/fstab, or use <fstab.h>

idhan
Experienced Solver
Experienced Solver
Posts: 88
Joined: Tue Feb 28, 2006 9:03 pm

Post by idhan » Thu Mar 20, 2008 5:47 pm

I want to thanks you help, but is there any wxwidgets solution instead of a specific api solution?

Post data: probably with you knowledge of the api windows, you could write a wxWidgets class and later some other api experts for linux and macos create the equivalent interface, that will be great.

lester
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 211
Joined: Sat Sep 02, 2006 7:24 pm
Location: Ukraine

Post by lester » Thu Mar 20, 2008 5:55 pm

Maybe no, that's internal wxWidgets function for wxGenericDirCtrl:

Code: Select all

// ----------------------------------------------------------------------------
// wxGetAvailableDrives, for WINDOWS, DOS, OS2, MAC, UNIX (returns "/")
// ----------------------------------------------------------------------------

size_t wxGetAvailableDrives(wxArrayString &paths, wxArrayString &names, wxArrayInt &icon_ids)
{
#if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)

#ifdef __WXWINCE__
    // No logical drives; return "\"
    paths.Add(wxT("\\"));
    names.Add(wxT("\\"));
    icon_ids.Add(wxFileIconsTable::computer);
#elif defined(__WIN32__)
    wxChar driveBuffer[256];
    size_t n = (size_t) GetLogicalDriveStrings(255, driveBuffer);
    size_t i = 0;
    while (i < n)
    {
        wxString path, name;
        path.Printf(wxT("%c:\\"), driveBuffer[i]);
        name.Printf(wxT("%c:"), driveBuffer[i]);

        // Do not use GetVolumeInformation to further decorate the
        // name, since it can cause severe delays on network drives.

        int imageId;
        int driveType = ::GetDriveType(path);
        switch (driveType)
        {
            case DRIVE_REMOVABLE:
                if (path == wxT("a:\\") || path == wxT("b:\\"))
                    imageId = wxFileIconsTable::floppy;
                else
                    imageId = wxFileIconsTable::removeable;
                break;
            case DRIVE_CDROM:
                imageId = wxFileIconsTable::cdrom;
                break;
            case DRIVE_REMOTE:
            case DRIVE_FIXED:
            default:
                imageId = wxFileIconsTable::drive;
                break;
        }

        paths.Add(path);
        names.Add(name);
        icon_ids.Add(imageId);

        while (driveBuffer[i] != wxT('\0'))
            i ++;
        i ++;
        if (driveBuffer[i] == wxT('\0'))
            break;
    }
#elif defined(__OS2__)
    APIRET rc;
    ULONG ulDriveNum = 0;
    ULONG ulDriveMap = 0;
    rc = ::DosQueryCurrentDisk(&ulDriveNum, &ulDriveMap);
    if ( rc == 0)
    {
        size_t i = 0;
        while (i < 26)
        {
            if (ulDriveMap & ( 1 << i ))
            {
                wxString path, name;
                path.Printf(wxT("%c:\\"), 'A' + i);
                name.Printf(wxT("%c:"), 'A' + i);

                // Note: If _filesys is unsupported by some compilers,
                //       we can always replace it by DosQueryFSAttach
                char filesysname[20];
#ifdef __WATCOMC__
                ULONG cbBuffer = sizeof(filesysname);
                PFSQBUFFER2 pfsqBuffer = (PFSQBUFFER2)filesysname;
                APIRET rc = ::DosQueryFSAttach(name.fn_str(),0,FSAIL_QUERYNAME,pfsqBuffer,&cbBuffer);
                if (rc != NO_ERROR)
                {
                    filesysname[0] = '\0';
                }
#else
                _filesys(name.fn_str(), filesysname, sizeof(filesysname));
#endif
                /* FAT, LAN, HPFS, CDFS, NFS */
                int imageId;
                if (path == wxT("A:\\") || path == wxT("B:\\"))
                    imageId = wxFileIconsTable::floppy;
                else if (!strcmp(filesysname, "CDFS"))
                    imageId = wxFileIconsTable::cdrom;
                else if (!strcmp(filesysname, "LAN") ||
                         !strcmp(filesysname, "NFS"))
                    imageId = wxFileIconsTable::drive;
                else
                    imageId = wxFileIconsTable::drive;
                paths.Add(path);
                names.Add(name);
                icon_ids.Add(imageId);
            }
            i ++;
        }
    }
#else // !__WIN32__, !__OS2__
    int drive;

    /* If we can switch to the drive, it exists. */
    for( drive = 1; drive <= 26; drive++ )
    {
        wxString path, name;
        path.Printf(wxT("%c:\\"), (char) (drive + 'a' - 1));
        name.Printf(wxT("%c:"), (char) (drive + 'A' - 1));

        if (wxIsDriveAvailable(path))
        {
            paths.Add(path);
            names.Add(name);
            icon_ids.Add((drive <= 2) ? wxFileIconsTable::floppy : wxFileIconsTable::drive);
        }
    }
#endif // __WIN32__/!__WIN32__

#elif defined(__WXMAC__)

    ItemCount volumeIndex = 1;
    OSErr err = noErr ;

    while( noErr == err )
    {
        HFSUniStr255 volumeName ;
        FSRef fsRef ;
        FSVolumeInfo volumeInfo ;
        err = FSGetVolumeInfo(0, volumeIndex, NULL, kFSVolInfoFlags , &volumeInfo , &volumeName, &fsRef);
        if( noErr == err )
        {
            wxString path = wxMacFSRefToPath( &fsRef ) ;
            wxString name = wxMacHFSUniStrToString( &volumeName ) ;

            if ( (volumeInfo.flags & kFSVolFlagSoftwareLockedMask) || (volumeInfo.flags & kFSVolFlagHardwareLockedMask) )
            {
                icon_ids.Add(wxFileIconsTable::cdrom);
            }
            else
            {
                icon_ids.Add(wxFileIconsTable::drive);
            }
            // todo other removable

            paths.Add(path);
            names.Add(name);
            volumeIndex++ ;
        }
    }

#elif defined(__UNIX__)
    paths.Add(wxT("/"));
    names.Add(wxT("/"));
    icon_ids.Add(wxFileIconsTable::computer);
#else
    #error "Unsupported platform in wxGenericDirCtrl!"
#endif
    wxASSERT_MSG( (paths.GetCount() == names.GetCount()), wxT("The number of paths and their human readable names should be equal in number."));
    wxASSERT_MSG( (paths.GetCount() == icon_ids.GetCount()), wxT("Wrong number of icons for available drives."));
    return paths.GetCount();
}

lester
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 211
Joined: Sat Sep 02, 2006 7:24 pm
Location: Ukraine

Post by lester » Thu Mar 20, 2008 5:57 pm

idhan wrote:Post data: probably with you knowledge of the api windows, you could write a wxWidgets class and later some other api experts for linux and macos create the equivalent interface, that will be great.
I'll try :)

DavidHart
Site Admin
Site Admin
Posts: 4008
Joined: Thu Jan 12, 2006 6:23 pm
Location: IoW, UK

Post by DavidHart » Thu Mar 20, 2008 6:25 pm

Hi,

See http://forums.wxwidgets.org/viewtopic.php?t=6804 for a Linux viewpoint.

Regards,

David

Post Reply