I don't write Rust but does this look (more) correct?
extern crate winapi;
use std::ptr::null_mut;
use winapi::shared::guiddef::{CLSID, IID};
use winapi::um::combaseapi::{CoInitialize, CoCreateInstance};
use winapi::um::shobjidl_core::CLSID_ShellWindows;
use winapi::um::winuser::GetForegroundWindow;
use winapi::Interface;
use winapi::shared::winerror::{HRESULT, SUCCEEDED};
use winapi::um::exdisp::IWebBrowser2;
use winapi::um::unknwnbase::IUnknown;
use winapi::um::oleidl::IDispatch;
use winapi::shared::wtypes::BSTR;
use winapi::um::winnt::LPCWSTR;
use winapi::shared::ntdef::VOID;
use std::ffi::OsString;
use std::os::windows::ffi::OsStringExt;
// Helper function to convert a wide string to a Rust string
wide_to_string(wide: &[u16]) -> String {
OsString::from_wide(&wide)
.to_string_lossy()
.into_owned()
}
// Helper function to convert BSTR to Rust String
fn bstr_to_string(bstr: BSTR) -> String {
unsafe {
let length = winapi::um::oleauto::SysStringLen(bstr) as usize;
let slice = std::slice::from_raw_parts(bstr, length);
wide_to_string(slice)
}
}
fn main() {
unsafe {
// Initialize COM
let hr = CoInitialize(null_mut());
if SUCCEEDED(hr) {
let mut shell_windows_ptr: *mut IUnknown = null_mut();
let shell_windows_iid: IID = IShellWindows::uuidof();
let shell_windows_clsid: CLSID = CLSID_ShellWindows;
// Create IShellWindows instance
let hr = CoCreateInstance(
&shell_windows_clsid,
null_mut(),
winapi::um::combaseapi::CLSCTX_ALL,
&shell_windows_iid,
&mut shell_windows_ptr as *mut _ as _,
);
if SUCCEEDED(hr) {
let shell_windows_dispatch: &IDispatch = &*(shell_windows_ptr as *mut IDispatch);
let hwnd = GetForegroundWindow() as LPCWSTR;
// Get Item of IShellWindows
let mut result = null_mut();
let mut params = winapi::um::oaidl::DISPPARAMS {
cArgs: 1,
cNamedArgs: 0,
rgvarg: &mut winapi::um::oaidl::VARIANTARG {
n1: winapi::um::oaidl::VARIANTARG_0 {
n2: winapi::um::oaidl::VARIANTARG_0_0 {
vt: winapi::um::oaidl::VT_I4 as u16,
wReserved1: 0,
wReserved2: 0,
wReserved3: 0,
n3: winapi::um::oaidl::VARIANTARG_0_0_0 {
lVal: hwnd as _,
}
}
}
},
rgdispidNamedArgs: null_mut(),
};
shell_windows_dispatch.Invoke(
winapi::um::exdisp::DISPID_ISHELLWINDOWS_ITEM,
&winapi::shared::guiddef::IID_NULL,
winapi::um::winnls::LOCALE_USER_DEFAULT,
winapi::um::oaidl::DISPATCH_METHOD,
&mut params,
&mut result,
null_mut(),
null_mut(),
);
// Convert result to IWebBrowser2
let web_browser2: &IWebBrowser2 = &*(result.pdispVal as *mut IWebBrowser2);
// Get LocationURL property of IWebBrowser2
let mut location_url: BSTR = null_mut();
web_browser2.get_LocationURL(&mut location_url);
let path = bstr_to_string(location_url);
println!("Path: {}", path);
} else {
println!("Failed to create IShellWindows instance.");
}
} else {
println!("Failed to initialize COM.");
}
}
extern crate winapi;
use std::ptr::null_mut;
use winapi::shared::guiddef::{CLSID, IID};
use winapi::um::combaseapi::{CoInitialize, CoCreateInstance};
use winapi::um::shobjidl_core::CLSID_ShellWindows;
use winapi::um::winuser::GetForegroundWindow;
use winapi::Interface;
use winapi::shared::winerror::{HRESULT, SUCCEEDED};
use winapi::um::exdisp::IWebBrowser2;
use winapi::um::unknwnbase::IUnknown;
use winapi::um::oleidl::IDispatch;
use winapi::shared::wtypes::BSTR;
use winapi::um::winnt::LPCWSTR;
use winapi::shared::ntdef::VOID;
use std::ffi::OsString;
use std::os::windows::ffi::OsStringExt;
// Helper function to convert a wide string to a Rust string
fn wide_to_string(wide: &[u16]) -> String {
OsString::from_wide(&wide)
.to_string_lossy()
.into_owned()
}
// Helper function to convert BSTR to Rust String
fn bstr_to_string(bstr: BSTR) -> String {
unsafe {
let length = winapi::um::oleauto::SysStringLen(bstr) as usize;
let slice = std::slice::from_raw_parts(bstr, length);
wide_to_string(slice)
}
}
fn main() {
unsafe {
// Initialize COM
let hr = CoInitialize(null_mut());
if SUCCEEDED(hr) {
let mut shell_windows_ptr: *mut IUnknown = null_mut();
let shell_windows_iid: IID = IShellWindows::uuidof();
let shell_windows_clsid: CLSID = CLSID_ShellWindows;
// Create IShellWindows instance
let hr = CoCreateInstance(
&shell_windows_clsid,
null_mut(),
winapi::um::combaseapi::CLSCTX_ALL,
&shell_windows_iid,
&mut shell_windows_ptr as *mut _ as _,
);
if SUCCEEDED(hr) {
let shell_windows_dispatch: &IDispatch = &*(shell_windows_ptr as *mut IDispatch);
let hwnd = GetForegroundWindow() as LPCWSTR;
// Get Item of IShellWindows
let mut result = null_mut();
let mut params = winapi::um::oaidl::DISPPARAMS {
cArgs: 1,
cNamedArgs: 0,
rgvarg: &mut winapi::um::oaidl::VARIANTARG {
n1: winapi::um::oaidl::VARIANTARG_0 {
n2: winapi::um::oaidl::VARIANTARG_0_0 {
vt: winapi::um::oaidl::VT_I4 as u16,
wReserved1: 0,
wReserved2: 0,
wReserved3: 0,
n3: winapi::um::oaidl::VARIANTARG_0_0_0 {
lVal: hwnd as _,
}
}
}
},
rgdispidNamedArgs: null_mut(),
};
shell_windows_dispatch.Invoke(
winapi::um::exdisp::DISPID_ISHELLWINDOWS_ITEM,
&winapi::shared::guiddef::IID_NULL,
winapi::um::winnls::LOCALE_USER_DEFAULT,
winapi::um::oaidl::DISPATCH_METHOD,
&mut params,
&mut result,
null_mut(),
null_mut(),
);
// Convert result to IWebBrowser2
let web_browser2: &IWebBrowser2 = &*(result.pdispVal as *mut IWebBrowser2);
// Get LocationURL property of IWebBrowser2
let mut location_url: BSTR = null_mut();
web_browser2.get_LocationURL(&mut location_url);
let path = bstr_to_string(location_url);
println!("Path: {}", path);
} else {
println!("Failed to create IShellWindows instance.");
}
} else {
println!("Failed to initialize COM.");
}
}
}
}