Home > front end >  Performing action while process is running in rust with GetExitCodeProcess in rust
Performing action while process is running in rust with GetExitCodeProcess in rust

Time:11-27

Hello my goal is to do something in a loop while a process is running. The following code assumes that I already have a valid Handle to the process.

my first attempt was:

let mut exit:u32 = 0;
while GetExitCodeProcess(h_process, exit as *mut u32).as_bool(){
}

thought this is might work since the GitHub doc for this function says that the second argument is lpexitcode: *mut u32, however this code leads to STATUS_ACCESS_VIOLATION error.

now I did get it to work since I remembered the solution for a kind of similar problem I had. the working code:

 let mut exit: [u32; 1] = [0; 1];
 while GetExitCodeProcess(h_process, exit.as_mut_ptr().cast()).as_bool() && exit[0] == 259 {}

My Problem is now that I don't really understand why the first attempt did not work and the second did. Can anyone explain this to me and is there any better way to store exit in an array ? Also I saw in the win doc, that the c function would set exit to STILL_ACTIVE if the process is still running. why is it not the same in the rust function because STILL_ACTIVE can be found in the rust Crate as well.

CodePudding user response:

First off, there is a lot of code missing in your example. GetExitCodeProcess is not a native Rust function, it definitely requires some library and an unsafe around it.

That said, as @IInspectable mentions, you probably need to pass a reference to your exit value instead of the exit value itself, as GetExitCodeProcess requires a pointer to a value:

let mut exit:u32 = 0;
while GetExitCodeProcess(h_process, &mut exit as *mut u32).as_bool(){
}
  • Related