#include "stdafx.h" #include "ttui/application.h" #include "ui/downloader_form.h" int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); UNREFERENCED_PARAMETER(hInstance); UNREFERENCED_PARAMETER(nCmdShow); const std::wstring command=GetCommandLineW(); StartupOptions startup; if (!ParseStartupCommandLine(command,startup)) { MessageBoxW(nullptr,L"启动参数无效。\n用法:ttdownloader.exe --add \"https://example.com/file.zip\"\n也可直接传入 HTTP/FTP/磁力下载地址,或使用 --add 打开空白添加窗口。",L"TTDownloader",MB_ICONERROR); return 1; } HANDLE instance_mutex = ::CreateMutexW(nullptr, TRUE, L"Local\\TTDownloader.SingleInstance"); if (!instance_mutex) { MessageBoxW(nullptr,L"无法访问客户端实例,请使用相同用户和权限启动。",L"TTDownloader",MB_ICONERROR); return 1; } if (::GetLastError() == ERROR_ALREADY_EXISTS) { HWND hwnd=nullptr; const auto deadline=GetTickCount64()+5000; do { hwnd=FindWindowW(DownloaderForm::kClassName.c_str(),nullptr); if (hwnd) break; Sleep(50); } while (GetTickCount64()((command.size()+1)*sizeof(wchar_t)),const_cast(command.c_str())}; DWORD_PTR result=0; delivered=SendMessageTimeoutW(hwnd,WM_COPYDATA,0,reinterpret_cast(&data),SMTO_ABORTIFHUNG|SMTO_BLOCK,3000,&result) && result; } } ::CloseHandle(instance_mutex); if (!delivered) MessageBoxW(nullptr,L"正在运行的客户端未能接收启动参数,请完全退出旧版后重试。",L"TTDownloader",MB_ICONERROR); return delivered?0:1; } DownloaderForm form; int result=ttui::RunApplication([&] { if (!form.Create(nullptr,DownloaderForm::kClassName.c_str(),L"TTDownloader",940,640,true,true)) { MessageBoxW(nullptr,L"无法创建主窗口。",L"TTDownloader",MB_ICONERROR); return false; } form.CenterWindow(); form.ShowWindow(); form.OpenStartupRequest(startup); return true; }); ::CloseHandle(instance_mutex); return result; }