VC操作Windows快捷方式(自己总结)
二个操作:新建和解析
主要用到的是COM组件。IShellLink和IPersistFile
需要添加的头函数
shobjidl.h
IPersistFile主要用到两个成员函数:
1、Save。保存内容到文件中去
2、Load。读取
Load的函数原型
HRESULT Load(
LPCOLSTR pszFileName, //快捷方式的文件名,应该是ANSI字符
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接口
//示例代码:
/*
注:在桌面上创建快捷方式,快捷方式的名称为"VC创建的快捷方式",快捷方式指向应用程序"c:\aa.exe"
快捷方式的快捷键:无
快捷方式的工作目录:c:\
快捷方式的备注:VC写写捷方式
快捷方式的运行方式:常规窗口
*/
HRESULT hRet;
IShellLink *pLink; //IShellLink对象指针
IPersistFile * ppf; //IPersisFil对象指针
WCHAR wsz[MAX_PATH]; //定义Unicode字符串
//初始化COM库
hRet = ::CoInitialize(NULL);
if ( hRet != S_OK) //初始化COM库失败,直接返回
{
AfxMessageBox("初始化COM库失败");
return;
}
//创建IShellLink实例
hRet = ::CoCreateInstance(CLSID_ShellLink,NULL,CLSCTX_INPROC_SERVER,IID_IShellLink,(void**)&pLink);
if ( hRet != S_ok)
{
AfxMessageBox("创建IShellLink实例失败");
}
else
{
//创建IShellLink实例成功,继续操作
//从IShellLink对象中获取IPersistFile接口
hRet = pLink->QueryInterface(IID_IpersistFile,(void**)&ppf);
if ( hRet != S_OK)
{
AfxMessageBox("获取IPersistFile接口失败");
}
else
{
//获取接口成功,继续操作
//设置快捷方式中的程序路径
pLink->SetPath("c:\\aa.exe");
//设置快捷方式的工作目录
pLink->SetWorkingDirectory("c:\\");
//确保快捷方式路径由ANSI字符串组成
MultiByteToWideChar(
CP_ACP,
0,
"C:\\Documents and Settings\\hjs\\桌面\\创建的快捷方式.lnk",
-1,
wsz,
MAX_PATH
);
//保存快捷方式
ppf->Save(wsz,TRUE);
}
}
//释放IPersistFile接口
ppf->Release();
//释放IShellLink对象
pLink->Release();
//释放COM接口
::CoUninitialize();
//创建快捷方式
BOOL CreateLink (LPSTR szPath,//快捷方式的目标应用程序名
LPSTR szLink)//快捷方式的数据文件名(*.lnk)
{
HRESULT hres ;
IShellLink * psl ;
IPersistFile* ppf ;
WORD wsz[ MAX_PATH] ;
//创建一个IShellLink实例
hres = CoCreateInstance( CLSID_ShellLink,
NULL,
CLSCTX_INPROC_SERVER,
IID_IShellLink,
(void **)&psl) ;
if( FAILED( hres))
return FALSE ;
//设置目标应用程序
psl -> SetPath( szPath) ;
//设置快捷键(此处设为Shift+Ctrl+'R')
psl -> SetHotkey( MAKEWORD('R',HOTKEYF_SHIFT ¦HOTKEYF_CONTROL) );
//从IShellLink获取其IPersistFile接口
//用于保存快捷方式的数据文件 (*.lnk)
hres = psl -> QueryInterface( IID_IPersistFile, (void**)&ppf );
if( FAILED( hres))
return FALSE ;
// 确保数据文件名为ANSI格式
MultiByteToWideChar( CP_ACP, 0, szLink, -1, wsz, MAX_PATH) ;
//调用IPersistFile::Save
//保存快捷方式的数据文件 (*.lnk)
hres = ppf -> Save( wsz, STGM_READWRITE );
//释放IPersistFile和IShellLink接口
ppf -> Release( ) ;
psl -> Release( ) ;
return TRUE;
}
//解析快捷方式
HRESULT ResolveIt(HWND hwnd, LPCSTR lpszLinkFile, LPSTR lpszPath)
{
HRESULT hres;
IShellLink* psl;
char szGotPath[MAX_PATH];
char szDescription[MAX_PATH];
WIN32_FIND_DATA wfd;
*lpszPath = 0; //Sev assume failure
//Sev Get a pointer to the IShellLink interface.
hres = CoCreateInstance(&CLSID_ShellLink, NULL,
CLSCTX_INPROC_SERVER, &IID_IShellLink, &psl);
if (SUCCEEDED(hres)) {
IPersistFile* ppf;
//Sev Get a pointer to the IPersistFile interface.
hres = psl->lpVtbl->QueryInterface(psl, &IID_IPersistFile,
&ppf);
if (SUCCEEDED(hres)) {
WORD wsz[MAX_PATH];
//Sev Ensure that the string is Unicode.
MultiByteToWideChar(CP_ACP, 0, lpszLinkFile, -1, wsz,
MAX_PATH);
//Sev 加载快捷方式.
hres = ppf->lpVtbl->Load(ppf, wsz, STGM_READ);
if (SUCCEEDED(hres)) {
//Sev 解析快捷方式.
hres = psl->lpVtbl->Resolve(psl, hwnd, SLR_ANY_MATCH);
if (SUCCEEDED(hres)) {
//Sev Get the path to the link target.
hres = psl->lpVtbl->GetPath(psl, szGotPath,
MAX_PATH, (WIN32_FIND_DATA *)&wfd,
SLGP_SHORTPATH );
if (!SUCCEEDED(hres)
HandleErr(hres); //Sev application-defined function
//Sev Get the description of the target.
hres = psl->lpVtbl->GetDescription(psl,
szDescription, MAX_PATH);
if (!SUCCEEDED(hres))
HandleErr(hres);
lstrcpy(lpszPath, szGotPath);
}
}
//Sev Release the pointer to the IPersistFile interface.
ppf->lpVtbl->Release(ppf);
}
//Sev Release the pointer to the IShellLink interface.
psl->lpVtbl->Release(psl);
}
return hres;
}