Autor Beitrag
LowSkills
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 111

Windows XP
Delphi 6 Professional
BeitragVerfasst: Sa 24.02.07 12:02 
Hallo zusammen.

Ich quäle mich nun schon seit einiger Zeit erfolglos mit dem automatisierten Erstellen eines Links herum.

Zum nachvollziehen:
Die Funktion:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
procedure CreateLink (Target, Args, WorkDir, ShortCutName, Icon: String; IconNo: Integer);
var
  IObj: IUnknown;
  Link: IShellLink;
  IPFile: IPersistFile;   
  TargetW: WideString;
begin
  IObj := CreateComObject (CLSID_ShellLink);
  Link := IObj as IShellLink;
  IPFile := IObj as IPersistFile;
  with Link do
  begin
    SetPath (PChar (Target));
    SetArguments (PChar (Args));
    SetShowCmd (SW_SHOWMINIMIZED);
    SetWorkingDirectory (PChar (WorkDir));
    SetIconLocation (PChar (Icon), IconNo);
  end;
  TargetW := ShortCutName;
  IPFile.Save (PWChar (TargetW), False);
end;



Der Aufruf:

ausblenden Delphi-Quelltext
1:
CreateLink('C:\','','C:\','Test1.lnk',GetSpecialFolder(CSIDL_DESKTOP)+'\Icons\Load.ico',0)					


Meine Probleme:
Ich habe nur sehr wenig Ahnung davon, was eigentlich innerhalb dieser Funktion geschieht, ich weis nur, dass am Ende ein Link rauskommen soll. Soweit bin ich auch schon, aber die Parameter sind alle mehr oder weniger durch stundenlanges herumprobieren rausgekommen, weil ich ja wie gesagt nicht genau weiss, was da eigentlich verlangt wird.

Wo kann ich oder muss ich im Aufruf reinschreiben, an welcher Stelle der Link erschaffen wird? Der wird bis dato immer im Verzeichnis erstellt, wo auch das Proggi liegt. Ich will den Ort aber selber festlegen.

Zweites Problem:
Wofür sind die Parameter "args" und "IconNo" gedacht? Was wird da von mir erwartet?

Ich hoffe, mir kann jemand dieses Kauderwelsch in für mich verstänliches Vokabular übersetzen.

Vielen Dank für eure Hilfe schonmal im Vorraus.

:)

Moderiert von user profile iconGausi: Delphi-Tags hinzugefügt
Saubäär
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 376



BeitragVerfasst: Sa 24.02.07 12:49 
Hi,

Also erstmal zum Verständnis: Du willst eine Verknüpfung erstellen - richtig?
Ich hab zwar auch keine Ahnung, was deine Procedure genau macht (oder nicht macht) aber diese Function funktioniert:

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
uses ShlObj, ActiveX, ComObj, ...

public
 { Public-Deklarationen }
 function CreateLink(const AFilename, ALNKFilename, ADescription: String) : Boolean;


function TForm1.CreateLink(const AFilename, ALNKFilename, ADescription: String) : Boolean;
var psl : IShellLink;
    ppf : IPersistFile;
    wsz : PWideChar;
begin
 Result := False;
 if SUCCEEDED(CoCreateInstance(CLSID_ShellLink, nil, CLSCTX_inPROC_SERVER, IID_IShellLinkA, psl)) then begin
  psl.SetPath(PChar(AFilename));
  psl.SetDescription(PChar(ADescription));
  psl.SetWorkingDirectory(PChar(ExtractFilePath(AFilename)));
  if SUCCEEDED(psl.QueryInterface(IPersistFile, ppf)) then begin
   GetMem(wsz, MAX_PATH * 2);
   try
    MultiByteToWideChar(CP_ACP, 0, PChar(ALNKFilename), -1, wsz, MAX_PATH);
    ppf.Save(wsz, true);
    Result := True;
   finally
    FreeMem(wsz, MAX_PATH * 2);
    end;
    end;
    end;
end;

//z.b. Button1.Click...
begin
 CreateLink(Application.ExeName, 'C:\test.lnk', 'Verknüpfung zu meiner Exe');
end;


Gruß

Saubäär
LowSkills Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 111

Windows XP
Delphi 6 Professional
BeitragVerfasst: Sa 24.02.07 14:48 
Danke für ide schnelle Antwort, werd das gleich mal ausprobieren.

Allerdings sieht das so aus, als wenn ich der *.lnk kein Icon zuweisen kann. Richtig?
Das wäre mir schon sehr wichtig dass das ding auch ein Icon bekommt, was ich mir aussuche, bzw. was dann automatisch mit der na du weist schon, wie ich das meine.

;)
Saubäär
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 376



BeitragVerfasst: Sa 24.02.07 16:36 
Hi LowSkills,

dann wilst du also ein andres Icon (und nicht das Programm Icon verwenden). Ja - kein Problem:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
function TForm1.CreateLink(const AFilename, ALNKFilename, ADescription, AIcon: String; AIconNo: Integer) : Boolean; // neuer Funktionskopf
var psl : IShellLink;
    ppf : IPersistFile;
    wsz : PWideChar;
begin
 Result := False;
 if SUCCEEDED(CoCreateInstance(CLSID_ShellLink, nil, CLSCTX_inPROC_SERVER, IID_IShellLinkA, psl)) then begin
  psl.SetPath(PChar(AFilename));
  psl.SetDescription(PChar(ADescription));
  psl.SetWorkingDirectory(PChar(ExtractFilePath(AFilename)));
  psl.SetIconLocation(PChar(AIcon), AIconNo); // Diese Zeile ist neu. Damit wird das Icon festgelegt
  if SUCCEEDED(psl.QueryInterface(IPersistFile, ppf)) then begin
   GetMem(wsz, MAX_PATH * 2);
   try
    MultiByteToWideChar(CP_ACP, 0, PChar(ALNKFilename), -1, wsz, MAX_PATH);
    ppf.Save(wsz, true);
    Result := True;
   finally
    FreeMem(wsz, MAX_PATH * 2);
    end;
    end;
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 CreateLink(Application.ExeName, 'C:\test.lnk', 'Meine Verknüpfung', ExtractFilePath(ParamStr(0)) + 'icon.ico', 0);
end;


Saubäär
LowSkills Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 111

Windows XP
Delphi 6 Professional
BeitragVerfasst: Sa 24.02.07 17:32 
Keine Ahnung ob ich mich da sehr ungeschickt angestellt habe oder was, aber das war für mich echt mal n ganz harter knochen. Größter Dank geht an Dich, Saubäär! :)

Ach ja, wenn du mal Hilfe brauchst, frag einfach... *räusper, hust*

;)

Danke!

- Thread closed, thx -