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

 

posted @ 2020-03-03 16:13  神迹丶  阅读(387)  评论(1编辑  收藏  举报
网站已运行: