How to get win32 COM objects e.g., to find hardware details


#1

I’d like to find out some hardware details on Windows. This is straightforward in Python:

import win32com.client
obj = win32com.client.GetObject('winmgmts:Win32_Processor')
# ... get the details ...

But searching for getobject in the winapi crate doesn’t produce any results, so I don’t know if it is supported.
There’s also a com_rs crate, but it hasn’t been updated in more than a year and doesn’t have any examples.

Does anyone have some example code that would get me started?


#2

In Rust working with COM is pretty similar to working with COM in C++. First you’d need to have the COM interface definitions. If winapi doesn’t have the COM interfaces you’ll be using then you’ll have to bind to the interface yourself using the RIDL! macro from winapi. Then you have to figure out what function to call to instantiate an instance of that interface. Typically you’d use CoCreateInstance to instantiate a COM object, but some APIs like DirectX have a special function to instantiate a factory object used to instantiate further objects.

Once you have your object you’d probably want to use some sort of ComPtr smart pointer to automatically handle AddRef/Release stuff. wio provides this for example: https://github.com/retep998/wio-rs/blob/master/src/com.rs

You probably are interested in the WMI COM api, so you can find more documentation on it at https://msdn.microsoft.com/en-us/library/aa389276

As it turns out there is already a PR for winapi that adds bindings to the WMI stuff so it won’t be too long before it’ll be merged and you can work off a fork for now if you want. https://github.com/retep998/winapi-rs/pull/603


#3

Thanks for that. I’ll wait for the wmi bindings to arrive in winapi.