Skip to content

GabrielMajeri/comptr-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

comptr-rs

Smart pointer for Component Object Model interfaces.

This crate is designed to be used together with winapi-rs. You can use the COM interfaces declared by it or use RIDL! macro from that same crate to declare your COM interfaces.

Non-null guarantee

The ComPtr type is built around the invariant that the pointer it manages will never be null. Since memory in COM is only freed when the last reference is released, and ComPtr only releases its reference when the destructor is run, the invariant is maintained.

Example

The following (not actually real) example shows how you would use the library:

// Import the crate.
extern crate comptr;
use comptr::ComPtr;

// This function is exported from some DLL.
extern "system" {
    fn CreateInterface(*mut *mut IUnknown);
}

let interface = ComPtr::new({
    let mut ptr = ptr::null_mut();

    unsafe {
        CreateInterface(&mut ptr);
    }

    // The pointer must be non-null or undefined behaviour could occur.
    if ptr == std::ptr::null_mut() {
        panic!("Failed to create COM interface.");
    }

    ptr
});

// Now you can use the interface, with 100% guarantee it is not null.
unsafe {
    interface.CallFunction();
}

Contributing

Issues (i.e. feature requests and bug reports) and pull-requests are welcome!

You can also help by writing code that uses this crate. The more users it has, the better tested in real-life scenarios it will be.

License

This crate is dual licensed under either of MIT / Apache 2.0, at your option.

Releases

No releases published

Packages

No packages published

Languages