refact: win, virtual display (#7767)

* refact: win, virtual display

Signed-off-by: fufesou <shuanglongchen@yeah.net>

* Update flutter-build.yml

---------

Signed-off-by: fufesou <shuanglongchen@yeah.net>
Co-authored-by: RustDesk <71636191+rustdesk@users.noreply.github.com>
This commit is contained in:
fufesou
2024-04-19 11:31:52 +08:00
committed by GitHub
parent a3c0911529
commit e83c28bf54
61 changed files with 1113 additions and 290 deletions

View File

@@ -669,4 +669,48 @@ extern "C"
AllocConsole();
freopen("CONOUT$", "w", stdout);
}
bool is_service_running_w(LPCWSTR serviceName)
{
SC_HANDLE hSCManager = OpenSCManagerW(NULL, NULL, SC_MANAGER_CONNECT);
if (hSCManager == NULL) {
return false;
}
SC_HANDLE hService = OpenServiceW(hSCManager, serviceName, SERVICE_QUERY_STATUS);
if (hService == NULL) {
CloseServiceHandle(hSCManager);
return false;
}
SERVICE_STATUS_PROCESS serviceStatus;
DWORD bytesNeeded;
if (!QueryServiceStatusEx(hService, SC_STATUS_PROCESS_INFO, reinterpret_cast<LPBYTE>(&serviceStatus), sizeof(serviceStatus), &bytesNeeded)) {
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
return false;
}
bool isRunning = (serviceStatus.dwCurrentState == SERVICE_RUNNING);
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
return isRunning;
}
} // end of extern "C"
extern "C"
{
int get_native_machine()
{
USHORT processMachine = 0;
USHORT nativeMachine = 0;
BOOL res = IsWow64Process2(GetCurrentProcess(), &processMachine, &nativeMachine);
if (res == TRUE) {
return (int)nativeMachine;
} else {
return -1;
}
}
}

View File

@@ -64,8 +64,6 @@ use windows_service::{
use winreg::enums::*;
use winreg::RegKey;
pub const DRIVER_CERT_FILE: &str = "RustDeskIddDriver.cer";
pub fn get_cursor_pos() -> Option<(i32, i32)> {
unsafe {
#[allow(invalid_value)]
@@ -462,6 +460,8 @@ extern "C" {
fn is_win_down() -> BOOL;
fn is_local_system() -> BOOL;
fn alloc_console_and_redirect();
fn get_native_machine() -> i32;
fn is_service_running_w(svc_name: *const u16) -> bool;
}
extern "system" {
@@ -1296,12 +1296,14 @@ fn get_uninstall(kill_self: bool) -> String {
{before_uninstall}
{uninstall_cert_cmd}
reg delete {subkey} /f
{uninstall_amyuni_idd}
if exist \"{path}\" rd /s /q \"{path}\"
if exist \"{start_menu}\" rd /s /q \"{start_menu}\"
if exist \"%PUBLIC%\\Desktop\\{app_name}.lnk\" del /f /q \"%PUBLIC%\\Desktop\\{app_name}.lnk\"
if exist \"%PROGRAMDATA%\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\{app_name} Tray.lnk\" del /f /q \"%PROGRAMDATA%\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\{app_name} Tray.lnk\"
",
before_uninstall=get_before_uninstall(kill_self),
uninstall_amyuni_idd=get_uninstall_amyuni_idd(&path),
app_name = crate::get_app_name(),
)
}
@@ -2367,3 +2369,81 @@ impl Drop for WallPaperRemover {
allow_err!(Self::set_wallpaper(Some(self.old_path.clone())));
}
}
// See winnt.h for more information.
#[derive(Clone, Copy)]
pub enum MachineArch {
Unknown = 0,
I386 = 0x014c,
ARM = 0x01c0,
AMD64 = 0x8664,
ARM64 = 0xAA64,
}
pub fn get_machine_arch() -> Result<MachineArch, io::Error> {
let native_machine = unsafe { get_native_machine() };
if native_machine != -1 {
let native_machine = native_machine as u16;
let check_types = [
MachineArch::I386,
MachineArch::AMD64,
MachineArch::ARM,
MachineArch::ARM64,
];
for check_type in check_types.iter() {
if *check_type as u16 == native_machine {
return Ok(*check_type);
}
}
Ok(MachineArch::Unknown)
} else {
Err(io::Error::last_os_error())
}
}
pub fn get_amyuni_exe_name() -> Option<String> {
match get_machine_arch() {
Ok(arch) => {
let exe = match arch {
MachineArch::I386 => "deviceinstaller.exe",
MachineArch::AMD64 => "deviceinstaller64.exe",
_ => {
log::error!("Unsupported machine architecture");
return None;
}
};
Some(exe.to_string())
}
Err(e) => {
log::warn!("Failed to get machine architecture: {}", e);
None
}
}
}
fn get_uninstall_amyuni_idd(path: &str) -> String {
let Some(exe) = get_amyuni_exe_name() else {
return "".to_string();
};
let work_dir = PathBuf::from(path).join("usbmmidd_v2");
if work_dir.join(&exe).exists() {
format!(
"pushd {} && .\\{exe} remove usbmmidd && popd",
work_dir.to_string_lossy()
)
} else {
"".to_string()
}
}
#[inline]
pub fn is_self_service_running() -> bool {
is_service_running(&crate::get_app_name())
}
pub fn is_service_running(service_name: &str) -> bool {
unsafe {
let service_name = wide_string(service_name);
is_service_running_w(service_name.as_ptr() as _)
}
}