program project1;
{$APPTYPE CONSOLE}
type
LPCSTR = PAnsiChar;
DWORD = Cardinal;
LongWord = Cardinal;
ULONG_PTR = UIntPtr;
LongBool = False..Boolean(4294967295);
BOOL = LongBool;
UINT = LongWord;
HWND = UIntPtr;
THandle = UIntPtr;
POverlapped = ^TOverlapped;
_OVERLAPPED = record
Internal: ULONG_PTR;
InternalHigh: ULONG_PTR;
Offset: DWORD;
OffsetHigh: DWORD;
hEvent: THandle;
end;
TOverlapped = _OVERLAPPED;
PSecurityAttributes = ^TSecurityAttributes;
_SECURITY_ATTRIBUTES = record
nLength: DWORD;
lpSecurityDescriptor: Pointer;
bInheritHandle: BOOL;
end;
TSecurityAttributes = _SECURITY_ATTRIBUTES;
const
GENERIC_WRITE = $40000000;
FILE_SHARE_READ = $00000001;
FILE_SHARE_WRITE = $00000002;
CREATE_NEW = 1;
OPEN_EXISTING = 3;
FILE_END = 2;
FILE_ATTRIBUTE_NORMAL = $00000080;
INVALID_HANDLE_VALUE = THandle(-1);
INVALID_SET_FILE_POINTER = DWORD(-1);
NO_ERROR = 0;
function WriteFile(hFile: THandle; const Buffer; nNumberOfBytesToWrite: DWORD; var lpNumberOfBytesWritten: DWORD; lpOverlapped: POverlapped): BOOL; stdcall; external 'kernel32.dll' name 'WriteFile';
function CreateFileA(lpFileName: LPCSTR; dwDesiredAccess, dwShareMode: DWORD; lpSecurityAttributes: PSecurityAttributes; dwCreationDisposition, dwFlagsAndAttributes: DWORD; hTemplateFile: THandle): THandle; stdcall; external 'kernel32.dll' name 'CreateFileA';
function SetFilePointer(hFile: THandle; lDistanceToMove: Longint; lpDistanceToMoveHigh: Pointer; dwMoveMethod: DWORD): DWORD; stdcall; external 'kernel32.dll' name 'SetFilePointer';
function CloseHandle(hObject: THandle): BOOL; stdcall; external 'kernel32.dll' name 'CloseHandle';
function MessageBoxA(hWnd: HWND; lpText, lpCaption: LPCSTR; uType: UINT): Integer; stdcall; external 'user32.dll' name 'MessageBoxA';
function GetLastError: DWORD; stdcall; external 'kernel32.dll' name 'GetLastError';
procedure PutDataToFile(const AFileName: string; AData: PAnsiChar);
var
hFile: THandle;
cnt: Cardinal;
DataSize: Integer;
dummy: DWORD;
error: DWORD;
begin
DataSize := StrLen(AData);
hFile := CreateFileA(PAnsiChar(AFileName), GENERIC_WRITE, FILE_SHARE_READ or FILE_SHARE_WRITE, nil,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (hFile = INVALID_HANDLE_VALUE) then
hFile := CreateFileA(PAnsiChar(AFileName), GENERIC_WRITE, FILE_SHARE_READ or FILE_SHARE_WRITE, nil,
CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0);
if (hFile <> INVALID_HANDLE_VALUE) then
begin
dummy := MessageBoxA(0, PAnsiChar('CreateFileA() success!'), PAnsiChar('Information'), 0);
dummy := SetFilePointer(hFile, 0, nil, FILE_END);
error := GetLastError;
if ((dummy <> INVALID_SET_FILE_POINTER) and (error = NO_ERROR)) then
begin
dummy := MessageBoxA(0, PAnsiChar('SetFilePointer() success!'), PAnsiChar('Information'), 0);
if WriteFile(hFile, AData^, DataSize, cnt, nil) then
dummy := MessageBoxA(0, PAnsiChar('WriteFile() success!'), PAnsiChar('Information'), 0)
else
dummy := MessageBoxA(0, PAnsiChar('WriteFile() failed.'), PAnsiChar('Information'), 0)
end
else
dummy := MessageBoxA(0, PAnsiChar('SetFilePointer() failed.'), PAnsiChar('Information'), 0);
if CloseHandle(hFile) then
dummy := MessageBoxA(0, PAnsiChar('CloseHandle() success!'), PAnsiChar('Information'), 0)
else
dummy := MessageBoxA(0, PAnsiChar('CloseHandle() failed.'), PAnsiChar('Information'), 0);
end
else
dummy := MessageBoxA(0, PAnsiChar('CreateFileA() failed.'), PAnsiChar('Information'), 0);
end;
begin
PutDataToFile('test.txt', 'Test file created');
ReadLn;
end.