草庐IT

c++ windows 将 GetLogicalDrives 的结果传递给 GetVolumeInformation

coder 2024-06-17 原文

你好,

我使用 GetLogicalDrives 获取所有驱动器,我想进一步使用它来检测驱动器类型,然后使用 GetVolumeInformation 检查特定驱动器的状态。但是,我无法在 GetVolumeInformation 和 GetDriveTypes 中使用 GetLogicalDrives(DWORD) 的结果,因为它们排除了 LPCWSTR。我如何转换 GetLogicalDrives 的结果并将其传递给 GetVolumeInformation 和 GetDriveTypes?

        TCHAR myDrives[] = L" A";
        DWORD myDrivesBitMask = GetLogicalDrives();
        WCHAR szTest[10];



      if (myDrivesBitMask == 0)
wprintf(L"GetLogicalDrives() failed with error code: %d\n", GetLastError());
        else {
            wprintf(L"This machine has the following logical drives:\n");
            while (myDrivesBitMask) {
                // Use the bitwise AND with 1 to identify 
                // whether there is a drive present or not. 
                if (myDrivesBitMask & 1) {
                    // Printing out the available drives                
                    wprintf(L"drive %s\n", myDrives);
                }
                // increment counter for the next available drive.   
                myDrives[1]++;
                // shift the bitmask binary right    
                myDrivesBitMask >>= 1;
            }
            wprintf(L"\n");
        }
UINT test;
for (i = 0; i<12; i++)
    {
        test = GetDriveType(myDrives[i]);
        switch (test)
        {
        case 0: printf("Drive %S is type %d - Cannot be determined.\n", myDrives[i], test);
            break;
        case 1: printf("Drive %S is type %d - Invalid root path/Not available.\n", myDrives[i], test);
            break;
        case 2: printf("Drive %S is type %d - Removable.\n", myDrives[i], test);
            break;
        case 3: printf("Drive %S is type %d - Fixed.\n", myDrives[i], test);
            break;
        case 4: printf("Drive %S is type %d - Network.\n", myDrives[i], test);
            break;
        case 5: printf("Drive %S is type %d - CD-ROM.\n", myDrives[i], test);

            break;
        case 6: printf("Drive %S is type %d - RAMDISK.\n", myDrives[i], test);
            break;
        default: "Unknown value!\n";
        }
    }



    (GetVolumeInformation(myDrives, volumeName, ARRAYSIZE(volumeName), &serialNumber, &maxComponentLen, &fileSystemFlags, fileSystemName, ARRAYSIZE(fileSystemName)))
            {
                _tprintf(_T("There is a CD/DVD in the CD/DVD rom"));
                _tprintf(_T("Volume Name: %s\n"), volumeName);
                _tprintf(_T("Serial Number: %lu\n"), serialNumber);
                _tprintf(_T("File System Name: %s\n"), fileSystemName);
                _tprintf(_T("Max Component Length: %lu\n"), maxComponentLen);

            }
            else
                _tprintf(_T("There is NO CD/DVD in the CD/DVD rom"));

最佳答案

由于您需要驱动器盘符来调用 GetDriveType()GetVolumeInformation(),因此使用 GetLogicalDriveStrings() 会更容易GetLogicalDrives(),例如:

WCHAR myDrives[105];
WCHAR volumeName[MAX_PATH];
WCHAR fileSystemName[MAX_PATH];
DWORD serialNumber, maxComponentLen, fileSystemFlags;
UINT driveType;

if (!GetLogicalDriveStringsW(ARRAYSIZE(myDrives)-1, myDrives))
{
    wprintf(L"GetLogicalDrives() failed with error code: %lu\n", GetLastError());
}
else
{
    wprintf(L"This machine has the following logical drives:\n");

    for (LPWSTR drive = myDrives; *drive != 0; drive += 4)
    {
        driveType = GetDriveTypeW(drive);
        wprintf(L"Drive %s is type %d - ", drive, driveType);

        switch (driveType)
        {
            case DRIVE_UNKNOWN:
                wprintf(L"Cannot be determined!");
                break;
            case DRIVE_NO_ROOT_DIR:
                wprintf(L"Invalid root path/Not available.");
                break;
            case DRIVE_REMOVABLE:
                wprintf(L"Removable.");
                break;
            case DRIVE_FIXED:
                wprintf(L"Fixed.");
                break;
            case DRIVE_REMOTE:
                wprintf(L"Network.");
                break;
            case DRIVE_CDROM:
                 wprintf(L"CD-ROM.");
                 break;
            case DRIVE_RAMDISK:
                wprintf(L"RAMDISK.");
                break;
            default:
                wprintf(L"Unknown value!");
        }
        wprintf(L"\n");

        if (driveType == DRIVE_CDROM)
        {
            if (GetVolumeInformationW(drive, volumeName, ARRAYSIZE(volumeName), &serialNumber, &maxComponentLen, &fileSystemFlags, fileSystemName, ARRAYSIZE(fileSystemName)))
            {
                wprintf(L"  There is a CD/DVD in the drive:\n");
                wprintf(L"  Volume Name: %s\n", volumeName);
                wprintf(L"  Serial Number: %08X\n", serialNumber);
                wprintf(L"  File System Name: %s\n", fileSystemName);
                wprintf(L"  Max Component Length: %lu\n", maxComponentLen);
            }
            else
            {
                wprintf(L"  There is NO CD/DVD in the drive");
            }
        }
    }
}

