ショートカットを作成する

備忘録です。

面倒な説明はいいから、とにかくWindowsシェルのショートカット(*.lnk)ファイルを作成したい、という方のための参考にどうぞ。

#include <objbase.h>
#include <ShlObj.h>
int main(int argc, char **argv)
{
	HRESULT hr;
	CoInitialize(0);
	IShellLink *pShellLink;
	IPersistFile *pPersistFile;
	hr = CoCreateInstance(CLSID_ShellLink, 0, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&pShellLink);
	if (SUCCEEDED(hr)) {
		hr = pShellLink->QueryInterface(IID_IPersistFile, (void**)&pPersistFile);
		if (SUCCEEDED(hr)) {
			pShellLink->SetPath(L"C:\\Windows\\notepad.exe");
			pShellLink->SetIconLocation(L"C:\\Windows\\notepad.exe", 0);
			pPersistFile->Save(L"C:\\text.lnk", TRUE);
			pPersistFile->Release();
		}
		pShellLink->Release();
	}
	CoUninitialize();
	return 0;
}