bool TForm1::CopyFile2() {
AnsiString srcPath = ExtractFilePath(Application->ExeName) + "Windows.pdf";
AnsiString destPath = ExtractFilePath(Application->ExeName) + "dest.pdf";
HANDLE hSrcFile, hDestFile;
hSrcFile = CreateFile(srcPath.c_str(), // open MYFILE.TXT
GENERIC_READ, // open for reading
FILE_SHARE_READ, // share for reading
NULL, // no security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template
if (hSrcFile == INVALID_HANDLE_VALUE)
return false;
hDestFile = CreateFile(destPath.c_str(), // create MYFILE.TXT
GENERIC_WRITE , // open for writing
0, // do not share
NULL, // no security
CREATE_ALWAYS, // overwrite existing
FILE_ATTRIBUTE_NORMAL, // normal file
NULL);
if (hDestFile == INVALID_HANDLE_VALUE)
return false;
char* buffer = new char[1024];
ULONG lpNumberOfBytesRead, lpNumberOfBytesWritten;
DWORD fileSize, offset = 0;
fileSize = GetFileSize(hSrcFile, NULL);
if (fileSize == INVALID_FILE_SIZE)
return false;
while (offset < fileSize) {
BOOL bFlag = ReadFile(hSrcFile, buffer, 1024, &lpNumberOfBytesRead, NULL);
if (!bFlag) {
return false;
}
WriteFile(hDestFile, buffer, 1024, &lpNumberOfBytesWritten, NULL);
offset += 1024;
}
CloseHandle(hSrcFile);
CloseHandle(hDestFile);
delete[] buffer;
return true;
}