I am developing a Delphi XE6 application and I created a DLL and the calling EXE application.
Let's say a DLL exports the following method:
procedure GetBuffer(out_buffer: PChar; out_buffer_length: integer); stdcall; export;
The calling application will usually need to do something like this to retrieve the content of the buffer:
// Call #1 => get the length of the buffer
GetBuffer(nil, aLength);
// Allocate memory
SetLength(aBuffer, aLength);
// Call #2 => get the actual content
GetBuffer(aBuffer, aLength);
What if this buffer is the content of a file, and it has changed between Call #1 and Call #2: the resulting aBuffer
might be truncated.
What would be the best way to make an atomic call to GetBuffer in that scenario ?