win32创建快捷方式

来源:互联网 发布:网络游戏server编程 编辑:程序博客网 时间:2024/06/05 08:45
VC操作Windows快捷方式

主要有两个操作:新建和解析主要用到的是COM组件。IShellLink和IPersistFile需要添加的头函数shobjidl.hIPersistFile主要用到两个成员函数:
1、Save。保存内容到文件中去
2、Load。读取Load的函数原型 

HRESULT Load( LPCOLSTR pszFileName, //快捷方式的文件名
                        DWORD dwMode            //读取方式);
dwMode可取如下值:
STGM_READ:只读
STGM_WRITE:只写
STGM_READWRITE:读写

IShellLink主要成员:
1、GetArguments:获得参数信息
2、GetDescription:获得描述信息(备注行)
3、GetHotkey:获得快捷键
4、GetIconLocation:获得图标
5、GetIDList:获得快捷方式的目标对象的item identifier list (Windows外壳中的每个对象如文件,目录和打印机等都有唯一的item identifiler list)
6、GetPath: 获得快捷方式的目标文件或目录的全路径
7、GetShowCmd:获得快捷方式的运行方式,比如常规窗口,最大化
8、GetWorkingDirectory:获得工作目录
9、Resolve:按照一定的搜索规则试图获得目标对象,即使目标对象已经被删除或移动,重命名下面是对应信息的设置方法
10、SetArguments
11、SetDescription
12、SetHotkey
13、SetIconLocation
14、SetIDList
15、SetPath
16、SetRelativePat
17、SetShowCmd
18、SetWorkingDirectory

常见操作:
一、初始化COM接口
二、创建IShellLink对象
三、从IShellLink对象中获取IPersistFile对象接口
四、操作IShellLink对象
五、释放IPersistFile对象接口

六、释放IShellLink对象

七、释放COM接口

<span style="font-family:Microsoft YaHei;">// 创建快捷方式BOOL creat_shortcut(IN const CString &szSrcFilePath,    IN const CString &szShortcutPath,   IN const CString &szParam = _T("")){// 初始化COMCoInitialize(NULL);HRESULT hr;IShellLink *psl;IPersistFile *ppf;// 创建COM实例并获取IShellLink接口hr = ::CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&psl);if( FAILED( hr ) ){return FALSE;}// 设置源文件路径psl->SetPath( szSrcFilePath.AllocSysString() );// 设置参数if (!szParam.IsEmpty()){psl->SetArguments(szParam);}// 获取文件接口hr = psl->QueryInterface(IID_IPersistFile, (void**)&ppf);if( FAILED( hr) ){return FALSE;}// 保存文件快捷方式hr = ppf->Save( szShortcutPath, STGM_READWRITE );if ( FAILED( hr ) ){return FALSE;}// 释放接口ppf->Release();psl->Release();// 卸载COM::CoUninitialize();return TRUE;}</span>


0 0