朱皮特的博客 自由的飞翔

快捷方式拾遗

2010-05-13
朱皮特
阅读量:

如何在快速启动栏创建快捷方式

HRESULT CreateShortcutToURL(LPCSTR pszURL, LPCSTR pszLinkFile);
HRESULT CreateInterShortcut (LPCSTR pszURL, LPCSTR pszURLfilename,
LPCSTR szDescription,LPCTSTR szIconFile = NULL,int nIndex = -1);

HRESULT CreateShortcutToURL(LPCSTR pszURL, LPCSTR pszLinkFile)
{
IUniformResourceLocator pURL=NULL; // Create an IUniformResourceLocator object
HRESULT hRes=CoCreateInstance( CLSID_InternetShortcut, NULL, CLSCTX_INPROC_SERVER, IID_IUniformResourceLocator, (LPVOID
)&pURL );
if(SUCCEEDED(hRes)){
IPersistFile*pPF=NULL;
hRes=pURL->SetURL(pszURL,0);
if(SUCCEEDED(hRes)){
WCHAR wsz [MAX_PATH]; //buffer for Unicode string
// Ensure that the string consists of ANSI characters.
MultiByteToWideChar(CP_ACP, 0, pszLinkFile, -1, wsz, MAX_PATH); hRes=pURL->QueryInterface(IID_IPersistFile, (void**)&pPF);
if(SUCCEEDED(hRes)){
//Save the shortcut via the IPersistFile::Save member function.
hRes=pPF->Save(wsz,TRUE); // Release the pointer to IPersistFile.
pPF->Release();
}
}
//Release the pointer to IUniformResourceLocator
pURL->Release();
}
return hRes;
}

//another method c:\winnt
//CoInitialize(NULL);
HRESULT CreateInterShortcut(LPCSTR pszURL, LPCSTR pszURLfilename, LPCSTR szDescription, LPCTSTR szIconFile, int nIndex)
{
IUniformResourceLocator *pHook;

HRESULT hres=CoCreateInstance(CLSID_InternetShortcut,NULL,CLSCTX_INPROC_SERVER,
IID_IUniformResourceLocator,(void*)&pHook);
if(SUCCEEDED(hres)){
IPersistFile
ppf;
IShellLink*psl;

//Query IShellLink for the IPersistFile interface for
hres=pHook->QueryInterface(IID_IPersistFile,(void)&ppf);
hres=pHook->QueryInterface(IID_IShellLink,(void
)&psl);
if(SUCCEEDED(hres)){
WCHAR wsz[MAX_PATH]; //buffer for Unicode string
//Set the path to the shortcut target.
pHook->SetURL(pszURL,0);

hres=psl->SetIconLocation(szIconFile,nIndex);
if(SUCCEEDED(hres)){
//Set the description of the shortcut.
hres=psl->SetDescription(szDescription);
if(SUCCEEDED(hres)){
//Ensure that the string consists of ANSI characters.
MultiByteToWideChar(CP_ACP,0,pszURLfilename,-1,wsz,MAX_PATH);
//Save the shortcut via the IPersistFile::Save member function.
hres=ppf->Save(wsz,TRUE);
}
}

//Release the pointer to IPersistFile.
ppf->Release();
psl->Release();
}

//Release the pointer to IShellLink.
pHook->Release();
}

return hres;
}

//HRESULT hRes = CreateShortcutToURL(“http://support.microsoft.com”,”c:\mssupport.url”);
HRESULT hRes =CreateInterShortcut(“http://www.baidu.com/”, “d:\myblog.url”,”大星星的空间”,”c:\1.ico”,0); if (SUCCEEDED(hRes))
{
//do something }


下一篇 tesseract初探

Comments

Content