MFC 创建快捷方式的两种方法

方法一:(不修改shell32.dll,不被360拦截)

void CreateLinkThenChangeIcon(CString fname_to_create_link, CString lnk_fname)
{//程序路径,快捷方式名称
	HRESULT hr = CoInitialize(NULL);
	if (SUCCEEDED(hr))
	{
		IShellLink *pisl;
		hr = CoCreateInstance(CLSID_ShellLink, NULL,
			CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&pisl);
		if (SUCCEEDED(hr))
		{
			IPersistFile* pIPF;
			pisl->SetPath(fname_to_create_link);
			hr = pisl->QueryInterface(IID_IPersistFile, (void**)&pIPF);
			if (SUCCEEDED(hr))
			{
				CString csStartPath;
				SHGetSpecialFolderPath(NULL,(LPWSTR)(LPCTSTR)csStartPath, CSIDL_DESKTOP, 0);
				CString csLnkPath;
				csLnkPath.Format(_T("%s\\%s"),csStartPath,lnk_fname);
				USES_CONVERSION;
				LPCOLESTR lpOleStr = csLnkPath;
				pIPF->Save(lpOleStr, FALSE);
				pIPF->Release();
			}
			pisl->Release();
		}
		CoUninitialize();
	}
}

方法二:(修改shell32.dll,被360拦截提示)

void CreateLinkThenChangeIcon(CString fname_to_create_link, CString lnk_fname)
{//程序路径,快捷方式名称
	CoInitialize( NULL );
	HRESULT hres;
	IShellLink *psl = NULL;
	IPersistFile *pPf = NULL;
	TCHAR buf[256];
	int id;
	LPITEMIDLIST pidl;
	hres = CoCreateInstance(CLSID_ShellLink,NULL,CLSCTX_INPROC_SERVER,IID_IShellLink,(LPVOID*)&psl);
	if(FAILED(hres))
		goto cleanup;
	hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&pPf);
	if(FAILED(hres))
		goto cleanup;
	hres = psl->SetPath(fname_to_create_link);
	if(FAILED(hres))
		goto cleanup;
	//place the shortcut on the desktop
	SHGetSpecialFolderLocation(NULL, CSIDL_DESKTOP, &pidl);
	SHGetPathFromIDList(pidl, buf);
	lstrcat(buf,_T("\\"));
	lstrcat(buf,lnk_fname);
	hres = pPf->Save((LPCOLESTR)buf, TRUE);

	if(FAILED(hres))
		goto cleanup;

	 GetSystemDirectory(buf, 256);
	 lstrcat(buf,_T("\\shell32.dll"));
	 hres = psl->SetIconLocation(buf, 1);
	 if(FAILED(hres))
	 	goto cleanup;

	hres = psl->GetIconLocation(buf, 256, &id);

	if(FAILED(hres))
		goto cleanup;

	pPf->Save((LPCOLESTR)buf, TRUE);

cleanup:
	if(pPf)pPf->Release();
	if(psl)psl->Release();

	CoUninitialize();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值