IT-Consultant: Patrice Terrier > Useful TIPS

Create .lib file from .dll

(1/2) > >>

Patrice Terrier:
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
--- End quote ---

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
--- End quote ---

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).

Patrice Terrier:

--- 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?
--- End quote ---

I didn't had time to test it yet (have to write the header first), but that's the purpose of it ;)

James C. Fuller:
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

Patrice Terrier:
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.

James C. Fuller:
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

Navigation

[0] Message Index

[#] Next page

Go to full version