スレッドと TFileStream のメソッドを使用してファイルをコピーするアプリケーションを作成しましたが、特に大きなファイルをコピーするときの速度に少しがっかりしました。それから、ファイル マッピングについて聞いたことがあります。ファイルへのアクセスがはるかに高速になるため、明らかに、はるかに高速にコピーする方法が得られる可能性があります。
私は初心者なのでしようとしていますが、ファイルマッピングを介してファイルをコピーすることができませんでした。(ファイルは test2.iso を作成しますが、代わりに 0ko 3GB ^ ^ を実行します。)
これが私のコードです。
procedure TForm1.Button1Click(Sender: TObject);
var
FFilehandle: THANDLE;
FFileMap: THANDLE;
FmappingPtr: pchar;
hFile2: THANDLE ;
SizeFile1,BytesWritten: DWORD ;
begin
FFilehandle := CreateFile('titan.iso',
GENERIC_WRITE OR GENERIC_READ,
FILE_SHARE_READ OR FILE_SHARE_WRITE,
nil,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if (FFilehandle <> INVALID_HANDLE_VALUE) then
begin
FFileMap := CreateFileMapping(FFileHandle, // handle to file to map
nil, // optional security attributes
PAGE_READWRITE, // protection for mapping object
0, // high-order 32 bits of object size
2*1024, // low-order 32 bits of object size
0); //
if (FFileMap <> NULL) then
begin
FMappingPtr := MapViewOfFile(FFileMap,
FILE_MAP_WRITE,
0,
0,
0);
if Assigned(FMappingPtr) then
begin
// Manipulation de FMappingPtr
hFile2 := CreateFile('test.iso', GENERIC_WRITE, 0, nil,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (hFile2 <> INVALID_HANDLE_VALUE) then
begin
SizeFile1 := GetFileSize(FFilehandle, NIL); // recupere taille du fichier 1
WriteFile(hFile2, Fmappingptr, SizeFile1, &BytesWritten, NIL); // Transfert la mémoire mappé dans file 2
// libere les ressources
end
else
MessageBox(0, 'Impossible de lire le fichier mappé', 'Error', 0);
UnmapViewOfFile(Fmappingptr);
CloseHandle(FFileMap);
CloseHandle(FFilehandle);
CloseHandle(hFile2);
end
else
begin
CloseHandle (FFileMap);
CloseHandle (FFileHandle);
MessageBox(0, 'Impossible de lire le fichier mappé', 'Error', 0);
end;
end
else
begin
CloseHandle (FFilemap);
MessageBox(0, 'Impossible de mappe le fichier en mémoire', 'Error', 0);
end;
end
else
MessageBox(NULL, 'Impossible d''ouvrir le fichier', 'Error', NULL);
end;
私の問題はどこですか?