Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
Only initialize com once per process
Browse files Browse the repository at this point in the history
  • Loading branch information
caesay committed Dec 26, 2023
1 parent 89027e1 commit 73e6b5e
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/Rust/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ fn main() -> Result<()> {

shared::dialogs::set_silent(silent);
logging::setup_logging(logfile, true, verbose, nocolor)?;
let _comguard = w::CoInitializeEx(co::COINIT::APARTMENTTHREADED | co::COINIT::DISABLE_OLE1DDE)?;

info!("Starting Clowd.Squirrel Setup ({})", env!("NGBV_VERSION"));
info!(" Location: {:?}", std::env::current_exe()?);
Expand Down
3 changes: 3 additions & 0 deletions src/Rust/src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ fn main() -> Result<()> {
#[cfg(target_os = "macos")]
let matches = root_command().get_matches();

#[cfg(target_os = "windows")]
let _comguard = winsafe::CoInitializeEx(winsafe::co::COINIT::APARTMENTTHREADED | winsafe::co::COINIT::DISABLE_OLE1DDE)?;

let default_log_file = {
let mut my_dir = env::current_exe().unwrap();
my_dir.pop();
Expand Down
18 changes: 4 additions & 14 deletions src/Rust/src/windows/shortcuts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,7 @@ use glob::glob;
use std::path::Path;
use winsafe::{self as w, co, prelude::*};

pub fn resolve_lnk(link_path: &str) -> Result<(String, String)> {
let _comguard = w::CoInitializeEx(co::COINIT::APARTMENTTHREADED | co::COINIT::DISABLE_OLE1DDE)?;
_resolve_lnk(link_path)
}

pub fn create_lnk(output: &str, target: &str, work_dir: &str) -> w::HrResult<()> {
let _comguard = w::CoInitializeEx(co::COINIT::APARTMENTTHREADED | co::COINIT::DISABLE_OLE1DDE)?;
_create_lnk(output, target, work_dir)
}

fn _create_lnk(output: &str, target: &str, work_dir: &str) -> w::HrResult<()> {
let me = w::CoCreateInstance::<w::IShellLink>(&co::CLSID::ShellLink, None, co::CLSCTX::INPROC_SERVER)?;
me.SetPath(target)?;
me.SetWorkingDirectory(work_dir)?;
Expand All @@ -23,7 +13,7 @@ fn _create_lnk(output: &str, target: &str, work_dir: &str) -> w::HrResult<()> {
Ok(())
}

fn _resolve_lnk(link_path: &str) -> Result<(String, String)> {
pub fn resolve_lnk(link_path: &str) -> Result<(String, String)> {
let me = w::CoCreateInstance::<w::IShellLink>(&co::CLSID::ShellLink, None, co::CLSCTX::INPROC_SERVER)?;
let pf = me.QueryInterface::<w::IPersistFile>()?;
pf.Load(link_path, co::STGM::READ)?;
Expand All @@ -38,7 +28,6 @@ fn _resolve_lnk(link_path: &str) -> Result<(String, String)> {
}

pub fn remove_all_shortcuts_for_root_dir<P: AsRef<Path>>(root_dir: P) -> Result<()> {
let _comguard = w::CoInitializeEx(co::COINIT::APARTMENTTHREADED)?;
let root_dir = root_dir.as_ref();
info!("Searching for shortcuts containing root: '{}'", root_dir.to_string_lossy());

Expand All @@ -59,7 +48,7 @@ pub fn remove_all_shortcuts_for_root_dir<P: AsRef<Path>>(root_dir: P) -> Result<
for path in paths {
if let Ok(path) = path {
trace!("Checking shortcut: '{}'", path.to_string_lossy());
let res = _resolve_lnk(&path.to_string_lossy());
let res = resolve_lnk(&path.to_string_lossy());
if let Ok((target, work_dir)) = res {
let target_match = super::is_sub_path(&target, root_dir).unwrap_or(false);
let work_dir_match = super::is_sub_path(&work_dir, root_dir).unwrap_or(false);
Expand All @@ -85,14 +74,15 @@ pub fn remove_all_shortcuts_for_root_dir<P: AsRef<Path>>(root_dir: P) -> Result<
#[test]
#[ignore]
fn test_can_resolve_existing_shortcut() {
let _comguard = w::CoInitializeEx(co::COINIT::APARTMENTTHREADED | co::COINIT::DISABLE_OLE1DDE).unwrap();
let link_path = r"C:\Users\Caelan\Desktop\Discord.lnk";
let (target, _workdir) = resolve_lnk(link_path).unwrap();
assert_eq!(target, "C:\\Users\\Caelan\\AppData\\Local\\Discord\\Update.exe");
}

#[test]
#[ignore]
fn shortcut_full_integration_test() {
let _comguard = w::CoInitializeEx(co::COINIT::APARTMENTTHREADED | co::COINIT::DISABLE_OLE1DDE).unwrap();
let desktop = w::SHGetKnownFolderPath(&co::KNOWNFOLDERID::Desktop, co::KF::DONT_UNEXPAND, None).unwrap();
let link_location = Path::new(&desktop).join("testclowd123hi.lnk");
let target = r"C:\Users\Caelan\AppData\Local\NonExistingAppHello123\current\HelloWorld.exe";
Expand Down
2 changes: 1 addition & 1 deletion src/Rust/src/windows/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,4 +378,4 @@ fn verify_authenticode_against_powershell(path: &str) -> bool {
let my_result = check_authenticode_signature(path).unwrap_or(false);
assert!(ps_result == my_result);
return my_result;
}
}

0 comments on commit 73e6b5e

Please sign in to comment.