How to use c++ virtual function on rust

c++ code

namespace Reaper 
{
	class IReaperFactoryCallback {
	public:
		virtual ~IReaperFactoryCallback() {};
		virtual bool register_reaper(int action, void *handler) = 0;
		virtual void unregister_reaper(int action) = 0;
	};
}

i want to give IReaperFactoryCallback *cb to rust, but i don't know how to get it and call register_reaper function

on c++

bool action_register_callback(IReaperFactoryCallback *cb)
{
	return cb->register_reaper(2, (void *)monitor);
}

typedef bool (*action_register_func_t)(IReaperFactoryCallback *cb);
action_register_func_t pf = (action_register_func_t)dlsym(handle, "action_register_callback");
pf(cb);

It's not a POD structure, you should try first to think how you can make this callable from C(make it a POD structure if you really want to pass a structure, although you probably want only a function).
There is almost no programming language that can communicate with C++ ABI(except D? - limited) because C++ ABI is not stable. One compiler might have this structure in one form while another might have it in another form.

1 Like

thanks, i'm still new to this.