To the people who commented:
The example I gave to you, was a simplified version of what I was trying to do. I was thinking this example is so simple, that people would recognize that it is a simplified version of something. I am sorry for assuming something like that. You know, not all C++ libraries have "classic C-style functions".
But on the other hand, you had no intentions to help me, right? Yes, obviously I can modify the C++ code in this example, but what would be the point in doing that? I came up with a problem, I needed an answer for that. I could not change the problem, because it would be a different problem then.
I solved it. It is working only for pure virtual method structs, but I only need those so, I'm fine with it.
C++:
struct numbers {
virtual int addnums(int a, int b) {
return a + b;
}
};
extern "C" numbers* get_numbers() {
numbers* num = new numbers();
return num;
}
Rust:
#![allow(non_snake_case)]
extern "system" {
fn get_numbers() -> *mut usize;
}
#[repr(C)]
pub struct numbers_vtbl {
pub addnums: unsafe extern "system" fn(This: *mut numbers, a: i32, b: i32) -> i32,
}
#[repr(C)]
pub struct numbers {
pub lpVtbl: *const numbers_vtbl,
}
impl numbers {
#[inline]
pub unsafe fn addnums(&self, a: i32, b: i32) -> i32 {
((*self.lpVtbl).addnums)(self as *const _ as *mut _, a, b)
}
}
fn main() {
unsafe {
let a = get_numbers() as *mut numbers;
let b = (*a).addnums(14, 53);
println!("{}", b);
}
}