Huzzah! I finally had time to publish my first crate. grob - gro(wable) b(uffer).
Everyone who has spent enough time with the Windows API (@jbe) eventually has to work with a function that requires a growable buffer. GetAdaptersAddresses
is a great example. When called the adapteraddresses
and sizepointer
parameters define a buffer where GetAdaptersAddresses
will store data. If the buffer is too small sizepointer
is set to the buffer size needed and an error is returned. The caller then has to allocate a larger buffer and try again. With this crate, calling a function like GetAdaptersAddresses
is as simple as this...
use windows::Win32::NetworkManagement::IpHelper::GetAdaptersAddresses;
use windows::Win32::NetworkManagement::IpHelper::GET_ADAPTERS_ADDRESSES_FLAGS;
use windows::Win32::Networking::WinSock::AF_UNSPEC;
use grob::{winapi_large_binary, RvIsError};
fn main() -> Result<(), Box<dyn std::error::Error>> {
winapi_large_binary(
|argument| {
RvIsError::new(unsafe {
GetAdaptersAddresses(
AF_UNSPEC.0 as u32,
GET_ADAPTERS_ADDRESSES_FLAGS(0),
None,
Some(argument.pointer()),
argument.size(),
)
})
},
|frozen_buffer| {
if let Some(mut p) = frozen_buffer.pointer() {
while p != std::ptr::null() {
println!("FriendlyName = {}", unsafe { (*p).FriendlyName.display() });
p = unsafe { (*p).Next };
}
}
Ok(())
},
)?;
Ok(())
}
All comments, both negative and positive, encouraged!