Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: windows process name in 0.29.6 are broken #1027

Merged

Conversation

poliorcetics
Copy link
Contributor

  • fix: zero init image name memory to avoid leftoverdata interfering
  • fix: ImageName.Length is in bytes, we want the size in wide-chars

Use the following:

use std::process::Command;

use sysinfo::{Pid, PidExt, ProcessExt, System, SystemExt};

fn main() {
    let mut ping_process = Command::new(r"C:\Windows\System32\PiNg.eXE")
        .args(["-n", "50", "127.0.0.1"])
        .stdout(std::process::Stdio::null())
        .spawn()
        .unwrap();
    let ping_id = ping_process.id();

    // For some reason, on my VM, retrieving the process too quickly will lead to
    // "process.name()" being empty. To avoid this, retry until the process has a name.
    loop {
        let mut system = System::default();
        system.refresh_process_specifics(Pid::from_u32(ping_id), Default::default());
        let proc = system.process(Pid::from_u32(ping_id)).unwrap();
        if !proc.name().is_empty() {
            println!("ping.exe: {}", proc.name());
            break;
        }
    }

    let _ = ping_process.kill();
}

Then run it:

Without the fix

C:\Users\alexis>pg2.exe
ping.exe: \Device\HarddiskVolume4\Windows\System32\PING.EXE   恀Ȁ        Ā †   衬疼䒏 뻐Ȏ ŐȎ     衭疽䒄က奰Ȏ 夐Ȏ 嚠

With the fix

C:\Users\alexis>pg2.exe
ping.exe: \Device\HarddiskVolume4\Windows\System32\PING.EXE

@GuillaumeGomez
Copy link
Owner

Outch, thanks a lot!

@poliorcetics
Copy link
Contributor Author

For tracking, it originated in #1017

@poliorcetics
Copy link
Contributor Author

uh, there is another bug:

C:\Users\alexis>pg2-0.29.5.exe
ping.exe: PiNg.eXE

I'll fix

@poliorcetics
Copy link
Contributor Author

Done:

C:\Users\alexis>pg2-fixed-2.exe
ping.exe: PING.EXE

@GuillaumeGomez
Copy link
Owner

Thanks!

@GuillaumeGomez GuillaumeGomez merged commit a9a505e into GuillaumeGomez:master Jul 26, 2023
65 of 68 checks passed
@poliorcetics poliorcetics deleted the fix-windows-process-name branch July 26, 2023 19:41
GuillaumeGomez pushed a commit that referenced this pull request Jul 26, 2023
* fix: zero init image name memory to avoid leftoverdata interfering
* fix: ImageName.Length is in bytes, we want the size in wide-chars
* fix: use filename instead of full imagename for process name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants