正确使用CopyFileEx和CopyProgressRouti

正确使用CopyFileEx和CopyProgressRouti

我做了一个复制文件的应用程序,通常是大文件。我想展示复印的进度,但我不能让它发挥作用。有人能告诉我我做错了什么吗?
现在它非常有趣。%变为49%,再变回0,再变回40,再变回0,再变回35,然后继续,直到它成功复制了文件,但我不明白为什么我会有这么奇怪的感觉。这可能是因为我像后来那样计算百分比变量?如果我想这样计算百分比:percent=totalByTestTransferred*100/TotalFileSize;那么我会得到一个编译器错误:“error C2676:binary'*':'LARGE_INTEGER'没有定义这个运算符,或者转换成预定义运算符可以接受的类型”,这就是我这样计算的原因。
提前谢谢!
坎皮
代码:

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;
}

最佳答案

__int64 percent = (TotalBytesTransferred.HighPart * 2147483648 + TotalBytesTransferred.LowPart) * 100 / (TotalFileSize.HighPart * 2147483648 + TotalFileSize.LowPart);

你有逻辑错误!TotalByTestTransferred更改为TotalFileSize

关于c - 如何正确使用CopyFileEx和CopyProgressRoutine函数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2339313/

10-11 16:35