diff --git a/Build/wallitor-core.dll b/Build/wallitor-core.dll index ac9fe12..064e9bb 100644 Binary files a/Build/wallitor-core.dll and b/Build/wallitor-core.dll differ diff --git a/Build/wallitor-core.exp b/Build/wallitor-core.exp index ef36f08..057c7b1 100644 Binary files a/Build/wallitor-core.exp and b/Build/wallitor-core.exp differ diff --git a/Build/wallitor-core.lib b/Build/wallitor-core.lib index ab7d441..3e69586 100644 Binary files a/Build/wallitor-core.lib and b/Build/wallitor-core.lib differ diff --git a/dllmain.cpp b/dllmain.cpp index 45b0740..8f2ce33 100644 --- a/dllmain.cpp +++ b/dllmain.cpp @@ -20,9 +20,9 @@ BOOL APIENTRY DllMain( HMODULE hModule, playerInstance* player_instance = NULL; - extern "C" __declspec(dllexport) void init(const char* ffpath,const char* videoPath, BOOL mute) { + extern "C" __declspec(dllexport) BOOL init(const char* ffpath,const char* videoPath, BOOL mute) { player_instance = new playerInstance(conFig(ffpath, videoPath, mute)); - player_instance->generate(); + return player_instance->generate(); } extern "C" __declspec(dllexport) void destroy() { diff --git a/playerInstance.cpp b/playerInstance.cpp index 37b52e6..80f1e50 100644 --- a/playerInstance.cpp +++ b/playerInstance.cpp @@ -1,5 +1,7 @@ #include "pch.h" #include "playerInstance.h" +#include +#include playerInstance::playerInstance(const conFig& config) { this->config = config; @@ -16,6 +18,33 @@ static BOOL CALLBACK EnumWindowsProc(_In_ HWND hwnd, _In_ LPARAM Lparam) { return TRUE; } +HWND findWindowTimeOut(const wchar_t* name, int timeoutMillis) { + auto start = std::chrono::steady_clock::now(); + HWND hwnd = nullptr; + + while (true) { + // 使用 FindWindowW 查找窗口 + hwnd = FindWindowW(name, 0); + + // 如果找到窗口,则返回 + if (hwnd != nullptr) { + return hwnd; + } + + // 计算当前经过的时间 + auto now = std::chrono::steady_clock::now(); + int elapsedMillis = std::chrono::duration_cast(now - start).count(); + + // 如果超时,则返回 nullptr + if (elapsedMillis > timeoutMillis) { + return nullptr; + } + + // 等待一段时间(例如100毫秒)再尝试查找 + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + } +} + BOOL playerInstance::showWindow(LPCWSTR lpParameter) { if (this->hFfplay != NULL) { DWORD dwPID = 0; @@ -29,10 +58,14 @@ BOOL playerInstance::showWindow(LPCWSTR lpParameter) { si.wShowWindow = SW_HIDE; PROCESS_INFORMATION pi{ 0 }; if (CreateProcess(this->config.ffpath.c_str(), (LPWSTR)lpParameter, 0, 0, 0, CREATE_NO_WINDOW, 0, 0, &si, &pi)) { - Sleep(600);//等待视频播放器启动完成 - HWND hProgman = FindWindow(L"Progman", 0);// 找到PI窗口 + //Sleep(600);//等待视频播放器启动完成 + //HWND hProgman = FindWindow(L"Progman", 0);// 找到PI窗口 + HWND hProgman = findWindowTimeOut(L"Progman",1000); + if (hProgman == nullptr) return FALSE; SendMessageTimeout(hProgman, 0x052c, 0, 0, 0, 100, 0);// 给它发特殊消息 - this->hFfplay = FindWindowW(L"SDL_app", 0);// 找到视频窗口 + //this->hFfplay = FindWindowW(L"SDL_app", 0);// 找到视频窗口 + this->hFfplay = findWindowTimeOut(L"SDL_app", 1000); + if (this->hFfplay == nullptr) return FALSE; SetParent(hFfplay, hProgman);// 将视频窗口设苦为PM的子窗口 int systemWidth = GetSystemMetrics(0); int systemHeight = GetSystemMetrics(1); @@ -49,6 +82,8 @@ BOOL playerInstance::showWindow(LPCWSTR lpParameter) { MoveWindow(hFfplay, 0, x, cRct.right - cRct.left, cRct.bottom - cRct.top, 0); }*/ EnumWindows(EnumWindowsProc, 0);// 找到第二个workerw窗口并隐藏它 + CloseHandle(pi.hProcess); + CloseHandle(pi.hThread); return TRUE; } CloseHandle(pi.hProcess); @@ -56,7 +91,7 @@ BOOL playerInstance::showWindow(LPCWSTR lpParameter) { return FALSE; } -void playerInstance::generate() { +BOOL playerInstance::generate() { std::wstring fcmd = L" \""; fcmd += this->config.videoPath + L"\""; fcmd += L" -noborder -loop 0"; @@ -64,7 +99,7 @@ void playerInstance::generate() { fcmd += L" -an"; } fcmd += L" -fs"; - this->showWindow(fcmd.c_str()); + return this->showWindow(fcmd.c_str()); } void playerInstance::exit() { diff --git a/playerInstance.h b/playerInstance.h index f3c9434..2809f97 100644 --- a/playerInstance.h +++ b/playerInstance.h @@ -9,7 +9,7 @@ private: public: playerInstance(const conFig& config); BOOL showWindow(LPCWSTR lpParameter); - void generate(); + BOOL generate(); void exit(); };