本文介绍了C ++ WMI结构数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在研究可与WMI一起用于多个设备的某些结构数组,例如CPU,内存,硬盘驱动器等.我遇到了一个桥梁,我似乎无法跨越...

基本上,我使用WMI并查询"Win32_LogicalDisk"以收集可用空间,大小等,并将其备份到数组中.我的问题是我的SmartStatus. "Win32_LogicalDisk"中的状态为空,因此我无法在此处查询.但是,"Win32_DiskDrive"中的状态不是空白,但不包含驱动器号或可用空间.因此,我确实需要同时查询两者.

我需要做的是将从"Win32_DiskDrive"->状态"查询中获得的结果添加到数组中的数据中.

我不好解释,但是第一个查询会像这样添加到数组中:

Hi,

Im looking into some struct array work with WMI for multiple devices, such as CPU, memory, hard drive etc... and i''ve come accross a bridge i can''t seem to cross...

Basically im using WMI and querying "Win32_LogicalDisk" to gather freespace, size, etc... and backing this up to an array. The problem i have is with my SmartStatus. The Status in "Win32_LogicalDisk" is blank, so i can''t query this here. The Status in "Win32_DiskDrive" however is not blank, but doesn''t contain drive letters or free space. So i do need to query both.

What i need to do is add the result i get from the query on "Win32_DiskDrive"->"Status" to the data in array.

Im bad at explaining but the first query will add to the array like so:

3
C:
 <- BLANK
19658754
11235
60%



第二个查询将找到状态",我需要将其添加到上面的空白区域,因此当我将结果打印在那里时.

代码:



Second query will find "Status" and i need to add it to the above Blank area, so when i print the result its all there.

CODE:

struct DriveInfo
{
	int		nType;
	char	DriveLetter[10];
	char	SmartStatus[50];
	float	TotalSpace;
	float	FreeSpace;
	float	PercentFree;
};

vector< ServiceInfo* >	ServiceList;
ServiceInfo	*FoundService	= NULL;

