Recent

Author Topic: How to link .def file?  (Read 2688 times)

giahung1997

  • Full Member
  • ***
  • Posts: 113
How to link .def file?
« on: February 13, 2019, 08:24:13 am »
Assuming: We have a LibTest.dll (C library) without LibTest.h header file so we cannot use h2pas. What we have is the dll file and a big .html api reference file. The library is too big so we cannot create a header by our own. I discovered we could create a .def file either by:

+Digital Mars' IMPLIB.exe
+OpenWatcomv2 Wlib.exe
+Pelles C Polib.exe
+MinGW dlltool.exe

The hard part is told FPC to link that .def file.
If we somehow managed to link that .def file, could we use the same procedure to workaround linking C++ library?
« Last Edit: February 13, 2019, 08:30:42 am by giahung1997 »

Thaddy

  • Hero Member
  • *****
  • Posts: 14382
  • Sensorship about opinions does not belong here.
Re: How to link .def file?
« Reply #1 on: February 13, 2019, 09:14:45 am »
Well, the def file contains just the export names (fortunately in order and sometimes with an optional ordinal index) and not the parameter list.
You need the API documentation to add the parameter list. That's the only way to turn it back in a possibly working header file and/or a pas unit.
But that way it CAN be done. If it is large, it is a lot of work, though. However you can limit yourself to the functions that you actually use.
Of course you also need to know the calling convention.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

giahung1997

  • Full Member
  • ***
  • Posts: 113
Re: How to link .def file?
« Reply #2 on: February 13, 2019, 09:34:32 am »
Well, the def file contains just the export names (fortunately in order and sometimes with an optional ordinal index) and not the parameter list.
You need the API documentation to add the parameter list. That's the only way to turn it back in a possibly working header file and/or a pas unit.
But that way it CAN be done. If it is large, it is a lot of work, though. However you can limit yourself to the functions that you actually use.
Of course you also need to know the calling convention.
Oh I post using my phone so somehow the edited part missed from my post.

Using the tool above from .def file you can generate .lib file so end up we will have an import library libtest.lib

I don't have my pc now so don't know if FPC could link that .lib file or it only accept .a format.

Using that procedure and write a small header file from the reference hopefully we could solve the problem. I wonder if the same steps could help linking c++ dll?

P/s: from the .def file you will know the calling convention, mostly stdcall  ;)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11458
  • FPC developer.
Re: How to link .def file?
« Reply #3 on: February 13, 2019, 09:52:05 am »
Free Pascal doesn't need import libs. It generates the relevant info on the fly.

Header wise, import libs only save adding the dll name to every symbol (to make it look more like linux/freebsd linking).

I think you are searching in the wrong direction.

Microsoft says it:

Quote
. A .def file is most useful when building a DLL. Because there are linker options that can be used instead of module-definition statements, .def files are generally not necessary. You can also use __declspec(dllexport) as a way to specify exported functions.

It seems they also have a role in passing parameters to the linker when creating the DLL.  Delphi/FPC afaik also do that automatically because there is a special "library" module with export statements. But probably only for the more common case.
« Last Edit: February 13, 2019, 09:55:03 am by marcov »

Thaddy

  • Hero Member
  • *****
  • Posts: 14382
  • Sensorship about opinions does not belong here.
Re: How to link .def file?
« Reply #4 on: February 13, 2019, 10:15:16 am »
@Marco,
The point is he needs a pascal header file and he doesn't have a .h file.
In that case he has enough information from the def file (the exported unmangled names and the html api documentation) to reverse engineer a header file. It is a lot of work but can be done in a way I described. I did something similar a few times over the years, but never on a huge library. Parts can be automated by writing a little tool to match the names extracted from the def  with parameter lists from the html API docs. That's better than relying on just the API docs, since these may contain other stuff, like macro's, and you just need to match the exports. I would use regexpr for that if the API is really huge. But a lot of work still needs to be done by hand.
« Last Edit: February 13, 2019, 10:22:49 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11458
  • FPC developer.
Re: How to link .def file?
« Reply #5 on: February 13, 2019, 10:22:43 am »
@Marco,
The point is he needs a pascal header file and he doesn't have a .h file.

How does one call functions from C++ then ? Or use classes? This is very weird to me. Maybe he needs to reinstall and tick some "SDK" box to also install the headers.

Quote
In that case he has enough information from the def file (the exported unmangled names and the html api documentation) to reverse engineer a header file. It is a lot of work but can be done in a way I described. I did it a few times over the years, but never on a huge library.

And only if you can it contains mangled -> unmangled mappings. Both of them. And there are only basic types or reference types (which is quite unlikely, since then they would have exported plain C).

A more logical way would be to try to use the dll in C++ (I can't imagine there is no way to do that, and that everybody must resort to reverse engineering defs) and write a wrapper. He is missing something else, and navelgazing on what he can find won't put him on a productive path.

The first took me a day, installing VS inclusive (well actually more, but that was because I had to learn to flatten C++ specific types like std:string). How hard can it be?
« Last Edit: February 13, 2019, 10:33:06 am by marcov »

Thaddy

  • Hero Member
  • *****
  • Posts: 14382
  • Sensorship about opinions does not belong here.
Re: How to link .def file?
« Reply #6 on: February 13, 2019, 10:24:26 am »
He states the library is C, not C++, unless I misunderstood. He has a C++ interface on top of that.
If that is not the case he needs to do more work, that's true.

The logical way out is to simply ask for the header files.....
« Last Edit: February 13, 2019, 10:27:12 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11458
  • FPC developer.
Re: How to link .def file?
« Reply #7 on: February 13, 2019, 10:33:43 am »
It is very strange to me that a library comes with SDK documentation but not with header files so that it could be used. That just doesn't compute.

C, C++ doesn't matter at all, they all need headerfiles.

giahung1997

  • Full Member
  • ***
  • Posts: 113
Re: How to link .def file?
« Reply #8 on: February 13, 2019, 11:09:42 am »
Oh I come back to clean the mess I have made. Just find the .h and everything done. I'm so sorry.

 

TinyPortal © 2005-2018