On Wed, Aug 19, 2020 at 11:42:35AM +0100, Richard W.M. Jones wrote: > In any case if we did the above we'd still have to hack every plugin > and filter so that it links to the import library. But there's a > lower-level technique we could use instead: > > https://nachtimwald.com/2012/07/15/calling-functions-in-exe-from-plugins-in-windows/ > https://stackoverflow.com/questions/770344/visual-c-linking-plugin-dll-against-exe#comment1772764_770374 > > This is to use GetProcAddress from the plugin (DLL) to get the address > of symbols in nbdkit.exe. It works a bit like dlsym. I hacked > together a test plugin to try this: > > static void > load (void) > { > int (*_nbdkit_parse_int) (const char *what, > const char *str, > int *r) = > GetProcAddress (GetModuleHandle (NULL), "nbdkit_parse_int"); > fprintf (stderr, "nbdkit_parse_int addr = %p\n", _nbdkit_parse_int); > > void (*_nbdkit_debug) (const char *msg, ...) = > GetProcAddress (GetModuleHandle (NULL), "nbdkit_debug"); > _nbdkit_debug ("calling nbdkit_debug now ..."); > } > > and on loading this plugin into server/nbdkit.exe: > > nbdkit_parse_int addr = 000000000040BD40 > nbdkit: debug: calling nbdkit_debug now ... > > So it really does work. > > I'm not quite sure exactly how to integrate this. Perhaps adding some > Windows-only code to plugin_init which does the appropriate > GetProcAddress calls, initializing some function pointers in the > plugin?
A note to myself since I've run out of time on this today. This is subtly difficult to make work without a lot of build changes. The problem is we have libraries like common/utils/libutils.a which are used by both the server and the plugins (libutils is not the only one, there are several). Imagine that libutils calls nbdkit_debug. Now inside the server this is a direct call. But inside a plugin or filter, on Windows, this must indirect through a function pointer void (*nbdkit_debug) (char *msg, ...). The only way to make this work is to compile all these libraries twice, once for server usage and once for plugins. This is where the technique of using an import library would be more successful, because it presumably does some kind of fixup at load time. But I couldn't easily get that to work, although I may have a look at it again to see if we can get past the libtool issues. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-df lists disk usage of guests without needing to install any software inside the virtual machine. Supports Linux and Windows. http://people.redhat.com/~rjones/virt-df/ _______________________________________________ Libguestfs mailing list [email protected] https://www.redhat.com/mailman/listinfo/libguestfs
