【问题标题】:GetFileSizeEx corrupts file handleGetFileSizeEx 损坏文件句柄
【发布时间】:2010-09-29 15:15:00
【问题描述】:

目前我正在使用 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。

感谢您的帮助。

【问题讨论】:

    标签: c++ c++builder-2010 c++builder-6


    【解决方案1】:

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

    【讨论】:

    • 感谢 builder 6 很“有帮助”,并认为这是一个正常值。
    猜你喜欢
    • 1970-01-01
    • 2016-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    相关资源
    最近更新 更多