GetDriveType Enhancement

 

Download Source Code and Example


This subroutine handles GetDeviceType when you have an arbitrary path. There are three problems with the GetDeviceType API: (1) if you have a path which contains a file name, it returns the error code DRIVE_NO_ROOT_DIR; (2) it works differently on Windows NT and Windows 95; (3) if you pass it a string that contains a name like "\\.\d\" it does not recognize the local drive. This code fixes these problems. It strips back the name until it has either a drive letter (C:, for example), or a network path name (\\ prefix). It discards any file name portion. It also handles paths that do not contain a drive or UNC name. The program knows how to compensate for the fact that Windows 95 and Windows NT _splitpath parse differently.

GetDriveType("C:") returns DRIVE_FIXED on NT 4.0 and DRIVE_NO_ROOT_DIR on Win95.

GetDriveType("C:\") returns DRIVE_FIXED on both systems.

The sample application, the GetDriveType Explorer, allows you to test this subroutine by typing in a test string. As each character is typed, you see the result of calling GetDriveType with the current string, the result of a _splitpath operation on the path, and the "cooked" result of applying getDriveType (note the initial lowercase letter).

For concisesness, the comments have been removed from the version below. They appear in the full source in the download.

UINT getDriveType(LPCTSTR fullpath)
    {
     TCHAR drv[_MAX_PATH];
     TCHAR path[_MAX_PATH];
     TCHAR filename[_MAX_PATH];
     TCHAR ext[_MAX_PATH];

     _splitpath(fullpath, drv, path, filename, ext);
     if(lstrlen(drv) != 0)
        { /* drv */
         lstrcat(drv, _T("\\"));
         return GetDriveType(drv);
        } /* drv */
     else
        { /* what else? */
         if(path[0] != _T('\\') ||
            (lstrlen(path) > 2 && path[0] == _T('\\') &&
                path[1] != _T('\\')))
            return GetDriveType(NULL);

         UINT result = GetDriveType(path);
         if(result == DRIVE_NO_ROOT_DIR &&
            lstrlen(path) >= 4 && path[0] == _T('\\') && path[1] == _T('\\')
                              && path[2] == _T('.') && path[3] == _T('\\'))
            { /* \\. */
             if(lstrlen(path) == 4) 
                lstrcat(path, filename); 
             LPTSTR p = &path[4]; 
             LPTSTR b = strchr(p, _T('\\'));
             if(b != NULL)
                { /* logical drive */
                 *b++ = _T(':');
                 *b++ = _T('\\');
                 *b = _T('\0');
                } /* logical drive */
             else
                lstrcat(p, _T(":\\"));

             return GetDriveType(p);
            } /* \\. */

         if(lstrlen(path) < 2 || path[0] != _T('\\') || path[1] != _T('\\'))
            return result;

         return GetDriveType(path);
        } /* what else? */
            
    }

Last updated: 28 July 1998



Comments

  • Thank you!!

    Posted by Legacy on 09/03/2002 12:00am

    Originally posted by: Marina Vaillant

    This article was exactly what I looked for after I saw the bug report in MSDN.
    You solve my problem, I added your method in my project and it reacts as expected.

    Thank you!
    Marina

    Reply
  • Bugs report

    Posted by Legacy on 07/24/2002 12:00am

    Originally posted by: lsnail

    There is some clerical error in GetDriveTypeExplorerDlg.cpp
    
    

    CString driveTypeToString(UINT type)
    {
    CString s;
    switch(type)
    { /* decode */
    ...
    case DRIVE_REMOTE:
    s.LoadString(IDS_DRIVE_REMOTE);
    break;
    case IDS_DRIVE_CDROM: // <-- here
    s.LoadString(IDS_DRIVE_CDROM);
    break;
    case IDS_DRIVE_RAMDISK: // <-- here
    s.LoadString(IDS_DRIVE_RAMDISK);
    break;
    ...
    default:
    s.LoadString(IDS_ERROR);
    break;
    } /* decode */
    return s;
    }

    The leading "IDS_" in the marked line must be removed as below:
    case DRIVE_CDROM: // <-- here
    s.LoadString(IDS_DRIVE_CDROM);
    break;
    case DRIVE_RAMDISK: // <-- here
    s.LoadString(IDS_DRIVE_RAMDISK);
    break;

    Reply
  • Help to Create a System

    Posted by Legacy on 11/07/2000 12:00am

    Originally posted by: abraham

    i need some tool machinery to create a system and i wont to now how much it going to cost and where can i get this and can you tell me what the tool names are thank you.

    Reply
  • Problem with CDROM using WinNT

    Posted by Legacy on 11/11/1999 12:00am

    Originally posted by: Peter Klimo

    This solution don't work with CD-ROM under WinNT !
    

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Live Event Date: October 29, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this eSeminar, Gene Kim will discuss these survey findings and will share woeful tales of artifact management gone wrong! Gene will also share examples of how …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds