2

I have a given 32-bit DLL (dds.dll) and a header file (dds.h, C++ style). I do have the source, but I don't want to change it. I'm on Windows using cygwin.

I want to access functions in the DLL through XS, not directly through Perl. I am new to XS.

[I can't figure out how to use Win32::API. The DLL functions have struct inputs and *struct inputs (the latter getting modified by the functions), and I can't figure out Win32::API::Struct. I suppose that would also be one way, but I've given up on that for now.]

I can pass lists of integers between Perl and XS, so that part works. My plan is to pass in a flat list of integers, #include "dll.h" in the XS code, turn the list into a struct in XS, pass the struct and other arguments to the DLL function, turn the resulting struct into a flat list of integers, and return the list to Perl.

I've reduced XSLoader.pm to the minimum that I need, partly to understand what goes on. It works for my own XS-generated .dll.

I've tried loading dds.dll in the same way, but I think it doesn't make sense, actually. I get past dl_require_symbols, and dl_undef_symbols() is OK too. Then dl_find_symbol looks for some kind of boot symbol and doesn't find it. I imagine that XS creates a bootstrap function which makes the XS functions visible to Perl through dl_install_xsub(), but normal DLLs don't have this.

In any event, I don't want to make dds.dll functions directly visible in Perl. I want to make dds.dll functions visible to the XS code. It sounds like a simple linking step, but I can't figure it out.

If there's a simpler way, I'm all for it too. Any help appreciated.

Xantium
  • 11,201
  • 10
  • 62
  • 89
  • I don't follow your thinking. You say you want to load and call a DLL from XS code, but then talk about `Win32::API` which is for doing that from Perl code. Similarly you are working with `XSLoader`, which is for dynamically loading code written in XS, not for ordinary system DLLs. What is it exactly that you need to achieve? – Borodin Jun 01 '14 at 11:17
  • Please show what you have written, as well as the `dds.h` header file or a link to where we can download it. – Borodin Jun 01 '14 at 11:22
  • 1
    You're trying to load a DLL in C, not Perl. This question has nothing to do with Perl at all. So why are you looking at Perl modules. Depending on how you want to load it, it's going to be something like passing `-lmydll` to the linker (`LIBS => [ '-lmydll' ]` in `Makefile.PL`) or it's going to be using whatever system call Windows provides for loading and unloading DLLs. – ikegami Jun 01 '14 at 15:25
  • normally you would have not just the dll and h files, but also a .lib import library and you could just link your XS to that. http://support.microsoft.com/kb/131313 may be helpful? – ysth Jun 01 '14 at 16:02
  • If you want to load a DLL from XS code at run time than you need to call `HMODULE handle = LoadLibrary("my.dll")` after which you can do `FARPROC function = GetProcAddress(handle, "my_function")` to get a procedure pointer – Borodin Jun 01 '14 at 18:12
  • ikegami got my intention. Sorry if I wasn't very clear. Thanks all for looking at it. – user3139990 Jun 01 '14 at 20:44

0 Answers0