Show HN: Superblocks AI – AI coding assistant for internal apps
I don&#x27;t write Rust but does this look (more) correct?<p><pre><code>    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;</code></pre>
use std::ffi::OsString;
    use std::os::windows::ffi::OsStringExt;<p><pre><code>     &#x2F;&#x2F; Helper function to convert a wide string to a Rust string
     wide_to_string(wide: &amp;[u16]) -&gt; String {
    OsString::from_wide(&amp;wide)
        .to_string_lossy()
        .into_owned()</code></pre>
}<p><pre><code>    &#x2F;&#x2F; Helper function to convert BSTR to Rust String
    fn bstr_to_string(bstr: BSTR) -&gt; String {
    unsafe {
        let length = winapi::um::oleauto::SysStringLen(bstr) as usize;
        let slice = std::slice::from_raw_parts(bstr, length);
        wide_to_string(slice)
    }</code></pre>
}<p><pre><code>   fn main() {
    unsafe {
        &#x2F;&#x2F; 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;

            &#x2F;&#x2F; Create IShellWindows instance
            let hr = CoCreateInstance(
                &amp;shell_windows_clsid,
                null_mut(),
                winapi::um::combaseapi::CLSCTX_ALL,
                &amp;shell_windows_iid,
                &amp;mut shell_windows_ptr as *mut _ as _,
            );

            if SUCCEEDED(hr) {
                let shell_windows_dispatch: &amp;IDispatch = &amp;*(shell_windows_ptr as *mut IDispatch);

                let hwnd = GetForegroundWindow() as LPCWSTR;

                &#x2F;&#x2F; Get Item of IShellWindows
                let mut result = null_mut();
                let mut params = winapi::um::oaidl::DISPPARAMS {
                    cArgs: 1,
                    cNamedArgs: 0,
                    rgvarg: &amp;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,
                    &amp;winapi::shared::guiddef::IID_NULL,
                    winapi::um::winnls::LOCALE_USER_DEFAULT,
                    winapi::um::oaidl::DISPATCH_METHOD,
                    &amp;mut params,
                    &amp;mut result,
                    null_mut(),
                    null_mut(),
                );

                &#x2F;&#x2F; Convert result to IWebBrowser2
                let web_browser2: &amp;IWebBrowser2 = &amp;*(result.pdispVal as *mut IWebBrowser2);

                &#x2F;&#x2F; Get LocationURL property of IWebBrowser2
                let mut location_url: BSTR = null_mut();
                web_browser2.get_LocationURL(&amp;mut location_url);

                let path = bstr_to_string(location_url);

                println!(&quot;Path: {}&quot;, path);
            } else {
                println!(&quot;Failed to create IShellWindows instance.&quot;);
            }
        } else {
            println!(&quot;Failed to initialize COM.&quot;);
        }
    }
    extern crate winapi;
</code></pre>
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;<p>&#x2F;&#x2F; Helper function to convert a wide string to a Rust string
fn wide_to_string(wide: &amp;[u16]) -&gt; String {
    OsString::from_wide(&amp;wide)
        .to_string_lossy()
        .into_owned()
}<p>&#x2F;&#x2F; Helper function to convert BSTR to Rust String
fn bstr_to_string(bstr: BSTR) -&gt; String {
    unsafe {
        let length = winapi::um::oleauto::SysStringLen(bstr) as usize;
        let slice = std::slice::from_raw_parts(bstr, length);
        wide_to_string(slice)
    }
}<p>fn main() {
    unsafe {
        &#x2F;&#x2F; Initialize COM
        let hr = CoInitialize(null_mut());<p><pre><code>        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;

            &#x2F;&#x2F; Create IShellWindows instance
            let hr = CoCreateInstance(
                &amp;shell_windows_clsid,
                null_mut(),
                winapi::um::combaseapi::CLSCTX_ALL,
                &amp;shell_windows_iid,
                &amp;mut shell_windows_ptr as *mut _ as _,
            );

            if SUCCEEDED(hr) {
                let shell_windows_dispatch: &amp;IDispatch = &amp;*(shell_windows_ptr as *mut IDispatch);

                let hwnd = GetForegroundWindow() as LPCWSTR;

                &#x2F;&#x2F; Get Item of IShellWindows
                let mut result = null_mut();
                let mut params = winapi::um::oaidl::DISPPARAMS {
                    cArgs: 1,
                    cNamedArgs: 0,
                    rgvarg: &amp;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,
                    &amp;winapi::shared::guiddef::IID_NULL,
                    winapi::um::winnls::LOCALE_USER_DEFAULT,
                    winapi::um::oaidl::DISPATCH_METHOD,
                    &amp;mut params,
                    &amp;mut result,
                    null_mut(),
                    null_mut(),
                );

                &#x2F;&#x2F; Convert result to IWebBrowser2
                let web_browser2: &amp;IWebBrowser2 = &amp;*(result.pdispVal as *mut IWebBrowser2);

                &#x2F;&#x2F; Get LocationURL property of IWebBrowser2
                let mut location_url: BSTR = null_mut();
                web_browser2.get_LocationURL(&amp;mut location_url);

                let path = bstr_to_string(location_url);

                println!(&quot;Path: {}&quot;, path);
            } else {
                println!(&quot;Failed to create IShellWindows instance.&quot;);
            }
        } else {
            println!(&quot;Failed to initialize COM.&quot;);
        }
    }
   }
 }</code></pre>