2010-09-29 114 views
0

当前我正在使用GetFileSizeEx来跟踪在写入日志文件之前有多大的日志文件。如果我们尝试创建一个大于100兆字节的文件,我们将停止记录数据,但空间有限。问题出于某种原因,GetFileSizeEx将破坏我正在使用的文件句柄。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); 
} 
} 

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

谢谢你的帮助。

回答

2

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

+0

非常感谢制造商6“有帮助”,并认为它是一个正常值。 – Alikar 2010-09-29 15:34:48