rodrigocfd / winsafe

Windows API and GUI in safe, idiomatic Rust.
https://crates.io/crates/winsafe
MIT License
523 stars 30 forks source link

System Related APIs #139

Closed mjzk closed 1 month ago

mjzk commented 1 month ago

Recently, I contributed to an opensource project, with some Windows APIs like windows::Win32::System::ProcessStatus::GetProcessMemoryInfo for reading process's memory stats. But these exposed APIs of windows crate are unsafe, the committer of that project do not like unsafe, and suggest me to upstream the change to your repo.

I have a look the winsafe sources, it seems we are just linking to the dll directly rather than depending on windows crate. And I do not see much APIs around the system side things.

So, some quick questions:

  1. do we have equvilant API of windows::Win32::System::ProcessStatus::GetProcessMemoryInfo?
  2. if not, do we have the plan to add this kinds of system APIs?
  3. if I contribute a single API of windows::Win32::System::ProcessStatus::GetProcessMemoryInfo , is it welcome?
  4. if welcome, do I need to depend on the linking to the all or can I use the windows crate?

Thanks!

rodrigocfd commented 1 month ago

do we have equvilant API of windows::Win32::System::ProcessStatus::GetProcessMemoryInfo?

Now it does, I just implemented it.

if I contribute a single API of windows::Win32::System::ProcessStatus::GetProcessMemoryInfo, is it welcome?

Sure.

However, although this API is rather simple, it belongs to PSAPI module, which WinSafe didn't have yet, but I was planning to add. Adding a new module is not exactly simple, so I thought it would be a great opportunity to do so. And so I did.

However, I have no experience with GetProcessMemoryInfo, so I need your help to test it and see if works correctly. Remember to add the psapi Cargo feature to your project.

Please report back on this.

if welcome, do I need to depend on the linking to the all or can I use the windows crate?

Not needed.

mjzk commented 1 month ago

@rodrigocfd you are so quick! Thanks! I will do some tests recently and report back.

mjzk commented 3 weeks ago

@rodrigocfd This works. for PROCESS_MEMORY_COUNTERS_EX, I only get WorkingSetSize.

Will it be greater if we support virtualSize via VirtualQueryEx? like

...
while (VirtualQueryEx(hProcess, (LPCVOID)address, &mbi, sizeof(mbi))) {
        virtualSize += mbi.RegionSize;
        address += mbi.RegionSize;
}
rodrigocfd commented 3 weeks ago

@mjzk Please open another issue, so it's easy for me to keep track of it.

mjzk commented 3 weeks ago

@rodrigocfd thanks for feedback. I do a digging on my provided codes. I think there is something wrong with my understanding of Windows process memory usage. The current API is enough. VirtualQueryEx is not for reading memory consumption. Thanks again.