How can I mark my structure as `com_class` in Rust?

Piotr Kosek 0 Reputation points
2024-11-02T03:50:13.6333333+00:00

Hello,

I am implementing a COM object in DLL in Rust. Every single tutorial I found (including ChatGPT and Github Copilot) describes that I need to use #[com_class] macro with my CLSID over my structure.

#[com_class("80e3c337-61c5-4966-90e6-fc498c3aaf9b")]
struct MyProvider { }

This does not compile due to error: cannot find attribute com_class in this scope.

I searched through windows-rs repo and used Feature search on official docs, but I could not found why com_class is missing. I assume there was a breaking change in API, but I cannot find any info on that whatsoever.

So how can I mark my struct as com class?

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,649 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Piotr Kosek 0 Reputation points
    2024-11-02T04:23:27.4366667+00:00

    Okay, after even more extensive search:

    com_class is part of intercom crate, not windows-rs. The latter is much younger, and probably that is the reason Google and AI is recommending using com_class. But it can be expand roughly to:

    #[no_mangle]
    pub extern "stdcall" fn DllGetClassObject(
        clsid: *const GUID,
        iid: *const GUID,
        ppv: *mut *mut core::ffi::c_void,
    ) -> HRESULT {
        if unsafe { *clsid } == GUID::from_values(0x80e3c337, 0x61c5, 0x4966, [0x90, 0xe6, 0xfc, 0x49, 0x8c, 0x3a, 0xaf, 0x9b]) {
            let class = MyProvider::new();
            unsafe { *ppv = Box::into_raw(Box::new(class)) as *mut _ };
            HRESULT(0) // S_OK
        } else {
            CLASS_E_CLASSNOTAVAILABLE
        }
    }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.