c++c++builder-2010c++builder-6

GetFileSizeEx corrupts file handle


Currently I'm using GetFileSizeEx to track how large a log files gets before I write to it. We have limited space and anything if we try to create a file larger than 100 megabytes we stop logging data. The problem is for some reason GetFileSizeEx will corrupt the file handle that I am using.

if( hFileHandle != INVALID_HANDLE_VALUE && hFileHandle != NULL )
{
 asDbgMsg = asDbgMsg + asDelimeter;
 dwBytesToWrite =asDbgMsg.Length();
 pWrtBuffer = asDbgMsg.c_str();
 // Get the file size we are about to write to.
 PLARGE_INTEGER lpFileSize;
 GetFileSizeEx(hFileHandle, lpFileSize);

 // Don't write out the file if it is more than 100 mb!
 if(lpFileSize->QuadPart < 104857600)
 {
  WriteFile( hFileHandle, pWrtBuffer, dwBytesToWrite, &dwBytesWritten, NULL );
 }
}

hFileHandle will go from a normal value (00000EB8) to ???? in Rad studio's debugger.

Now I've solved this by using the GetFileSize function instead:

if( hFileHandle != INVALID_HANDLE_VALUE && hFileHandle != NULL )
{
 asDbgMsg = asDbgMsg + asDelimeter;
 dwBytesToWrite =asDbgMsg.Length();
 pWrtBuffer = asDbgMsg.c_str();
 // Get the file size we are about to write too.
 DWORD test;
 GetFileSize(hFileHandle, &test);
 // Don't write out the file if it is more than 100 mb!
 if(test < 104857600)
 {
  WriteFile( hFileHandle, pWrtBuffer, dwBytesToWrite, &dwBytesWritten, NULL );
 }
}

However, I'd rather not use the non-extended function. I've deleted the file to ensure no other process has a lock on it, but it still has an issue when its created the file. I should note that this error does not happen under builder 6, only Rad Studio 2010.

Thank you for the help.


Solution

  • Try using LARGE_INTEGER instead of PLARGE_INTEGER. Normally PLARGE_INTEGER is a pointer, not a value.