通过WMI将驱动器号添加到阵列始终会返回错误... [英] Adding drive letters to an array via WMI always returns wrong...
问题描述
我正在查询WMI以从系统中获取硬盘信息.然后,我将信息添加到数组中.每次显示while循环时,信息都是正确的.
当我循环数组并打印该数组时,它始终将驱动器打印为Z,但确实打印了正确数量的驱动器.有任何想法吗?
Im querying WMI to get the hard disk information from my system. I''m then adding the information to an array. When the while loop is displayed each turn, the information is correct.
When i loop the array and print this, it always prints drive as Z but does print the correct amount of drives. Any Ideas please?
struct DriveInfo
{
char *DriveLetter;
int nType;
};
vector< DriveInfo* >DriveList;
bool WMIReader::GetInfo( CMainDlg *pThis )
{
char pBuffer[ 256 ];
DriveInfo *FoundDrive = new DriveInfo;
ZeroMemory( FoundDrive, sizeof( DriveInfo ) );
WMIReader *Reader = new WMIReader( );
if( !Reader->Initialized ) { Reader->Initialized = Reader->Setup( ); }
if( Reader->Initialized )
{
if( Reader->GetFirstDevice( L"Win32_LogicalDisk" ) )
{
while( Reader->GetNextDevice( ) )
{
Reader->ReadProperty( L"DeviceID", pBuffer );
FoundDrive->DriveLetter = pBuffer;
DriveList.push_back( FoundDrive );
//Prints the drive letter correctly
Tools.AppendWindowText( pThis->m_StatusWindow, "Volume: %s\\", FoundDrive->DriveLetter );
}
for( int i = 0; i < DriveList.size( ); ++i )
{
DriveInfo* CurrentDrive = DriveList.at( i );
//Prints all drives as 'Z'
Tools.AppendWindowText( pThis->m_StatusWindow, "Volume: %s\\", CurrentDrive->DriveLetter );
}
}
}
return true;
}
推荐答案
您只有一个DriveInfo实例,因此,每当您分配一个驱动器号时,它都会更新列表中的每个条目,当然,所有条目指向那个DriveInfo.
在while循环中,为找到的每个驱动器创建一个新的DriveInfo.
艾伦.
You have only one instance of DriveInfo and so whenever you assign a drive letter it updates every entry in the list which, of course, all point to that one DriveInfo.
In the while loop create a new DriveInfo for each drive found.
Alan.
那是因为这里:
That is because here:
FoundDrive->DriveLetter = pBuffer;
您将指针存储在pBuffer处.因此,每个DriveInfo
实例都在同一缓冲区的DriveLetter
成员中都有一个指针.该缓冲区pBuffer
在迭代驱动器的while
循环的每个迭代中都会被覆盖,因此,在循环之后,pBuffer
将包含最后一个驱动器号(例如Z),并且每个DriveInfo
>在此缓冲区有一个指针,它们都将报告"最后一个驱动器.
尝试将DriveLetter
从char *
更改为CString
或std::string
,或者至少为每个DriveInfo
实例分配一个新的char缓冲区并将该缓冲区复制到其中.或简单地将其作为char
,然后将缓冲区中的一个特定字母复制到其中.
you store a pointer at pBuffer. So each and every instance of DriveInfo
will have a pointer in it''s DriveLetter
member at the very same buffer. This buffer, pBuffer
is overwritten in each itearion of the while
loop that iterates the drives, thus, after the loop, pBuffer
will contain the very last drive letter (e.g. Z), and since each and every DriveInfo
has a pointer at this buffer, they will all "report" this very last drive.
Try changing DriveLetter
from char *
to CString
, or std::string
, or at least allocate a new char buffer for every DriveInfo
instance and copy the buffer into them. Or make it simply a char
and copy that one particular letter from the buffer into them.
谢谢,我想这一定是我想念的小东西!
基本上,我将您的解决方案应用于以下方面:
Thanks, I thought it musta been something small I missed!
Basically i applied your solution to come up with the following:
struct DriveInfo
{
char DriveLetter[50];
int nType;
};
vector< DriveInfo* >DriveList;
DriveInfo *FoundDrive = NULL;
bool WMIReader::GetInfo( CEngTechDlg *pThis )
{
char pBuffer[ 256 ];
FoundDrive = new DriveInfo;
WMIReader *Reader = new WMIReader( );
if( !Reader->Initialized ) { Reader->Initialized = Reader->Setup( ); }
if( Reader->Initialized )
{
if( Reader->GetFirstDevice( L"Win32_LogicalDisk" ) )
{
while( Reader->GetNextDevice( ) )
{
Reader->ReadProperty( L"DeviceID", pBuffer );
strcpy( FoundDrive->DriveLetter, pBuffer );
DriveList.push_back( FoundDrive );
FoundDrive = new DriveInfo;
ZeroMemory( FoundDrive, sizeof( DriveInfo ) );
}
Reader->EndQuery( );
for( int j = 0; j < DriveList.size( ); ++j )
{
DriveInfo* CurrentDrive = DriveList.at( j );
Tools.AppendWindowText( pThis->m_StatusWindow, "Volume: %s\\", CurrentDrive->DriveLetter );
}
}
}
return true;
}
提出了以下内容:
This came up with the following:
Volume: D:\
Volume: E:\
Volume: T:\
Volume: U:\
Volume: V:\
Volume: W:\
Volume: X:\
Volume: Y:\
Volume: Z:\
感谢您的帮助.非常感谢.
Appreciate your help there. Thanks a lot.
这篇关于通过WMI将驱动器号添加到阵列始终会返回错误...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!