3.这个例子是用来读取SCSI设备控制器的信息Computer = "MyMachineName"
Set SCSIs = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & Computer & "\root\cimv2").ExecQuery("Select * From Win32_SCSIController")
For Each mycard In SCSIs
MsgBox mycard.DeviceID
NextWin32_SCSIController类的详细信息,请查阅MSDN文章。4、列举所有的SCSI磁盘ID信息。Computer = "MyMachineName"
Set SCSIs = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & Computer & "\root\cimv2").ExecQuery("Select * From Win32_DiskDrive")
For Each mycard In SCSIs
MsgBox mycard.SCSITargetId
Next-缺省情况下,Windows ME/2000是支持WMI的.如果要使你的系统支持WMI,请访问这个URL:http://msdn.microsoft.com/code/sample.asp?url=/msdn-files/027/001/576/msdncompositedoc.xml&frame=trueNOTE:上面的代码在Win2000下测试通过. 有些WMI类在不同的操作系统下有不同的支持和实现,具体情况还需参照MSDN文章
Set SCSIs = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & Computer & "\root\cimv2").ExecQuery("Select * From Win32_SCSIController")
For Each mycard In SCSIs
MsgBox mycard.DeviceID
NextWin32_SCSIController类的详细信息,请查阅MSDN文章。4、列举所有的SCSI磁盘ID信息。Computer = "MyMachineName"
Set SCSIs = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & Computer & "\root\cimv2").ExecQuery("Select * From Win32_DiskDrive")
For Each mycard In SCSIs
MsgBox mycard.SCSITargetId
Next-缺省情况下,Windows ME/2000是支持WMI的.如果要使你的系统支持WMI,请访问这个URL:http://msdn.microsoft.com/code/sample.asp?url=/msdn-files/027/001/576/msdncompositedoc.xml&frame=trueNOTE:上面的代码在Win2000下测试通过. 有些WMI类在不同的操作系统下有不同的支持和实现,具体情况还需参照MSDN文章
Set SCSIs = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & Computer & "\root\cimv2").ExecQuery("Select * From Win32_SCSIController")
For Each mycard In SCSIs
MsgBox mycard.DeviceID
NextWin32_SCSIController类的详细信息,请查阅MSDN文章。4、列举所有的SCSI磁盘ID信息。Computer = "MyMachineName"
Set SCSIs = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & Computer & "\root\cimv2").ExecQuery("Select * From Win32_DiskDrive")
For Each mycard In SCSIs
MsgBox mycard.SCSITargetId
Next
-缺省情况下,Windows ME/2000是支持WMI的.如果要使你的系统支持WMI,请访问这个URL:http://msdn.microsoft.com/code/sample.asp?url=/msdn-files/027/001/576/msdncompositedoc.xml&frame=trueNOTE:上面的代码在Win2000下测试通过. 有些WMI类在不同的操作系统下有不同的支持和实现,具体情况还需参照MSDN文章
Alias "GetVolumeInformationA" (ByVal lpRootPathName As String, _
ByVal pVolumeNameBuffer As String, ByVal nVolumeNameSize As Long, _
lpVolumeSerialNumber As Long, lpMaximumComponentLength As Long, _
lpFileSystemFlags As Long, ByVal lpFileSystemNameBuffer As String, _
ByVal nFileSystemNameSize As Long)
Private Const MAX_FILENAME_LEN = 256
Public Function DriveSerial(ByVal sDrv As String) As Long
'Usage:
'Dim ds As Long
'ds = DriveSerial("C")
Dim RetVal As Long
Dim str As String * MAX_FILENAME_LEN
Dim str2 As String * MAX_FILENAME_LEN
Dim a As Long
Dim b As Long
GetVolumeInformation sDrv & ":\", str, MAX_FILENAME_LEN, RetVal, _
a, b, str2, MAX_FILENAME_LEN
DriveSerial = RetVal
End Function
如果我们需要某个磁盘的序列号的话,只要DriverSerial(该磁盘的盘符)即可。如 DriverASerialNumber=DriverSerial("A")。
'DISKSERIAL.defLIBRARY DISKSN
EXPORTS GetDiskInfo'从微软网站载下来并加工过,如果在98下,必须要求SMARTVSD.vxd在System\Iosubsys下,该文件默认不会安装,需要手工从Win98_54.cab解压
'HDID.CPP
#include <stdio.h>
#include <conio.h>
#include <windows.h>#pragma pack(1) // Required to ensure correct SMART IOCTL structure setup
#include "smart.h"//
// Define global buffers.
//
BYTE IdOutCmd[sizeof(SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE - 1];
PIDSECTOR m_DISKINFO;BOOL WINAPI DllMain(HMODULE hModule,DWORD fdwReason,LPVOID lpvReserved)
{
//m_DeviceCount=0;
return true;
}//---------------------------------------------------------------------
// Open SMART to allow DeviceIoControl communications.
//---------------------------------------------------------------------
HANDLE OpenSMART(BYTE nDrive)
{
HANDLE hSMARTIOCTL = INVALID_HANDLE_VALUE;
char hd[80];
OSVERSIONINFO VersionInfo;
memset((void*)&VersionInfo,0,sizeof(VersionInfo));
VersionInfo.dwOSVersionInfoSize=sizeof(VersionInfo);
GetVersionEx(&VersionInfo);
switch (VersionInfo.dwPlatformId){
case VER_PLATFORM_WIN32s:
return hSMARTIOCTL;
case VER_PLATFORM_WIN32_WINDOWS:
// Version Windows 95 OSR2, Windows 98
hSMARTIOCTL = CreateFile("\\\\.\\SMARTVSD", 0,0,0,
CREATE_NEW, 0, 0) ;
break;
case VER_PLATFORM_WIN32_NT:
// Windows NT, Windows 2000
if (nDrive < MAX_IDE_DRIVES){
sprintf(hd,"\\\\.\\PhysicalDrive%d",nDrive);
hSMARTIOCTL = CreateFile(hd,GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,
OPEN_EXISTING,0,NULL) ;
}
break;
}
return hSMARTIOCTL;
}/****************************************************************************
*
* DoIDENTIFY
*
* FUNCTION: Send an IDENTIFY command to the drive
* bDriveNum = 0-3
* bIDCmd = IDE_ID_FUNCTION or IDE_ATAPI_ID
*
****************************************************************************/
BOOL DoIDENTIFY(HANDLE hSMARTIOCTL, PSENDCMDINPARAMS pSCIP,
PSENDCMDOUTPARAMS pSCOP, BYTE bIDCmd, BYTE bDriveNum, PDWORD lpcbBytesReturned)
{
//
// Set up data structures for IDENTIFY command.
// pSCIP->cBufferSize = IDENTIFY_BUFFER_SIZE; pSCIP->irDriveRegs.bFeaturesReg = 0;
pSCIP->irDriveRegs.bSectorCountReg = 1;
pSCIP->irDriveRegs.bSectorNumberReg = 1;
pSCIP->irDriveRegs.bCylLowReg = 0;
pSCIP->irDriveRegs.bCylHighReg = 0; //
// Compute the drive number.
//
pSCIP->irDriveRegs.bDriveHeadReg = 0xA0 | ((bDriveNum & 1) << 4); //
// The command can either be IDE identify or ATAPI identify.
//
pSCIP->irDriveRegs.bCommandReg = bIDCmd;
pSCIP->bDriveNumber = bDriveNum;
pSCIP->cBufferSize = IDENTIFY_BUFFER_SIZE; return ( DeviceIoControl(hSMARTIOCTL, DFP_RECEIVE_DRIVE_DATA,
(LPVOID)pSCIP, sizeof(SENDCMDINPARAMS) - 1,
(LPVOID)pSCOP, sizeof(SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE - 1,
lpcbBytesReturned, NULL) );
}/****************************************************************************
*
* DoEnableSMART
*
* FUNCTION: Send a SMART_ENABLE_SMART_OPERATIONS command to the drive
* bDriveNum = 0-3
*
****************************************************************************/
BOOL DoEnableSMART(HANDLE hSMARTIOCTL, PSENDCMDINPARAMS pSCIP, PSENDCMDOUTPARAMS pSCOP, BYTE bDriveNum, PDWORD lpcbBytesReturned)
{
//
// Set up data structures for Enable SMART Command.
//
pSCIP->cBufferSize = 0; pSCIP->irDriveRegs.bFeaturesReg = SMART_ENABLE_SMART_OPERATIONS;
pSCIP->irDriveRegs.bSectorCountReg = 1;
pSCIP->irDriveRegs.bSectorNumberReg = 1;
pSCIP->irDriveRegs.bCylLowReg = SMART_CYL_LOW;
pSCIP->irDriveRegs.bCylHighReg = SMART_CYL_HI; //
// Compute the drive number.
//
pSCIP->irDriveRegs.bDriveHeadReg = 0xA0 | ((bDriveNum & 1) << 4);
pSCIP->irDriveRegs.bCommandReg = IDE_EXECUTE_SMART_FUNCTION;
pSCIP->bDriveNumber = bDriveNum; return ( DeviceIoControl(hSMARTIOCTL, DFP_SEND_DRIVE_COMMAND,
(LPVOID)pSCIP, sizeof(SENDCMDINPARAMS) - 1,
(LPVOID)pSCOP, sizeof(SENDCMDOUTPARAMS) - 1,
lpcbBytesReturned, NULL) );
}//---------------------------------------------------------------------
//---------------------------------------------------------------------
VOID ChangeByteOrder(PCHAR szString, USHORT uscStrSize)
{USHORT i;
CHAR temp; for (i = 0; i < uscStrSize; i+=2)
{
temp = szString[i];
szString[i] = szString[i+1];
szString[i+1] = temp;
}
}/****************************************************************************
*
* DisplayIdInfo
*
* Display the contents of the ID buffer
*
****************************************************************************/
VOID DisplayIdInfo(PIDSECTOR pids, PSENDCMDINPARAMS pSCIP, BYTE bIDCmd, BYTE bDfpDriveMap, BYTE bDriveNum)
{
ChangeByteOrder(pids->sModelNumber,
sizeof pids->sModelNumber); ChangeByteOrder(pids->sFirmwareRev,
sizeof pids->sFirmwareRev); ChangeByteOrder(pids->sSerialNumber,
sizeof pids->sSerialNumber);
}DWORD WINAPI GetDiskInfo(BYTE nDrive,PIDSECTOR DiskInfo)
{
HANDLE hSMARTIOCTL = 0;
DWORD cbBytesReturned;
GETVERSIONOUTPARAMS VersionParams;
SENDCMDINPARAMS scip;
SENDCMDOUTPARAMS OutCmd;
BYTE bDfpDriveMap = 0;
BYTE bIDCmd; // IDE or ATAPI IDENTIFY cmd
//
// Try to get a handle to SMART IOCTL, report failure and exit if
// can't.
//
if ((hSMARTIOCTL = OpenSMART(nDrive)) != INVALID_HANDLE_VALUE)
{
//
// Get the version, etc of SMART IOCTL
//
memset((void*)&VersionParams, 0, sizeof(VersionParams)); DeviceIoControl(hSMARTIOCTL, DFP_GET_VERSION,
NULL,
0,
&VersionParams,
sizeof(VersionParams),
&cbBytesReturned, NULL) ; //
// If there is a IDE device at number "nDrive" issue commands
// to the device.
//
//if (VersionParams.bIDEDeviceMap >> nDrive & 1)
//{ //
// Try to enable SMART so we can tell if a drive supports it.
// Ignore ATAPI devices.
// if (!(VersionParams.bIDEDeviceMap >> nDrive & 0x10))
{ memset(&scip, 0, sizeof(scip));
memset(&OutCmd, 0, sizeof(OutCmd)); if (DoEnableSMART(hSMARTIOCTL,
&scip,
&OutCmd,
nDrive,
&cbBytesReturned))
{
//
// Mark the drive as SMART enabled
//
bDfpDriveMap |= (1 << nDrive);
}
}
//
// Now, get the ID sector for all IDE devices in the system.
// If the device is ATAPI use the IDE_ATAPI_ID command,
// otherwise use the IDE_ID_FUNCTION command.
//
bIDCmd = (VersionParams.bIDEDeviceMap >> nDrive & 0x10) ? \
IDE_ATAPI_ID : IDE_ID_FUNCTION; memset(&scip, 0, sizeof(scip));
memset(IdOutCmd, 0, sizeof(IdOutCmd)); if ( DoIDENTIFY(hSMARTIOCTL,
&scip,
(PSENDCMDOUTPARAMS)&IdOutCmd,
bIDCmd,
nDrive,
&cbBytesReturned))
{
m_DISKINFO=(PIDSECTOR) ((PSENDCMDOUTPARAMS)IdOutCmd)->bBuffer;
DisplayIdInfo(m_DISKINFO,
&scip,
bIDCmd,
bDfpDriveMap,
nDrive);
memcpy(DiskInfo,m_DISKINFO,sizeof(IDSECTOR));
return(1);
}
//}
//
// Close SMART.
//
CloseHandle(hSMARTIOCTL);
return(0);
}
else
return(-1);
}