采用aria2命令行告诉下载,示例代码如下所示:

1、配置aria2目录,网上下载即可

2、

CString strPath = L"C:\\Users\\14713\\Desktop\\Aria2Sample\\Debug\\aria2";
     SetCurrentDirectory(strPath);
     std::wstring strCmd = L"aria2c  https://dldir1.qq.com/qqfile/qq/TIM3.4.8/TIM3.4.8.22138.exe -x 16 -s 16 -c";    std::string strResult = runCmd(strCmd);

3、命令行运行方法:

std::string runCmd(std::wstring& strCmd)
 {
     //安全属性
     SECURITY_ATTRIBUTES sa;
     HANDLE hRead, hWrite;
     std::string strResult;
     sa.nLength = sizeof(SECURITY_ATTRIBUTES);
     sa.lpSecurityDescriptor = NULL;
     sa.bInheritHandle = TRUE;    strCmd = L"cmd /c " + strCmd;//加上"cmd /c"是为了能执行类似dir的命令,执行字符串指定的命令然后终断
    //创建匿名管道,导向DOS输入输出通道
     if (!CreatePipe(&hRead, &hWrite, &sa, 0))
     {
         return NULL;
     }
     STARTUPINFO si;
     PROCESS_INFORMATION pi;
     ZeroMemory(&si, sizeof(si));
     ZeroMemory(&pi, sizeof(pi));
     si.cb = sizeof(STARTUPINFO);
     GetStartupInfo(&si);
     si.hStdError = hWrite;//数据输出用的文件句柄
     si.hStdOutput = hWrite;//新创建进程的标准输出连在写管道一端
     si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;    //执行cmd命令,并在命名中管道中写入cmd命令返回的串
     if (!CreateProcess(NULL, const_cast <wchar_t*>(strCmd.c_str()), NULL, NULL, TRUE, NULL, NULL, NULL, &si, &pi))
     {
         return NULL;
     }    CloseHandle(hWrite);//关闭管道句柄
    char buffer[4096] = { 0 };
     DWORD bytesRead;    while (ReadFile(hRead, buffer, 4096, &bytesRead, NULL))//从匿名管道中读取数据
     {
         strResult += buffer;        memset(&buffer, 0, 4096);
         Sleep(200);//防止乱码
     }
     CloseHandle(hRead);
     return strResult;
 }