网站建设资讯

NEWS

网站建设资讯

在桌面上创建快捷方式

BOOL CreateLink(LPWSTR szPath, LPCWSTR szLink)
{
	HRESULT hres;
	IShellLink * psl;
	IPersistFile* ppf;
	WORD wsz[MAX_PATH];

	CoInitialize(NULL);
	hres = CoCreateInstance(
		CLSID_ShellLink,
		NULL,
		CLSCTX_INPROC_SERVER,
		IID_IShellLink,
		(void **)&psl);

	if (FAILED(hres))
	{
		return FALSE;
	}
	psl->SetPath(szPath);
	psl->SetHotkey(MAKEWORD('M', HOTKEYF_SHIFT | HOTKEYF_CONTROL));

	hres = psl->QueryInterface(IID_IPersistFile, (void**)&ppf);
	if (FAILED(hres))
		return FALSE;

	//MultiByteToWideChar(CP_ACP, 0, szLink, -1, (LPWSTR)&wsz[0], MAX_PATH);

	hres = ppf->Save((LPCOLESTR)szLink, STGM_READWRITE);

	ppf->Release();
	psl->Release();
	
	CoUninitialize();

	return TRUE;
}

VOID  CreateShortCut(CString  strAppName)
{
	TCHAR DeskTopPath[MAX_PATH];
	TCHAR currDirPath[MAX_PATH];
	LPITEMIDLIST pidl = NULL;

	if (SUCCEEDED(SHGetFolderLocation(NULL,
		CSIDL_DESKTOP, NULL, 0, &pidl)))
	{
		SHGetPathFromIDList(pidl, DeskTopPath);
		IMalloc* pMalloc = NULL;
		::SHGetMalloc(&pMalloc);
		pMalloc->Free(pidl);
		pMalloc->Release();
	}

	CString tmp1 = GetApplicationPath2() + strAppName;

	TCHAR szFilename[MAX_PATH];
	TCHAR  szDrive[MAX_PATH];
	TCHAR  szPath[MAX_PATH];
	TCHAR  szExt[MAX_PATH];
	_wsplitpath_s(tmp1, szDrive,255, szPath,255, szFilename,255, szExt,255);

	USES_CONVERSION;
	if (_access(W2A(tmp1), 0) == -1)return;
	
	CString tmp;
	tmp = DeskTopPath;
	tmp += "\\";
	tmp += szFilename;
	tmp += _T(".lnk");
	if (!CreateLink(tmp1.GetBuffer(), tmp))
		AfxMessageBox(_T("创建快捷方式出错!"));

	tmp1.ReleaseBuffer();
}

当前文章:在桌面上创建快捷方式
分享路径:http://cdweb.net/article/iiicsj.html