关于c++ windows 将 GetLogicalDrives 的结果传递给 GetVolumeInformation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42322387/

有关c++ windows 将 GetLogicalDrives 的结果传递给 GetVolumeInformation的更多相关文章

  1. ruby - 在 Ruby 程序执行时阻止 Windows 7 PC 进入休眠状态 - 2

    我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0

  2. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  3. ruby - 在 Windows 机器上使用 Ruby 进行开发是否会适得其反? - 2

    这似乎非常适得其反,因为太多的gem会在window上破裂。我一直在处理很多mysql和ruby​​-mysqlgem问题(gem本身发生段错误,一个名为UnixSocket的类显然在Windows机器上不能正常工作,等等)。我只是在浪费时间吗?我应该转向不同的脚本语言吗? 最佳答案 我在Windows上使用Ruby的经验很少,但是当我开始使用Ruby时,我是在Windows上,我的总体印象是它不是Windows原生系统。因此,在主要使用Windows多年之后,开始使用Ruby促使我切换回原来的系统Unix,这次是Linux。Rub

  4. ruby - rails 3 redirect_to 将参数传递给命名路由 - 2

    我没有找到太多关于如何执行此操作的信息,尽管有很多关于如何使用像这样的redirect_to将参数传递给重定向的建议:action=>'something',:controller=>'something'在我的应用程序中,我在路由文件中有以下内容match'profile'=>'User#show'我的表演Action是这样的defshow@user=User.find(params[:user])@title=@user.first_nameend重定向发生在同一个用户Controller中,就像这样defregister@title="Registration"@user=Use

  5. ruby-on-rails - 如何生成传递一些自定义参数的 `link_to` URL? - 2

    我正在使用RubyonRails3.0.9,我想生成一个传递一些自定义参数的link_toURL。也就是说,有一个articles_path(www.my_web_site_name.com/articles)我想生成如下内容:link_to'Samplelinktitle',...#HereIshouldimplementthecode#=>'http://www.my_web_site_name.com/articles?param1=value1¶m2=value2&...我如何编写link_to语句“alàRubyonRailsWay”以实现该目的?如果我想通过传递一些

  6. 报告回顾丨模型进化狂飙,DetectGPT能否识别最新模型生成结果? - 2

    导读语言模型给我们的生产生活带来了极大便利,但同时不少人也利用他们从事作弊工作。如何规避这些难辨真伪的文字所产生的负面影响也成为一大难题。在3月9日智源Live第33期活动「DetectGPT:判断文本是否为机器生成的工具」中,主讲人Eric为我们讲解了DetectGPT工作背后的思路——一种基于概率曲率检测的用于检测模型生成文本的工具,它可以帮助我们更好地分辨文章的来源和可信度,对保护信息真实、防止欺诈等方面具有重要意义。本次报告主要围绕其功能,实现和效果等展开。(文末点击“阅读原文”,查看活动回放。)Ericmitchell斯坦福大学计算机系四年级博士生,由ChelseaFinn和Chri

  7. ruby - 在 Ruby 中按名称传递函数 - 2

    如何在Ruby中按名称传递函数?(我使用Ruby才几个小时,所以我还在想办法。)nums=[1,2,3,4]#Thisworks,butismoreverbosethanI'dlikenums.eachdo|i|putsiend#InJS,Icouldjustdosomethinglike:#nums.forEach(console.log)#InF#,itwouldbesomethinglike:#List.iternums(printf"%A")#InRuby,IwishIcoulddosomethinglike:nums.eachputs在Ruby中能不能做到类似的简洁?我可以只

  8. ruby - 使用 `+=` 和 `send` 方法 - 2

    如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:

  9. Vscode+Cmake配置并运行opencv环境(Windows和Ubuntu大同小异) - 2

    之前在培训新生的时候,windows环境下配置opencv环境一直教的都是网上主流的vsstudio配置属性表,但是这个似乎对新生来说难度略高(虽然个人觉得完全是他们自己的问题),加之暑假之后对cmake实在是爱不释手,且这样配置确实十分简单(其实都不需要配置),故斗胆妄言vscode下配置CV之法。其实极为简单,图比较多所以很长。如果你看此文还配不好,你应该思考一下是不是自己的问题。闲话少说,直接开始。0.CMkae简介有的人到大二了都不知道cmake是什么,我不说是谁。CMake是一个开源免费并且跨平台的构建工具,可以用简单的语句来描述所有平台的编译过程。它能够根据当前所在平台输出对应的m

  10. 深度学习部署:Windows安装pycocotools报错解决方法 - 2

    深度学习部署:Windows安装pycocotools报错解决方法1.pycocotools库的简介2.pycocotools安装的坑3.解决办法更多Ai资讯:公主号AiCharm本系列是作者在跑一些深度学习实例时,遇到的各种各样的问题及解决办法,希望能够帮助到大家。ERROR:Commanderroredoutwithexitstatus1:'D:\Anaconda3\python.exe'-u-c'importsys,setuptools,tokenize;sys.argv[0]='"'"'C:\\Users\\46653\\AppData\\Local\\Temp\\pip-instal

随机推荐