0.VC(Custom)-创建和解析桌面快捷方式(示例源码)

RT:

创建:

void CreateShortcut(LPCTSTR lpszExe, LPCTSTR lpszLnk)
{
	::CoInitialize(NULL);

	IShellLink *psl = NULL;
	IPersistFile *ppf = NULL;

	HRESULT hr = ::CoCreateInstance(
		CLSID_ShellLink,
		NULL,
		CLSCTX_INPROC_SERVER,
		IID_IShellLink,
		(LPVOID*)&psl);

	if (!SUCCEEDED(hr))
	{
		return;
	}

	psl->SetPath(lpszExe);
	//psl->SetArguments();
	//psl->SetDescription();
	//psl->SetHotkey();
	//psl->SetIconLocation();
	//psl->SetShowCmd();

	TCHAR szWorkPath[MAX_PATH] = {0};
	::lstrcpy(szWorkPath, lpszExe);
	LPTSTR lp = szWorkPath;

	while (*lp)
	{
		lp++;
	}
	while (_T('\\')!=(*lp))
	{
		lp--;
	}
	*lp = 0;

	psl->SetWorkingDirectory(szWorkPath);

	hr = psl->QueryInterface(
		IID_IPersistFile,
		(LPVOID*)&ppf);
	if (!SUCCEEDED(hr))
	{
		return;
	}

	USES_CONVERSION;
	ppf->Save(T2COLE(lpszLnk), TRUE);

	if (ppf)
	{
		ppf->Release();
	}

	if (psl)
	{
		psl->Release();
	}

	::CoUninitialize();
}


解析:

void  GetLinkPath(CString szLink,  CString& szPath) 
{ 
	CoInitialize(NULL); 
	szPath = _T("");

	IShellLink* psl = NULL;   
	TCHAR szGotPath[MAX_PATH] = {0}; 
	WIN32_FIND_DATA wfd = {0}; 

	HRESULT hr = ::CoCreateInstance(
		CLSID_ShellLink,
		NULL,
		CLSCTX_INPROC_SERVER,
		IID_IShellLink,
		(LPVOID*)&psl);

	if (!SUCCEEDED(hr))
	{
		return;
	}

	IPersistFile*  ppf = NULL; 
	hr =  psl->QueryInterface(IID_IPersistFile, (void**)&ppf); 
	if (!SUCCEEDED(hr))
	{
		return;
	}

	hr = ppf->Load(szLink.GetBuffer(), STGM_READ); 
	szLink.ReleaseBuffer();
	if (!SUCCEEDED(hr))
	{
		return;
	}

	hr = psl->Resolve(0, 0); 
	if (!SUCCEEDED(hr))
	{
		return;
	}
			
	hr = psl-> GetPath(szGotPath, 
						MAX_PATH,  
						(WIN32_FIND_DATA*)&wfd, 
						SLGP_SHORTPATH); 
	if (SUCCEEDED(hr))
	{
		szPath = szGotPath;
	}
			
	ppf->Release(); 
	psl->Release(); 
	CoUninitialize(); 
} 


调用:

	CreateShortcut(
		_T("C:\\WINDOWS\\system32\\notepad.exe"),  
		_T("C:\\Documents and Settings\\guoyou.huang\\桌面\\hgy的记事本.lnk")
		);

	CString szPath;
	GetLinkPath(_T("C:\\Documents and Settings\\guoyou.huang\\桌面\\我的记事本.lnk"),
		szPath);


随手写的,标记下,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值