PureBasic

Forums PureBasic
Nous sommes le Sam 21/Juil/2018 18:03

Heures au format UTC + 1 heure




Poster un nouveau sujet Répondre au sujet  [ 21 messages ]  Aller à la page Précédente  1, 2
Auteur Message
 Sujet du message: Re: Accès direct Secteur de disque
MessagePosté: Jeu 22/Juin/2017 20:42 
Hors ligne

Inscription: Ven 29/Juin/2007 17:50
Messages: 3121
Ça me rappelle ceci :
Code:
g=c800:5
Exécution complètement obsolète aujourd'hui sous Debug.com pour faire un pré-formatage matériel (Esdi et Seagate STxxxx). Des centaines d'heures de formatage de disques durs de 55 méga. Ça doit sûrement parler à certains d'entre vous.


Haut
 Profil  
Répondre en citant le message  
 Sujet du message: Re: Accès direct Secteur de disque
MessagePosté: Ven 23/Juin/2017 7:53 
Hors ligne
Avatar de l’utilisateur

Inscription: Ven 11/Fév/2005 17:34
Messages: 4115
Localisation: Arras, France
Ollivier a écrit:
Ça me rappelle ceci :
Code:
g=c800:5
Exécution complètement obsolète aujourd'hui sous Debug.com pour faire un pré-formatage matériel (Esdi et Seagate STxxxx). Des centaines d'heures de formatage de disques durs de 55 méga. Ça doit sûrement parler à certains d'entre vous.

Ouaip ! :)


Haut
 Profil  
Répondre en citant le message  
 Sujet du message: Re: Accès direct Secteur de disque
MessagePosté: Ven 23/Juin/2017 9:17 
Hors ligne
Avatar de l’utilisateur

Inscription: Dim 02/Oct/2011 16:17
Messages: 1934
Localisation: 50200 Coutances
Oui, je me souviens vaguement

_________________
Microsoft Windows 10 Famille 64 bits : Carte mère : ASRock 970 Extreme3 R2.0 : Carte Graphique NVIDIA GeForce GT 640 : Processeur AMD FX 6300 6 cœurs 12 threads 3,50 GHz PB 5.62, 5.70 LTS
Un homme doit être poli, mais il doit aussi être libre !


Haut
 Profil  
Répondre en citant le message  
 Sujet du message: Re: Accès direct Secteur de disque
MessagePosté: Jeu 21/Juin/2018 7:07 
Hors ligne
Avatar de l’utilisateur

Inscription: Jeu 21/Juin/2018 6:01
Messages: 6
Salut Jérome,
Àprès une année, j'imagine que tu as déjà trouvé la réponse... À tous hasards...

