windows shell 创建快捷方式和读取快捷方式的代码

不说了 直接贴代码:
#include <windows.h>
#include <ShlObj.h>
#include <comutil.h>

#pragma comment(lib, "comsuppw.lib") 

struct SHORTCUTSTRUCT
{
	LPTSTR pszTarget;
	LPTSTR pszDesc;
	WORD wHotKey;
	LPTSTR pszIconPath;
	WORD wIconIndex;
};
typedef SHORTCUTSTRUCT* LPSHORTCUTSTRUCT;


HRESULT SHCreateShortCut(LPCTSTR, LPSHORTCUTSTRUCT);
HRESULT SHResolveShortcut(LPCTSTR, LPSHORTCUTSTRUCT);

int WINAPI WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in LPSTR lpCmdLine, __in int nShowCmd )
{
	/*char * szchar = "xxxxxxx";//这段代码是为了测试这个ConvertStringToBSTR
	BSTR szbstr = _com_util::ConvertStringToBSTR(szchar);
	SysFreeString(szbstr); 
	MessageBox(NULL,szbstr,TEXT("Test"),MB_OK);*/
	CoInitialize(NULL);
	SHORTCUTSTRUCT shortcut;
	shortcut.pszTarget = TEXT("C:\\Users\\Administrator\\Desktop\\(Un)RegisterOcxDll.exe");
	shortcut.pszDesc = TEXT("(Un)RegisterOcxDll.exe");
	shortcut.wHotKey = 1;
	shortcut.pszIconPath = NULL;
	shortcut.wIconIndex = 1;

	SHCreateShortCut(TEXT("C:\\Users\\Administrator\\Desktop\\123.lnk"),&shortcut);
	CoUninitialize();

}

HRESULT SHCreateShortCut(LPCTSTR szLnkFile, LPSHORTCUTSTRUCT lpss)
{
	WCHAR wszLnkFile[MAX_PATH] = {0};
	IShellLink* pShellLink = NULL;
	IPersistFile* pPF = NULL;
	// 验证SHORTCUTSTRUCT指针
	if(lpss == NULL)
		return E_FAIL;
	// 建立COM服务器,假设CoInitialize()已经被调用
	HRESULT hr = CoCreateInstance(CLSID_ShellLink, NULL,
		CLSCTX_INPROC_SERVER, IID_IShellLink,
		reinterpret_cast<LPVOID*>(&pShellLink));
	if(FAILED(hr))
		return hr;
	//设置属性
	pShellLink->SetPath(lpss->pszTarget);
	pShellLink->SetDescription(lpss->pszDesc);
	pShellLink->SetHotkey(lpss->wHotKey);
	pShellLink->SetIconLocation(lpss->pszIconPath, lpss->wIconIndex);
	//取得IPersistFile接口
	hr = pShellLink->QueryInterface(
		IID_IPersistFile, reinterpret_cast<LPVOID*>(&pPF));
	if(FAILED(hr))
	{
		pShellLink->Release();
		return hr;
	}
	// 保存LNK(Unicode名)
	MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szLnkFile, -1, wszLnkFile, MAX_PATH);
	hr = pPF->Save(wszLnkFile, TRUE);
	// 清理
	pPF->Release();
	pShellLink->Release();
	return hr;
}

HRESULT SHResolveShortcut(LPCTSTR szLnkFile, LPSHORTCUTSTRUCT lpss)
{
	WCHAR wszLnkFile[MAX_PATH] = {0};
	IShellLink* pShellLink = NULL;
	IPersistFile* pPF = NULL;
	// 建立合适的COM服务器
	HRESULT hr = CoCreateInstance(CLSID_ShellLink, NULL,CLSCTX_INPROC_SERVER,
		IID_IShellLink,reinterpret_cast<LPVOID*>(&pShellLink));
	if(FAILED(hr))
		return hr;
	// 取得装入.LNK 文件的IPersistFile接口
	hr = pShellLink->QueryInterface(IID_IPersistFile,
		reinterpret_cast<LPVOID*>(&pPF));
	if(FAILED(hr))
	{
		pShellLink->Release();
		return hr;
	}
	// 装入快捷方式(Unicode 名)
	MultiByteToWideChar(CP_ACP, 0, szLnkFile, -1, wszLnkFile, MAX_PATH);
	hr = pPF->Load(wszLnkFile, STGM_READ);
	if(FAILED(hr))
	{
		pPF->Release();
		pShellLink->Release();
		return hr;
	}
	// 解析连接
	hr = pShellLink->Resolve(NULL, SLR_ANY_MATCH);
	if(FAILED(hr))
	{
		pPF->Release();
		pShellLink->Release();
		return hr;
	}
	// 抽取信息,充填到lpss
	if(lpss != NULL)
	{
		TCHAR szPath[MAX_PATH] = {0};
		TCHAR szDesc[MAX_PATH] = {0};
		TCHAR szIcon[MAX_PATH] = {0};
		WORD w = 0;
		WORD wIcon = 0;
		WIN32_FIND_DATA wfd;
		pShellLink->GetPath(szPath, MAX_PATH, &wfd, SLGP_SHORTPATH);
		pShellLink->GetDescription(szDesc, MAX_PATH);
		pShellLink->GetHotkey(&w);
		pShellLink->GetIconLocation(szIcon, MAX_PATH,
			reinterpret_cast<int*>(&wIcon));
		lpss->pszTarget = szPath;
		lpss->pszDesc = szDesc;
		lpss->pszIconPath = szIcon;
		lpss->wHotKey = w;
		lpss->wIconIndex = wIcon;
	}
	pPF->Release();
	pShellLink->Release();
	return hr;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

iot-genius

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值