Author Topic: Create .lib file from .dll  (Read 12357 times)

0 Members and 1 Guest are viewing this topic.

Offline Patrice Terrier

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2401
  • Gender: Male
    • www.zapsolution.com
Create .lib file from .dll
« on: February 09, 2013, 03:59:48 PM »
Create .lib file from .dll

In my attempt to convert my PB code to C, i found this TIP that is able to create a .LIB file from a .DLL assuming that you have installed first a copy of Visual Studio. (based on Adrian Henke's blog)
 
There is a MS KB article showing how to generate a .lib file from a .dll, however the required steps are not described detailed enough, so here is a quick guide:
 
Open the Visual Studio Command Prompt, you find its shortcut in Start->Programs->Microsoft Visual Studio->Tools. Now run the dumpbin command to get a list of all exported functions of your dll:
 
dumpbin /exports C:\yourpath\yourlib.dll >yourlib.def
 
This will create the yourlib.def text file, with something like this:

Quote
Microsoft (R) COFF/PE Dumper Version 10.00.40219.01
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file C:\travail\gdimage\gdimage.dll

File Type: DLL

  Section contains the following exports for GDImage.dll

    00000000 characteristics
      314328 time date stamp Sat Feb 07 09:47:36 1970
        0.00 version
           1 ordinal base
         398 number of functions
         398 number of names

    ordinal hint RVA      name

          1    0 0002FDF3 CLOCKPROC
          2    1 0000D758 DLLMAIN
          3    2 00038421 DrawGLSphere
          4    3 00039750 GL_AlphaBlend
          5    4 00041260 GL_BillBoard
          6    5 000408D9 GL_BindTexture
          7    6 0003EE66 GL_ChartAxis
          8    7 0003F0CF GL_ChartAxisPanel
          9    8 0003DC6A GL_ChartBar
         10    9 0003DC0C GL_ChartCone
         11    A 0003DBB1 GL_ChartCylinder
         12    B 000409C4 GL_ChartGetFontName
         13    C 00040FF6 GL_ChartGetMousing
         14    D 00040F1C GL_ChartGetTextLength
         15    E 00040E48 GL_ChartLabel
...etc

Now edit the.DEF file with a text editor, and keep just the name of the exported sub/function, then add the word EXPORTS on the first line, like this :

Quote
EXPORTS
CLOCKPROC
DLLMAIN
DrawGLSphere
GL_AlphaBlend
GL_BillBoard
GL_BindTexture
GL_ChartAxis
GL_ChartAxisPanel
GL_ChartBar
GL_ChartCone
GL_ChartCylinder
GL_ChartGetFontName
GL_ChartGetMousing
GL_ChartGetTextLength
GL_ChartLabel
...etc

Save the edited text to file.

Now from that definition file, we can finally create the .lib file. Using the “lib” tool from the Visual Studio Command Prompt:
 
lib /def:C:\mypath\mylib.def /OUT:C:\mypath\mylib.lib
 
Because of the missing /MACHINE option, X86, will be used by default, but that is just fine with PB/DLL code.

Of course you will have also to provide the appropriate header file to use with it.

Thanks to Adrian Henke.

For those who may need it, i have attached the GDImage.lib file to this post (requires also: gdimage.dll and gdimage.h).
« Last Edit: February 09, 2013, 04:26:28 PM by Patrice Terrier »
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Offline Patrice Terrier

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2401
  • Gender: Male
    • www.zapsolution.com
Re: Create .lib file from .dll
« Reply #1 on: February 09, 2013, 04:20:18 PM »
Quote
So this means we could write DLL's in PowerBASIC, then make then into LIB files that could be compiled in with our Visual Studio apps?

I didn't had time to test it yet (have to write the header first), but that's the purpose of it ;)
« Last Edit: February 09, 2013, 04:22:31 PM by Patrice Terrier »
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Offline James C. Fuller

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 672
Re: Create .lib file from .dll
« Reply #2 on: February 09, 2013, 06:26:59 PM »
Patrice,
  I am working on a tutorial for bc9 on the creation and use of dll's using several different c/c++ compilers.
