创建桌面快捷方式

BOOL CreateDesktopShotCut( CString strName,//在桌面显示的名称 
						  CString strSourcePath)//文件绝对路径名称
{
	if(FAILED(CoInitialize(NULL)))
		return FALSE;

	CString strDestDir;
	int i=CSIDL_DESKTOPDIRECTORY;

	LPITEMIDLIST pidl;
	LPMALLOC pShell;
	char Path[MAX_PATH+1];
	if(SUCCEEDED(SHGetMalloc(&pShell)) && SUCCEEDED(SHGetSpecialFolderLocation(NULL,i,&pidl)))
	{
		if(!SHGetPathFromIDList(pidl,Path))
		{
			pShell->Free(pidl);
			::CoUninitialize();
			return FALSE;
		}

		pShell->Release();
		strDestDir.Format("%s",Path);
		strDestDir=strDestDir+"\\" +strName+".lnk";
		IShellLink* psl;
		if(SUCCEEDED(CoCreateInstance(CLSID_ShellLink,NULL,CLSCTX_INPROC_SERVER,IID_IShellLink,(LPVOID*)&psl)))
		{
			psl->SetPath(strSourcePath);
			IPersistFile* ppf;

			WCHAR wsz[MAX_PATH];
			MultiByteToWideChar(CP_THREAD_ACP,MB_PRECOMPOSED,strDestDir,-1,wsz,MAX_PATH);
			if(SUCCEEDED(psl->QueryInterface(IID_IPersistFile,(LPVOID*)&ppf)) && SUCCEEDED(ppf->Save(wsz,TRUE)))
			{
				ppf->Release();
				psl->Release();
				::CoUninitialize();
				return TRUE;
			}
			else
			{
				ppf->Release();
				psl->Release();
				::CoUninitialize();
				return FALSE;
			}
		}
		else
		{
			::CoUninitialize();
			return FALSE;
		}
	}
	else
	{
		::CoUninitialize();
		return FALSE;
	}

	return FALSE;
}

void ChelloDlg::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	CreateDesktopShotCut( "我的快捷方式","F:\\TesT.exe"); //文件绝对路径名称
}


你可能感兴趣的:(thread,server,null,Path)