Skip to content

armgit / WacOS

Public Imported from GitHub
0

A new style desktop shell for Windows 11: menu bar, Dock, Finder, Spotlight, Control Center and Liquid Glass, in native C++.

Rust
2.2 KB
Raw Blame
//! Sonda do WPD (Windows Portable Devices) para o Finder do WacOS.
//! cargo run -p wacos-comum --example wpdprobe
use comum::windows::core::{PCWSTR, PWSTR};
use comum::windows::Win32::Devices::PortableDevices::{IPortableDeviceManager, PortableDeviceManager};
use comum::windows::Win32::System::Com::{
    CoCreateInstance, CoInitializeEx, CoTaskMemFree, CLSCTX_INPROC_SERVER, COINIT_APARTMENTTHREADED,
};

fn wide(s: &str) -> Vec<u16> {
    let mut v: Vec<u16> = s.encode_utf16().collect();
    v.push(0);
    v
}
unsafe fn ler(p: PWSTR) -> String {
    if p.is_null() {
        return String::new();
    }
    let mut n = 0usize;
    while *p.0.add(n) != 0 {
        n += 1;
    }
    String::from_utf16_lossy(std::slice::from_raw_parts(p.0, n))
}

fn main() {
    unsafe {
        let hr = CoInitializeEx(None, COINIT_APARTMENTTHREADED);
        println!("CoInitializeEx -> {:?}", hr);
        let mgr: IPortableDeviceManager = match
            CoCreateInstance(&PortableDeviceManager, None, CLSCTX_INPROC_SERVER)
        {
            Ok(m) => m,
            Err(e) => {
                println!("manager falhou: {:?}", e);
                return;
            }
        };
        println!("IPortableDeviceManager ok");
        let mut n = 0u32;
        let hr = mgr.GetDevices(std::ptr::null_mut(), &mut n);
        println!("GetDevices(null) -> {:?} n={}", hr, n);
        if n == 0 {
            println!("nenhum dispositivo WPD conectado");
            return;
        }
        let mut ids: Vec<PWSTR> = vec![PWSTR::null(); n as usize];
        let mut got = 0u32;
        let hr = mgr.GetDevices(ids.as_mut_ptr(), &mut got);
        println!("GetDevices(buf) -> {:?} got={}", hr, got);
        for id in ids.iter().take(got as usize) {
            let pnp = ler(*id);
            let w = wide(&pnp);
            let mut len = 0u32;
            let _ = mgr.GetDeviceFriendlyName(PCWSTR(w.as_ptr()), PWSTR::null(), &mut len);
            let mut buf = vec![0u16; (len + 1) as usize];
            let hr = mgr.GetDeviceFriendlyName(PCWSTR(w.as_ptr()), PWSTR(buf.as_mut_ptr()), &mut len);
            let nome = ler(PWSTR(buf.as_mut_ptr()));
            println!("  device={} name={:?} hr={:?}", pnp, nome, hr);
            CoTaskMemFree(Some(id.0 as *const _));
        }
    }
}