Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Windows 11 Desktop-Shortcut erstellen (https://www.delphipraxis.net/214519-windows-11-desktop-shortcut-erstellen.html)

DieDolly 23. Jan 2024 20:05

Windows 11 Desktop-Shortcut erstellen
 
Ich habe leider gerade kein Windows 11 aber mir wurde gesagt, dass der Code unten keinen Desktop-Shortcut mehr unter Windows 11 erstellt.
Gibt es da neue Restriktionen unter Windows 11? Weiß da jemand was?

Delphi-Quellcode:
// Create desktop shortcut
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(PChar(AFileName));
   psl.SetArguments(PChar(AParameter));
   psl.SetWorkingDirectory(PChar(ExtractFilePath(AFileName)));

   if Succeeded(psl.QueryInterface(IPersistFile, ppf)) then
    begin
     ppf.Save(PChar(ALinkDestination + ALinkFileName + '.lnk'), True);
     Result := True;
    end;
  end;
end;

// Delete desktop shortcut
procedure deleteShortCuts(const AProfileName: string; ALinkDestination: string);
begin
 ALinkDestination := ALinkDestination + AProfileName + '.lnk';

 if FileExists(ALinkDestination) then
  DeleteFile(ALinkDestination);
end;

himitsu 23. Jan 2024 21:49

AW: Windows 11 Desktop-Shortcut erstellen
 
ALinkDestination ist wirklich ein Pfad, inkl. abschließendem \ ?
Delphi-Referenz durchsuchenTPath.Combine


Stell dir mal vor, jemand würde die Rückgabewerte wirklich auswerten, dann wüsste derjenige*innen vermutlich was, ob und warum es nicht ginge. :roll:

Delphi-Quellcode:
uses Winapi.ShlObj, Winapi.Ole2, System.Win.ComObj;

procedure CreateLink(const AFileName, ALinkFileName, AParameter, ALinkDestination: string);
var
  res: HRESULT;
  psl: IShellLink;
  ppf: IPersistFile;
begin
  res := CoCreateInstance(CLSID_ShellLink, nil, CLSCTX_inPROC_SERVER, IID_IShellLinkW, psl);
  if Succeeded(res) then begin
    psl.SetPath(PChar(AFileName));
    psl.SetArguments(PChar(AParameter));
    psl.SetWorkingDirectory(PChar(ExtractFileDir(AFileName)));
    res := psl.QueryInterface(IID_IPersistFile, ppf);
    if Succeeded(res) then
      ppf.Save(PChar(ALinkDestination + ALinkFileName + '.lnk'), True)
    else
      RaiseLastOSError(res);
  end else
    RaiseLastOSError(res);
end;
oder gleich ganz (denn ALLE deine genutzten Funktionen besitzen ein Result, was oft komplett ignoriert wird)
und da ich jetzt mal faul bin ...
Delphi-Quellcode:
procedure CreateLink(const AFileName, ALinkFileName, AParameter, ALinkDestination: string);
var
  psl: IShellLink;
  ppf: IPersistFile;
begin
  OleCheck(CoCreateInstance(CLSID_ShellLink, nil, CLSCTX_inPROC_SERVER, IID_IShellLinkW, psl));
  OleCheck(psl.SetPath(PChar(AFileName)));
  OleCheck(psl.SetArguments(PChar(AParameter)));
  OleCheck(psl.SetWorkingDirectory(PChar(ExtractFileDir(AFileName))));
  OleCheck(psl.QueryInterface(IID_IPersistFile, ppf));
  OleCheck(ppf.Save(PChar(ALinkDestination + ALinkFileName + '.lnk'), True));
end;
Delphi-Referenz durchsuchenOleCheck (ErrorBit) oder doch vielleicht Delphi-Referenz durchsuchenCheckOSError (ungleich 0 aka S_OK);

Aber beim
Delphi-Quellcode:
RaiseLastOSError(res, 'nochwas')
oder
Delphi-Quellcode:
raise Exception.Create('sonstwas: ' + SysErrorMessage(Cardinal(res)));
hätte man noch einen Bonus.
noch (vielleicht), da https://quality.embarcadero.com/browse/RSP-44293

DieDolly 24. Jan 2024 15:15

AW: Windows 11 Desktop-Shortcut erstellen
 
Zitat:

ALinkDestination ist wirklich ein Pfad, inkl. abschließendem \ ?
Unter Windows 10 funktioniert der Code von mir da oben.

himitsu 24. Jan 2024 15:22

AW: Windows 11 Desktop-Shortcut erstellen
 
Was er macht, wenn alles Problemlos geht, ist nicht alles.

Es kommt auch drauf an, was passiert, wenn nicht alles so läuft, wie gewollt.


Alle Zeitangaben in WEZ +1. Es ist jetzt 04:25 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