繁体   English   中英

GetFileSizeEx损坏文件句柄

[英]GetFileSizeEx corrupts file handle

目前,我正在使用GetFileSizeEx来跟踪日志文件写入前的大小。 我们的空间有限,如果尝试创建一个大于100 MB的文件,我们将停止记录数据。 问题是由于某种原因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 studio的调试器中。

现在,我通过使用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 );
 }
}

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

感谢您的帮助。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM