Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1612933
  • 博文数量: 441
  • 博客积分: 20087
  • 博客等级: 上将
  • 技术积分: 3562
  • 用 户 组: 普通用户
  • 注册时间: 2006-06-19 15:35
文章分类

全部博文(441)

文章存档

2014年(1)

2012年(1)

2011年(8)

2010年(16)

2009年(15)

2008年(152)

2007年(178)

2006年(70)

分类: C/C++

2007-04-02 10:56:06

DWORD GetLogicalDrives(VOID);

MSDN Description: If the function succeeds, the return value is a bitmask representing the currently available disk drives. Bit position 0 (the least-significant bit) is drive A, bit position 1 is drive B, bit position 2 is drive C, and so on.

该函数的返回值是一个无符号的32位整数,每一位代表一个驱动器,Bit 0代表驱动器A, Bit 1代表驱动器B, Bit 2代表驱动器C,依次类推。如果该位为1,表示该驱动器存在,否则不存在。

类似函数:

DWORD GetLogicalDriveStrings( DWORD nBufferLength, // size of buffer
                              LPTSTR lpBuffer // pointer to buffer for drive strings );

Parameters

nBufferLength
Specifies the maximum size, in characters, of the buffer pointed to by lpBuffer. This size does not include the terminating null character.
lpBuffer
Pointer to a buffer that receives a series of null-terminated strings, one for each valid drive in the system, that end with a second null character. The following example shows the buffer contents with representing the terminating null character.
c:\d:\ 

Return Values

If the function succeeds, the return value is the length, in characters, of the strings copied to the buffer, not including the terminating null character. Note that an ANSI-ASCII null character uses one byte, but a Unicode null character uses two bytes.

If the buffer is not large enough, the return value is greater than nBufferLength. It is the size of the buffer required to hold the drive strings.

If the function fails, the return value is zero.


UINT GetDriveType( LPCTSTR lpRootPathName // pointer to root path );
The GetDriveType function determines whether a disk drive is a removable, fixed, CD-ROM, RAM disk, or network drive.

Parameters

lpRootPathName
Pointer to a null-terminated string that specifies the root directory of the disk to return information about. If lpRootPathName is NULL, the function uses the root of the current directory.

Return Values

The return value specifies the type of drive. It can be one of the following values:

Value Meaning
DRIVE_UNKNOWN The drive type cannot be determined.
DRIVE_NO_ROOT_DIR The root directory does not exist.
DRIVE_REMOVABLE The disk can be removed from the drive.
DRIVE_FIXED The disk cannot be removed from the drive.
DRIVE_REMOTE The drive is a remote (network) drive.
DRIVE_CDROM The drive is a CD-ROM drive.
DRIVE_RAMDISK The drive is a RAM disk.

依据上面的函数我们来看一个实际的例子:

/* GetDriveInfo1.c */
#include
#include

int main()
{
    DWORD dwRtn = GetLogicalDrives();
    char c = 'A';
    char drive[4];
    char output[64];

    while ( dwRtn )
    {
        if ( dwRtn & 1  )
        {
            sprintf(drive, "%c:\\",  c);
            switch ( GetDriveType(drive) )
            {
            case DRIVE_FIXED:
                sprintf(output, "%c: -- Drive Fixed\n", c);
                break;
            case DRIVE_REMOVABLE:
                sprintf(output, "%c: -- Drive Removable\n", c);
                break;
            case DRIVE_REMOTE:
                sprintf(output, "%c: -- Drive Remote\n", c);
                break;
            case DRIVE_CDROM:
                sprintf(output, "%c: -- CD-Rom\n", c);
                break;
            case DRIVE_RAMDISK:
                sprintf(output, "%c: -- Ram Disk\n", c);
                break;
            default:
                sprintf(output, "%c: -- Unknown drive\n", c);
                break;
            }
            printf("%s", output);
        }
        c++;
        dwRtn >>= 1;
    }
    return 0;
}

该例子根据GetLogicalDrives获取驱动器数目和名称,再根据GetDriveType获取驱动器的类型。
驱动器是从'A'开始的,如果存在,则打印出来。将GetLogicalDrives的返回值循环移位,然后再和1相与就能得出驱动器的数目和盘符。而GetDriveType的返回值则代表了驱动器的类型。
用GetLogicalDriveStrings来获得驱动器:

/* GetDriveInfo2.c */

#include
#include

int main()
{
   
    char Buf[256];
    char TmpDrive[4];
    int i, j = 0;
    char strType[32];
    UINT uType;
    DWORD dwRtn ;
   
    memset(Buf, 0, 256);
    dwRtn = GetLogicalDriveStrings(256, Buf);
   
    for ( i=0; i < (int)dwRtn; i++ )
    {
        if ( Buf[i] == 0  )
        {
            if ( Buf[i+1] != 0 || (Buf[i+1] == 0 && Buf[i+2] == 0 ))
            {
                TmpDrive[j] = Buf[i];
                j = 0;
                uType = GetDriveType(TmpDrive);
                switch(uType)
                {
                case DRIVE_REMOVABLE:
                    sprintf(strType, "Removable");
                    break;
                case DRIVE_FIXED:
                    sprintf(strType, "Fixed Drive");
                    break;
                case DRIVE_REMOTE:
                    sprintf(strType, "Remote Drive");
                    break;
                case DRIVE_CDROM:
                    sprintf(strType, "CD-Rom");
                    break;
                case DRIVE_RAMDISK:
                    sprintf(strType, "Ram disk");
                    break;
                default:
                    sprintf(strType, "Unknow type");
                    break;
                }
                printf("%s - %s\n", TmpDrive, strType);
            }
        }
        if ( Buf[i])
            TmpDrive[j++] = Buf[i];

        if ( Buf[i] == 0 && Buf[i+1] == 0 && Buf[i+2] == 0)
            return 0;
    }
   
    return 0;
}
阅读(1812) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~