0

目前,我正在使用 GetFileSizeEx 来跟踪日志文件在写入之前的大小。我们的空间有限,如果我们尝试创建大于 100 兆字节的文件,我们将停止记录数据。问题是由于某种原因 GetFileSizeEx 会损坏我正在使用的文件句柄。

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 将从正常值 (00000EB8) 变为 ???? 在 Rad 工作室的调试器中。

现在我通过使用 GetFileSize 函数解决了这个问题:

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 );
 }
}

但是,我宁愿不使用非扩展功能。我已删除该文件以确保没有其他进程对其进行锁定,但在创建文件时仍然存在问题。我应该注意到这个错误不会在 builder 6 下发生,只有 Rad Studio 2010。

感谢您的帮助。

4

1 回答 1

2

尝试使用 LARGE_INTEGER 而不是 PLARGE_INTEGER。通常 PLARGE_INTEGER 是一个指针,而不是一个值。

于 2010-09-29T15:19:08.713 回答