vector< DriveInfo* >	DriveList;
DriveInfo	*FoundDrive		= NULL;
UINT ContinueCommands( CXenoClientDlg *pThis )
{
	char pBuffer[100];
	int	StatusInfo;
	FoundService	=	new ServiceInfo( );
	FoundDrive		=	new DriveInfo( );

	while( true )
	{
		if( App.LoggedIn )
		{
			CWMIReader *Services = new CWMIReader( );
			if( !Services->Initialized ) { Services->Initialized = Services->Setup( ); }

			if( Services->Initialized )
			{
				if( Services->GetFirstDevice( L"Win32_Service" ) )
				{
					Services->ReadPropertyString( L"DisplayName", FoundService->Name );
					Services->ReadPropertyString( L"StartMode", FoundService->StartupType );
					Services->ReadPropertyBool( L"Started", FoundService->Started );
					if( !strcmp( FoundService->StartupType, "Auto" ) )
					{
						Tools.Log( "Service: [%s] - [%s] - [%s]", FoundService->Name, FoundService->StartupType, !strcmp( FoundService->Started, "1" ) ? "Started" : "Not Started" );

						ServiceList.push_back( FoundService );
						FoundService = new ServiceInfo;
						ZeroMemory( FoundService, sizeof( ServiceInfo ) );

						while( Services->GetNextDevice( ) )
						{
							Services->ReadPropertyString( L"DisplayName", FoundService->Name );
							Services->ReadPropertyString( L"StartMode", FoundService->StartupType );
							Services->ReadPropertyBool( L"Started", FoundService->Started );
							if( !strcmp( FoundService->StartupType, "Auto" ) )
							{
								Tools.Log( "Service: [%s] - [%s] - [%s]", FoundService->Name, FoundService->StartupType, !strcmp( FoundService->Started, "1" ) ? "Started" : "Not Started" );

								ServiceList.push_back( FoundService );
								FoundService = new ServiceInfo;
								ZeroMemory( FoundService, sizeof( ServiceInfo ) );
							}
						}

						Services->EndQuery( );
					}
				}

				if( Services->GetFirstDevice( L"Win32_LogicalDisk" ) )
				{
					Services->ReadPropertyInt( L"DriveType", pBuffer ); FoundDrive->nType = atoi( pBuffer );
					if( FoundDrive->nType == 3 )
					{
						Services->ReadPropertyString( L"DeviceID", FoundDrive->DriveLetter );
						Services->ReadPropertyString( L"Size", pBuffer ); FoundDrive->TotalSpace = atof( pBuffer )/1024.0f/1024.0f/1024.0f;
						Services->ReadPropertyString( L"FreeSpace", pBuffer ); FoundDrive->FreeSpace = atof( pBuffer )/1024.0f/1024.0f/1024.0f;
						FoundDrive->PercentFree = ( ( FoundDrive->FreeSpace / FoundDrive->TotalSpace ) * 100.0f );

						DriveList.push_back( FoundDrive );
						FoundDrive = new DriveInfo;
						ZeroMemory( FoundDrive, sizeof( DriveInfo ) );

						while( Services->GetNextDevice( ) )
						{
							Services->ReadPropertyInt( L"DriveType", pBuffer ); FoundDrive->nType = atoi( pBuffer );
							if( FoundDrive->nType == 3 )
							{
								Services->ReadPropertyString( L"DeviceID", FoundDrive->DriveLetter );
								Services->ReadPropertyString( L"Size", pBuffer ); FoundDrive->TotalSpace = atof( pBuffer )/1024.0f/1024.0f/1024.0f;
								Services->ReadPropertyString( L"FreeSpace", pBuffer ); FoundDrive->FreeSpace = atof( pBuffer )/1024.0f/1024.0f/1024.0f;
								FoundDrive->PercentFree = ( ( FoundDrive->FreeSpace / FoundDrive->TotalSpace ) * 100.0f );

								DriveList.push_back( FoundDrive );
								FoundDrive = new DriveInfo;
								ZeroMemory( FoundDrive, sizeof( DriveInfo ) );
							}
						}

						Services->EndQuery( );
					}
				}

				if( Services->GetFirstDevice( L"Win32_DiskDrive" ) )
				{
					Services->ReadPropertyString( L"MediaType", pBuffer );
					if( strstr( pBuffer, "Fixed" ) )
					{
						Services->ReadPropertyString( L"Status", FoundDrive->SmartStatus );

						while( Services->GetNextDevice( ) )
						{
							Services->ReadPropertyString( L"MediaType", pBuffer );
							if( strstr( pBuffer, "Fixed" ) )
							{
								Services->ReadPropertyString( L"Status", FoundDrive->SmartStatus );
							}
						}
						Services->EndQuery( );
					}
				}

				for( int i = 0; i < DriveList.size( ); ++i )
				{
					DriveInfo* CurrentDrive = DriveList.at( i );
					Tools.Log( "Drive %s\\ has a status of \"%s\". %.0fGB Free Space, %.0fGB Total Space. %.0f%% Free Space", CurrentDrive->DriveLetter, CurrentDrive->SmartStatus, CurrentDrive->FreeSpace, CurrentDrive->TotalSpace, CurrentDrive->PercentFree );
				}
			}

			ExitThread( 0 );
		}
	}

	return 0;
}

推荐答案

Tools.Log( "Drive %s\\ has a status of \"%s\". %.0fGB Free Space, %.0fGB Total Space. %.0f%% Free Space",
    CurrentDrive[0]->DriveLetter,
    CurrentDrive[1]->SmartStatus,
    CurrentDrive[0]->FreeSpace,
    CurrentDrive[0]->TotalSpace,
    CurrentDrive[0]->PercentFree
);



虽然看起来很乱.我可能会将信息分成两个名称不同的变量,以明确表示它们的含义.



It looks messy though. I''d probably split the info into two variables with different names, to be clear on what they represent.


这篇关于C ++ WMI结构数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 17:03
查看更多