Simple smaple call DLL

Share your advanced PureBasic knowledge/code with the community.
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Simple smaple call DLL

Post by fweil »

Code updated for 5.20+

Just for helping people :

read carefully the source code ... and adapt it ....

Code: Select all

;
; Attempt to list a DLL content 2.pb
; By fweil 20040514
;
Global NewList DumpString.s()

#Main_Window = 0

Global ProgramName.s, Directory_SDK.s, Dir_SDK.s

DebugLevel 2

Procedure AddToolTip(Handle, Text.s) ; This replaces the GadgetToolTip procedure of PureBasic giving a higher quality tooltip for gadgets. API based.
  hToolTip = CreateWindowEx_(0, "tooltips_class32", "", $D0000000 | #TTS_BALLOON, 0, 0, 0, 0, WindowID(#Main_Window), 0, GetModuleHandle_(0), 0) ; Tooltip window creation
  SendMessage_(hToolTip, #RB_SETTEXTCOLOR - 1, $C0FFFF, 0) ; Foreground color tooltip : Don't know why #RB_SETTEXTCOLOR - 1 !
  SendMessage_(hToolTip, #RB_SETBKCOLOR, $400000, 0) ; Background color tooltip
  SendMessage_(hToolTip, #RB_SIZETORECT + 1, 0, 180) ; Maximum width of tooltip : Don't know why #RB_SIZETORECT + 1 !
  Button.TOOLINFO\cbSize = SizeOf(TOOLINFO) ; Structure information fulfillment
  Button\uFlags = #TTF_IDISHWND | #TTF_SUBCLASS ; Indicates that the uId member is the window handle to the tool. If this flag is not set, uId is the identifier of the tool. | Indicates that the tooltip control should subclass the tool's window to intercept messages, such as WM_MOUSEMOVE. If not set, you need to use the TTM_RELAYEVENT message to forward messages to the tooltip control. For a list of messages that a tooltip control processes, see TTM_RELAYEVENT.
  Button\hWnd = Handle
  Button\uId = Handle
  Button\lpszText = @Text
  SendMessage_(hToolTip, #RB_SETBARINFO, 0, Button) ; Send structure content to the tooltip window.
EndProcedure

Procedure ParseDLL(DLLName.s)
  ClearList(DumpString())
  If DLLName <> ""
    BatchFileName.s = "ParseDLL_" + GetFilePart(DLLName) + ".bat"
    OutputFileName.s = "ParseDLL_" + GetFilePart(DLLName) + ".pdl"
    ProgramParameters.s = " " + DLLName + " > " + OutputFileName
    If CreateFile(0, BatchFileName)
      Debug ProgramName + " " + ProgramParameters, 2
      WriteStringN(0, ProgramName + " " + ProgramParameters)
      CloseFile(0)
    EndIf
    RunProgram(BatchFileName, "", "", 1 | 2)
    DeleteFile(BatchFileName)
    LengthToRead = FileSize(DLLName)
    *Buffer = AllocateMemory(LengthToRead)
    If ReadFile(0, DLLName)
      ReadData(0, *Buffer, LengthToRead)
      CloseFile(0)
    EndIf
    Debug LengthToRead
    Debug DLLName
    
    Debug "********* HexDump header *********", 2
    
    StepLength = 32
    Limit = $3FFF
    For i = 0 To Limit
      Addr.s = Hex(i)
      While Len(Addr) < 4
        Addr = "0" + Addr
      Wend
      a$ = Addr + " | "
      b$ = ""
      For j = i To i + StepLength - 1
        If j > LengthToRead
          Break 2
        EndIf
        Byte.b = PeekB(*Buffer + j)
        Cod.s = Hex(Byte & $FF)
        While Len(Cod) < 2
          Cod = "0" + Cod
        Wend
        a$ = a$ + Cod + " "
        If Byte < 32
          b$ = b$ + " "
        Else
          b$ = b$ + Chr(Byte)
        EndIf
      Next
      a$ = a$ + " - " + b$
      Debug a$, 2
      a$ = ""
      i + StepLength - 1
    Next
    If a$ <> ""
      Debug a$, 2
    EndIf
    
    Debug "********* HexDump footer *********", 2
    
    StepLength = 32
    Limit = LengthToRead
    For i = Limit - $1000 To Limit
      Addr.s = Hex(i)
      While Len(Addr) < 4
        Addr = "0" + Addr
      Wend
      a$ = Addr + " | "
      b$ = ""
      For j = i To i + StepLength - 1
        If j > LengthToRead
          Break 2
        EndIf
        Byte.b = PeekB(*Buffer + j)
        Cod.s = Hex(Byte & $FF)
        While Len(Cod) < 2
          Cod = "0" + Cod
        Wend
        a$ = a$ + Cod + " "
        If Byte < 32
          b$ = b$ + " "
        Else
          b$ = b$ + Chr(Byte)
        EndIf
      Next
      a$ = a$ + " - " + b$
      Debug a$, 2
      a$ = ""
      i + StepLength - 1
    Next
    If a$ <> ""
      Debug a$, 2
    EndIf
    
    If ReadFile(0, OutputFileName)
      i = 1
      Repeat
        a$ = Trim(ReadString(0))
        While FindString(a$, "  ", 1)
          a$ = ReplaceString(a$, "  ", " ")
        Wend
        If a$ <> ""
          AddElement(DumpString())
          DumpString() = a$
          Debug Str(i) + " - " + Chr(34) + a$ + Chr(34), 3
          i + 1
        EndIf
      Until Eof(0)
      CloseFile(0)
      DeleteFile(OutputFileName)
    EndIf
  EndIf
EndProcedure

;
;
;
CurrentDirectory.s = Space(255)
GetCurrentDirectory_(255, @CurrentDirectory)
Directory_SDK = "C:\Program Files\Microsoft SDK\Bin\Win64\"
Dir_SDK = Space(255)
GetShortPathName_(Directory_SDK, Dir_SDK, 255)
ProgramName = Dir_SDK + "Dumpbin.exe /EXPORTS"


#Gadget_ExplorerTree = 14
#Gadget_ExplorerCombo = 15
#Gadget_ExplorerList = 16
#Gadget_ListIcon = 17
#Gadget_Button = 18
#Escape = 99
Quit.l = #False
WindowXSize.l = 800
WindowYSize.l = 600
CurrentDirectory.s = Space(255)
GetCurrentDirectory_(255, CurrentDirectory)
If OpenWindow(#Main_Window, 0, 0, WindowXSize, WindowYSize, "My Explorer", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_TitleBar | #PB_Window_ScreenCentered)
  AddKeyboardShortcut(#Main_Window, #PB_Shortcut_Escape, #Escape)
  hExplorerTreeGadget = ExplorerTreeGadget(#Gadget_ExplorerTree, 0, 0, WindowXsize / 4, WindowYSize / 3, CurrentDirectory + "\*.*", #PB_Explorer_NoFiles | #PB_Explorer_AlwaysShowSelection)
  AddToolTip(hExplorerTreeGadget, "Directories explorer")
  hExplorerListGadget = ExplorerListGadget(#Gadget_ExplorerList, WindowXsize / 4, 0, 3 * WindowXSize / 4, WindowYSize / 3, CurrentDirectory + "\*.dll", #PB_Explorer_NoParentFolder | #PB_Explorer_AlwaysShowSelection)
  AddToolTip(hExplorerListGadget, "Files explorer")
  hListIconGadget = ListIconGadget(#Gadget_ListIcon, 0, WindowYSize / 3 + 30, WindowXSize, 2 * WindowYSize / 3 - 30, "ordinal", 40)
  AddToolTip(hListIconGadget, "Exports list")
  hButtonGadget = ButtonGadget(#Gadget_Button, 0, WindowYSize / 3 + 5, 60, 20, "Button")
  GadgetToolTip(#Gadget_Button, "Button")
  AddToolTip(hButtonGadget, "Button")
  AddGadgetColumn(#Gadget_ListIcon, 1, "hint", 40)
  AddGadgetColumn(#Gadget_ListIcon, 2, "RVA", 80)
  AddGadgetColumn(#Gadget_ListIcon, 3, "name", 480)
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Quit = #True
      Case #PB_Event_Menu
        Select EventMenu()
          Case #Escape
            Quit = #True
        EndSelect
      Case #PB_Event_Gadget
        Select EventGadget()
          Case #Gadget_ExplorerTree
            Debug Str(GetGadgetState(#Gadget_ExplorerTree)) + " " + GetGadgetText(#Gadget_ExplorerTree), 3
            SetGadgetText(#Gadget_ExplorerList, GetGadgetText(#Gadget_ExplorerTree))
          Case #Gadget_ExplorerList
            Debug Str(GetGadgetState(#Gadget_ExplorerList)) + " " + GetGadgetText(#Gadget_ExplorerList) + " " + GetGadgetItemText(#Gadget_ExplorerList, GetGadgetState(#Gadget_ExplorerList), 0), 3
            EventType = EventType()
            Select EventType
              Case #PB_EventType_LeftDoubleClick
                ThisName.s = GetGadgetText(#Gadget_ExplorerTree) + GetGadgetItemText(#Gadget_ExplorerList, GetGadgetState(#Gadget_ExplorerList), 0)
                If LCase(GetExtensionPart(ThisName)) = "dll"
                  DLLName.s = Space(255)
                  GetShortPathName_(ThisName, DLLName, 255)
                  Debug "Calling DLL parser with " + ThisName, 1
                  Debug "----------", 1
                  ParseDLL(DLLName)
                  NumberOfFunctions = 0
                  NumberOfNames = 0
                  ;
                  ;                        While CountGadgetItems(#Gadget_ListIcon) > 0
                  ;                          RemoveGadgetItem(#Gadget_ListIcon, CountGadgetItems(#Gadget_ListIcon) - 1)
                  ;                        Wend
                  ;                       
                  FreeGadget(#Gadget_ListIcon)
                  hListIconGadget = ListIconGadget(#Gadget_ListIcon, 0, WindowYSize / 3 + 30, WindowXSize, 2 * WindowYSize / 3 - 30, "ordinal", 40)
                  AddToolTip(hListIconGadget, "Exports list")
                  AddGadgetColumn(#Gadget_ListIcon, 1, "hint", 40)
                  AddGadgetColumn(#Gadget_ListIcon, 2, "RVA", 80)
                  AddGadgetColumn(#Gadget_ListIcon, 3, "name", 480)
                  
                  SelectElement(DumpString(), 9)
                  If FindString(DumpString(), "number of functions", 1)
                    NumberOfFunctions = Val(DumpString())
                  EndIf
                  SelectElement(DumpString(), 10)
                  If FindString(DumpString(), "number of names", 1)
                    NumberOfNames = Val(DumpString())
                  EndIf
                  Debug Str(NumberOfFunctions) + " functions and " + Str(NumberOfNames) + " names found", 1
                  If NumberOfFunctions Or NumberOfNames
                    SelectElement(DumpString(), 8)
                    Debug DumpString(), 1
                    Debug "----------", 2
                    For i = 12 To NumberOfNames + 11
                      SelectElement(DumpString(), i)
                      ;                              Debug Str(i) + " : " + Str(i) + " : " + DumpString(), 2
                      AddGadgetItem(#Gadget_ListIcon, -1, ReplaceString(DumpString(), " ", Chr(10)))
                    Next
                  EndIf
                  Debug "==========", 1
                Else
                  Debug ThisName, 3
                EndIf
              Case #PB_EventType_Change
                If GetGadgetText(#Gadget_ExplorerTree) <> GetGadgetText(#Gadget_ExplorerList)
                  SetGadgetText(#Gadget_ExplorerTree, GetGadgetText(#Gadget_ExplorerList))
                EndIf
            EndSelect
        EndSelect
      Case #WM_SIZE
        WindowXSize = WindowWidth(#Main_Window)
        WindowYSize = WindowHeight(#Main_Window)
        ResizeGadget(#Gadget_ExplorerTree, 0, 0, WindowXsize / 4, WindowYSize / 3)
        ResizeGadget(#Gadget_ExplorerList, WindowXsize / 4, 0, 3 * WindowXSize / 4, WindowYSize / 3)
        ResizeGadget(#Gadget_ListIcon, 0, WindowYSize / 3 + 30, WindowXSize, 2 * WindowYSize / 3 - 30)
        ResizeGadget(#Gadget_Button, 0, WindowYSize / 3 + 5, 60, 20)
    EndSelect
  Until Quit
EndIf
End
Rgrds
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
freewilli
New User
New User
Posts: 6
Joined: Wed Nov 26, 2003 5:59 am

Post by freewilli »

Crashes.. ;-(
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post by fweil »

Works well on my licensed 3.90 running with Windows 2000 !

What about your installation ?

Rgrds
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

..

Post by NoahPhense »

fweil wrote:Works well on my licensed 3.90 running with Windows 2000 !

What about your installation ?

Rgrds
windows 2000, PB 3.91 b1, i can use the explorer window.. but it doesnt
list any files in my directories..

- np
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post by fweil »

NoahPhense,

Don't understand why. I use regular stuff to fill the gadgets.

Could you debug when running the app to see what goes wrong on your side. It really does not bug on my PC. Or let me know what I can do for better help.

Rgrds
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

..

Post by NoahPhense »

fweil wrote:NoahPhense,

Don't understand why. I use regular stuff to fill the gadgets.

Could you debug when running the app to see what goes wrong on your side. It really does not bug on my PC. Or let me know what I can do for better help.

Rgrds
I have the Microsoft SDK installed, but I don't have this structure:

Directory_SDK = "C:\Program Files\Microsoft SDK\Bin\Win64"

...

- np
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post by fweil »

This is not a strcuture but a string variable declared at top of the source code.

Just you have to update this path to make Dumpbin.exe accessible by using ProgramName = Dir_SDK + "Dumpbin.exe /EXPORTS"

That's just easy.

...
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

..

Post by NoahPhense »

fweil wrote:This is not a strcuture but a string variable declared at top of the source code.

Just you have to update this path to make Dumpbin.exe accessible by using ProgramName = Dir_SDK + "Dumpbin.exe /EXPORTS"

That's just easy.

...
Yeah I understand structures and directories. I'm saying....

That I have the SDK installed.. But I do NOT have that structure, which
includes that binary executable "Dumpbin.exe" .. so maybe, you have
some 'other' version of the Microsoft SDK installed?

I don't even have a Bin directory below my 'Microsoft SDK' directory.


I do have one in my MASM32 directory.. ;)

- np
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post by fweil »

Sorry, I misunderstood you.

This prog is part of the SDK I got from MSDN.

If I am not wrong you should find it easily by requesting Google with "download dumpbin.exe", or tell me were you want I post it to you.

Rgrds
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post by fweil »

As I wrote it it should give a tree explorer with folders only at left and a list explorer with files at right top of the window.

Well, I am not focused on it now. If you found how to run it well that is good.

Rgrds
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

..

Post by NoahPhense »

fweil wrote:As I wrote it it should give a tree explorer with folders only at left and a list explorer with files at right top of the window.

Well, I am not focused on it now. If you found how to run it well that is good.

Rgrds
Yeah, I still haven't gotten it to work.. maybe it's a 3.91 b1 thing.. dunno

- np
nessie
User
User
Posts: 60
Joined: Mon Jul 07, 2003 1:19 pm
Location: Glasgow / Scotland
Contact:

Post by nessie »

HI,

It kind of worked for me and I'm using XP sp1 (PB 3.90). It list the folders and displays the Dll's fine. When it comes to displaying the contents it crashes at the end of the listing. The code below is the code that causes the crash.

Code: Select all

                        If FindString(DumpString(), "number of functions", 1) 
                           NumberOfFunctions = Val(DumpString()) 
                        EndIf 
                        SelectElement(DumpString(), 10) 
                        If FindString(DumpString(), "number of names", 1) 
                            NumberOfNames = Val(DumpString()) 
                        EndIf
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post by fweil »

...,

It means probably you are trying to read a file that does not contain a standard header.

Maybe you can add a test replacing the block code from line 210 to line 228 by this to bypass possible issue of linked list empty :

Code: Select all

                        If CountList(DumpString()) > 10
                            SelectElement(DumpString(), 9)
                            If FindString(DumpString(), "number of functions", 1)
                                NumberOfFunctions = Val(DumpString())
                            EndIf
                            SelectElement(DumpString(), 10)
                            If FindString(DumpString(), "number of names", 1)
                                NumberOfNames = Val(DumpString())
                            EndIf
                            Debug Str(NumberOfFunctions) + " functions and " + Str(NumberOfNames) + " names found", 1
                            If NumberOfFunctions Or NumberOfNames
                                SelectElement(DumpString(), 8)
                                Debug DumpString(), 1
                                Debug "----------", 2
                                For i = 12 To NumberOfNames + 11
                                  SelectElement(DumpString(), i)
    ;                              Debug Str(i) + " : " + Str(i) + " : " + DumpString(), 2
                                  AddGadgetItem(#Gadget_ListIcon, -1, ReplaceString(DumpString(), " ", Chr(10)))
                                Next
                            EndIf
                        EndIf
                        Debug "==========", 1
What file are you trying to parse, if it is public ?

Rgrds
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
nessie
User
User
Posts: 60
Joined: Mon Jul 07, 2003 1:19 pm
Location: Glasgow / Scotland
Contact:

Post by nessie »

fweil,

:oops: I had forgot to reinstall the sdk (sorry) I tried your code again and the original code works perfectly. I should have known it would be something i was doing wrong.
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post by fweil »

Nice ... Hope this sample code will make possible to enhance DLL knowledge and use.

Rgrds
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
Post Reply