这里给出一个创建快捷方式的例子,是使用IShellLink和IPersistFile两个COM接口实现的。
例子代码如下:
CFileDialog openDlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_EXPLORER );
if (IDCANCEL==openDlg.DoModal())
{
return;
}
CString csFile, csFileLnk, csDirectory;
csFile = openDlg.GetPathName();
csFileLnk = csFile.Left(csFile.GetLength()-openDlg.GetFileExt().GetLength()-1);
csFileLnk += _T(".lnk");
csDirectory = csFile.Left(csFile.GetLength()-openDlg.GetFileName().GetLength());
HRESULT hres=NULL;
IShellLink* psl;
// Get a pointer to the IShellLink interface.
hres = CoInitialize(NULL);
if (!SUCCEEDED(hres))
{
AfxMessageBox(_T("COM initialized failed."));
return;
}
hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl);
if (!SUCCEEDED(hres))
{
AfxMessageBox(_T("Create instance failed."));
CoUninitialize();
return;
}
psl->SetDescription(_T("测试快捷方式"));
psl->SetIconLocation(csFile, 0);
psl->SetPath(csFile);
psl->SetShowCmd(SW_SHOWNORMAL);
psl->SetWorkingDirectory(csDirectory);
IPersistFile* ppf;
hres = psl->QueryInterface(IID_IPersistFile, (void**)&ppf);
if (!SUCCEEDED(hres))
{
AfxMessageBox(_T("Query interface failed."));
CoUninitialize();
return;
}
WCHAR wsz[MAX_PATH];
MultiByteToWideChar(CP_ACP,
0,
_T(csFileLnk),
-1,
wsz,
MAX_PATH);
hres = ppf->Save(wsz, FALSE);
if (!SUCCEEDED(hres))
{
AfxMessageBox(_T("Save failed."));
}
ppf->Release();
psl->Release();
CoUninitialize();
AfxMessageBox(_T("创建成功!"));
-------------------------------------------------Other------------------------------------------------------
void CCsdn9Dlg::OnOK()
{
// TODO: Add extra validation here
//CreateStreamOnHGlobal
//IPicture::Save
LPBYTE lpBits=NULL;
long size=0;
HICON hicon=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_ICON1));
PICTDESC pdiconsrc;
pdiconsrc.cbSizeofstruct=sizeof(PICTDESC);
pdiconsrc.icon.hicon=hicon;
pdiconsrc.picType=PICTYPE_ICON;
IPicture* pIPicture=NULL;
HRESULT hr;
IDispatch* pDisp=NULL;
hr = OleCreatePictureIndirect(&pdiconsrc,
IID_IDispatch,
TRUE,
(void**)&pDisp);
if(SUCCEEDED(hr))
{
hr = pDisp->QueryInterface(&pIPicture);
if(SUCCEEDED(hr))
{
lpBits = (LPBYTE) GlobalAlloc(GMEM_MOVEABLE|GMEM_NODISCARD, 64*1024);
IStream* pStream;
CreateStreamOnHGlobal(lpBits,false,&pStream);
hr=pIPicture->SaveAsFile(pStream,false,&size);
if(pStream)
pStream->Release();
if(SUCCEEDED(hr)&&size>0)
{
//you can use
char* pchar=(char*)lpBits;
//icon is stored in pchar, length is size
//to create another hicon
//use OleLoadPicture(pStream,...)
//IPicture::get_Handle to get the icon handle
}
pIPicture->Release();
if(lpBits)
GlobalFree((HGLOBAL)lpBits);
}
pDisp->Release();
}
}
void SaveIcon(HICON hIconToSave, LPCTSTR sIconFileName)
{
if(hIconToSave==NULL || sIconFileName==NULL)
return;
//warning: this code snippet is not bullet proof.
//do error check by yourself [masterz]
PICTDESC picdesc;
picdesc.cbSizeofstruct = sizeof(PICTDESC);
picdesc.picType = PICTYPE_ICON ;
picdesc.icon.hicon = hIconToSave;
IPicture* pPicture=NULL;
OleCreatePictureIndirect(&picdesc, IID_IPicture, TRUE,(VOID**)&pPicture);
LPSTREAM pStream;
CreateStreamOnHGlobal(NULL,TRUE,&pStream);
LONG size;
HRESULT hr=pPicture->SaveAsFile(pStream,TRUE,&size);
char pathbuf[1024];
strcpy(pathbuf,sIconFileName);
CFile iconfile;
iconfile.Open(pathbuf, CFile::modeCreate|CFile::modeWrite);
LARGE_INTEGER li;
li.HighPart =0;
li.LowPart =0;
ULARGE_INTEGER ulnewpos;
pStream->Seek( li,STREAM_SEEK_SET,&ulnewpos);
ULONG uReadCount = 1;
while(uReadCount>0)
{
pStream->Read(pathbuf,sizeof(pathbuf),&uReadCount);
if(uReadCount>0)
iconfile.Write(pathbuf,uReadCount);
}
pStream->Release();
iconfile.Close();
}
博客给出使用IShellLink和IPersistFile两个COM接口创建快捷方式的例子,包含选择文件、初始化COM、创建实例、设置快捷方式属性、保存等步骤,并给出了详细的代码实现,最后提示创建成功。





