caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* External DLL call with mingw OCaml
@ 2009-09-20 19:34 Matthieu Dubuget
  2009-09-20 20:37 ` [Caml-list] " David Allsopp
  0 siblings, 1 reply; 2+ messages in thread
From: Matthieu Dubuget @ 2009-09-20 19:34 UTC (permalink / raw)
  To: caml-list

Hello,

recently, I had to call a third-party DLL (Windows, mingw flavour of OCaml).
It was not easy, because of symbol names decorations (I think).
I'd like to know if there is a more simple way?

Here is the way I found, after asking some questions to internet:

1/ First I had to create a .def file for the third party DLL:
pexports.exe Visa32.dll > visa.def

2/ Then I had to edit the visa.def file, in order to add @nn to symbol 
names.

3/ Import lib generation
dlltool --input-def visa.def --dllname Visa32.dll --output-lib libvisa.a -k

4/ My C-stub, that give access to the functions of Visa32.dll to OCaml
ocamlc -c test_stub.c

Is then turned into a library that depends on Visa32.dll
ocamlmklib -o test_dll -lvisa test_stub.o

5/ Test module compilation
ocamlc -c test.ml
ocamlc -a -o test.cma -dllib dlltest_dll test.cmo

6/ Load test.cma into toplevel


The main problem is that it is a trial error process in order to find 
the @nn to add into visa.def.
I deduce them from the error message I receive (I don't remember if this 
is at step 5/ or 6/) about
visa_function@nn that can't be found.

Salutations

Matthieu Dubuget


^ permalink raw reply	[flat|nested] 2+ messages in thread

* RE: [Caml-list] External DLL call with mingw OCaml
  2009-09-20 19:34 External DLL call with mingw OCaml Matthieu Dubuget
@ 2009-09-20 20:37 ` David Allsopp
  0 siblings, 0 replies; 2+ messages in thread
From: David Allsopp @ 2009-09-20 20:37 UTC (permalink / raw)
  To: 'Matthieu Dubuget', caml-list

Matthieu Dubuget wrote:
> Hello,
> 
> recently, I had to call a third-party DLL (Windows, mingw flavour of
> OCaml).
> It was not easy, because of symbol names decorations (I think).

It is indeed not easy - fortunately, it's a once-only thing (for each DLL)

> I'd like to know if there is a more simple way?

Not as far as I'm aware - no site I've ever seen gives a reasonable
explanation for why the "@nn" decorations are necessary (gcc or any other
compiler should be able to infer it from the header at link time, but
neither gcc nor the MS compilers provide a way to do this)

<snip>

> 3/ Import lib generation
> dlltool --input-def visa.def --dllname Visa32.dll --output-lib
> libvisa.a -k

The irony is that the -k (--kill-at) removes the @nn from the symbols :o)

<snip>

> The main problem is that it is a trial error process in order to find
> the @nn to add into visa.def.

This shouldn't be trial and error - you can work out the numbers from the C
definitions (which presumably you know as you're writing stubs). For the
most part for 32-bit code, it'll be 4 x the number of parameters to the
function but in more exact terms it's the number of bytes occupied by the
parameters (so sizeof the *type* of each arg) which you can deduce from the
.h file. Alternatively, you can just put them all as @0 initially and use
the error messages - it'll always give you the correct one (provided that
your headers are correct)

Hope this reassures, even if it doesn't offer an easier way!


David


PS This is the site I used originally -
http://www.emmestech.com/moron_guides/moron1.html though I'm not sure you'd
ever find a moron who wanted/needed to do this kind of thing!!


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2009-09-20 20:37 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-09-20 19:34 External DLL call with mingw OCaml Matthieu Dubuget
2009-09-20 20:37 ` [Caml-list] " David Allsopp

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).