I found that the translator was exporting DllMain. I could not find one example where it was exported.
In fact it's use is for the OS only so should not be exported.
I noticed DllMain is listed in your dumpbin of gdimage.dll.

James

Offline Patrice Terrier

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2401
  • Gender: Male
    • www.zapsolution.com
Re: Create .lib file from .dll
« Reply #3 on: February 09, 2013, 07:04:12 PM »
James,

Correct, a few functions, like DLLMAIN and a few callback like CLOCKPROC, should not be part of the DEF file (indeed all the UPERCASE name should be ignored).
I have done a quick cut and paste from the resulting file created by dumpbin, without looking in detail  to all the exported sub/function names.
« Last Edit: February 09, 2013, 07:06:54 PM by Patrice Terrier »
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Offline James C. Fuller

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 672
Re: Create .lib file from .dll
« Reply #4 on: February 09, 2013, 08:49:23 PM »
Good tip.
tcc uses def files instead of lib for imports so I use tiny_impdef to create the def file from a dll.
A bit less editing than the dumpbin output.
This is the link to my tcc setup:
http://bc9basic.99k.org/tcc.7z

James

Offline Frederick J. Harris

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1012
  • Gender: Male
    • Frederick J. Harris
Re: Create .lib file from .dll
« Reply #5 on: February 11, 2013, 07:26:19 PM »
Jim Dunn said ...

Quote
So this means we could write DLL's in PowerBASIC, then make then into LIB files that could be compiled in with our Visual Studio apps?

As far as I know, PowerBASIC created Dlls should be directly usable from Microsoft's C/C++ languages, including usage through Visual Studio, so long as the C/C++ binaries are 32 bit too.

Its not real clear to me how simply getting the exports from a Dll is that much help, unless its a dll one wouldn't have written oneself, or one for which the source code isn't available?  One must still translate it oneself, not so?   

Oh!  Just occurred to me... doing that would allow use of the PowerBASIC Dll without doing LoadLibrary() / GetProcAddress() on all the functions.  That would help some.
« Last Edit: February 11, 2013, 07:29:33 PM by Frederick J. Harris »

Offline Patrice Terrier

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2401
  • Gender: Male
    • www.zapsolution.com
Re: Create .lib file from .dll
« Reply #6 on: February 11, 2013, 07:48:56 PM »
Quote
Oh!  Just occurred to me... doing that would allow use of the PowerBASIC Dll without doing LoadLibrary() / GetProcAddress() on all the functions
Yes, that's the purpose of it, being able to use .LIB means implicit linking, much easier to use with third party 32-bit addon DLLs written in PB (aka: WinLIFT/GDImage).

And the market is so much larger  ;)

...
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Offline Marc Pons

  • Newbie
  • *
  • Posts: 41
  • Gender: Male
  • Programming for hobby
Re: Create .lib file from .dll
« Reply #7 on: March 04, 2013, 01:42:55 PM »
I think you should look at PellesC  Polib tool


extract from the help file :

Command line options (POLIB)
 
POLIB has the following general syntax:
POLIB [ { filespec | @response-file | option } ... ]

The filespec argument specifies the name of a library, an object file, or an executable file. Wildcards are allowed.

If you have many files or options - use a response file. It is a text file with the same format as the command line. Line feeds are treated as spaces. The name of the response file must be preceded by a @ character on the command line. The response file may use either OEM or UTF-16LE encoding (from version 5.0).

An option starts with the / (or -) character, may be given in upper or lower case, and can be one of the following:

Option Description
/DEF Creates an import library from a module-definition file. [2.73]
/EXPLODE Extracts all members from the library.
/EXTRACT Extracts one named member from the library.
/LIST Displays the name of all library members.
/MACHINE Specifies the target processor. [2.73]
/MAKEDEF Creates a module-definition file from an import library. [4.00]
/NOUND Specifies no underscore for exported functions. [3.00]
/OUT Specifies the name of the output file.
/REMOVE Removes the named member from the library.
/VERBOSE Displays more information.


with the /out option you can have the lib file immediatly.

But 1 question, are you sure you don't need the dll after ;
I think the lib is just a file to help for declarations but normally you still have to give the dll with your program ?