Add current_Dir

This commit is contained in:
2025-04-01 15:46:28 +08:00
parent efa8aa6e96
commit c62a8c10b7
3 changed files with 25 additions and 6 deletions

View File

@@ -162,4 +162,9 @@ impl Config {
pub fn hook_dll(&self) -> Vec<String> { pub fn hook_dll(&self) -> Vec<String> {
self.get_str_vec("hook_dll").unwrap_or(vec![]) self.get_str_vec("hook_dll").unwrap_or(vec![])
} }
#[cfg(windows)]
pub fn current_dir(&self) -> Option<String> {
self.get_str("current_dir").map(|s| s.to_owned())
}
} }

View File

@@ -119,12 +119,12 @@ impl Main {
#[cfg(windows)] #[cfg(windows)]
fn call(cml: Vec<String>) -> Result<ExitStatus, windows::PopenError> { fn call(cml: Vec<String>) -> Result<ExitStatus, windows::PopenError> {
let t = Vec::<String>::new(); let t = Vec::<String>::new();
windows::call(&cml, &t).map(|c| ExitStatus::Exited(c)) windows::call(&cml, &t, None::<String>).map(|c| ExitStatus::Exited(c))
} }
#[cfg(windows)] #[cfg(windows)]
fn call2(cml: Vec<String>, dlls: Vec<String>) -> Result<ExitStatus, windows::PopenError> { fn call2(cml: Vec<String>, dlls: Vec<String>, cdir: Option<String>) -> Result<ExitStatus, windows::PopenError> {
windows::call(&cml, &dlls).map(|c| ExitStatus::Exited(c)) windows::call(&cml, &dlls, cdir).map(|c| ExitStatus::Exited(c))
} }
fn restore(&self) -> Result<(), Error> { fn restore(&self) -> Result<(), Error> {
@@ -223,7 +223,7 @@ impl Main {
#[cfg(not(windows))] #[cfg(not(windows))]
let e = Self::call(cml)?; let e = Self::call(cml)?;
#[cfg(windows)] #[cfg(windows)]
let e = Self::call2(cml, self._cfg.hook_dll())?; let e = Self::call2(cml, self._cfg.hook_dll(), self._cfg.current_dir())?;
#[cfg(windows)] #[cfg(windows)]
if hide { if hide {
windows::show_window(); windows::show_window();

View File

@@ -50,7 +50,7 @@ pub enum PopenError {
CreateThreadFailed, CreateThreadFailed,
} }
pub fn call<S: AsRef<OsStr>, T: AsRef<OsStr>>(argv: &[S], dlls: &[T]) -> Result<u32, PopenError> { pub fn call<S: AsRef<OsStr>, T: AsRef<OsStr>, C: AsRef<OsStr>>(argv: &[S], dlls: &[T], cdir: Option<C>) -> Result<u32, PopenError> {
let job = unsafe { CreateJobObjectA(null_mut(), null()) }; let job = unsafe { CreateJobObjectA(null_mut(), null()) };
if job.is_null() { if job.is_null() {
println!("Failed to create job: {}.", unsafe { GetLastError() }); println!("Failed to create job: {}.", unsafe { GetLastError() });
@@ -105,6 +105,20 @@ pub fn call<S: AsRef<OsStr>, T: AsRef<OsStr>>(argv: &[S], dlls: &[T]) -> Result<
} }
let mut cmlw: Vec<_> = cml.encode_wide().collect(); let mut cmlw: Vec<_> = cml.encode_wide().collect();
cmlw.resize(cmlw.len() + 1000, 0); cmlw.resize(cmlw.len() + 1000, 0);
let mut cdir = match cdir.as_ref() {
Some(c) => {
let mut c: Vec<_> = c.as_ref().encode_wide().collect();
c.push(0);
Some(c)
},
None => None,
};
let cdir = match cdir.as_mut() {
Some(c) => {
c.as_mut_ptr()
}
None => null_mut(),
};
let re = unsafe { let re = unsafe {
CreateProcessW( CreateProcessW(
null(), null(),
@@ -114,7 +128,7 @@ pub fn call<S: AsRef<OsStr>, T: AsRef<OsStr>>(argv: &[S], dlls: &[T]) -> Result<
1, 1,
CREATE_SUSPENDED, CREATE_SUSPENDED,
null_mut(), null_mut(),
null_mut(), cdir,
addr_of_mut!(si), addr_of_mut!(si),
addr_of_mut!(pi), addr_of_mut!(pi),
) != 0 ) != 0