C++ 回调函数,拷贝文件

#include <iostream>
#include <windows.h>
using namespace std;

struct ConText1
{
    int id;
    string name;
};
//转换longlong无符号整数类型
unsigned long long translate(LARGE_INTEGER num) 
{
    unsigned long long reslut = num.HighPart;
    reslut << 32;
    reslut += num.LowPart;
    return reslut;
}

DWORD CALLBACK CopyProgress(
    LARGE_INTEGER TotalFileSize,            // 文件总大小
    LARGE_INTEGER TotalBytesTransferred,    // 已经拷贝的文件大小
    LARGE_INTEGER StreamSize,                // total number of bytes for this stream
    LARGE_INTEGER StreamBytesTransferred,    // total number of bytes transferred for this stream
    DWORD dwStreamNumber,                    // the current stream
    DWORD dwCallbackReason,                    // reason for callback
    HANDLE hSourceFile,                        // handle to the source file
    HANDLE hDestinationFile,                // handle to the destination file
    LPVOID lpData                            // 上下文,用于传递需要额外定义的传递的参数
    )
{
    //创建上下文
    ConText1* context = (ConText1*)lpData;
    //设置百分比
    unsigned long long total = translate(TotalFileSize);
    unsigned long long copied = translate(TotalBytesTransferred);
    printf("[%d][%s]进度:%lld,%lld \n", context->id,context->name.c_str(),copied,total);


    return PROGRESS_CONTINUE;
}

int main()
{
    //定义一个结构体并传给宝贝文件回调的上下文函数
    ConText1 context;
    context.id = 1;
    context.name = "helloworld";
    //调用拷贝文件回调函数
    BOOL ss = CopyFileEx("C:\\soft\\tools\\CometAssistant.exe", "C:\\CometAssistant.exe", &CopyProgress, &context, NULL, NULL);

    cout << (ss ? "拷贝成功\n" : "拷贝失败\n");


    return 0;
}

 

原文链接: https://www.cnblogs.com/shenji/p/12402898.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    C++ 回调函数,拷贝文件

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/333125

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月1日 下午8:59
下一篇 2023年3月1日 下午8:59

相关推荐