#include "wl_base_help_plugin.h" // This must be included before many other Windows headers. #include #include #include // For getPlatformVersion; remove unless needed for your plugin implementation. #include #include #include #include #include #include typedef LONG NTSTATUS, * PNTSTATUS; #define STATUS_SUCCESS (0x00000000) typedef NTSTATUS(WINAPI* RtlGetVersionPtr)(PRTL_OSVERSIONINFOW); RTL_OSVERSIONINFOW GetRealOSVersion() { HMODULE hMod = ::GetModuleHandleW(L"ntdll.dll"); if (hMod) { RtlGetVersionPtr fnRtlGetVersion = (RtlGetVersionPtr)::GetProcAddress(hMod, "RtlGetVersion"); if (fnRtlGetVersion != nullptr) { RTL_OSVERSIONINFOW rovi = { 0 }; rovi.dwOSVersionInfoSize = sizeof(rovi); if (STATUS_SUCCESS == fnRtlGetVersion(&rovi)) { return rovi; } } } RTL_OSVERSIONINFOW rovi = { 0 }; return rovi; } bool IsWindows11OrGreater() { RTL_OSVERSIONINFOW rovi = GetRealOSVersion(); return (rovi.dwMajorVersion > 10) || (rovi.dwMajorVersion == 10 && rovi.dwBuildNumber >= 22000); } void ShowConsole() { HWND consoleWindow = GetConsoleWindow(); if (consoleWindow != NULL) { ShowWindow(consoleWindow, SW_SHOW); } } void HideConsole() { HWND consoleWindow = GetConsoleWindow(); if (consoleWindow != NULL) { ShowWindow(consoleWindow, SW_HIDE); } } bool IsProcessRunning(const std::wstring& processName) { bool isRunning = false; HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (hSnapshot != INVALID_HANDLE_VALUE) { PROCESSENTRY32 pe32; pe32.dwSize = sizeof(PROCESSENTRY32); if (Process32First(hSnapshot, &pe32)) { do { if (processName == pe32.szExeFile) { isRunning = true; break; } } while (Process32Next(hSnapshot, &pe32)); } CloseHandle(hSnapshot); } return isRunning; } void KillProcess(const std::wstring& processName) { HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (hSnapshot != INVALID_HANDLE_VALUE) { PROCESSENTRY32 pe32; pe32.dwSize = sizeof(PROCESSENTRY32); if (Process32First(hSnapshot, &pe32)) { do { if (processName == pe32.szExeFile) { HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pe32.th32ProcessID); if (hProcess != NULL) { TerminateProcess(hProcess, 0); CloseHandle(hProcess); break; } } } while (Process32Next(hSnapshot, &pe32)); } CloseHandle(hSnapshot); } } namespace wl_base_help { // static void WlBaseHelpPlugin::RegisterWithRegistrar( flutter::PluginRegistrarWindows *registrar) { auto channel = std::make_unique>( registrar->messenger(), "wl_base_help", &flutter::StandardMethodCodec::GetInstance()); auto plugin = std::make_unique(registrar); channel->SetMethodCallHandler( [plugin_pointer = plugin.get()](const auto &call, auto result) { plugin_pointer->HandleMethodCall(call, std::move(result)); }); registrar->AddPlugin(std::move(plugin)); } WlBaseHelpPlugin::WlBaseHelpPlugin(flutter::PluginRegistrarWindows* registrar) : registrar(registrar) {} WlBaseHelpPlugin::~WlBaseHelpPlugin() {} void WlBaseHelpPlugin::HandleMethodCall( const flutter::MethodCall& method_call, std::unique_ptr> result) { if (method_call.method_name().compare("isProcessRunning") == 0) { const auto* arguments = std::get_if(method_call.arguments()); auto processName = std::get(arguments->find(flutter::EncodableValue("processName"))->second); bool isRunning = IsProcessRunning(std::wstring(processName.begin(), processName.end())); result->Success(flutter::EncodableValue(isRunning)); } else if (method_call.method_name().compare("killProcess") == 0) { const auto* arguments = std::get_if(method_call.arguments()); auto processName = std::get(arguments->find(flutter::EncodableValue("processName"))->second); KillProcess(std::wstring(processName.begin(), processName.end())); result->Success(); } else if (method_call.method_name().compare("showConsole") == 0) { ShowConsole(); result->Success(); } else if (method_call.method_name().compare("hideConsole") == 0) { HideConsole(); result->Success(); }else if (method_call.method_name().compare("runAsAdministrator") == 0) { HWND hwnd = GetMainWindow(); // 调用您的提权逻辑,传递 hwnd TryRunAsAdmin(hwnd, result); //result->Success(flutter::EncodableValue(true)); } else if (method_call.method_name().compare("isRunningAsAdmin") == 0) { bool isAdmin = IsRunningAsAdmin(); result->Success(flutter::EncodableValue(isAdmin)); } else if (method_call.method_name().compare("getPlatformVersion") == 0) { std::ostringstream version_stream; version_stream << "Windows "; if (IsWindows11OrGreater()) { version_stream << "11+"; }else if (IsWindows10OrGreater()) { version_stream << "10+"; } else if (IsWindows8OrGreater()) { version_stream << "8"; } else if (IsWindows7OrGreater()) { version_stream << "7"; } result->Success(flutter::EncodableValue(version_stream.str())); } else { result->NotImplemented(); } } HWND WlBaseHelpPlugin::GetMainWindow() { if (registrar) { windowHwnd = registrar->GetView()->GetNativeWindow(); } return ::GetAncestor(windowHwnd, GA_ROOT); } bool WlBaseHelpPlugin::IsRunningAsAdmin() { BOOL isAdmin = FALSE; PSID adminGroup = NULL; SID_IDENTIFIER_AUTHORITY ntAuthority = SECURITY_NT_AUTHORITY; if (AllocateAndInitializeSid(&ntAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &adminGroup)) { if (!CheckTokenMembership(NULL, adminGroup, &isAdmin)) { isAdmin = FALSE; } FreeSid(adminGroup); } return isAdmin != FALSE; } void WlBaseHelpPlugin::TryRunAsAdmin(HWND hwnd, std::unique_ptr>& result){ wchar_t szPath[MAX_PATH]; if (GetModuleFileName(NULL, szPath, ARRAYSIZE(szPath))) { std::wstring params = L"--runas-admin"; SHELLEXECUTEINFO sei = { sizeof(sei) }; sei.lpVerb = L"runas"; sei.lpFile = szPath; sei.lpParameters = params.c_str(); sei.hwnd = hwnd; sei.nShow = SW_NORMAL; if (!ShellExecuteEx(&sei)) { DWORD dwError = GetLastError(); if (dwError == ERROR_CANCELLED) { // 用户拒绝了 UAC 提示 result->Error("UAC_CANCELLED", "User cancelled the UAC prompt."); } else { // 发生其他错误 std::ostringstream errMsg; errMsg << "Failed to execute with error code: " << dwError; result->Error("EXECUTION_FAILED", errMsg.str()); } } else { result->Success(); ExitProcess(0); // 退出当前进程 } } else { result->Error("GET_MODULE_FAILED", "Failed to get module file name."); } } } // namespace wl_base_help