?
Solved

Create ShortCut Problem

Posted on 2001-06-19
3
Medium Priority
?
578 Views
Last Modified: 2008-03-17
I need to create shortcut, I know to get the StartMenu folder via registry, but when I create the shortcut uses CLSID_ShellLink I've got error on converting interfaces :

incompatible types:'OLE2.IUnknown' and 'System.IUnknown'

below is my code :

procedure CreateShortCut(Filename, Argument, ShellName: string);
var
  MyObject  : IUnknown;
  MySlink   : IShellLink;
  MyFile    : IPersistFile;
  WFileName : WideString;

begin
  MyObject := CreateComObjects(CLSID_ShellLink);
  MySLink  := MyObject as IShellLink;
  MyPFile  := MyObject as IpersistFile;
  with MySLink do begin
    SetArguments(Arguments);
    SetPath(Pchar(FileName));
    SetWorkingDirectory(Pchar(ExtractFilePatch
    (FileName)));
  end;
  WFileName := ShellPath;
  MyPFile.Save(PwChar(WFileName),False);
end;
0
Comment
Question by:comicboy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 20

Expert Comment

by:Madshi
ID: 6208050
double post, please delete this one
0
 
LVL 3

Accepted Solution

by:
f15iaf earned 200 total points
ID: 6211903
Uses
  Registry, ActiveX, Comobj, ShlObj;

type
  ShortcutType = (_DESKTOP, _QUICKLAUNCH, _SENDTO, _STARTMENU, _OTHERFOLDER);


procedure CreateShortcut(SourceFileName: string; Location: ShortcutType; SubDirectory : string);
var
  MyObject : IUnknown;
  MySLink  : IShellLink;
  MyPFile  : IPersistFile;
  Directory,
  LinkName  : string;
  WFileName : WideString;
  MyReg,
  QuickLaunchReg : TRegIniFile;
begin
  MyObject := CreateComObject(CLSID_ShellLink);
  MySLink := MyObject as IShellLink;
  MyPFile := MyObject as IPersistFile;

  MySLink.SetPath(PChar(SourceFileName));

  MyReg := TRegIniFile.Create('Software\MicroSoft\Windows\CurrentVersion\Explorer');
  try
    LinkName := ChangeFileExt(SourceFileName, '.lnk');
    LinkName := ExtractFileName(LinkName);
    case Location of
      _OTHERFOLDER  : Directory := SubDirectory;
      _DESKTOP      : Directory := MyReg.ReadString('Shell Folders', 'Desktop', '');
      _STARTMENU    : Directory := MyReg.ReadString('Shell Folders', 'Start Menu', '');
      _SENDTO       : Directory := MyReg.ReadString('Shell Folders', 'SendTo', '');
      _QUICKLAUNCH:
        begin
          QuickLaunchReg := TRegIniFile.Create('Software\MicroSoft\Windows\CurrentVersion\GrpConv');
          try
            Directory := QuickLaunchReg.ReadString('MapGroups', 'Quick Launch', '');
          finally
            QuickLaunchReg.Free;
          end;
        end;
    end;
    if Directory <> '' then
      begin
        if (SubDirectory <> '') and (Location <> _OTHERFOLDER) then
         WFileName := Directory + '\'+ SubDirectory +'\' + LinkName
        else
         WFileName := Directory + '\' + LinkName;
        MyPFile.Save(PWChar(WFileName), False);
      end;
  finally
    MyReg.Free;
  end;
end;
0
 

Author Comment

by:comicboy
ID: 6495660
Actually this is a double post, and I've already got the answer from the other question, anyway since f15iaf's trying to help me too then I'll give the point then :) thanks
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month11 days, 22 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question