Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   Delphi Verknüpfung (.lnk) Datei erzeugen (https://www.delphipraxis.net/195049-verknuepfung-lnk-datei-erzeugen.html)

Fukiszo 29. Jan 2018 19:13

Verknüpfung (.lnk) Datei erzeugen
 
Guten Abend,
ich bin immernoch am aufarbeiten alter Sources und bin nun hier gelandet:

Delphi-Quellcode:
function MakeALink(const sNewPathAndLinkfilename : String; pTargetPathAndFilename, pAddCommandLine,
                   pTargetWorkPath, pIconFromExe : PChar; iIconIndex : Integer; pDescription : PChar) : Boolean;
// Create the shortcut named in the LinkFileName argument, and load its data from the non-nil arguments. Return True if successful.
// small bug/feature shortpathnames are auto-expanded to long file format using quotes.
// how to:
// MakeALink ( 'sNewPathAndLinkfilename.lnk', pChar('pTargetPathAndFilename'), pChar('pAddCommandLine'), pChar('TargetWorkPath'), pChar(pIconFromExe), iIconIndex, pChar('pDescription'))
// where 'sNewPathAndLinkfilename.lnk' = Drive:\Path\to\Filename.lnk - file that we create
//       'pTargetPathAndFilename' = Drive:\Path\to\Source.ext - file that the link will be linked to
//       'pAddCommandLine' = '' - any additional Command Line Flags
//       'TargetWorkPath' = can be empty or a specific path
//       'pIconFromExe' = FileName that contain Icon (.ico/.exe./.dll etc)
//       'iIconIndex' = 0 for first Icon inside 'pIconFromExe'
//       'pDescription' = optional Description, will displayed as a hint
VAR
  vUNK : IUnknown;
  vISL : IShellLink;
  vIPF : IPersistFile;
  fNameW : ARRAY[0..MAX_PATH] OF WideChar;
begin
  Result := False;
  try
    StringToWideChar(sNewPathAndLinkfilename, fNameW, MAX_PATH);
    vUNK := CreateComObject(CLSID_ShellLink);
    vISL := vUNK AS IShellLink;
    vIPF := vUNK AS IPersistFile;
    IF pTargetPathAndFilename <> nil THEN
      IF vISL.SetPath(pTargetPathAndFilename) <> S_OK THEN Exit;
    IF pAddCommandLine <> nil THEN
      IF vISL.SetArguments(pAddCommandLine) <> S_OK THEN Exit;
    IF (pIconFromExe <> nil) THEN
      IF vISL.SetIconLocation(pIconFromExe, iIconIndex) <> S_OK THEN Exit;
    IF pTargetWorkPath <> nil THEN
      IF vISL.SetWorkingDirectory(pTargetWorkPath) <> S_OK THEN Exit;
    IF pDescription <> nil THEN
      IF vISL.SetDescription(pDescription) <> S_OK THEN Exit;
    IF vIPF.Save(@fNameW, False) <> S_OK THEN Exit;
    Result := True;
  except
    ON Exception DO;
  end;
end;
Gibt es eine komfortablere Art so etwas zu erreichen?
(Sorry falls Unicode bis jetzt noch nicht berücksichtigt wurde)

Grüße

ps:
_except
__ON Exception DO;
_end;
so wie ich es einsetze soll es so sein.

edit:
gibt es eine Möglichkeit eine .lnk Datei auch wieder einzulesen? (also die Werte innerhalb der .lnk Datei)

LTE5 29. Jan 2018 19:20

AW: Verknüpfung (.lnk) Datei erzeugen
 
So?

Delphi-Quellcode:
function createLink(const aFileName, aLinkFileName, aParameter, aLinkDestination: string): Boolean;
var
 psl: IShellLink;
 ppf: IPersistFile;
begin
 Result := False;

 if Succeeded(CoCreateInstance(CLSID_ShellLink, nil, CLSCTX_inPROC_SERVER, IID_IShellLinkW, psl)) then
  begin
   psl.SetPath(PWideChar(aFileName));
   psl.SetArguments(PWideChar(aParameter));
   psl.SetWorkingDirectory(PWideChar(ExtractFilePath(aFileName)));

   if Succeeded(psl.QueryInterface(IPersistFile, ppf)) then
    begin
     ppf.Save(PWideChar(aLinkDestination + aLinkFileName + '.lnk'), True);
     Result := True;
    end;
  end;
end;

function GetDesktopFolder: string;
var
 LStr: array [0 .. MAX_PATH] of Char;
begin
 SetLastError(ERROR_SUCCESS);
 Result := '';
 if SHGetFolderPath(0, CSIDL_DESKTOPDIRECTORY, 0, 0, @LStr) = S_OK then
  Result := IncludeTrailingPathDelimiter(LStr);
end;

function GetStartMenuFolder: string;
var
 LStr: array [0 .. MAX_PATH] of Char;
begin
 SetLastError(ERROR_SUCCESS);
 Result := '';
 if SHGetFolderPath(0, CSIDL_STARTMENU, 0, 0, @LStr) = S_OK then
  Result := IncludeTrailingPathDelimiter(LStr);
end;

B := createLink(ParamStr(0), 'Shortcut Name', 'Parameter', GetDesktopFolder);
Sieht man auch hier
http://www.delphipraxis.net/191958-v...erstellen.html

Fukiszo 29. Jan 2018 19:38

AW: Verknüpfung (.lnk) Datei erzeugen
 
Vielen Dank für die schnelle Antwort!
Hast Du zufällig auch eine Routine rumliegen mit der man den Inhalt einer .lnk Datei wieder auslesen kann?

Grüße

LTE5 29. Jan 2018 19:41

AW: Verknüpfung (.lnk) Datei erzeugen
 
Guck mal hier. Könnte deine Frage beantworten

How can You Open and Edit Windows .lnk Shortcut Files?

Fukiszo 29. Jan 2018 19:56

AW: Verknüpfung (.lnk) Datei erzeugen
 
Dort wird beschrieben wie man eine .lnk Datei mit Notepad und Hexeditor bearbeitet,
ich wollt schon innerhalb Delphi bleiben und hat gehofft das es da auch so eine "simple" methode gibt.
Ziel sollte sein eine .lnk datei auszuwerten und falls ein update nötig ist einfach eine neue erstellen.

Ich schau mal was ich mit der .pdf von microsoft anfangen kann um es mit Delphi zu realisieren.

Grüße


edit:
hab was passendes gefunden:

Delphi-Quellcode:
function GetLinkInfo(WinhWnd:HWND;LnkObj:string):String;
var
  hres        : HRESULT;
  psl         : IShellLink;
  ppf         : IPersistFile;
  wfd         : Win32_Find_Data;
  Path        : array[0..MAX_PATH] of Char;
begin
  result := '????';
  //initializes the Component Object Model(COM) library
  CoInitialize(nil);
  // Call CoCreateInstance to obtain the IShellLink Interface pointer.
  hres:=CoCreateInstance(CLSID_ShellLink, nil,
                              CLSCTX_INPROC_SERVER, IID_IShellLinkA, psl);
  if NOT SUCCEEDED(hres) then exit;
  // The IShellLink Interface supports the IPersistFile
  // interface. Get an interface pointer to it.
  hres:=psl.QueryInterface(IID_IPersistFile,ppf);
  if SUCCEEDED(hres) then begin
    // Convert the given link name string to wide character string.
    // and Load the file.
    hres:=ppf.Load(StringToOLEStr(LnkObj),STGM_READ);
    if SUCCEEDED(hres) then begin
      // Resolve the link by calling the Resolve() interface function.
      hres:=psl.Resolve(WinhWnd,SLR_ANY_MATCH OR SLR_NO_UI);
      if SUCCEEDED(hres) then begin
        hres:=psl.GetPath(Path,MAX_PATH,wfd,SLGP_SHORTPATH);
        if SUCCEEDED(hres) then result:=Path;
      end;
    end;
  end;
end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 18:32 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz