RustAudio / vst3-sys

Raw Bindings to the VST3 API
Other
288 stars 18 forks source link

Question: Finding and returning interface vptr #39

Closed lokanchung closed 3 years ago

lokanchung commented 3 years ago

Hi,

I'm new to Rust and was migrating some of my C++ code into rust using this crate. I was implementing IEditController::createView and I had to return self as pointer. In C++ the language took care of choosing correct vptr for the return type,

In Rust, only way I found is to find the member added by proc_macro and returning it. *self.__iplugview as *mut c_void (something like this, iirc Rust compiler required some additional casting) Is there a clean way to find right vptr from struct and return it?

m-hilgendorf commented 3 years ago

You can call the IUnknown methods on the struct if it implements another COM interface to get its vtable pointer:

unsafe fn create_view(&self, _:FIDString) -> *mut c_void {
  let mut ptr: *mut c_void = std::ptr::null_mut();
  let guid = /* whatever the GUID is you're after, I don't know the cleanest way to grab it at the moment */
  self.query_interface(&guid as *const _, &mut ptr as *mut *mut c_void);
  ptr
}
lokanchung commented 3 years ago

I hoped to be able to do it statically but it seems like it's not possible at the moment. (recent version of com-rs seem to have a way) This is not common case, so I think I can get away with query_interface() or ComPtr::get_interface(). Thank you.

m-hilgendorf commented 3 years ago

Thanks for using the repo! I looked at converting to a more recent com-rs but the macro interface has changed drastically since this project first started, and it would be a big overhaul.