GetFileSizeEx 损坏文件句柄

发布于 2024-09-25 13:52:34 字数 1351 浏览 14 评论 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 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以往的大感动 2024-10-02 13:52:34

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文