两个用到的com接口
1、IShellLink
IShellLink接口函数:
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 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. |
GetPath | Gets the path and file name of a Shell link object. |
GetShowCmd | Gets the show command for a Shell link object.(SW_SHOWNORMAL、SW_SHOWMAXIMIZED、SW_SHOWMINNOACTIVE) |
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 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 pointer to an item identifier list (PIDL) for a Shell link object. |
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. |
注:
HRESULT SetHotkey(WORD
wHotkey
);
这个WORD值,低位上是
virtual key code
,高位上可以是ALT,CTRL等组合键。可以用MAKEWORD(low, high)来生成一个WORD,如Ctrl+F12可以用MAKEWORD(VK_F12, HOTKEYF_CONTROL)表示。
2、IPersistFile
IPersistFile接口函数:
IsDirty | Checks an object for changes since it was last saved to its current file. |
Load | Opens the specified file and initializes an object from the file contents. |
Save | Saves the object into the specified file. |
SaveCompleted | Notifies the object that it can revert from NoScribble mode to Normal mode. |
GetCurFile | Gets the current name of the file associated with the object. |
示例:
/*
函数功能:对指定文件在指定的目录下创建其快捷方式
函数参数:
lpszFileName 指定文件,为NULL表示当前进程的EXE文件。(全路径)
lpszLnkFileDir 指定目录,不能为NULL。
lpszLnkFileName 快捷方式名称,为NULL表示EXE文件名。(仅文件名)
lpszWorkDir 工作目录
wHotkey 为0表示不设置快捷键
pszDescription 备注
iShowCmd 运行方式,默认为常规窗口
lpszIconPath 图标路径
lpszParam 命令行参数
*/
BOOL CreateFileShortcut(LPCTSTR lpszFileName, LPCTSTR lpszLnkFileDir, LPCTSTR lpszLnkFileName, LPCTSTR lpszWorkDir, WORD wHotkey, LPCTSTR lpszDescription, int iShowCmd, LPCTSTR lpszIconPath, LPCTSTR lpszParam)
{
if (lpszLnkFileDir == NULL)
return FALSE;
HRESULT hr;
IShellLink *pLink; //IShellLink对象指针
IPersistFile *ppf; //IPersisFil对象指针
::CoInitialize(NULL);
//创建IShellLink对象
hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&pLink);
if (FAILED(hr))
return FALSE;
//从IShellLink对象中获取IPersistFile接口
hr = pLink->QueryInterface(IID_IPersistFile, (void**)&ppf);
if (FAILED(hr))
{
pLink->Release();
return FALSE;
}
TCHAR szFile[MAX_PATH] = {0};
GetModuleFileName(NULL, szFile, ARRAYSIZE(szFile));
//目标
if (lpszFileName == NULL)
{
pLink->SetPath(szFile);
}
else
pLink->SetPath(lpszFileName);
//工作目录
if (lpszWorkDir != NULL)
pLink->SetPath(lpszWorkDir);
//快捷键
if (wHotkey != 0)
pLink->SetHotkey(wHotkey);
//备注
if (lpszDescription != NULL)
pLink->SetDescription(lpszDescription);
//显示方式
pLink->SetShowCmd(iShowCmd);
/设置图标
if(lpszIconPath && lpszIconPath[0])
pLink->SetIconLocation(lpszIconPath, 0);
//设置参数
if(lpszParam)
pLink->SetArguments(lpszParam);
//快捷方式的路径 + 名称
TCHAR szBuffer[MAX_PATH];
if (lpszLnkFileName != NULL) //指定了快捷方式的名称
_sntprintf_s(szBuffer, ARRAYSIZE(szBuffer), _TRUNCATE, _T("%s\\%s"), lpszLnkFileDir, lpszLnkFileName);
else
{
//没有指定名称,就从取指定文件的文件名作为快捷方式名称。
TCHAR szPEFileName[MAX_PATH] = {0};
if (lpszFileName != NULL)
_tcsncpy_s(szPEFileName, ARRAYSIZE(szPEFileName), lpszFileName, _TRUNCATE);
else
_tcsncpy_s(szPEFileName, ARRAYSIZE(szPEFileName), szFile, _TRUNCATE);
TCHAR* szFileNameEnd = NULL;
szFileNameEnd = PathFindFileName(szPEFileName);
if (szFileNameEnd == NULL)
{
ppf->Release();
pLink->Release();
return FALSE;
}
//注意后缀名要从.exe改为.lnk
_sntprintf_s(szBuffer, ARRAYSIZE(szBuffer), _TRUNCATE,_T("%s\\%s"), lpszLnkFileDir, szFileNameEnd);
int nLen = _tcslen(szBuffer);
szBuffer[nLen - 3] = 'l';
szBuffer[nLen - 2] = 'n';
szBuffer[nLen - 1] = 'k';
}
//保存快捷方式到指定目录下
hr = ppf->Save(szBuffer, TRUE);
ppf->Release();
pLink->Release();
CoUninitialize();
return SUCCEEDED(hr);
}