最近有個項目需要用winform調用迅雷下載。上網百度後發現迅雷自帶的com組件從迅雷5之後就廢掉了,除了能添加任務,其余功能全不能用。
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
class thunder
{
//迅雷下載開放引擎
[DllImport("XLDownload.dll", EntryPoint = "XLInitDownloadEngine")]
public static extern bool XLInitDownloadEngine();
[DllImport("XLDownload.dll", EntryPoint = "XLUninitDownloadEngine")]
public static extern bool XLUninitDownloadEngine();
[DllImport("XLDownload.dll", EntryPoint = "XLURLDownloadToFile", CharSet = CharSet.Auto)]
public static extern int XLURLDownloadToFile(string pszFileName,string pszUrl, string pszRefUrl, ref Int32 lTaskId);
[DllImport("XLDownload.dll", EntryPoint = "XLQueryTaskInfo", CharSet = CharSet.Auto)]
public static extern int XLQueryTaskInfo(int lTaskId, ref int plStatus, ref UInt64 pullFileSize, ref UInt64 pullRecvSize);
[DllImport("XLDownload.dll", EntryPoint = "XLGetErrorMsg",CharSet = CharSet.Auto)]
public static extern int XLGetErrorMsg(int dwErrorId, string pszBuffer,ref int dwSize);
[DllImport("XLDownload.dll", EntryPoint = "XLContinueTask", CharSet = CharSet.Auto)]
public static extern int XLContinueTask(int lTaskId);
[DllImport("XLDownload.dll", EntryPoint = "XLContinueTaskFromTdFile", CharSet = CharSet.Auto)]
public static extern int XLContinueTaskFromTdFile(string pszTdFileFullPath, ref int lTaskId);
[DllImport("XLDownload.dll", EntryPoint = "XLPauseTask", CharSet = CharSet.Auto)]
public static extern int XLPauseTask(int lTaskId, ref int lNewTaskId);
[DllImport("XLDownload.dll", EntryPoint = "XLStopTask", CharSet = CharSet.Auto)]
public static extern int XLStopTask(int lTaskId);
public const int TaskStatus_Connect = 0; // 已經建立連接
public const int TaskStatus_Download = 2; // 開始下載
public const int TaskStatus_Pause = 10; // 暫停
public const int TaskStatus_Success = 11; // 成功下載
public const int TaskStatus_Fail = 12; // 下載失敗
public const int XL_SUCCESS = 0;
public const int XL_ERROR_FAIL = 0x10000000;
// 尚未進行初始化
public const int XL_ERROR_UNINITAILIZE = XL_ERROR_FAIL + 1;
// 不支持的協議,目前只支持HTTP
public const int XL_ERROR_UNSPORTED_PROTOCOL = XL_ERROR_FAIL + 2;
// 初始化托盤圖標失敗
public const int XL_ERROR_INIT_TASK_TRAY_ICON_FAIL = XL_ERROR_FAIL + 3;
// 添加托盤圖標失敗
public const int XL_ERROR_ADD_TASK_TRAY_ICON_FAIL = XL_ERROR_FAIL + 4;
// 指針為空
public const int XL_ERROR_POINTER_IS_NULL = XL_ERROR_FAIL + 5;
// 字符串是空串
public const int XL_ERROR_STRING_IS_EMPTY = XL_ERROR_FAIL + 6;
// 傳入的路徑沒有包含文件名
public const int XL_ERROR_PATH_DONT_INCLUDE_FILENAME = XL_ERROR_FAIL + 7;
// 創建目錄失敗
public const int XL_ERROR_CREATE_DIRECTORY_FAIL = XL_ERROR_FAIL + 8;
// 內存不足
public const int XL_ERROR_MEMORY_ISNT_ENOUGH = XL_ERROR_FAIL + 9;
// 參數不合法
public const int XL_ERROR_INVALID_ARG = XL_ERROR_FAIL + 10;
// 任務不存在
public const int XL_ERROR_TASK_DONT_EXIST = XL_ERROR_FAIL + 11;
// 文件名不合法
public const int XL_ERROR_FILE_NAME_INVALID = XL_ERROR_FAIL + 12;
// 沒有實現
public const int XL_ERROR_NOTIMPL = XL_ERROR_FAIL + 13;
// 已經創建的任務數達到最大任務數,無法繼續創建任務
public const int XL_ERROR_TASKNUM_EXCEED_MAXNUM = XL_ERROR_FAIL + 14;
// 任務類型未知
public const int XL_ERROR_INVALID_TASK_TYPE = XL_ERROR_FAIL + 15;
// 文件已經存在
public const int XL_ERROR_FILE_ALREADY_EXIST = XL_ERROR_FAIL + 16;
// 文件不存在
public const int XL_ERROR_FILE_DONT_EXIST = XL_ERROR_FAIL + 17;
// 讀取cfg文件失敗
public const int XL_ERROR_READ_CFG_FILE_FAIL = XL_ERROR_FAIL + 18;
// 寫入cfg文件失敗
public const int XL_ERROR_WRITE_CFG_FILE_FAIL = XL_ERROR_FAIL + 19;
// 無法繼續任務,可能是不支持斷點續傳,也有可能是任務已經失敗
// 通過查詢任務狀態,確定錯誤原因。
public const int XL_ERROR_CANNOT_CONTINUE_TASK = XL_ERROR_FAIL + 20;
// 無法暫停任務,可能是不支持斷點續傳,也有可能是任務已經失敗
// 通過查詢任務狀態,確定錯誤原因。
public const int XL_ERROR_CANNOT_PAUSE_TASK = XL_ERROR_FAIL + 21;
// 緩沖區太小
public const int XL_ERROR_BUFFER_TOO_SMALL = XL_ERROR_FAIL + 22;
// 調用XLInitDownloadEngine的線程,在調用XLUninitDownloadEngine之前已經結束。
// 初始化下載引擎線程,在調用XLUninitDownloadEngine之前,必須保持執行狀態。
public const int XL_ERROR_INIT_THREAD_EXIT_TOO_EARLY = XL_ERROR_FAIL + 23;
// TP崩潰
public const int XL_ERROR_TP_CRASH = XL_ERROR_FAIL + 24;
// 任務不合法,調用XLContinueTaskFromTdFile繼續任務。內部任務切換失敗時,會產生這個錯誤。
public const int XL_ERROR_TASK_INVALID = XL_ERROR_FAIL + 25;
}
}