Jump to content

PeterPanettone

Members
  • Content Count

    1354
  • Joined

  • Last visited

  • Days Won

    5

Everything posted by PeterPanettone

  1. PeterPanettone

    Fix for bug in JclShell

    Why do you want to throw away the fish before eating it?
  2. PeterPanettone

    Fix for bug in JclShell

    This is a more SECURE version of SaveShortcutShellLinkAsAdministrator for the demo app: procedure TformMain.SaveShortcutShellLink(const AFile: string); var SL: JclShell.TShellLink; HR: Integer; begin SL.Target := 'C:\Windows\System32\notepad.exe'; SL.HotKey := Winapi.Windows.MakeWord(Byte(HotKey1.HotKey), Byte(HotKey1.Modifiers)); // other ShellLink properties: SL.Description := 'My description'; HR := JclShell.ShellLinkCreate(SL, AFile); if HR <> Winapi.Windows.S_OK then begin if not SaveShortcutShellLinkAsAdministrator(AFile, SL) then MessageDlg('The Shortcut ShellLink could not be saved even with Administrator rights.', mtError, [mbOK], 0); end; end; function TformMain.SaveShortcutShellLinkAsAdministrator(const AFile: string; AShellLink: JclShell.TShellLink): Boolean; // SECURELY create Shortcut ShellLink as administrator var ts, ThisTempLnkFile, ThisParams, SourceHash, TargetHash: string; function GetHashFromFile(const AFileToHash: string): string; var IdMD5: IdHashMessageDigest.TIdHashMessageDigest5; FS: TFileStream; begin IdMD5 := IdHashMessageDigest.TIdHashMessageDigest5.Create; FS := TFileStream.Create(AFileToHash, fmOpenRead or fmShareDenyWrite); try Result := IdMD5.HashStreamAsHex(FS); finally FS.Free; IdMD5.Free; end; end; begin Result := False; // create temporary ShellLink Shortcut: System.SysUtils.DateTimeToString(ts, 'yymmddhhnnsszzz', Now); ThisTempLnkFile := System.SysUtils.IncludeTrailingBackslash(System.IOUtils.TPath.GetTempPath) + ts + '.lnk'; if JclShell.ShellLinkCreate(AShellLink, ThisTempLnkFile) = Winapi.Windows.S_OK then begin // get the MD5 hash of the temporary ShellLink Shortcut (to compare it later with the target hash): SourceHash := GetHashFromFile(ThisTempLnkFile); end else begin MessageDlg('Could not create a temporary ShellLink Shortcut', mtError, [mbOK], 0); EXIT; end; // MOVE the temporary ShellLink Shortcut to the real target: // https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/cmd ThisParams := '/C move ' + '"' + ThisTempLnkFile + '"' + ' ' + '"' + AFile + '"'; Result := JclShell.ShellExecAndWait('cmd.exe', ThisParams, 'runas', SW_HIDE); if not Result then EXIT; //OR: Winapi.ShellAPI.ShellExecute(0, 'runas', 'cmd.exe', PChar(ThisParams), '', Winapi.Windows.SW_HIDE); // Check whether the target file exists: if not FileExists(AFile) then EXIT; // Compare the MD5 hashes of the source file and of the target file: TargetHash := GetHashFromFile(AFile); Result := SameText(SourceHash, TargetHash); end;
  3. PeterPanettone

    Open Url From Current Line

    Put the caret on a source code line containing the URL of a web page: Then press Alt+T -> U to run this IDE tool Open URL From Current Line: Here is the source code: program OpenUrlFromCurrentLine; {$APPTYPE CONSOLE} {$R *.res} uses Winapi.Windows, Winapi.ShellAPI, System.Classes, System.RegularExpressions, System.SysUtils; var ThisLine: Integer; ThisLineStr, ThisURL: string; ThisSource: TStringList; begin // This is explained at: https://en.delphipraxis.net/ try if ParamCount > 0 then begin if ParamCount = 2 then begin if FileExists(ParamStr(1)) then begin if System.SysUtils.TryStrToInt(ParamStr(2), ThisLine) then begin ThisSource := TStringList.Create; try ThisSource.LoadFromFile(ParamStr(1)); if ThisSource.Count >= (ThisLine) then begin ThisLineStr := ThisSource[ThisLine - 1]; ThisURL := TRegEx.Match(ThisLineStr, '\b(?:(?:https?|ftp|file)://|www\.|ftp\.)[-A-Z0-9+&@#/%=~_|$?!:,.]*[A-Z0-9+&@#/%=~_|$]', [roIgnoreCase]).Value; if ThisURL <> '' then Winapi.ShellAPI.ShellExecute(0, 'open', PChar(ThisURL), nil, nil, SW_SHOW); end; finally ThisSource.Free; end; end; end; end; end; //Readln; except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; end. Build it. Then create a new IDE tool and configure it like this: Now you are ready to have fun. Merry Christmas!
  4. PeterPanettone

    Fix for bug in JclShell

    This has nothing to do directly with JclShell, but it adds useful functionality to the demo app: Since ShellLink Shortcuts are often created in protected locations such as in StartMenu directories or on a Desktop (e.g. "C:\Users\Public\Desktop") I have added a routine which automatically saves the ShellLink Shortcut with Administrator rights if the saving with normal user rights fails: procedure TformMain.SaveShortcutShellLink(const AFile: string); var SL: JclShell.TShellLink; HR: Integer; begin SL.Target := 'C:\Windows\System32\notepad.exe'; // Uncomment the following code line to include the modifier keys in the saved ShellLink Shortcut: SL.HotKey := Winapi.Windows.MakeWord(Byte(HotKey1.HotKey), Byte(HotKey1.Modifiers)); // other ShellLink properties: SL.Description := 'My description'; HR := JclShell.ShellLinkCreate(SL, AFile); if HR <> Winapi.Windows.S_OK then SaveShortcutShellLinkAsAdministrator(AFile, SL); end; procedure TformMain.SaveShortcutShellLinkAsAdministrator(const AFile: string; AShellLink: JclShell.TShellLink); var ts, ThisTempLnkFile, ThisParams: string; ThisShellExecExResult: Boolean; begin System.SysUtils.DateTimeToString(ts, 'yymmddhhnnsszzz', Now); ThisTempLnkFile := System.SysUtils.IncludeTrailingBackslash(System.IOUtils.TPath.GetTempPath) + ts + '.lnk'; JclShell.ShellLinkCreate(AShellLink, ThisTempLnkFile); // https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/cmd ThisParams := '/C move ' + '"' + ThisTempLnkFile + '"' + ' ' + '"' + AFile + '"'; ThisShellExecExResult := JclShell.ShellExecEx('cmd.exe', ThisParams, 'runas', SW_HIDE); //Winapi.ShellAPI.ShellExecute(0, 'runas', 'cmd.exe', PChar(ThisParams), '', Winapi.Windows.SW_HIDE); end;
  5. PeterPanettone

    Fix for bug in JclShell

    But even much simpler would be: procedure TformMain.LoadShortcutShellLink(const AFile: string); var SL: JclShell.TShellLink; begin JclShell.ShellLinkResolve(AFile, SL); HotKey1.HotKey := SL.HotKey_; end; Andreas, could you please implement this in JclShell. Thank you! And also the opposite conversion should be done internally in JclShell. In this way, the user would not have to do any conversions himself: procedure TformMain.SaveShortcutShellLink(const AFile: string); var SL: JclShell.TShellLink; HotKeyModifiers: Byte; begin SL.Target := 'C:\Windows\System32\notepad.exe'; SL.HotKey_ := HotKey1.HotKey; JclShell.ShellLinkCreate(SL, AFile); end; For this, the declaration of JclShell.TShellLink.Hotkey needs to be set to TShortcut. This really is a bug in JclShell. To make this compatible to existing code, I would redeclare the TShellLink record in JclShell in this way: // Shortcuts / Shell link type PShellLink = ^TShellLink; TShellLink = record Arguments: string; ShowCmd: Integer; WorkingDirectory: string; IdList: PItemIDList; Target: string; Description: string; IconLocation: string; IconIndex: Integer; HotKey: Word; // Use ShellLinkShortCut() to convert it to a TShortCut or simply use: HotKey_: TShortCut; end;
  6. PeterPanettone

    Fix for bug in JclShell

    This is the new code added by Andreas to JclShell: function ShellLinkShortCut(const Link: TShellLink): TShortCut; type THotKeyModifiers = set of (hkShift, hkCtrl, hkAlt, hkExt); var Modifiers: THotKeyModifiers; begin if Link.HotKey = 0 then Result := scNone else begin Modifiers := THotKeyModifiers(HiByte(Link.HotKey)); Result := LoWord(LoByte(Link.HotKey)); if hkShift in Modifiers then Result := Result or scShift; if hkCtrl in Modifiers then Result := Result or scCtrl; if hkAlt in Modifiers then Result := Result or scAlt; end; end; This reduces the code in my demo app to load the ShellLink Shortcut to: procedure TformMain.LoadShortcutShellLink(const AFile: string); var SL: JclShell.TShellLink; begin JclShell.ShellLinkResolve(AFile, SL); HotKey1.HotKey := JclShell.ShellLinkShortCut(SL); end;
  7. PeterPanettone

    Fix for bug in JclShell

    Congratulations, Andreas is a very fast guy! Thanks, Andreas!
  8. PeterPanettone

    Fix for bug in JclShell

    How would you resolve this "handling error"?
  9. PeterPanettone

    Fix for bug in JclShell

    I have tried to report the bug here: https://issuetracker.delphi-jedi.org/bug_report.php ...but I got this error message: This page isn’t working issuetracker.delphi-jedi.org is currently unable to handle this request. HTTP ERROR 500
  10. PeterPanettone

    F6 Search feature does not work anymore?

    It was already disabled: Right now, IDE Insight Search (F6) does work (even after IDE restart). Let's wait until it gets disabled again.
  11. PeterPanettone

    F6 Search feature does not work anymore?

    On the linked page, you wrote: "To reset the IDE Insight toolbar, select View -> Toolbars -> Customize to open the customize dialog. Under the Toolbars tab select the IDE Insight toolbar, click the Reset button and confirm." There is no "IDE Insight toolbar" item in my list of toolbars in Delphi Rio 10.3.3:
  12. PeterPanettone

    F6 Search feature does not work anymore?

    BUT: Even when I reactivate the "wuppdi Welcome Page" then the F6 Search feature still works! Maybe the deactivation/activation of the "wuppdi Welcome Page" simply resets the IDE Insight toolbar as Uwe Raabe has said above? But then the question is: What deactivates the IDE Insight in the first place?? And: Why is the "wuppdi Welcome Page" the only IDE Insight search result available when IDE Insight stops working? I have restarted the IDE for a test (while the "wuppdi Welcome Page" is still activated) : After the restart of the IDE the IDE Insight search is now still working!
  13. PeterPanettone

    F6 Search feature does not work anymore?

    It seems that the "wuppdi Welcome Page" is the culprit. When I deactivate the wuppdi Welcome Page: ...then the F6 Search feature works again!!
  14. A colleague has posted this very useful quality report. Please vote for it: https://quality.embarcadero.com/browse/RSP-27320 The Object Inspector combo-box drop-down list is not resizable: This makes it impossible or difficult to read drop-down menu items that are longer than the Object Inspector combo-box width. So please make this drop-down menu RESIZABLE like in this example: This would be very useful!
  15. PeterPanettone

    IDE title bar anomaly?

    Right after starting the Delphi Rio 10.3.3 IDE, the IDE window title bar looks quite normal: But when I double-click on the IDE window title bar to MAXIMIZE the IDE window, the HEIGHT of the title bar decreases from 32 pixels to 27 pixels: When I RESTORE the IDE window to its previous (not maximized) size, the title bar returns to its previous height of 32 pixels. Can anyone confirm this?
  16. PeterPanettone

    Open Url From Current Line

    Let's make an intelligent conclusion and let's agree on it: Good code styles are very important and we should follow them. But do not try to apply code styles to your home-baked Christmas cookies!
  17. PeterPanettone

    Open Url From Current Line

    That's the proof, thank you: There is no such thing as "unnecessary nesting"!
  18. PeterPanettone

    Open Url From Current Line

    That's not a definition. Try harder.
  19. PeterPanettone

    Open Url From Current Line

    Define "unnecessary nesting".
  20. PeterPanettone

    Open Url From Current Line

    I have found a new DEFINITION of "Early Return": "STUPIDITY TRYING TO LOOK SMART"
  21. PeterPanettone

    Open Url From Current Line

    That's not an "experience" but a FALSE ASSUMPTION (like e.g. climate change caused by cow farts).
  22. PeterPanettone

    Open Url From Current Line

    Thomas, didn't you say you are a fan of MANGA PORN COMICS?
  23. PeterPanettone

    Open Url From Current Line

    I too, if they are LOGICAL. But the above are not.
×