如何正确使用CopyFileEx和CopyProgressRoutine函数?

时间:2010-02-26 03:31:11

标签: c windows mfc

我制作了一个复制文件的应用程序,通常是大文件。我想展示复制的进度,但我不能把它带到工作中。有人能告诉我我做错了什么吗? 现在它非常有趣。 %回到49%,回到0,然后回到40,然后又回到0再回到35,这继续,直到它成功复制文件,但我不明白为什么得到这样的奇怪的感知。这可能是因为我计算百分比变量就像我以后做的那样?如果我想以这种方式计算百分比:percent = TotalBytesTransferred * 100 / TotalFileSize;然后我得到一个编译器错误:“错误C2676:二进制'*':'LARGE_INTEGER'没有定义此运算符或转换为预定义运算符可接受的类型”这就是我计算的原因。

提前致谢!

kampi

代码:

UINT CopyThread( LPVOID pParam )
{
 ....
 ....
 ....
 ret = CopyFileExA( Address.from, Address.to, &MyCopyProgressRoutine, ptr, FALSE,FALSE);
 ....
 ....
}  

DWORD CALLBACK MyCopyProgressRoutine(
LARGE_INTEGER TotalFileSize, // file size
LARGE_INTEGER TotalBytesTransferred, // bytes transferred
LARGE_INTEGER StreamSize, // bytes in stream
LARGE_INTEGER StreamBytesTransferred, // bytes transferred for stream
DWORD dwStreamNumber, // current stream
DWORD dwCallbackReason, // callback reason
HANDLE hSourceFile, // handle to source file
HANDLE hDestinationFile, // handle to destination file
LPVOID lpData // from CopyFileEx
)
{
 TCHAR currentprog[20];

 CGetFileListDlg* ptr = (CGetFileListDlg*)lpData;
 // do something with pointer, e.g. ptr->m_Progressbar.Pos blahblah 

 //ptr->m_fileprog.Clear();
//ptr->UpdateWindow();
 DWORD percent = (TotalBytesTransferred.HighPart*2^31 + TotalBytesTransferred.LowPart) * 100/(TotalFileSize.HighPart*2^31 + TotalBytesTransferred.LowPart);

wsprintf( currentprog, _T("%d %%"),percent );
ptr->m_fileprog.SetWindowText( currentprog );

return  PROGRESS_CONTINUE;
}

4 个答案:

答案 0 :(得分:2)

This post可能会帮助你。你可以做64位算术。

答案 1 :(得分:0)

__int64 percent = (TotalBytesTransferred.HighPart * 2147483648 + TotalBytesTransferred.LowPart) * 100 / (TotalFileSize.HighPart * 2147483648 + TotalFileSize.LowPart);
你有逻辑错误! TotalBytesTransferred更改为TotalFileSize

答案 2 :(得分:0)

我使用:

int percent = floor ((100.0 * (float)TotalBytesTransferred.QuadPart / (float)TotalFileSize.QuadPart));

答案 3 :(得分:0)

百分比计算可以使用

uint64_t  percent= ((uint64_t)TotalBytesTransferred.HighPart<<32 + TotalBytesTransferred.LowPart) * 100/((uint64_t)TotalFileSize.HighPart<<32 + TotalFileSize.LowPart);

(防止对2 ^ 32的值进行硬编码)