Page 1 of 1

PB4 - (Tiny) LibImporter

Posted: Tue Apr 25, 2006 12:28 am
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

Posted: Tue Apr 25, 2006 2:44 am
by jack
I run a short test, it did't output anything, is it designed for stdcall only?

Posted: Tue Apr 25, 2006 6:51 am
by Flype
yes it is,

do you have a .lib which is not stdcall ?
i'm interested to look at it if possible ?

Posted: Tue Apr 25, 2006 11:48 am
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?

Posted: Tue Apr 25, 2006 3:33 pm
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.

Posted: Tue Apr 25, 2006 11:16 pm
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?

Posted: Tue Apr 25, 2006 11:37 pm
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:

Posted: Wed Apr 26, 2006 12:18 am
by SFSxOI
Ahhh, OK then . got it :)

All in all then it seems to work :)

Thank You :)