Mr.Victor

梦里不知身是客,一晌贪欢!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

在桌面建立快捷方式

Posted on 2011-08-21 00:22  Mr-Victor  阅读(823)  评论(0编辑  收藏  举报

本实例是《杨老师之Blog——COM组件设计与应用(四)》中的实例四,本人实现后并加以注释。

在阅读代码之前,先看一下关于“快捷方式”组件的结构示意图。


   快捷方式组件的接口结构示意图

  从结构图中可以看出,“快捷方式”组件(CLSID_ShellLink),有3个(其实不止)接口,每个接口完成一组相关功能的函数。IShellLink 接口(IID_IShellLink)提供快捷方式的参数读写功能(见下图),IPersistFile 接口(IID_IPersistFile)提供快捷方式持续性文件的读写功能。对象的持续性,是一个非常常用,并且功能强大的接口家族。但今天,我们只要了解其中两函数,就可以了:IPersistFile::Save()和IPersistFile:Load()。


    快捷方式中的各种属性
程序代码:
void CCreateShortcutDlg::OnBnClickedButton1()
{
  ::CoInitialize(NULL);    //COM初始化
 
  CString m_strExe = _T("C:\\Windows\\notepad.exe");    //EXE文件全路径名
  CString m_strLnk = _T("C:\\Users\\Mr.Victor\\Desktop\\我的记事本.lnk");   //快捷方式文件全路径名
  IShellLink *psl = NULL;
  IPersistFile *ppf = NULL;

  HRESULT hr = ::CoCreateInstance(    //启动组件
    CLSID_ShellLink,                        //快捷方式CLSID
    NULL,                         //聚合用
    CLSCTX_INPROC_SERVER,      //进程内(Shell32.dll)服务
    IID_IShellLink,              //IShellLink的IID
    (LPVOID*)&psl                //得到接口指针
  );

  if(SUCCEEDED(hr))
  {
    psl->SetPath(m_strExe);   //全路径程序名(上图中的 目标(T):
//     psl->SetArguments();   //命令行参数
//     psl->SetDescription();   //备注
//     psl->SetHotkey();    //快捷键
//     psl->SetIconLocation();   //图标
//     psl->SetShowCmd();    //窗口尺寸

    //根据EXE的文件名,得到路径名
    TCHAR szWorkPath[MAX_PATH];
    ::lstrcpy(szWorkPath,m_strExe);
    LPTSTR lp = szWorkPath;
    while(*lp)
      lp++;
    CString str = "\\";
    while(str != *lp)
      lp--;
    *lp = 0;

    //设置EXE程序的默认工作目录
    psl->SetWorkingDirectory(szWorkPath);   //上图中的 起始位置(S):

    hr = psl->QueryInterface( //查找持续性文件接口指针
      IID_IPersistFile,          //持续性接口IID
      (LPVOID*)&ppf);        //得到接口指针
   
    if(SUCCEEDED(hr))
    {
      USES_CONVERSION;  //转换为UNICODE字符串
      ppf->Save(T2COLE(m_strLnk),TRUE); //保存
    }
  }

  if(ppf)
    ppf->Release();
  if(psl)
    psl->Release();

  ::CoUninitialize();
}

总结:
ISheelLink

The IShellLink interface has the following methods.

MethodDescription
GetArguments

Gets the command-line arguments associated with a Shell link object.  获得命令行参数

GetDescription

Gets the description string for a Shell link object.  获得描述信息(备注行)

GetHotkey

Gets the keyboard shortcut (hot key) for a Shell link object.  获得快捷键

GetIconLocation

Gets the location (path and index) of the icon for a Shell link object.  获得图标

GetIDList

Gets the list of item identifiers for a Shell link object.  获得快捷方式的目标对象的item identifier list  (Windows外壳中的每个对象如文件,目录和打印机等都有唯一的item identifiler list)

GetPath

Gets the path and file name of a Shell link object.  获得快捷方式的目标文件或目录的全路径

GetShowCmd

Gets the show command for a Shell link object.  获得快捷方式的运行方式,比如常规窗口,最大化

GetWorkingDirectory

Gets the name of the working directory for a Shell link object.  获得工作目录

Resolve

Attempts to find the target of a Shell link, even if it has been moved or renamed.  按照一定的搜索规则试图获得目标对象,即使目标对象已经被删除和移动,重命名

SetArguments

Sets the command-line arguments for a Shell link object. 设置命令行参数

SetDescription

Sets the description for a Shell link object. The description can be any application-defined string. 设置描述信息(备注行)

SetHotkey

Sets a keyboard shortcut (hot key) for a Shell link object. 设置快捷键

SetIconLocation

Sets the location (path and index) of the icon for a Shell link object.  设置图标

SetIDList

Sets the PIDL for a Shell link object.  设置快捷方式的目标对象的item identifier list 

SetPath

Sets the path and file name of a Shell link object. 设置快捷方式的目标文件或目录的全路径

SetRelativePath

Sets the relative path to the Shell link object.

SetShowCmd

Sets the show command for a Shell link object. The show command sets the initial show state of the window.  设置快捷方式的运行方式,比如常规窗口,最大化

SetWorkingDirectory

Sets the name of the working directory for a Shell link object.  设置工作目录

 IPersistFile
IPersistFile主要用到两个成员函数:
1、Save
HRESULT Save(
  [in]  LPCOLESTR pszFileName,      
  [in]  BOOL fRemember    
);
参数:
  pszFileName:The absolute path of the file to which the object should be saved. If pszFileName is NULL, the object should save its data to the current file, if there is one.
  fRemember:Indicates whether the pszFileName parameter is to be used as the current working file. If TRUE, pszFileName becomes the current file and the object should clear its dirty flag after the save. If FALSE, this save operation is a Save A Copy As ... operation. In this case, the current file is unchanged and the object should not clear its dirty flag. If pszFileName is NULL, the implementation should ignore the fRemember flag.
返回值:
  If the object was successfully saved, the return value is S_OK. Otherwise, it is S_FALSE. This method can also return various storage errors.

2、Load
HRESULT Load(
  [in]  LPCOLESTR pszFileName,
  [in]  DWORD dwMode
);
参数:
  pszFileName:The absolute path of the file to be opened.
  dwMode:The access mode to be used when opening the file. Possible values are taken from the STGM enumeration. The method can treat this value as a suggestion, adding more restrictive permissions if necessary. If dwMode is 0, the implementation should open the file using whatever default permissions are used when a user opens the file.
  dwMode可取如下值:
    STGM_READ:只读
    STGM_WRITE:只写
    STGM_READWRITE:读写
返回值:
This method can return the following values.

Return codeDescription
S_OK

The method completed successfully.

E_OUTOFMEMORY

The object could not be loaded due to a lack of memory.

E_FAIL

The object could not be loaded for some reason other than a lack of memory.

 
一般情况操作如下:
  一、初始化COM接口
  二、创建IShellLink对象
  三、从IShellLink对象中获取IPersistFile对象接口
  四、操作IShellLink对象
  五、释放IPersistFile对象接口
  六、释放IShellLink对象
  七、释放COM接口

IShellLink接口获取快捷方式
  IShellLink接口获取指定的快捷方式的参数的函数主要有:GetArguments、GetDescription、GetPath、GetWorkingDirectory等。 
  GetPath用于获取该快捷方式链接的应用程序的地址,
  GetArguments用于获取该程序启动时的参数,
  GetDescription用于获取备注内容。       
  GetWorkingDirectory用于获取起始位置。
  GetPath和GetArguments得结果合起来是快捷方式属性中的目标栏的内容。

图中标注的4个点分别是GetPath、GetArguments、GetWorkingDirectory、GetDescription读取的内容。