建立程序的桌面快捷方式

void filesys::cIShellLink(WCHAR* path, CString strTemp)
{
	::CoInitialize(NULL);
	//创建2个接口
	CComPtr<IShellLink> spShellLink;
	HRESULT hr=spShellLink.CoCreateInstance(CLSID_ShellLink);
	CComPtr<IPersistFile> spPersistFile;

	//取得此EXE的文件名及路径
	CString FileName;
	FileName.Format(_T("%s\\Civ4BeyondSword.exe"), path);
	hr=spShellLink->SetPath(FileName);

	CString ArgumentsName;
	TCHAR *ModName = strTemp.GetBuffer(strTemp.GetLength());
	strTemp.ReleaseBuffer();

	//加exe之后的加参数
	ArgumentsName.Format(_T("mod=\\%s"), ModName);
	hr=spShellLink->SetArguments(ArgumentsName);

	hr=spShellLink->SetWorkingDirectory(path);
	hr=spShellLink->SetDescription(ModName);
	hr=spShellLink->QueryInterface(IID_IPersistFile, (void **)&spPersistFile);

	//取得桌面的全路径
	LPITEMIDLIST pidl;
	hr = SHGetSpecialFolderLocation(NULL, CSIDL_DESKTOP, &pidl);
	TCHAR szPath[1024];
	SHGetPathFromIDList(pidl, szPath);

	//替换成当前登陆的用户名
	CString Cstr1,Cstr2,Cstr3;
	Cstr1 = szPath;
	Cstr2.Format(_T("\\%s\\"), functions::Instance().GetProcessUser());
	Cstr3.Format(_T("\\%s\\"), functions::Instance().GetLoginUser());
	Cstr1.Replace(Cstr2, Cstr3);

	//创建快捷方式
	CComBSTR strLinkFilePath( Cstr1 );

	CString ShellLinkFileName;
	ShellLinkFileName.Format(_T("\\%s.lnk"),strTemp);
	strLinkFilePath.Append(ShellLinkFileName);

	hr=spPersistFile->Save(strLinkFilePath, TRUE);
	spPersistFile.Release();
	spShellLink.Release();
	::CoUninitialize();

}

下边可能要加,唔记得了。。。

#include <Wtsapi32.h>
#pragma comment(lib, "Wtsapi32.lib")

#include "psapi.h"
#pragma comment(lib, "psapi.lib")
CString functions::GetProcessUser()
{
	HANDLE hToken = NULL;
	TOKEN_USER *pTokenUser = NULL;
	CString re;

	if( OpenProcessToken(::GetCurrentProcess(), TOKEN_QUERY, &hToken) )
	{
		DWORD dwNeedLen = 0;
		GetTokenInformation(hToken, TokenUser, NULL, 0, &dwNeedLen);
		if (0 < dwNeedLen)
		{
			pTokenUser = (TOKEN_USER*)new BYTE[dwNeedLen];
			if ( GetTokenInformation(hToken, TokenUser, pTokenUser, dwNeedLen, &dwNeedLen) )
			{
				SID_NAME_USE sn;
				WCHAR szDomainName[MAX_PATH];
				DWORD dwDmLen = MAX_PATH;
				WCHAR szUserName[MAX_PATH];
				DWORD nNameLen = MAX_PATH;
				LookupAccountSid(NULL, pTokenUser->User.Sid, szUserName, &nNameLen, szDomainName, &dwDmLen, &sn);
				re = szUserName;
			}
		}
	}

	if (hToken)
		::CloseHandle(hToken);
	if (pTokenUser)
		delete[] (WCHAR*)pTokenUser;

	return re;
}

CString functions::GetLoginUser()
{
	WCHAR* szLogName;
	DWORD dwSize;
	CString re;
	if (WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSUserName, &szLogName, &dwSize))
	{
		re = szLogName;
		WTSFreeMemory(szLogName);
	}
	return re;
}
VC++