1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| // ----------------------------------------------------------------------------------------------------
// Get Physical Disk Serial number of a logical drive letter
//
// Author Serge Girard aka SergioMaster https://www.developpez.net
// Modification by Cirec
//-----------------------------------------------------------------------------------------------------
// Part of this code was generated by the Wmi Delphi Code Creator (WDCC) Version 1.9.9.350
// http://code.google.com/p/wmi-delphi-code-creator/
// Blog http://theroadtodelphi.wordpress.com/wmi-delphi-code-creator/
// Author Rodrigo Ruz V. (RRUZ) Copyright (C) 2011-2015
//-----------------------------------------------------------------------------------------------------
//
// LIABILITY DISCLAIMER
// THIS GENERATED CODE IS DISTRIBUTED "AS IS". NO WARRANTY OF ANY KIND IS EXPRESSED OR IMPLIED.
// YOU USE IT AT YOUR OWN RISK. THE AUTHOR NOT WILL BE LIABLE FOR DATA LOSS,
// DAMAGES AND LOSS OF PROFITS OR ANY OTHER KIND OF LOSS WHILE USING OR MISUSING THIS CODE.
//
//----------------------------------------------------------------------------------------------------
unit UnitWMI;
interface
uses
SysUtils,
ActiveX,
ComObj,
Variants;
function GetSerialNumberOfDrive(drive: string): string;
implementation
function _GetSerialNumberOfDrive(drive: string): string;
const
WbemUser = '';
WbemPassword = '';
WbemComputer = 'localhost';
wbemFlagForwardOnly = $00000020;
var
FSWbemLocator : OLEVariant;
FWMIService : OLEVariant;
DisquesPhys, Partitions, DisquesLogiques: OLEVariant;
WMIObjet, WMIPartition, WMIDisque: OLEVariant;
oEnum, PartNum, DisqueNum: IEnumvariant;
iValue : LongWord;
requete : string;
begin;
result := '';
FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
FWMIService := FSWbemLocator.ConnectServer(WbemComputer, 'root\CIMV2', WbemUser,
WbemPassword);
DisquesPhys := FWMIService.ExecQuery('SELECT * FROM Win32_DiskDrive', 'WQL',
wbemFlagForwardOnly);
oEnum := IUnknown(DisquesPhys._NewEnum) as IEnumVariant;
while oEnum.Next(1, WMIObjet, iValue) = 0 do
begin
Requete := 'ASSOCIATORS OF {Win32_DiskDrive.DeviceID=' +
QuotedStr(string(WMIObjet.DeviceID)) +
'} WHERE AssocClass = Win32_DiskDriveToDiskPartition';
Partitions := FWMIService.ExecQuery(requete, 'WQL', wbemFlagForwardOnly);
PartNum := IUnknown(Partitions._NewEnum) as IEnumVariant;
while PartNum.Next(1, WMIPartition, iValue) = 0 do
begin
requete := 'ASSOCIATORS OF {Win32_DiskPartition.DeviceID=' +
QuotedStr(string(WMIPartition.DeviceID)) +
'} WHERE AssocClass = Win32_LogicalDiskToPartition';
DisquesLogiques := FWMIService.ExecQuery(requete, 'WQL', wbemFlagForwardOnly);
DisqueNum := IUnknown(DisquesLogiques._NewEnum) as IEnumVariant;
while DisqueNum.Next(1, WMIDisque, iValue) = 0 do
begin
if (not VarIsNull(WMIObjet.DeviceID)) and (not VarIsNull(WMIObjet.SerialNumber)) then
if string(WMIDisque.DeviceID) = Drive then
result := string(WMIObjet.SerialNumber);
end;
end;
WMIObjet := Unassigned;
WMIPartition := Unassigned;
WMIDisque := Unassigned;
end;
end;
// Ajouté par Cirec 12/03/2017 https://www.developpez.net
type
PQuadChar = ^TQuadChar;
TQuadChar = array[0..1, 0..1] of AnsiChar;
function GetSerialNumberOfDrive(drive: string): string;
var
HexSNumber,
StrSNumber : AnsiString;
P : PQuadChar;
I : Integer;
begin
HexSNumber := _GetSerialNumberOfDrive(drive);
P := @HexSNumber[1];
if Length(HexSNumber) > 20 then
begin
// on a un SN au format Hexa
SetLength(StrSNumber, Length(HexSNumber) div 2);
I := 1;
repeat
Byte(StrSNumber[I]) := StrToIntDef('$' + string(P^[1]), 0);
Byte(StrSNumber[I + 1]) := StrToIntDef('$' + string(P^[0]), 0);
Inc(I, 2);
Inc(P);
until I >= Length(StrSNumber);
end
else
begin
// on a un SN au format Text
SetLength(StrSNumber, Length(HexSNumber));
I := 1;
repeat
StrSNumber[I] := P^[0][1];
StrSNumber[I + 1] := P^[0][0];
StrSNumber[I + 2] := P^[1][1];
StrSNumber[I + 3] := P^[1][0];
Inc(I, 4);
Inc(P);
until I >= Length(StrSNumber);
end;
Result := Trim(string(StrSNumber));
end;
end. |
Partager