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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
| unit fmchkdsk;
interface
uses
Windows, Messages, SysUtils, Classes;
const
fmifs = 'fmifs.dll' ;
WM_GETOBJ = WM_USER + 701 ;
// flags media
FMIFS_HARDDISK = $0C ;
FMIFS_FLOPPY = $08 ;
// Commandes Output
type
TextOutput = record
Lines: DWORD ;
Output: PANSICHAR ; //PCHAR
end ;
PTextOutput = ^TextOutput ;
// Commandes Callback
TCallBackCommand = ( PROGRESS, DONEWITHSTRUCTURE, UNKNOWN2,
UNKNOWN3, UNKNOWN4, UNKNOWN5, INSUFFICIENTRIGHTS,
UNKNOWN7, UNKNOWN8, UNKNOWN9, UNKNOWNA, DONE,
UNKNOWNC, UNKNOWND, OUTPUT, STRUCTUREPROGRESS,
UNKNOWN10) ; // pas tous utilisés
var
// Commandes Chkdsk
Chkdsk: procedure (
DriveRoot: PWCHAR;
Format: PWChar ; // essayé PANSICHAR partout a la place de PCHAR -> même résultat
CorrectErrors: BOOL;
Verbose: BOOL;
CheckOnlyIfDirty: BOOL;
ScanDrive: BOOL;
Unused2: DWORD;
Unused3: DWORD;
Callback: Pointer); stdcall;
[...]
type
TMediaType = (mtHardDisk, mtFloppy) ;
TFileSystem = (fsNTFS, fsFAT, fsFAT32) ;
TProgressEvent = Procedure (Percent: integer; var Cancel: boolean) of object ;
TInfoEvent = Procedure (Info: string; var Cancel: boolean) of object ;
TFmtChkDsk = class(TComponent)
private
{ Private declarations }
fProgressEvent: TProgressEvent ;
fInfoEvent: TInfoEvent ;
fDoneOK: boolean ;
fFileSysProblem: boolean ;
fFreeSpaceAlloc: boolean ;
fFirstErrorLine: string ;
protected
{ Protected declarations }
function CheckDriveExists (const WDrive: WideString;
CheckInUse: boolean ; var WFormat: WideString): boolean ;
function doProgressEvent (const Percent: integer): boolean ;
function doInfoEvent (const Info: string): boolean ;
procedure WMGETOBJ (var msg: TMessage); message WM_GETOBJ;
public
{ Public declarations }
function LoadFmifs: boolean ;
FileSystem: TFileSystem;
function CheckDisk (const DrvRoot: string; CorrectErrors, Verbose,
CheckOnlyIfDirty, ScanDrive: boolean): boolean ;
end;
FmtChkException = class(Exception);
var
Fmifsib: THandle = 0 ;
Fmifs_Loaded: Boolean = false ; // Check si les fonctions DLL sont chargées
FmtObj: TObject ;
implementation
// Définitions callback
function ChkDskCallback (Command: TCallBackCommand; SubAction: DWORD;
ActionInfo: Pointer): Boolean; stdcall;
var
flag: pboolean ;
percent: pinteger ;
toutput: PTextOutput ;
Obj: TObject ;
info: string ;
progper: integer ;
cancelflag: boolean ;
begin
result := true ;
cancelflag := false ;
progper := -1 ;
info := '' ;
// Obj := TObject (SendMessage (HInstance, WM_GETOBJ, 0, 0)) ;
Obj := FmtObj ;
if NOT Assigned (TFmtChkDsk (Obj)) then exit ;
case Command of
Progress:
begin
percent := ActionInfo ;
progper := percent^ ;
end ;
Output:
begin
toutput := ActionInfo ;
info := Trim (toutput^.Output) ;
// texte tel que sorti
if (Pos ('found problems', info) > 0) or
(Pos ('Correcting errors', info) > 0) or
(Pos ('Errors found', info) > 0) or
(Pos ('(fix) option', info) > 0) then
begin
TFmtChkDsk (Obj).fFileSysProblem := true ;
if TFmtChkDsk (Obj).fFirstErrorLine = '' then
TFmtChkDsk (Obj).fFirstErrorLine := info ;
end ;
if (Pos ('free space marked as allocated', info) > 0) then
begin
TFmtChkDsk (Obj).fFreeSpaceAlloc := true ;
if TFmtChkDsk (Obj).fFirstErrorLine = '' then
TFmtChkDsk (Obj).fFirstErrorLine := info ;
end ;
end ;
Done:
begin
flag := ActionInfo ;
TFmtChkDsk (Obj).fDoneOK := flag^ ;
if flag^ then
info := 'Chkdsk : Terminé avec succès'
else
info := 'Chkdsk: Dans l''impossibilité de terminer' ;
end ;
else
info := 'ChkDsk Callback: ' + IntToStr (Ord (Command)) ;
end ;
if progper >= 0 then cancelflag := TFmtChkDsk (Obj).doProgressEvent (progper) ;
if info <> '' then cancelflag := TFmtChkDsk (Obj).doInfoEvent (info) ;
result := NOT cancelflag ;
end ;
procedure TChkDsk.WMGETOBJ (var msg: TMessage);
begin
msg.Result := Integer (TFmtChkDsk) ;
end ;
function TFmtChkDsk.doProgressEvent (const Percent: integer): boolean ;
begin
result := false ;
if Assigned (fProgressEvent) then fProgressEvent (Percent, result) ;
end ;
function TFmtChkDsk.doInfoEvent (const Info: string): boolean ;
begin
result := false ;
if Assigned (fInfoEvent) then fInfoEvent (Info, result) ;
end ;
function TChkDsk.CheckDriveExists (const WDrive: WideString;
CheckInUse: boolean ; var WFormat: WideString): boolean ;
var
FileSysName : Array[0..MAX_PATH] of WChar;
VolumeName : Array[0..MAX_PATH] of WChar;
maxcomlen, flags: longword;
handle: THandle ;
voldev: WideString ;
begin
if (Length (WDrive) < 2) or (WDrive [2] <> ':') then
begin
raise FmtChkException.Create('Spécification disque non valide: ' + WDrive);
exit ;
end ;
[...]
function TChkDsk.CheckDisk (const DrvRoot: string; CorrectErrors, Verbose,
CheckOnlyIfDirty, ScanDrive: boolean): boolean ;
var
wdrive, wformat: widestring ;
begin
result := false ;
if NOT LoadFmifs then exit ;
wdrive := Uppercase (DrvRoot) ;
if NOT CheckDriveExists (wdrive, CorrectErrors, wformat) then exit ;
FmtObj := Self ;
fDoneOK := false ;
fFileSysProblem := false ;
fFreeSpaceAlloc := false ;
fFirstErrorLine := '' ;
Chkdsk (PWchar (wdrive), PWchar (wformat), CorrectErrors, Verbose,
CheckOnlyIfDirty, ScanDrive, 0, 0, @ChkDskCallback) ;
if fFileSysProblem then
result := true // ignore stopped if got an error
else
result := fDoneOK ;
end ;
function TChkDsk.VolumeCompression (const DrvRoot: string; Enable: boolean): boolean ;
var
wdrive, wformat: widestring ;
begin
result := false ;
if NOT LoadFmifs then exit ;
wdrive := Uppercase (DrvRoot) ;
if NOT CheckDriveExists (wdrive, true, wformat) then exit ;
result := EnableVolumeCompession (PWchar (wdrive), Enable) ;
end ;
[...]
result := false ;
Fmifs_Loaded := True ;
Fmifsib := LoadLibrary (fmifs) ;
if Fmifsib = 0 then exit ;
Chkdsk := GetProcAddress (Fmifsib, 'Chkdsk') ;
EnableVolumeCompession := GetProcAddress (Fmifsib, 'EnableVolumeCompession') ;
result := Assigned (Chkdsk) ;
end ;
[...]
end. |
Partager