I use winsock to receive a 1024byte buffer like this:
var
buffer : array[0..1023] of byte;
endarray : array of byte;
hFile : THandle;
dwWritten : DWORD;
dwRead : DWORD;
begin
SetLength (endarray, 3000); //fixxed size (doesn't really matter here, cause I know the size)
hFile := CreateFileW('test.bin', GENERIC_WRITE, FILE_SHARE_WRITE, nil, CREATE_NEW, 0, 0);
SetFilePointer(hFile, 0, nil, FILE_BEGIN);
repeat
dwRead := recv(MySock, Buffer, 1024, 0);
WriteFile(hFile, buffer[0], dwRead, dwWritten, nil); // works fine!
// Add the buffer to the endarray but how?
until (dwRead = 0) or (dwRead = -1)
[...]
CloseHandle (hFile);
end;
How can I add the buffer to the endarray automatically so it actually gets appended to the end?
Like this:
And also remove the first line of code from your function that pre-allocates
endarray.If you’d rather allocate the buffer once (as per the code in the question) then you can code it like this:
But that’s a buffer overrun waiting to happen!