Solved

Create ShortCut Problem

Posted on 2001-06-19
3
571 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
3 Comments
 
LVL 20

Expert Comment

by:Madshi
Comment Utility
double post, please delete this one
0
 
LVL 3

Accepted Solution

by:
f15iaf earned 50 total points
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now