遍历磁盘中的卷(方法一)

右击”我的电脑”-“管理”-“磁盘管理”,会看到计算机中的卷,
这里写图片描述
那么如何调用api查看呢?
可以调用GetLogicalDriveStrings函数,函数原型为:
DWORD GetLogicalDriveStrings(
DWORD nBufferLength,
LPTSTR lpBuffer
);

代码如下:

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

# define BUFSIZE 1024

int main()
{
    PTCHAR szDrive;
    wchar_t szLogicalDriveStrings[BUFSIZE];
    //因为szLogicalDriveStrings可能会有一些垃圾数据,所以永ZeroMemory清空
    ZeroMemory(szLogicalDriveStrings, BUFSIZE);

    GetLogicalDriveStrings(BUFSIZE - 1, szLogicalDriveStrings);
        //由于不包括表示终止的‘\0’字符,所以要减1
    //如果有c盘,就有c:\,如果有d盘,就是c:\ d:\  (c:\和d:\之间有个'\0',所有的结果都在一个字符串里)
    //实际等价于c:\\0d:\\0e:\\0\0(假如只有c,d,e盘,最后面会有两个\0)


    wprintf(L"%s\n", szLogicalDriveStrings);//但是这里的结果只有c:\,
    //因为c语言或c++中遇到'\0'就表示字符串结束了

    szDrive = (PTCHAR)szLogicalDriveStrings;
    do
    {
        wprintf(L"%s", szDrive);
        szDrive += (wcslen(szDrive)+1);
    } while (*szDrive!='\0');
    printf("\n");

    //以下是msdn的文档
    /*GetLogicalDriveStrings    

The GetLogicalDriveStrings function fills a buffer with strings that specify valid drives in the system.


DWORD GetLogicalDriveStrings(
  DWORD nBufferLength,
  LPTSTR lpBuffer
);

Parameters
nBufferLength 
[in] Maximum size of the buffer pointed to by lpBuffer, in TCHARs. This size does not include the terminating null character. If this parameter is zero, lpBuffer is not used. 
lpBuffer 
[out] Pointer to a buffer that receives a series of null-terminated strings, one for each valid drive in the system, plus with an additional null character. Each string is a device name. 
Return Value
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. To get extended error information, use the GetLastError function.

Remarks

        */

    system("pause");
    return 0;
}

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值