PB4 - (Tiny) LibImporter

Share your advanced PureBasic knowledge/code with the community.
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

PB4 - (Tiny) LibImporter

Post by Flype »

Code updated for 5.20+

by selecting a 'mydll.lib' file, this program generates automatically

1/ 'mydll.pb' --> Code Block for Import/EndImport
2/ 'mydll.res' --> Resident file to be put into 'purebasic/residents/'

Code: Select all

Procedure.s MakeFn(fn.s, len.l)
  
  Protected i.l
  
  fn + ".l("
  
  If len
    For i = 1 To (len/4)
      fn + Chr(96+i) + ".l, "
    Next
    fn = Left(fn, Len(fn)-2)
  EndIf
  
  ProcedureReturn fn + ") ; " + Str(len)
  
EndProcedure
Procedure.l IsFn(List fn.s(), name.s)
  
  ForEach fn()
    If fn() = name
      ProcedureReturn #True
    EndIf
  Next
  
EndProcedure
Procedure.l ImportLib(libName.s)
  
  Protected NewList fn.s()
  Protected line.s, name.s, arg.s
  
  If ReadFile(0, libName)
    While Eof(0) = #False
      line = ReadString(0)
      If Left(line, 7) = "__imp__"
        arg = StringField(line, 2, "@")
        If arg
          name = MakeFn(Mid(line, 8, Len(line)-Len(arg)-8), Val(arg))
          If IsFn(fn(), name) = #False
            AddElement(fn()) : fn() = name
          EndIf
        EndIf
      EndIf
    Wend
    CloseFile(0)
  EndIf
  
  If ListSize(fn())
    SortList(fn(), 0)
    If CreateFile(1, Left(libName, Len(libName)-4) + ".pb")
      WriteStringN(1, "; imported with purebasic v" + StrF(#PB_Compiler_Version, 1))
      WriteStringN(1, "Import " + #DQUOTE$ + libName + #DQUOTE$)
      ForEach fn()
        WriteStringN(1, #TAB$ + fn())
      Next
      WriteStringN(1, "EndImport")
      CloseFile(1)
      ProcedureReturn #True
    EndIf
  EndIf
  
EndProcedure

libName.s = OpenFileRequester("Select .lib file", GetCurrentDirectory(), "*.lib|*.lib", 0)

If libName
  libName = GetFilePart(libName)
  If ImportLib(libName)
    libName = Left(libName, Len(libName)-4)
    RunProgram(libName+".pb")
    RunProgram(#PB_Compiler_Home+"compilers/pbcompiler.exe","/RESIDENT "+libName+".res "+libName+".pb ",GetCurrentDirectory())
  EndIf
EndIf

End
Last edited by Flype on Wed Apr 26, 2006 12:54 am, edited 1 time in total.
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Post by jack »

I run a short test, it did't output anything, is it designed for stdcall only?
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

yes it is,

do you have a .lib which is not stdcall ?
i'm interested to look at it if possible ?
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

Hmmm, Does this assume that PB is installed in the default directory like this??:

C:\program files\purebasic\compilers\pbcompiler.exe

or does it matter?
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

I use this in the source code :

Code: Select all

#PB_Compiler_Home + "compilers/pbcompiler.exe
So it should work everywhere, for everybody.
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

OK, got it to work now. Had to put the .lib in the same directory as the importer tho. Anyway, since i'm playing around with the MS detours library right now I thought i'd try it on that..detours.lib. It produced the desired detours.pb file, however, when it created the .pb file it made everything long. Like this function for example:

Code: Select all

CreateProcessA.l(a.l, b.l, c.l, d.l, e.l, f.l, g.l, h.l, i.l, j.l)
some of those parameters should be strings, or at least they are in actual usage. So did this generate the file correctly?

BTW, the detours.lib is a C++ library, so is this only for .c librariers?
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

with .lib files,

the importer can retrieve the function list.
the importer can retrieve the total size in bytes of the arguments list.

it can't 'guess' the number of arguments.
it can't 'guess' the type of arguments.

So i choose to make longs.
it allow me to test function easier than if i have to rewrite all by hand.
but i still need the documentation of the library i'm testing.

:wink:
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

Ahhh, OK then . got it :)

All in all then it seems to work :)

Thank You :)
Post Reply