Advertisement
SharkyEXE

Untitled

Nov 23rd, 2022
2,283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Command.s = ProgramParameter()  ; e.g.: "SET varname="
  2.  
  3. EnableExplicit
  4.  
  5. Structure STRUC_DirInfo
  6.   sName.s
  7.   sFullName.s
  8. EndStructure
  9.  
  10. Structure opcode
  11.   CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
  12.     mov.u
  13.   CompilerElse
  14.     mov.a
  15.   CompilerEndIf
  16.   addr.i
  17.   push.a
  18.   ret.a
  19. EndStructure
  20.  
  21. Structure hookstruct
  22.   addr.i
  23.   hook.opcode
  24.   orig.a[SizeOf(opcode)]
  25. EndStructure
  26.  
  27. Procedure Hook(*OldFunctionAddress, *NewFunctionAddress)
  28.   Protected *hook_ptr.hookstruct
  29.  
  30.   If *OldFunctionAddress = 0 Or *NewFunctionAddress = 0
  31.     ProcedureReturn #Null
  32.   EndIf
  33.  
  34.   *hook_ptr = AllocateMemory(SizeOf(hookstruct), #PB_Memory_NoClear)
  35.   *hook_ptr\addr = *OldFunctionAddress
  36.   CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
  37.     *hook_ptr\hook\mov = $B848
  38.   CompilerElse
  39.     *hook_ptr\hook\mov = $B8
  40.   CompilerEndIf
  41.   *hook_ptr\hook\addr = *NewFunctionAddress
  42.   *hook_ptr\hook\push = $50
  43.   *hook_ptr\hook\ret = $C3
  44.  
  45.   CopyMemory(*OldFunctionAddress, @*hook_ptr\orig, SizeOf(opcode))
  46.   If WriteProcessMemory_(GetCurrentProcess_(), *OldFunctionAddress, @*hook_ptr\hook, SizeOf(opcode), #Null)=0
  47.     FreeMemory(*hook_ptr)
  48.     ProcedureReturn #Null
  49.   Else
  50.     ProcedureReturn *hook_ptr
  51.   EndIf
  52. EndProcedure
  53.  
  54. Procedure UnHook(*hook_ptr.hookstruct)
  55.   Protected retValue.i
  56.  
  57.   If *hook_ptr
  58.     If *hook_ptr\addr
  59.       If WriteProcessMemory_(GetCurrentProcess_(), *hook_ptr\addr, @*hook_ptr\orig, SizeOf(opcode), #Null)
  60.         retValue = *hook_ptr\addr
  61.         FreeMemory(*hook_ptr)
  62.         ProcedureReturn retValue
  63.       EndIf
  64.     EndIf
  65.   EndIf
  66.  
  67.   ProcedureReturn #Null
  68. EndProcedure
  69.  
  70. Procedure Block_FindFirstChangeNotification(lpPathName, bWatchSubtree, dwNotifyFilter.l)
  71.   ProcedureReturn #INVALID_HANDLE_VALUE
  72. EndProcedure
  73.  
  74. Procedure Event_ResizeWindow()
  75.   Protected Window = EventWindow()
  76.   Protected exlFiles = GetWindowData(Window)
  77.   SetGadgetItemAttribute(exlFiles, 0, #PB_Explorer_ColumnWidth, GadgetWidth(exlFiles) - 25)
  78. EndProcedure
  79.  
  80. Procedure FillDirList(Gadget, IconImg, sParentDir.s, List Dirs.STRUC_DirInfo())
  81.   Protected Dir, i, MaxIndex, ItemIndex, sCurDirPart.s, sDirPart.s, sName.s, Count, lvi.LV_ITEM
  82.  
  83.   If sParentDir And IsGadget(Gadget) And GadgetType(Gadget) = #PB_GadgetType_ListIcon
  84.     If Right(sParentDir, 1) <> "\" : sParentDir + "\" : EndIf
  85.     ClearList(Dirs())
  86.     SendMessage_(GadgetID(Gadget), #WM_SETREDRAW, 0, 0)
  87.     ClearGadgetItems(Gadget)
  88.     lvi\mask = #LVIF_INDENT
  89.    
  90.     MaxIndex = CountString(sParentDir, "\") - 1
  91.     For i = 0 To MaxIndex
  92.       sDirPart = StringField(sParentDir, i + 1, "\")
  93.       sCurDirPart + sDirPart + "\"
  94.       If AddElement(Dirs())
  95.         Dirs()\sFullName = sCurDirPart
  96.         If i = 0
  97.           AddGadgetItem(Gadget, -1, sCurDirPart, ImageID(IconImg))
  98.         Else
  99.           AddGadgetItem(Gadget, -1, sDirPart, ImageID(IconImg))
  100.         EndIf
  101.         lvi\iItem = i
  102.         SendMessage_(GadgetID(Gadget), #LVM_GETITEM, 0, lvi)
  103.         lvi\iIndent = i
  104.         SendMessage_(GadgetID(Gadget), #LVM_SETITEM, 0, lvi)
  105.       EndIf
  106.     Next
  107.     ;Get the child directories.
  108.     Dir = ExamineDirectory(#PB_Any, sParentDir, "*.*")
  109.     If Dir
  110.       While NextDirectoryEntry(Dir)
  111.         If DirectoryEntryType(Dir) = #PB_DirectoryEntry_Directory
  112.           sName = DirectoryEntryName(Dir)
  113.           If sName <> "." And sName <> ".."
  114.             If AddElement(Dirs())
  115.               Dirs()\sName = sName
  116.               Dirs()\sFullName = sParentDir + sName + "\"
  117.               Count + 1
  118.             EndIf
  119.           EndIf
  120.         EndIf
  121.       Wend
  122.       FinishDirectory(Dir)
  123.      
  124.       If Count > 0
  125.         i = MaxIndex + 1
  126.         Count = ListSize(Dirs()) - 1
  127.         SortStructuredList(Dirs(), #PB_Sort_Ascending | #PB_Sort_NoCase, OffsetOf(STRUC_DirInfo\sName), #PB_String, i, Count)
  128.         SelectElement(Dirs(), i)
  129.         Repeat
  130.           AddGadgetItem(Gadget, -1, Dirs()\sName, ImageID(IconImg))
  131.           lvi\iItem = i
  132.           SendMessage_(GadgetID(Gadget), #LVM_GETITEM, 0, lvi)
  133.           lvi\iIndent = MaxIndex + 1
  134.           SendMessage_(GadgetID(Gadget), #LVM_SETITEM, 0, lvi)
  135.           i + 1
  136.         Until NextElement(Dirs()) = 0
  137.       EndIf
  138.     EndIf
  139.     SetGadgetState(Gadget, MaxIndex)    ;Select the last directory item of "sParentDir" var.
  140.     SendMessage_(GadgetID(Gadget), #WM_SETREDRAW, 1, 0)
  141.     RedrawWindow_(GadgetID(Gadget), 0, 0, #RDW_ERASE | #RDW_INVALIDATE | #RDW_UPDATENOW)
  142.   EndIf
  143. EndProcedure
  144.  
  145. Procedure WndProc_ExplorerList(hWnd, uMsg, wParam, lParam)
  146.   Protected Gadget, old = GetProp_(hWnd, "OldWndProc")
  147.   Static IsRedrawingBlocked
  148.  
  149.   If uMsg = #WM_SETREDRAW
  150.     IsRedrawingBlocked = 1 - wParam
  151.    
  152.   ElseIf uMsg = #WM_ERASEBKGND
  153.     If IsRedrawingBlocked
  154.       Gadget = GetProp_(hWnd, "PB_ID")
  155.       If IsGadget(Gadget)
  156.         PostEvent(#PB_Event_Gadget, 0, Gadget, #PB_EventType_FirstCustomValue)
  157.       EndIf
  158.     Else
  159.       ProcedureReturn 1
  160.     EndIf
  161.    
  162.   ElseIf uMsg = #WM_NCDESTROY
  163.     RemoveProp_(hWnd, "OldWndProc")
  164.   EndIf
  165.   ProcedureReturn CallWindowProc_(old, hWnd, uMsg, wParam, lParam)
  166. EndProcedure
  167.  
  168. Procedure.s OpenSelectDirWindow(sInitDir.s, ParentWindow)
  169.   Protected Event, EventType, sXML.s, Xml, Dialog, Window, hWndMain, Img, sCurDir.s, sPrevDrive.s, sResult.s
  170.   Protected strPath, exlFiles, excDrive, liFolders, btnOK, btnCancel, hLiFolders, sfi.SHFILEINFO, Index
  171.   Protected *FindFirstChangeNotificationW, *FindFirstChangeNotificationA, LibKernel32
  172.   Protected NewList Dirs.STRUC_DirInfo()
  173.   sXML = "<dialogs>" +
  174.          "  <window name='SelDir' text='Select Directory' minwidth='330' minheight='350' flags='#PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget | #PB_Window_Invisible'>" +
  175.          "    <gridbox columns='2' colexpand='yes' rowexpand='item:3'>" +
  176.          "      <text name='txtFolder' text='Folder name:' colspan='2' height='10'/>" +
  177.          "      <string name='strPath' colspan='2' flags='#PB_String_ReadOnly' height='10'/>" +
  178.          "      <listicon name='liFolders' text='Folders' width='150' flags='#PB_ListIcon_AlwaysShowSelection'/>" +
  179.          "      <vbox expand='item:2'>" +
  180.          "        <text name='txtType' text='File type: *.*' height='10'/>" +
  181.          "        <explorerlist name='exlFiles' height='150' flags='#PB_Explorer_FullRowSelect | #PB_Explorer_NoFolders | #PB_Explorer_NoParentFolder | #PB_Explorer_NoDirectoryChange | #PB_Explorer_NoDriveRequester | #PB_Explorer_NoMyDocuments | #PB_Explorer_AutoSort'/>" +
  182.          "        <text name='txtDrive' text='Drives:' height='10'/>" +
  183.          "        <explorercombo name='excDrive' height='22' flags='#PB_Explorer_DrivesOnly | #PB_Explorer_NoMyDocuments'/>" +
  184.          "      </vbox>" +
  185.          "      <empty/>" +
  186.          "      <singlebox expand='no' align='right' margin='left:0,right:0,horizontal:0'>" +
  187.          "        <hbox expand='equal' spacing='8' height='13'>" +
  188.          "          <button name='btnOK' text='OK' width='75' height='12'/>" +
  189.          "          <button name='btnCancel' text='Cancel' width='75' height='12'/>" +
  190.          "        </hbox>" +
  191.          "      </singlebox>" +
  192.          "    </gridbox>" +
  193.          "  </window>" +
  194.          "</dialogs>"
  195.   If sInitDir = "" : sInitDir = "C:\" : EndIf
  196.   If Right(sInitDir, 1) <> "\" : sInitDir + "\" : EndIf
  197.   If ParentWindow <> -1 : DisableWindow(ParentWindow, 1) : EndIf
  198.    
  199.   Xml = ParseXML(#PB_Any, sXML)
  200.   If Xml And XMLStatus(Xml) = #PB_XML_Success
  201.     Dialog = CreateDialog(#PB_Any)
  202.     If Dialog And OpenXMLDialog(Dialog, Xml, "SelDir", 0, 0, 500, 450)
  203.       strPath = DialogGadget(Dialog, "strPath")
  204.       btnOK = DialogGadget(Dialog, "btnOK")
  205.       btnCancel = DialogGadget(Dialog, "btnCancel")
  206.       exlFiles = DialogGadget(Dialog, "exlFiles")
  207.       excDrive = DialogGadget(Dialog, "excDrive")
  208.       liFolders = DialogGadget(Dialog, "liFolders")
  209.      
  210.       ;Remove the window icon.
  211.       Window = DialogWindow(Dialog)
  212.       hWndMain = WindowID(Window)
  213.       SendMessage_(hWndMain, #WM_SETICON, 0, 0)   ;for the old version of Windows.
  214.       SetWindowLongPtr_(hWndMain, #GWL_STYLE, GetWindowLong_(hWndMain, #GWL_STYLE) | #WS_CLIPCHILDREN)
  215.       SetWindowLongPtr_(hWndMain, #GWL_EXSTYLE, GetWindowLong_(hWndMain, #GWL_EXSTYLE) | #WS_EX_DLGMODALFRAME)
  216.       SetWindowData(Window, exlFiles)
  217.      
  218.       hLiFolders = GadgetID(liFolders)
  219.       SetWindowLongPtr_(hLiFolders, #GWL_STYLE, GetWindowLongPtr_(hLiFolders, #GWL_STYLE) | #LVS_NOSORTHEADER);#LVS_NOCOLUMNHEADER)
  220.       SetWindowPos_(hLiFolders, 0, 0, 0, 0, 0, #SWP_NOMOVE | #SWP_NOSIZE | #SWP_NOZORDER | #SWP_FRAMECHANGED)
  221.      
  222.       For Index = 1 To 3
  223.         RemoveGadgetColumn(exlFiles, 1)
  224.       Next
  225.      
  226.       ;Get a directory icon.
  227.       Img = CreateImage(#PB_Any, 16, 16, 32, #PB_Image_Transparent)
  228.       If Img
  229.         If SHGetFileInfo_(GetTemporaryDirectory(), 0, sfi, SizeOf(SHFILEINFO), #SHGFI_ICON | #SHGFI_SMALLICON)
  230.           If StartDrawing(ImageOutput(Img))
  231.             DrawImage(sfi\hIcon, 0, 0)
  232.             StopDrawing()
  233.           EndIf
  234.           DestroyIcon_(sfi\hIcon)
  235.         EndIf
  236.       EndIf
  237.      
  238.       ;Block automatic updates of the ExplorerList gadget.
  239.       LibKernel32 = OpenLibrary(#PB_Any, "Kernel32.dll")
  240.       If LibKernel32
  241.         *FindFirstChangeNotificationW = Hook(GetFunction(LibKernel32, "FindFirstChangeNotificationW"), @Block_FindFirstChangeNotification())
  242.         *FindFirstChangeNotificationA = Hook(GetFunction(LibKernel32, "FindFirstChangeNotificationA"), @Block_FindFirstChangeNotification())
  243.       EndIf
  244.      
  245.       SetProp_(GadgetID(exlFiles), "OldWndProc", SetWindowLongPtr_(GadgetID(exlFiles), #GWLP_WNDPROC, @WndProc_ExplorerList()))
  246.      
  247.       ;Set the initial directory.
  248.       If FileSize(sInitDir) = -2
  249.         SetGadgetText(excDrive, sInitDir)
  250.         sPrevDrive = GetGadgetText(excDrive)
  251.         FillDirList(liFolders, Img, sInitDir, Dirs())
  252.         SendMessage_(GadgetID(exlFiles), #WM_SETREDRAW, 0, 0)
  253.         SetGadgetText(exlFiles, sInitDir)
  254.         If Right(sInitDir, 2) <> ":\"
  255.           sInitDir = RTrim(sInitDir, "\")
  256.         EndIf
  257.         SetGadgetText(strPath, sInitDir)
  258.       EndIf
  259.      
  260.       BindEvent(#PB_Event_SizeWindow, @Event_ResizeWindow(), Window)
  261.       HideWindow(Window, 0)
  262.      
  263.       Repeat
  264.         Event = WaitWindowEvent()
  265.         If Event = #PB_Event_ActivateWindow
  266.           RedrawWindow_(hWndMain, 0, 0, #RDW_INVALIDATE | #RDW_UPDATENOW | #RDW_ERASE | #RDW_INTERNALPAINT | #RDW_ALLCHILDREN)
  267.          
  268.         ElseIf Event = #PB_Event_Gadget
  269.           EventType = EventType()
  270.           Select EventGadget()
  271.             Case excDrive
  272.               sCurDir = GetGadgetText(excDrive)
  273.               If sPrevDrive <> sCurDir       ;The ExplorerCombo gadget has no #PB_EventType_Change event.
  274.                 SetGadgetText(strPath, sCurDir)
  275.                 FillDirList(liFolders, Img, sCurDir, Dirs())
  276.                 SetGadgetText(exlFiles, sCurDir)
  277.                 sPrevDrive = sCurDir
  278.               EndIf
  279.              
  280.             Case exlFiles
  281.               If EventType = #PB_EventType_FirstCustomValue
  282.                 SendMessage_(GadgetID(exlFiles), #WM_SETREDRAW, 1, 0)
  283.                 RedrawWindow_(GadgetID(exlFiles), 0, 0, #RDW_INVALIDATE | #RDW_UPDATENOW | #RDW_NOERASE | #RDW_INTERNALPAINT)
  284.               EndIf
  285.              
  286.             Case liFolders
  287.               If EventType = #PB_EventType_LeftDoubleClick
  288.                 Index = GetGadgetState(liFolders)
  289.                 If Index >= 0
  290.                   SelectElement(Dirs(), Index)
  291.                   sCurDir = Dirs()\sFullName
  292.                   FillDirList(liFolders, Img, sCurDir, Dirs())
  293.                   SendMessage_(GadgetID(exlFiles), #WM_SETREDRAW, 0, 0)
  294.                   SetGadgetText(exlFiles, sCurDir)
  295.                   If Right(sCurDir, 2) <> ":\"
  296.                     sCurDir = RTrim(sCurDir, "\")
  297.                   EndIf
  298.                   SetGadgetText(strPath, sCurDir)
  299.                 EndIf
  300.               EndIf
  301.              
  302.             Case btnOK
  303.               sResult = GetGadgetText(strPath)
  304.               Break
  305.             Case btnCancel
  306.               Break
  307.           EndSelect
  308.         EndIf
  309.       Until Event = #PB_Event_CloseWindow And EventWindow() = Window
  310.      
  311.       If LibKernel32
  312.         UnHook(*FindFirstChangeNotificationW)
  313.         UnHook(*FindFirstChangeNotificationA)
  314.         CloseLibrary(LibKernel32)
  315.       EndIf
  316.      
  317.       CloseWindow(Window)
  318.      
  319.       While WindowEvent() : Wend
  320.     EndIf
  321.   EndIf
  322.   If ParentWindow <> -1 : DisableWindow(ParentWindow, 0) : SetActiveWindow(ParentWindow) : EndIf
  323.   ProcedureReturn sResult
  324. EndProcedure
  325.  
  326. If Result
  327.  
  328.   Path$ = Space(#MAX_PATH)       ;Create a buffer to receive the string as a result of SHGetPathFromIDList function.
  329.  
  330.   SHGetPathFromIDList_(Result, @Path$)   ;Convert the returned result to a file system path.
  331.  
  332.   If Path$
  333.     ; The converted path string is as follows.
  334.     ; C:\
  335.     ; C:\Windows
  336.    
  337.     Path$ = Chr('"')+Path$+Chr('"')
  338.    
  339.     If command And Right(command,1) <> "=" : command + " " : EndIf
  340.    
  341.     OpenConsole()
  342.     PrintN(command + Path$)
  343.    
  344.     Sleep_(5000)
  345.    
  346.   EndIf
  347. EndIf
  348.  
  349. Define e
  350. If OpenWindow(0, 0, 0, 222, 200, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  351.   ButtonGadget(0, 10, 10, 160, 23, "Show window")
  352.  
  353.   Repeat
  354.     e = WaitWindowEvent()
  355.     If e = #PB_Event_Gadget And EventGadget() = 0
  356.       Debug OpenSelectDirWindow("C:\windows\", 0)
  357.     EndIf
  358.   Until e = #PB_Event_CloseWindow
  359. EndIf
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement