GetFileSizeEx 损坏文件句柄
目前,我正在使用 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 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 );
}
}
但是,我不想使用非扩展函数。我已删除该文件以确保没有其他进程对其进行锁定,但在创建该文件时仍然存在问题。我应该注意,这个错误不会发生在 builder 6 下,只会发生在 Rad Studio 2010 下。
感谢您的帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用 LARGE_INTEGER 而不是 PLARGE_INTEGER。通常 PLARGE_INTEGER 是一个指针,而不是一个值。
Try using LARGE_INTEGER instead of PLARGE_INTEGER. Normally PLARGE_INTEGER is a pointer, not a value.