Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检索WINDOWS驱动器的卷详细信息-使用';字符[]和#x27;至';LPCWSTR';转换_Windows_Winapi_Wchar T_Lpcwstr - Fatal编程技术网

检索WINDOWS驱动器的卷详细信息-使用';字符[]和#x27;至';LPCWSTR';转换

检索WINDOWS驱动器的卷详细信息-使用';字符[]和#x27;至';LPCWSTR';转换,windows,winapi,wchar-t,lpcwstr,Windows,Winapi,Wchar T,Lpcwstr,我正在尝试获取WINDOWS系统驱动器标签的卷详细信息,以及相应的卷序列号。我已经试了一个小时,并且构建了一个语法错误的代码。目前,我发现以下错误- 错误C2664:“GetVolumeInformation W”:无法将参数1从“char[]”转换为“LPCWSTR” 这是我的密码: // getVolDrive.cpp : Defines the entry point for the console application. #include "stdafx.h" #includ

我正在尝试获取WINDOWS系统驱动器标签的
卷详细信息
,以及相应的卷序列号。我已经试了一个小时,并且构建了一个语法错误的代码。目前,我发现以下错误-
错误C2664:“GetVolumeInformation W”:无法将参数1从“char[]”转换为“LPCWSTR”
这是我的密码:

    // getVolDrive.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <direct.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#include <sstream>
#include <string>
#include <ctype.h>
#include <algorithm>

using namespace std;
//wchar_t mydrives[5];// = " A: ";
char mydrives[] = " A: ";

string retVolSno(char drives[]) //wchar_t drives[]
{
    DWORD dwSerial;
    stringstream ss;
    cout<<drives<<endl;
    if(!GetVolumeInformation(drives, NULL, 0, &dwSerial, NULL, NULL, NULL, 0))
    {
        ss<<"Error: "<<GetLastError();
    }
    else
    {
        ss<<hex<<dwSerial;
    }
    return ss.str();
}

int _tmain(int argc, _TCHAR* argv[])
{
    string cVolSno;
    ULONG DriveMask = _getdrives(); 
    if(DriveMask == 0)
        printf("_getdrives() failed with failure code: %d\n", GetLastError());
    else
    {
        printf("This machine has the following logical drives:\n");
        while (DriveMask)
            { 
                cout << "In While" << endl;
                if(DriveMask & 1)
                printf("%s", mydrives);
                wcout << mydrives << endl;
                cVolSno = retVolSno(mydrives);
                cout<<cVolSno<<endl;
                ++mydrives[1];
                DriveMask >>= 1;
            }
    }
    //std::transform(cVolSno.begin(), cVolSno.end(),cVolSno.begin(), ::toupper);
    //cout<<cVolSno<<endl;
    _getch();
    return 0;
}
输出:

    // getVolDrive.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <direct.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#include <sstream>
#include <string>
#include <ctype.h>
#include <algorithm>

using namespace std;
//wchar_t mydrives[5];// = " A: ";
char mydrives[] = " A:\\\\ ";

string retVolSno(char drives[]) //wchar_t drives[]
{
    DWORD dwSerial;
    stringstream ss;
    wchar_t text[10];
    mbstowcs(text,drives,100); //strlen(drives)+1
    LPWSTR ptr = text;

    if(!GetVolumeInformation(ptr, NULL, 0, &dwSerial, NULL, NULL, NULL, 0))
    {
        ss<<"Error: "<<GetLastError();
    }
    else
    {
        ss<<hex<<dwSerial;
    }
    return ss.str();
}

int _tmain(int argc, _TCHAR* argv[])
{
    string cVolSno;
    ULONG DriveMask = _getdrives(); 
    if(DriveMask == 0)
        printf("_getdrives() failed with failure code: %d\n", GetLastError());
    else
    {
        printf("This machine has the following logical drives:\n");
        while (DriveMask)
            {
                if(DriveMask & 1)
                printf("%s \n", mydrives);
                cVolSno = retVolSno(mydrives);
                std::transform(cVolSno.begin(), cVolSno.end(),cVolSno.begin(), ::toupper);
                cout<<cVolSno<<endl;
                ++mydrives[1];
                DriveMask >>= 1;
            }
    }
    //std::transform(cVolSno.begin(), cVolSno.end(),cVolSno.begin(), ::toupper);
    //cout<<cVolSno<<endl;
    _getch();
    return 0;
}
This machine has the following logical drives:
ERROR: 123
ERROR: 123
 C:\\
ERROR: 123
 D:\\
ERROR: 123
 E:\\
ERROR: 123
从中,如果传递了驱动器号,则第一个参数应带有尾随斜杠

lpRootPathName [in, optional]
A pointer to a string that contains the root directory of the volume to be described.
If this parameter is NULL, the root of the current directory is used. 
A trailing backslash is required. 
For example, you specify \\MyServer\MyShare as \\MyServer\MyShare\, or the C drive as C:\

我至少看到了这些主要问题:

1)
wchar\u t
是正确的类型,因为您是为UNICODE编译的,您可以使用
TCHAR
宏编写通用代码,或者显式地将缓冲区声明为
wchar\u t
,但这就是要做的

2) 出现该错误是因为您向
GetVolumeInformation()
传递了错误的路径(需要尾随反斜杠,因此
A:
必须变成
A:\

此外,请注意,实现相同结果的方法稍微简单一些,您可以使用
GetLogicalDriveStrings()
直接获得
NULL
分隔字符串列表。例如,使用(不要忘记UNICODE)拆分它,并对每个条目使用
c_str()

编辑关于修改后的代码:
为什么驱动路径是
A:\\
(转义到
A:\\\
)?只需一个尾随的反斜杠,因此
mydrives
必须声明为:

wchar_t mydrives[] = L"A:\\";
编辑2:您的代码中有更多错误,因此我将发布一个经过审阅的版本。我会改变更多的事情,但我会指出哪些事情实际上不起作用

  • 功能
    retVolSno
    读取卷序列号。原始版本几乎正确,在修改后的版本中执行无用的字符转换。你所要做的只是接受一个
    wchar\t
    驱动路径

  • 全局变量
    mydrives
    。实际上,不需要任何全局变量。它必须是
    wchar\t
    ,路径前后的空格无效。需要一个尾随的反斜杠。必须相应地更改递增字符值的行(
    ++mydrives[0];
    )(索引0而不是1)

  • 检查驱动器是否可用。在
    之后,如果(DriveMask&1)
    您确实忘记了
    {
    ,那么您将不会打印驱动器名,但即使在不可用的驱动器上也会执行
    GetVolumeInformation()
    。这就是缩进非常重要的原因

  • <> L> > P> >你混合Unicode/非Unicode和C/C++的东西。我强烈建议你选择其中一个,并且保持它(C或C++ + Unicode或Unicode)。例如,你使用C函数<代码> Primff()/Code >打印东西,你既有代码< STD::String 和 WCARGYTT 事物。

    让我们把所有的东西放在一起,形成一个工作版本。首先是读取给定驱动器路径的序列号的函数:

    wstring getVolumeSerialNumber(const wchar_t* drivePath)
    {
        DWORD dwSerial;
        wstringstream ss;
    
        if (!GetVolumeInformation(drivePath, NULL, 0, &dwSerial, NULL, NULL, NULL, 0))
            ss << L"Error: " << GetLastError();
        else
            ss << hex << dwSerial;
    
        return ss.str();
    }
    
    wstring getVolumeSerialNumber(常量wchar\u t*drivePath)
    {
    DWORD dwSerial;
    wstringstreamss;
    if(!GetVolumeInformation(驱动路径,NULL,0,&dwSerial,NULL,NULL,NULL,0))
    
    谢谢你的回复,但是当我声明它是char mydrives[]=“A:\\”;
    时,在控制台中我只得到了`C:`我猜这是错误的路径,因为返回了错误123。我不知道如何将这个char转换为L类型!你有:驱动器吗?你必须使用wchar\t(因为您是为UNICODE编译的,除非您显式调用GetVolumeInformation)并将字符串设置为:\\(因为后面需要一个反斜杠).你能试一下我的代码并解释我哪里做错了吗?我已经试着声明了
    wchar\t
    ,但仍然是相同的错误代码returned@highlander141有几件事你必须改变,我编辑了我的答案,以提供一个工作的例子。
    int _tmain(int argc, _TCHAR* argv[])
    {
        wchar_t drive[] = L"A:\\";
    
        ULONG driveMask = _getdrives(); 
        if (driveMask == 0)
            wcout << L"_getdrives() failed with failure code: " << GetLastError() << endl;
        else
        {
            wcout << L"This machine has the following logical drives:" << endl;
            while (driveMask)
            {
                if (driveMask & 1) 
                {
                    wcout << drive << endl;
                    wcout << getVolumeSerialNumber(drive) << endl;
                }
    
                ++drive[0];
                driveMask >>= 1;
            }
        }
    
        wcin.ignore();
    
        return 0;
    }