Pour lire un disque "physique" dans son intégralité, tu peut utiliser CreateFile("\\.\PhysicalDrive0"... (PhysicalDrive1, PhysicalDrive2, etc.)
CreateFile("\\.\PhysicalDrive0", GENERIC_READ OR GENERIC_WRITE, FILE_SHARE_READ OR FILE_SHARE_WRITE, BYVAL 0, OPEN_EXISTING, 0, 0)
Ceci nécessite un manifeste qui réclamera la permission Admin - level='requireAdministrator'

Pour lire un volume, utilise CreateFile("\\.\C:" ("\\.\D:", "\\.\E:", etc.)
CreateFile("\\.\C:", GENERIC_READ OR GENERIC_WRITE, FILE_SHARE_READ OR FILE_SHARE_WRITE, BYVAL 0, OPEN_EXISTING, 0, 0)

Voici du code que j'ai écris en PowerBASIC, tu pourras convertir si tu en as envie.

Code:
'-------------------------------------------------------------------------------------

'Cylinder start at zero, heads start at zero, and sectors at one but IOCTL_DISK_GET_DRIVE_GEOMETRY
'return CylinderCount, HeadCount, and SectorsCount so no need to add one.
'The math is TotalSectorCount = Cylinders * Heads(or Tracks) * SectorsByTrack
'But the main disparity reason reside somewhere else...
'
'IOCTL_DISK_GET_DRIVE_GEOMETRY/DISK_GEOMETRY will return CHS value that are depreciated
'by today standard. Becose of the way it was structured CHS is unable to give correct size
'for big disk, Microsoft is now using "Logical block adressing" or LBA wich is simply a sector number.
'The first LBA have number zero, unlike CHS where first sector is one.
'
'For a USB drive, since there is no mechanics, meaning no real cylinder, head, or sectorByTrack,
'the size don't have to fit exactly in a CHS sheme. So Microsoft took a simple approach
'for IOCTL_DISK_GET_DRIVE_GEOMETRY under modern Windows, always return 255 heads, always
'return 63 sectorsByTrack, always return 512 BytesPerSector and use the next formula
'to return calculated cylinder count...
'
'Cylinder = DiskSizeInBytes \ 255 * 63 * 512
'Note the "\" for integer division wich is not the same as "/" for Floating-point division.
'Here is what you got: 63 = 520,093,696 \ 255 * 63 * 512
'
'This way the cylinder count will always be small enough to stop legacy software to try to
'access a USB drive past the real end. They did not developped a sofisticated formula to get
'closer to the real size becose the main purpose is simply to protect.
'
'Now, to get the size of your USB stick, use IOCTL_DISK_GET_LENGTH_INFO/GET_LENGTH_INFORMATION
'as you did or use IOCTL_DISK_GET_DRIVE_GEOMETRY_EX/DISK_GEOMETRY_EX.
'The CHS geometry is still present for legacy purpose but there is also DiskSize
'as LARGE_INTEGER wich will give you the disk size in bytes.
'
'LbaCount = DiskSizeInByte / BytesBySector
'
'Now you can access any sector from zero to LbaCount - 1 using SetFilePointer/ReadFile.
'
'Note that modern hard disk work with 4096 bytes chunks, the main reason for the need
'of aligned partitions, but all this is transparent in the actual case.
'
'Note also that the old code posted at Disk sector Read-Write on any OS use
'the legacy CHS way and need to be updated.
'_____________________________________________________________________________

'This is a demo to show how to directly access
'a hard disk under any NT version of windows.

'Use it at your own risk, very easy to destroy the content of a hard-disk
'if you are not familiar with direct disk access.

'Accessing an NT system is fairly easy using CreateFile("\\.\PhysicalDrive0"...

'Thank to Semen Matusovski, William Burns and Lance Edmonds

'Pierre Bellisle
'_____________________________________________________________________________

#COMPILE EXE '#Win 9.07#
#REGISTER NONE
#DIM ALL
#INCLUDE "Win32Api.inc"
#INCLUDE "CommCtrl.inc"
#RESOURCE "SectorReader10.pbr" 'Set manifest to get admin right

GLOBAL hDlg AS DWORD

$AppName         = "Direct disk read"

%LabelDisk       = 101
%LabelDiskCount  = 102
%LabelBlockCount = 103
%LabelCyl        = 104
%LabelHead       = 105
%LabelSect       = 106
%LabelMeg        = 107
%LabelStatus     = 108
%ComboDisk       = 201
%ButtonRead      = 301
%ButtonTry       = 302
%EditBlockIndex  = 401
%EditHex         = 402
%OptionPhysical  = 501
%OptionLogical   = 502
%UpDownH         = 601

%SECURITY_NT_AUTHORITY          = 005
%BlockSize                      = 512
%IOCTL_DISK_GET_DRIVE_GEOMETRY  = &H00070000
%FILE_DEVICE_DISK               = &h07              '%IOCTL_DISK_GET_LENGTH_INFO
%IOCTL_DISK_BASE                = %FILE_DEVICE_DISK '%IOCTL_DISK_GET_LENGTH_INFO
%METHOD_BUFFERED                = 0                 '%IOCTL_DISK_GET_LENGTH_INFO
%FILE_READ_ACCESS               = &h01              '%IOCTL_DISK_GET_LENGTH_INFO
%IOCTL_DISK_GET_LENGTH_INFO     = &h7405C           '%IOCTL_DISK_GET_LENGTH_INFO
%FSCTL_ALLOW_EXTENDED_DASD_IO   = &H00090083        'CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 32, METHOD_NEITHER,  FILE_ANY_ACCESS)


%Unknown        = 00 'Format is unknown
%F5_1Pt2_512    = 01 '5.25", 1.2MB,  512 bytes/sector
%F3_1Pt44_512   = 02 '3.5",  1.44MB, 512 bytes/sector
%F3_2Pt88_512   = 03 '3.5",  2.88MB, 512 bytes/sector
%F3_20Pt8_512   = 04 '3.5",  20.8MB, 512 bytes/sector
%F3_720_512     = 05 '3.5",  720KB,  512 bytes/sector
%F5_360_512     = 06 '5.25", 360KB,  512 bytes/sector
%F5_320_512     = 07 '5.25", 320KB,  512 bytes/sector
%F5_320_1024    = 08 '5.25", 320KB,  1024 bytes/sector
%F5_180_512     = 09 '5.25", 180KB,  512 bytes/sector
%F5_160_512     = 10 '5.25", 160KB,  512 bytes/sector
%RemovableMedia = 11 'Removable media other than floppy
%FixedMedia     = 12 'Fixed hard disk media
%F3_120M_512    = 13 '3.5",   120M Floppy
%F3_640_512     = 14 '3.5" ,  640KB,  512 bytes/sector
%F5_640_512     = 15 '5.25",  640KB,  512 bytes/sector
%F5_720_512     = 16 '5.25",  720KB,  512 bytes/sector
%F3_1Pt2_512    = 17 '3.5" ,  1.2Mb,  512 bytes/sector
%F3_1Pt23_1024  = 18 '3.5" ,  1.23Mb, 1024 bytes/sector
%F5_1Pt23_1024  = 19 '5.25",  1.23MB, 1024 bytes/sector
%F3_128Mb_512   = 20 '3.5" MO 128Mb   512 bytes/sector
%F3_230Mb_512   = 21 '3.5" MO 230Mb   512 bytes/sector
%F8_256_128     = 22 '8",     256KB,  128 bytes/sector
%F3_200Mb_512   = 23 '3.5",   200M Floppy (HiFD)
%F3_240M_512    = 24 '3.5",   240Mb Floppy (HiFD)
%F3_32M_512     = 25 '3.5",   32Mb Floppy

TYPE GET_LENGTH_INFORMATION
Length AS QUAD
END TYPE

TYPE DISK_GEOMETRY
Cylinders         AS QUAD
MediaType         AS DWORD '0 unknown, 11 RemovableMedia, 12 FixedMedia, else floppy
TracksPerCylinder AS DWORD
SectorsPerTrack   AS DWORD
BytesPerSector    AS DWORD
END TYPE

DECLARE FUNCTION SetFilePointerEx LIB "Kernel32.dll" ALIAS "SetFilePointerEx" _
(BYVAL hFile AS DWORD, BYVAL liDistanceToMove AS QUAD, lpNewFilePointer AS QUAD, BYVAL dwMoveMethod AS DWORD) AS LONG
'_____________________________________________________________________________

FUNCTION IsUserLocalAdmin() AS LONG 'IsUserAnAdmin api may not be there in future Windows release, so doing it the safe way.
LOCAL NtAuthority         AS SID_IDENTIFIER_AUTHORITY
LOCAL AdministratorsGroup AS LONG
LOCAL IsMember            AS LONG

NtAuthority.value(5) = %SECURITY_NT_AUTHORITY 'SECURITY_NT_AUTHORITY = 5

IF AllocateAndInitializeSid(NtAuthority, 2, %SECURITY_BUILTIN_DOMAIN_RID, _
                             %DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, _
                             AdministratorsGroup) THEN
    IF CheckTokenMembership(%NULL, BYVAL AdministratorsGroup, IsMember) THEN
      FUNCTION = IsMember
    END IF
    FreeSid(BYVAL AdministratorsGroup)
END IF

END FUNCTION
'_____________________________________________________________________________

FUNCTION GeometryMediaTypeGet(BYVAL MediaType AS DWORD) EXPORT AS STRING

SELECT CASE MediaType
   CASE %Unknown        : FUNCTION = "Format is unknown"
   CASE %F5_1Pt2_512    : FUNCTION = "A 5.25"" floppy, with 1.2MB and 512 bytes/sector."
   CASE %F3_1Pt44_512   : FUNCTION = "A 3.5"" floppy, with 1.44MB and 512 bytes/sector."
   CASE %F3_2Pt88_512   : FUNCTION = "A 3.5"" floppy, with 2.88MB and 512 bytes/sector."
   CASE %F3_20Pt8_512   : FUNCTION = "A 3.5"" floppy, with 20.8MB and 512 bytes/sector."
   CASE %F3_720_512     : FUNCTION = "A 3.5"" floppy, with 720KB and 512 bytes/sector."
   CASE %F5_360_512     : FUNCTION = "A 5.25"" floppy, with 360KB and 512 bytes/sector."
   CASE %F5_320_512     : FUNCTION = "A 5.25"" floppy, with 320KB and 512 bytes/sector."
   CASE %F5_320_1024    : FUNCTION = "A 5.25"" floppy, with 320KB and 1024 bytes/sector."
   CASE %F5_180_512     : FUNCTION = "A 5.25"" floppy, with 180KB and 512 bytes/sector."
   CASE %F5_160_512     : FUNCTION = "A 5.25"" floppy, with 160KB and 512 bytes/sector."
   CASE %RemovableMedia : FUNCTION = "Removable media other than floppy."
   CASE %FixedMedia     : FUNCTION = "Fixed hard disk media."
   CASE %F3_120M_512    : FUNCTION = "A 3.5"" floppy, with 120MB and 512 bytes/sector."
   CASE %F3_640_512     : FUNCTION = "A 3.5"" floppy, with 640KB and 512 bytes/sector."
   CASE %F5_640_512     : FUNCTION = "A 5.25"" floppy, with 640KB and 512 bytes/sector."
   CASE %F5_720_512     : FUNCTION = "A 5.25"" floppy, with 720KB and 512 bytes/sector."
   CASE %F3_1Pt2_512    : FUNCTION = "A 3.5"" floppy, with 1.2MB and 512 bytes/sector."
   CASE %F3_1Pt23_1024  : FUNCTION = "A 3.5"" floppy, with 1.23MB and 1024 bytes/sector."
   CASE %F5_1Pt23_1024  : FUNCTION = "A 5.25"" floppy, with 1.23MB and 1024 bytes/sector."
   CASE %F3_128Mb_512   : FUNCTION = "A 3.5"" floppy, with 128MB and 512 bytes/sector."
   CASE %F3_230Mb_512   : FUNCTION = "A 3.5"" floppy, with 230MB and 512 bytes/sector."
   CASE %F8_256_128     : FUNCTION = "An 8"" floppy, with 256KB and 128 bytes/sector."
   CASE %F3_200Mb_512   : FUNCTION = "A 3.5"" floppy, with 200MB and 512 bytes/sector. (HiFD.)"
   CASE %F3_240M_512    : FUNCTION = "A 3.5"" floppy, with 240MB and 512 bytes/sector. (HiFD.)"
   CASE %F3_32M_512     : FUNCTION = "A 3.5"" floppy, with 32MB and 512 bytes/sector."
END SELECT

END FUNCTION
'_____________________________________________________________________________

FUNCTION MediaTypeGet(BYVAL MediaType AS DWORD) EXPORT AS STRING

SELECT CASE MediaType
   CASE %DRIVE_UNKNOWN     : FUNCTION = "DRIVE_UNKNOWN"
   CASE %DRIVE_NO_ROOT_DIR : FUNCTION = "DRIVE_NO_ROOT_DIR"
   CASE %DRIVE_REMOVABLE   : FUNCTION = "DRIVE_REMOVABLE"
   CASE %DRIVE_REMOTE      : FUNCTION = "DRIVE_REMOTE"
   CASE %DRIVE_CDROM       : FUNCTION = "DRIVE_CDROM"
   CASE %DRIVE_RAMDISK     : FUNCTION = "DRIVE_RAMDISK"
   CASE %DRIVE_FIXED       : FUNCTION = "DRIVE_FIXED"
END SELECT

END FUNCTION
'_____________________________________________________________________________

FUNCTION GetVolumeInfo(zDisk AS ASCIIZ * 4)AS LONG
LOCAL zVolumeName        AS ASCIIZ * 21
LOCAL zFileSystem        AS ASCIIZ * 21
LOCAL VolumeSerialNumber AS DWORD
LOCAL FileNameMaxLen     AS DWORD
LOCAL FileSystemFlag     AS LONG

IF GetVolumeInformation(zDisk, _
                         zVolumeName, 20, _
                         VolumeSerialNumber, _
                         FileNameMaxLen, _
                         FileSystemFlag, _
                         zFileSystem, 20) = %FALSE THEN
ELSE
   FUNCTION = %TRUE 'Media inserted
END IF
END FUNCTION
'_____________________________________________________________________________________

FUNCTION GetPhysicalDiskId(BYREF sBuffer AS STRING, BYVAL BufferLen AS DWORD) EXPORT AS DWORD
LOCAL DiskGeometry      AS DISK_GEOMETRY
LOCAL sPhysicalDiskList AS STRING
LOCAL PhysicalDiskIndex AS DWORD
LOCAL PhysicalDiskCount AS DWORD
LOCAL hDevice           AS DWORD

DO
   hDevice = CreateFile("\\.\PhysicalDrive" & FORMAT$(PhysicalDiskIndex), _ 'Could be a HD, flash, CdRom, etc.
                         %GENERIC_READ, %FILE_SHARE_READ OR %FILE_SHARE_WRITE, BYVAL 0, %OPEN_EXISTING, 0, 0)
   IF hDevice = %INVALID_HANDLE_VALUE THEN
     EXIT DO 'No more devices
   ELSE
     IF DeviceIoControl(hDevice, %IOCTL_DISK_GET_DRIVE_GEOMETRY, BYVAL 0, _ 'Will be TRUE if device is a disk
                        BYVAL 0, BYVAL VARPTR(DiskGeometry), SIZEOF(DISK_GEOMETRY), 0, BYVAL 0 ) THEN
       sPhysicalDiskList = sPhysicalDiskList & FORMAT$(PhysicalDiskIndex, "00") & ":" 'Disk are zero based, Result example: "00:01:04:06"
       INCR PhysicalDiskCount
     END IF
     CloseHandle(hDevice)
     INCR PhysicalDiskIndex
   END IF
LOOP
IF PhysicalDiskCount THEN
   sPhysicalDiskList = LEFT$(sPhysicalDiskList, -1) 'Remove last ":"
   sBuffer           = LEFT$(sPhysicalDiskList, BufferLen) 'Respect buffer lenght
ELSE
   sBuffer = ""
END IF
FUNCTION = PhysicalDiskCount

END FUNCTION
'_____________________________________________________________________________

FUNCTION GetLogicalDiskId(BYREF sBuffer AS STRING, BYVAL BufferLen AS DWORD) EXPORT AS DWORD
LOCAL sLogicalDiskList      AS STRING
LOCAL sLogicalFixedDiskList AS STRING
LOCAL LogicalDiskIndex      AS DWORD
LOCAL LogicalDiskCount      AS DWORD
LOCAL LogicalFixedDiskCount AS DWORD
LOCAL LogicalDiskListLen    AS DWORD
LOCAL zDrive                AS ASCIIZ * 4

sLogicalDiskList   = NUL$(26 * 4 + 1)
LogicalDiskListLen = GetLogicalDriveStrings(LEN(sLogicalDiskList), BYVAL STRPTR(sLogicalDiskList)) 'C:\<NULL>D:\<NULL>R:\<NULL><NULL>
IF LogicalDiskListLen <= BufferLen THEN 'Success
   REPLACE $NUL WITH "" IN sLogicalDiskList
   LogicalDiskCount = LogicalDiskListLen / 4
   FOR LogicalDiskIndex = 0 TO LogicalDiskCount - 1
     zDrive = MID$(sLogicalDiskList, LogicalDiskIndex * 3 + 1, 3)
     SELECT CASE GetDriveType(zDrive)
       CASE %DRIVE_UNKNOWN
       CASE %DRIVE_NO_ROOT_DIR
       CASE %DRIVE_REMOTE
       CASE %DRIVE_CDROM
       CASE %DRIVE_RAMDISK
      'CASE %DRIVE_REMOVABLE
       CASE %DRIVE_FIXED, %DRIVE_REMOVABLE
         IF GetVolumeInfo(zDrive) THEN 'Is there a media inserted
           sLogicalFixedDiskList = sLogicalFixedDiskList & zDrive
           INCR LogicalFixedDiskCount
         END IF
     END SELECT
   NEXT
   sBuffer  = sLogicalFixedDiskList
ELSE 'sBuffer is too small, LogicalDiskListLen is the required size
   sBuffer  = ""
END IF
FUNCTION = LogicalFixedDiskCount

END FUNCTION
'_____________________________________________________________________________

FUNCTION GetPhysicalDiskGeometry(BYVAl hDisk AS DWORD, BYREF DiskGeometry AS DISK_GEOMETRY) EXPORT AS DWORD

'Does not work on USB FAT16
IF DeviceIoControl(hDisk, %IOCTL_DISK_GET_DRIVE_GEOMETRY, BYVAL 0, _
                    BYVAL 0, BYVAL VARPTR(DiskGeometry), SIZEOF(DISK_GEOMETRY), 0, BYVAL 0 ) THEN
   FUNCTION = %TRUE
END IF

END FUNCTION
'_____________________________________________________________________________

FUNCTION OpenPhysicalDisk(BYVAL PhysicalDiskId AS DWORD, BYVAL WritePermission AS LONG) EXPORT AS DWORD
LOCAL zDisk AS ASCIIZ * 20

zDisk = "\\.\PhysicalDrive" & FORMAT$(PhysicalDiskId) 'Physical disk as "\\.\PhysicalDrive0"

IF WritePermission THEN 'Read & Write
   FUNCTION = CreateFile(zDisk, %GENERIC_READ OR %GENERIC_WRITE, _
                                %FILE_SHARE_READ OR %FILE_SHARE_WRITE, BYVAL 0, %OPEN_EXISTING, 0, 0)
ELSE 'Read only
   FUNCTION = CreateFile(zDisk, %GENERIC_READ, _
                                %FILE_SHARE_READ OR %FILE_SHARE_WRITE, BYVAL 0, %OPEN_EXISTING, 0, 0 )
END IF

END FUNCTION
'_____________________________________________________________________________

FUNCTION OpenLogicalDisk(BYVAL sLogicalDisk AS STRING, BYVAL WritePermission AS LONG) EXPORT AS DWORD
LOCAL zDisk AS ASCIIZ * 8

zDisk =  "\\.\" & LEFT$(sLogicalDisk, 2) 'Logical disk as "\\.\C:"
IF WritePermission THEN 'Read & Write
   FUNCTION = CreateFile(zDisk, %GENERIC_READ OR %GENERIC_WRITE, _
                                %FILE_SHARE_READ OR %FILE_SHARE_WRITE, BYVAL 0, %OPEN_EXISTING, 0, 0)
ELSE 'Read only
   FUNCTION = CreateFile(zDisk, %GENERIC_READ, _
                                %FILE_SHARE_READ OR %FILE_SHARE_WRITE, BYVAL 0, %OPEN_EXISTING, 0, 0 )
END IF

END FUNCTION
'_____________________________________________________________________________

FUNCTION GetPhysicalDiskBlockCount(BYVAL hDisk AS DWORD) EXPORT AS QUAD
LOCAL DiskGeometry AS DISK_GEOMETRY
LOCAL OldErrMode   AS LONG

'DeviceIoControl(BYVAL hDisk, %FSCTL_ALLOW_EXTENDED_DASD_IO, BYVAL %NULL, 0, _
'                BYVAL %NULL, 0, BytesReturned, BYVAL %NULL)
'Remarks: A call using the FSCTL_ALLOW_EXTENDED_DASD_IO control code should only be used with great caution
'         by programmers familiar with the underlying structure of a hard disk drive and file system.
'         Improper use or inaccurate checking in subsequent write operations to the partition
'         can result in damage to data on the partition, or destruction of the entire partition.
'         The FSCTL_ALLOW_EXTENDED_DASD_IO control code is used to signal the file system driver
'         not to perform any I/O boundary checks on read or write calls made with the specified handle.
'         FSCTL_ALLOW_EXTENDED_DASD_IO allows access to hidden sectors, a part of the partition
'         that might exist between the first sector of the partition (the boot parameter block)
'         and the first useful sector of the partition. FSCTL_ALLOW_EXTENDED_DASD_IO also
'         allows access to lost clusters, which might exist between the last useful cluster
'         and the end of the partition.

OldErrMode = SetErrorMode(%SEM_FAILCRITICALERRORS) 'Prevent pop-up dialogs if device is not present or loaded
IF DeviceIoControl(hDisk, %IOCTL_DISK_GET_DRIVE_GEOMETRY, BYVAL 0, _
                    BYVAL 0, BYVAL VARPTR(DiskGeometry), SIZEOF(DISK_GEOMETRY), 0, BYVAL 0 ) THEN
   FUNCTION  = DiskGeometry.Cylinders * DiskGeometry.TracksPerCylinder * DiskGeometry.SectorsPerTrack
END IF
SetErrorMode(OldErrMode)

END FUNCTION
'_____________________________________________________________________________

FUNCTION GetLogicalDiskBlockCount(BYVAL sDisk AS STRING) EXPORT AS QUAD
LOCAL FreeBytesForUser AS QUAD 'Available to user
LOCAL UserDiskSize     AS QUAD 'Available to user
LOCAL FreeByteOnDisk   AS QUAD
LOCAL OldErrMode       AS LONG

'DeviceIoControl(BYVAL hDisk, %FSCTL_ALLOW_EXTENDED_DASD_IO, BYVAL %NULL, 0, _ '
'                BYVAL %NULL, 0, BytesReturned, BYVAL %NULL)
'Remarks: A call using the FSCTL_ALLOW_EXTENDED_DASD_IO control code should only be used with great caution
'         by programmers familiar with the underlying structure of a hard disk drive and file system.
'         Improper use or inaccurate checking in subsequent write operations to the partition
'         can result in damage to data on the partition, or destruction of the entire partition.
'         The FSCTL_ALLOW_EXTENDED_DASD_IO control code is used to signal the file system driver
'         not to perform any I/O boundary checks on read or write calls made with the specified handle.
'         FSCTL_ALLOW_EXTENDED_DASD_IO allows access to hidden sectors, a part of the partition
'         that might exist between the first sector of the partition (the boot parameter block)
'         and the first useful sector of the partition. FSCTL_ALLOW_EXTENDED_DASD_IO also
'         allows access to lost clusters, which might exist between the last useful cluster
'         and the end of the partition.

OldErrMode = SetErrorMode(%SEM_FAILCRITICALERRORS) 'Prevent pop-up dialogs if device is not present or loaded

GetDiskFreeSpaceEx(BYVAL STRPTR(sDisk), FreeBytesForUser, UserDiskSize, FreeByteOnDisk)
FUNCTION = UserDiskSize / %BlockSize
SetErrorMode(OldErrMode)

END FUNCTION
'_____________________________________________________________________________

FUNCTION DiskRead(BYVAL hDisk AS DWORD, BYVAL BlockIndex AS QUAD, BYREF sBuffer AS STRING, BYVAL BufferLen AS DWORD) EXPORT AS DWORD
LOCAL FilePointer    AS QUAD
LOCAL NewFilePointer AS QUAD
LOCAL BytesReturned  AS DWORD

FilePointer = BlockIndex * %BlockSize
SetFilePointerEx(hDisk, FilePointer, NewFilePointer, %FILE_BEGIN)
IF ReadFile(hDisk, BYVAL STRPTR(sBuffer), BufferLen, BytesReturned, BYVAL %NULL) THEN
   FUNCTION = BytesReturned
END IF

END FUNCTION
'_____________________________________________________________________________

FUNCTION DiskWrite(BYVAL hDisk AS DWORD, BYVAL BlockIndex AS QUAD, BYREF sBuffer AS STRING, BYVAL BufferLen AS DWORD) EXPORT AS DWORD
LOCAL FilePointer    AS QUAD
LOCAL NewFilePointer AS QUAD
LOCAL BytesWritten   AS DWORD

FilePointer = BlockIndex * %BlockSize
SetFilePointerEx(hDisk, FilePointer, NewFilePointer, %FILE_BEGIN)
IF WriteFile(hDisk, BYVAL STRPTR(sBuffer), BufferLen, BytesWritten, BYVAL %NULL) THEN
   FUNCTION = BytesWritten
END IF

END FUNCTION
'_____________________________________________________________________________

FUNCTION CloseDisk(BYREF hDisk AS DWORD) EXPORT AS DWORD

CloseHandle(hDisk)
FUNCTION = GetLastError()
hDisk = 0

END FUNCTION
'_____________________________________________________________________________

FUNCTION StringToHexView(sString AS STRING) AS STRING 'See D:\Basic\Bas\SRC\Hex\HexToStrGary03.bas
LOCAL  sBuffer    AS STRING
LOCAL  LooperLine AS LONG
LOCAL  LooperChar AS LONG
LOCAL  CharPos    AS LONG
LOCAL  StringLen  AS LONG

StringLen  = LEN(sString)
FOR LooperLine = 0 TO StringLen - 1 STEP 16

   sBuffer = sBuffer & HEX$(LooperLine, 4) & ": "  'Offset

   FOR LooperChar = 1 TO 16 'Hex
     CharPos = LooperLine + LooperChar
     IF CharPos <= StringLen THEN
       sBuffer = sBuffer & HEX$(ASC(sString, CharPos), 2) & $SPC
     END IF
   NEXT

   '000000: 31 32 33 34 35 36 37 38 39 30 41 42 43 44 45 46 ; 1234567890ABCDEF
   sBuffer = sBuffer & SPACE$(54 - (LEN(sBuffer) MOD 74)) & "; "

   FOR LooperChar = 1 TO 16 'Ascii
     CharPos = LooperLine + LooperChar
     IF CharPos <= StringLen THEN
       SELECT CASE ASC(sString, CharPos)
         CASE 0, 1, 9, 10, 13, 27, 28, 29, 30, 31, _ '"Courier New"
              127, 129, 140, 141, 143, 144, 152, 157
           sBuffer = sBuffer & "."
         CASE ELSE
           sBuffer = sBuffer & MID$(sString, CharPos, 1)
       END SELECT

     END IF
   NEXT
   sBuffer = sBuffer & $CRLF
NEXT
FUNCTION = sBuffer

END FUNCTION
'______________________________________________________________________________

CALLBACK FUNCTION DlgProc()
LOCAL  pNotifyMessageHeader         AS NMHDR POINTER
LOCAL  pNotifyMessageUpDown         AS NM_UPDOWN POINTER
LOCAL  MinMaxInfoPtr                AS MINMAXINFO POINTER
DIM    UpDownAccelleration(0 TO 7)  AS UDACCEL
LOCAL  DiskGeometry                 AS DISK_GEOMETRY
STATIC sLogicalDisk                 AS STRING
LOCAL  sBuffer                      AS STRING
STATIC sDisk                        AS STRING
LOCAL  BlockIndex                   AS QUAD
STATIC BlockCount                   AS QUAD
LOCAL  UpDownAccellerationStepCount AS DWORD
STATIC hDisk                        AS DWORD
STATIC PhysicalDisk                 AS DWORD
STATIC Disk                         AS DWORD
STATIC DiskCount                    AS DWORD
STATIC DiskError                    AS LONG
LOCAL  ReadCount                    AS LONG
LOCAL  Looper                       AS LONG
STATIC UpDownValH                   AS LONG
STATIC UpDownDeltaH                 AS LONG
STATIC UpDownStartPosition          AS LONG

SELECT CASE CBMSG

   CASE %WM_INITDIALOG
     UpDownStartPosition = 0 - 2147483648 + 1 'Needed for 2 tb, LONG -2,147,483,648 to +2,147,483,647  (+1 so the control limit will be respected)
     'Sets the acceleration for the up-down control
     UpDownAccelleration(0).nSec = 1
     UpDownAccelleration(0).nInc = 1
     UpDownAccelleration(1).nSec = 2
     UpDownAccelleration(1).nInc = 10
     UpDownAccelleration(2).nSec = 3
     UpDownAccelleration(2).nInc = 100
     UpDownAccelleration(3).nSec = 4
     UpDownAccelleration(3).nInc = 1000
     UpDownAccelleration(4).nSec = 5
     UpDownAccelleration(4).nInc = 10000
     UpDownAccelleration(5).nSec = 6
     UpDownAccelleration(5).nInc = 100000
     UpDownAccelleration(6).nSec = 7
     UpDownAccelleration(6).nInc = 1000000
     UpDownAccelleration(7).nSec = 8
     UpDownAccelleration(7).nInc = 10000000
     UpDownAccellerationStepCount = 8
     CONTROL SEND hDlg, %UpDownH, %UDM_SETACCEL, UpDownAccellerationStepCount, VARPTR(UpDownAccelleration(0))

     PostMessage(hDlg, %WM_COMMAND, MAKDWD(%OptionPhysical, %BN_CLICKED), GetDlgItem(hDlg, %OptionPhysical))

   CASE %WM_COMMAND
     SELECT CASE LOWRD(CBWPARAM)

       CASE %OptionLogical, %OptionPhysical
         IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN

           BlockIndex = 0
           CONTROL SET TEXT hDlg, %EditBlockIndex, "0"
           CONTROL GET CHECK hDlg, %OptionPhysical TO PhysicalDisk
           COMBOBOX RESET hDlg, %ComboDisk
           IF PhysicalDisk THEN
             sBuffer = NUL$(3 * 26)
             DiskCount = GetPhysicalDiskId(sBuffer, LEN(sBuffer))
             IF DiskCount = 0 THEN
               MessageBox(hDlg, BYCOPY "No disk fatal error", BYCOPY $AppName, %MB_ICONINFORMATION OR %MB_OK)
               PostMessage(hDlg, %WM_SYSCOMMAND, %SC_CLOSE, BYVAL 0)
             ELSE
               FOR Looper = 1 TO DiskCount
                 COMBOBOX ADD hDlg, %ComboDisk, PARSE$(sBuffer, ":", Looper)
               NEXT
               Disk = VAL(PARSE$(sBuffer, ":", 1)) 'Set to first disk, zero based
               COMBOBOX SELECT hDlg, %ComboDisk, 1 'Set to first disk, zero based
             END IF
           ELSE 'OptionLogical
             sBuffer = NUL$(3 * 26)
             DiskCount = GetLogicalDiskId(sBuffer, LEN(sBuffer))
             IF DiskCount = 0 THEN
               MessageBox(hDlg, BYCOPY "No disk fatal error", BYCOPY $AppName, %MB_ICONINFORMATION OR %MB_OK)
               PostMessage(hDlg, %WM_SYSCOMMAND, %SC_CLOSE, BYVAL 0)
             ELSE
               FOR Looper = 1 TO DiskCount
                 COMBOBOX ADD hDlg, %ComboDisk, PARSE$(sBuffer, "\", Looper) & "\"
               NEXT
               sDisk = PARSE$(sBuffer, "\", 1) & "\" 'Set to first disk,
               COMBOBOX SELECT hDlg, %ComboDisk, 1
             END IF
           END IF
           CONTROL SET TEXT hDlg, %LabelDiskCount, "Disk found: " & STR$(DiskCount)
           PostMessage(hDlg, %WM_COMMAND, MAKDWD(%ComboDisk, %CBN_EDITCHANGE), GetDlgItem(hDlg, %ComboDisk))
         END IF

       CASE %ComboDisk
         IF CBCTLMSG = %CBN_EDITCHANGE OR CBCTLMSG = 1 THEN
           IF hDisk THEN CloseDisk(hDisk)
           Disk       = 0
           BlockCount = 0
           BlockIndex = 0
           CONTROL SET TEXT hDlg, %EditBlockIndex, FORMAT$(BlockIndex) '"0"
           IF PhysicalDisk THEN
             CONTROL GET TEXT hDlg, %ComboDisk TO sBuffer
             Disk       = VAL(sBuffer)
             hDisk      = OpenPhysicalDisk(Disk, %FALSE) '%FALSE for open in read only mode
             BlockCount = GetPhysicalDiskBlockCount(hDisk)
             IF GetPhysicalDiskGeometry(hDisk, DiskGeometry) THEN
               CONTROL SET TEXT hDlg, %LabelCyl,  "Cyl : "  & FORMAT$(DiskGeometry.Cylinders, "0,")
               CONTROL SET TEXT hDlg, %LabelHead, "Head: "  & FORMAT$(DiskGeometry.TracksPerCylinder)
               CONTROL SET TEXT hDlg, %LabelSect, "Sect: "  & FORMAT$(DiskGeometry.SectorsPerTrack)
             END IF
           ELSE 'Logical disk
             COMBOBOX GET TEXT hDlg, %ComboDisk TO sDisk 'sLogicalDisk
             hDisk      = OpenLogicalDisk(sDisk, %FALSE) '%FALSE for open in read only mode
             BlockCount = GetLogicalDiskBlockCount(sDisk)
             CONTROL SET TEXT hDlg, %LabelCyl,  ""
             CONTROL SET TEXT hDlg, %LabelHead, ""
             CONTROL SET TEXT hDlg, %LabelSect, ""
           END IF
           CONTROL SET TEXT hDlg, %LabelMeg,  "Bytes: " & FORMAT$(BlockCount * %BlockSize, "0,")
           CONTROL SEND hDlg, %UpDownH, %UDM_SETRANGE32, UpDownStartPosition, _ 'LONG -2,147,483,648 '
                                                         UpDownStartPosition + BlockCount - 1
           CONTROL SEND hDlg, %UpDownH, %UDM_SETPOS32, 0, UpDownStartPosition 'Reset to start pos
           CONTROL SET TEXT hDlg, %LabelBlockCount, "Block: " & FORMAT$(BlockCount, "0,")
           DIALOG POST hDlg, %WM_COMMAND, %ButtonRead, %BN_CLICKED
         END IF

       CASE %EditBlockIndex
         IF HIWRD(CBWPARAM) = %EN_CHANGE THEN
             CONTROL GET TEXT hDlg, %EditBlockIndex TO sBuffer
             BlockIndex = VAL(sBuffer)
             IF BlockIndex > BlockCount - 1 THEN BlockIndex = BlockCount - 1
             IF BlockIndex < 0 THEN BlockIndex = 0
             CONTROL SEND hDlg, %UpDownH, %UDM_SETPOS32, 0, UpDownStartPosition + BlockIndex
         END IF

       CASE %ButtonRead, %IDOK
         IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN
           IF DiskError = %FALSE THEN
             CONTROL GET TEXT hDlg, %EditBlockIndex TO sBuffer
             BlockIndex = VAL(sBuffer)
             IF BlockIndex > BlockCount - 1 THEN BlockIndex = BlockCount - 1
             IF BlockIndex < 0 THEN BlockIndex = 0
             CONTROL SET TEXT hDlg, %EditBlockIndex, FORMAT$(BlockIndex)
             CONTROL SEND hDlg, %UpDownH, %UDM_SETPOS32, 0, UpDownStartPosition + BlockIndex
             sBuffer = NUL$(%BlockSize) '4096
             ReadCount = DiskRead(hDisk, BlockIndex, sBuffer, LEN(sBuffer))
             IF ReadCount = 0 THEN
               DiskError = %TRUE
               CONTROL SET TEXT hDlg, %EditHex, ""
               MessageBox(hDlg, "Disk error on block " & FORMAT$(BlockIndex, "0,"), _
                          $AppName, %MB_ICONINFORMATION OR %MB_OK)
               DiskError = %FALSE
             ELSE
               CONTROL SET TEXT hDlg, %EditHex, StringToHexView(sBuffer)
             END IF
           END IF
         END IF

     END SELECT

   CASE %WM_NOTIFY
     pNotifyMessageHeader = CBLPARAM
     IF @pNotifyMessageHeader.Code = %UDN_DELTAPOS THEN
        IF (@pNotifyMessageHeader.idFrom = %UpDownH) THEN
           pNotifyMessageUpDown = CBLPARAM
           UpDownDeltaH = @pNotifyMessageUpDown.iDelta
        END IF
     END IF

   CASE %WM_VSCROLL, %WM_HSCROLL
     IF GetDlgCtrlID(CBLPARAM) = %UpDownH THEN
       LOCAL UpDownError AS LONG
       UpDownValH = SendMessage(CBLPARAM, %UDM_GETPOS32, %FALSE, UpDownError) '+ UpDownStartPosition
       CONTROL SET TEXT hDlg, %EditBlockIndex, FORMAT$(UpDownValH - UpDownStartPosition) '-2147483648 'Needed for 2 tb, LONG -2,147,483,648 to +2,147,483,647
       DIALOG POST hDlg, %WM_COMMAND, %ButtonRead, %BN_CLICKED
     END IF

   CASE %WM_DESTROY
     IF hDisk THEN CloseDisk(hDisk)

  END SELECT

END FUNCTION
'_____________________________________________________________________________

FUNCTION PBMAIN()
LOCAL hFont             AS DWORD
LOCAL hIconBig          AS DWORD
LOCAL hIconSmall        AS DWORD
LOCAL DiskGeometry      AS DISK_GEOMETRY
LOCAL hDisk             AS DWORD
LOCAL PhysicalDiskCount AS DWORD
LOCAL PhysicalDiskId    AS DWORD
LOCAL LogicalDiskCount  AS DWORD
LOCAL ReadCount         AS DWORD
LOCAL BlockCount        AS QUAD
LOCAL BlockIndex        AS QUAD
LOCAL sDisk             AS STRING
LOCAL sBuffer           AS STRING

IF IsUserLocalAdmin = %FALSE THEN
   MessageBox(%HWND_DESKTOP, "Please restart program as administrator.", $AppName & " - Error", %MB_OK)
ELSE
   DIALOG FONT "Segoe UI", 9
   DIALOG NEW %HWND_DESKTOP, $AppName & " for Windows NT/2000/XP/Vista/Seven/Eight/Ten",,, _
   398, 360, %WS_CAPTION OR %WS_MINIMIZEBOX OR %WS_SYSMENU, 0 TO hDlg

   CONTROL ADD FRAME, hDlg, -1, "", 02, 0, 394, 58, %BS_LEFT OR %BS_TOP, %WS_EX_LEFT
   CONTROL ADD OPTION, hDlg, %OptionPhysical, "&Physical", 7, 6, 35, 9, %BS_LEFT OR %BS_VCENTER OR %WS_TABSTOP OR %WS_GROUP, %WS_EX_LEFT
   CONTROL ADD OPTION, hDlg, %OptionLogical, "&Logical", 47, 6, 35, 9, %BS_LEFT OR %BS_VCENTER OR %WS_TABSTOP, %WS_EX_LEFT
   CONTROL SET OPTION hDlg, %OptionPhysical, %OptionPhysical, %OptionLogical
   CONTROL ADD LABEL, hDlg, %LabelDiskCount, "Disk found:", 7, 18, 60, 9
   CONTROL ADD LABEL, hDlg, %LabelDisk, "Hard Disk", 7, 31, 50, 9
   CONTROL ADD COMBOBOX, hDlg, %ComboDisk, , 57, 29, 31, 72, _
   %CBS_DROPDOWNLIST OR %CBS_HASSTRINGS OR %CBS_SORT OR %WS_TABSTOP, %WS_EX_CLIENTEDGE OR %WS_EX_LEFT
   CONTROL ADD LABEL, hDlg, %LabelBlockCount, "LB:  00000000", 100, 7, 90, 9, %SS_NOTIFY
   CONTROL ADD LABEL, hDlg, %LabelMeg, "Meg: 00000000", 100, 17, 90, 9, %SS_NOTIFY
   CONTROL ADD LABEL, hDlg, %LabelCyl, "Cylinder: 00000000", 215, 7, 80, 9, %SS_NOTIFY
   CONTROL ADD LABEL, hDlg, %LabelHead, "Head:     00000", 215, 17, 80, 9, %SS_NOTIFY
   CONTROL ADD LABEL, hDlg, %LabelSect, "Sector:   00000", 215, 27, 80, 9, %SS_NOTIFY
   CONTROL ADD LABEL, hDlg, -1, "LogicalBlock", 300, 7, 90, 9
   CONTROL ADD TEXTBOX, hDlg, %EditBlockIndex, "0", 300, 17, 45, 12
   CONTROL ADD BUTTON, hDlg, %ButtonRead, "Read", 350, 16, 40, 13
   CONTROL ADD "msctls_updown32", hDlg, %UpDownH, "", 305, 32, 0, 0, %WS_CHILD OR %WS_VISIBLE OR %UDS_HORZ
   CONTROL SET SIZE hDlg, %UpDownH, 36, 15
   CONTROL ADD LABEL, hDlg, %LabelStatus, "Status", 47, 46, 250, 9

   CONTROL ADD TEXTBOX, hDlg, %EditHex, "", 3, 63, 393, 295, %WS_CHILD OR %WS_VISIBLE OR _
   %WS_TABSTOP OR %WS_HSCROLL OR %WS_VSCROLL OR %ES_LEFT OR %ES_MULTILINE OR _
   %ES_NOHIDESEL OR %ES_AUTOHSCROLL OR %ES_AUTOVSCROLL OR %ES_WANTRETURN, _
   %WS_EX_CLIENTEDGE OR %WS_EX_LEFT OR %WS_EX_LTRREADING OR %WS_EX_RIGHTSCROLLBAR
    hFont = CreateFont(16, 0, _      'Height, Width usually 0,
                       0, 0, _       'Escapement(angle), Orientation
                       0, 0, 0, 0, _ 'Bold, Italic, Underline, Strikethru
                       0, %OUT_TT_PRECIS, %CLIP_DEFAULT_PRECIS, %DEFAULT_QUALITY, %FF_DONTCARE, _
                       BYCOPY "Courier New") 'Consolas
   SendDlgItemMessage(hDlg, %EditHex, %WM_SETFONT, hFont, %TRUE)

   ExtractIconEx("shell32.dll", 8, BYVAL VARPTR(hIconBig), BYVAL VARPTR(hIconSmall), 1)
   SetClassLong(hDlg, %GCL_HICONSM, hIconSmall) 'Set an icon
   SetClassLong(hDlg, %GCL_HICON, hIconBig) 'Set an icon
   SendMessage(hDlg, %WM_SETICON, %ICON_SMALL, hIconSmall)
   SendMessage(hDlg, %WM_SETICON, %ICON_BIG, hIconBig)

   DIALOG SHOW MODAL hDlg CALL DlgProc

   DestroyIcon(hIconSmall)
   DestroyIcon(hIconBig)
   DeleteObject(hFont)

END IF

END FUNCTION
'_____________________________________________________________________________
'


Haut
 Profil  
Répondre en citant le message  
 Sujet du message: Re: Accès direct Secteur de disque
MessagePosté: Jeu 21/Juin/2018 14:59 
Hors ligne
Avatar de l’utilisateur

Inscription: Lun 24/Sep/2012 20:57
Messages: 36
Localisation: Somme
Et non je n'avais pas trouvé merci!


Haut
 Profil  
Répondre en citant le message  
 Sujet du message: Re: Accès direct Secteur de disque
MessagePosté: Jeu 21/Juin/2018 22:17 
Hors ligne
Avatar de l’utilisateur

Inscription: Jeu 21/Juin/2018 6:01
Messages: 6
Voici un aperçu des options "physique" et "volume" du dialogue Direct disk read


Haut
 Profil  
Répondre en citant le message  
Afficher les messages postés depuis:  Trier par  
Poster un nouveau sujet Répondre au sujet  [ 21 messages ]  Aller à la page Précédente  1, 2

Heures au format UTC + 1 heure


Qui est en ligne

Utilisateurs parcourant ce forum: Aucun utilisateur enregistré et 1 invité


Vous ne pouvez pas poster de nouveaux sujets
Vous ne pouvez pas répondre aux sujets
Vous ne pouvez pas éditer vos messages
Vous ne pouvez pas supprimer vos messages

Rechercher:
Aller à:  
cron

 


Powered by phpBB © 2008 phpBB Group | Traduction par: phpBB-fr.com
subSilver+ theme by Canver Software, sponsor Sanal Modifiye