function TForm1.ReadDrives: Integer;
{******************************************************************************}
{** Laufwerke holen: **}
{** DriveLetter, HaID, Target, LUN, Identfier und DeviveName. **}
{******************************************************************************}
const
Root2k = '
';
Root98 = '
Enum\Scsi\';
var
Reg : TRegistry;
Root :
String;
RegKey :
String;
OSInfo : TOSVersionInfo;
szDrives :
array[0..105]
of Char;
hDevice : THandle;
buf :
array[0..1023]
of Char;
pDrive : PChar;
pscsiAddr : PSCSI_ADDRESS;
returned : Integer;
Begin
Result := 0;
SetLength(FDrive, Result);
OSInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfo);
GetVersionEx(OSInfo);
ZeroMemory(@szDrives, 106);
GetLogicalDriveStrings(105, szDrives);
pDrive := szDrives;
while pDrive^ <> '
'
do
begin
if GetDriveType(pDrive) = DRIVE_CDROM
then
begin
ZeroMemory(@buf, 1024);
pscsiAddr := PSCSI_ADDRESS(@buf);
pscsiAddr^.Length := sizeof(TSCSI_ADDRESS);
hDevice := CreateFile(PChar(Format('
\\.\%s:', [pDrive[0]])),
GENERIC_READ,
FILE_SHARE_READ,
nil,
OPEN_EXISTING,
0,
0 );
if hDevice <> INVALID_HANDLE_VALUE
then
if DeviceIoControl(hDevice,
IOCTL_SCSI_GET_ADDRESS,
nil,
0,
pscsiAddr,
sizeof(TSCSI_ADDRESS),
Cardinal(returned),
nil)
then begin
SetLength(FDrive, Result + 1);
FDrive[Result].HaId := pscsiAddr^.PortNumber;
FDrive[Result].Target := pscsiAddr^.TargetId;
FDrive[Result].Lun := pscsiAddr^.Lun;
FDrive[Result].Letter := pDrive^;
// Registry öffnen
Reg := TRegistry.Create;
Reg.RootKey := HKEY_LOCAL_MACHINE;
// OS abhängigen RootKey setzen
if OSInfo.dwPlatformId = VER_PLATFORM_WIN32_NT
then Root := Root2k
else Root := Root98;
RegKey := Format('
%sHARDWARE\DEVICEMAP\Scsi\Scsi Port %d\Scsi Bus 0\Target Id %d\Logical Unit Id %d',
[Root, FDrive[Result].HaId, FDrive[Result].Target, FDrive[Result].Lun]);
if Reg.KeyExists(RegKey)
then begin
if Reg.OpenKeyReadOnly(RegKey)
then
// Ist es ein CDRom?
if Reg.ReadString('
Type') = '
CdRomPeripheral'
then begin
// Name holen
FDrive[Result].Identifier := Reg.ReadString('
Identifier');
// Device
FDrive[Result].DeviceName := Reg.ReadString('
DeviceName');
end;
// Schlüssel schliessen
Reg.CloseKey;
end;
// Schlüssel schliessen
Reg.CloseKey;
// Registry schliessen
Reg.Free;
// anzahl erhöhen
inc(Result);
end;
CloseHandle(hDevice);
end;
pDrive := pDrive + lstrlen( pDrive ) + 1;
end;
end;