Giter Site home page Giter Site logo

comptr-rs's Introduction

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.

comptr-rs's People

Contributors

gabrielmajeri avatar

Watchers

 avatar  avatar  avatar

comptr-rs's Issues

Should work on Linux

According to Microsoft, COM is a cross-platform API. However, Windows is the only OS that embraces the COM API.

Still, it would be great to remove the dependency on winapi-rs / make it optional, and make this crate compile on Linux.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.