怎样创建一个快捷方式

void __fastcall TMainForm::Shortcut_Log( AnsiString LinkName, AnsiString LinkFile )
{
IShellLinkA* pLink;
IPersistFile* pPersistFile;

if(SUCCEEDED(CoInitialize(NULL)))
{
if(SUCCEEDED(CoCreateInstance(CLSID_ShellLink, NULL,
CLSCTX_INPROC_SERVER,
IID_IShellLink, (void **) &pLink)))
{
pLink->SetPath( LinkFile.c_str() );
//pLink->SetDescription( SoftExplain.SubString(2, SoftExplain.Length() - 1 ).c_str() );
pLink->SetWorkingDirectory( ExtractFileDir( LinkFile ).c_str() );
pLink->SetShowCmd(SW_SHOW);

if(SUCCEEDED(pLink->QueryInterface(IID_IPersistFile,
(void **)&pPersistFile)))
{

WideString strShortCutLocation;
strShortCutLocation = WideString( LinkName ) + ".lnk";
pPersistFile->Save(strShortCutLocation.c_bstr(), TRUE);
pPersistFile->Release();
}
pLink->Release();
}
CoUninitialize();
}
}
专访张慧华:厨师、程序员到J-UI联合创始人对我有用[0] 丢个板砖[0] 引用 | 举报 | 管理

Libran
Libran
等级:
2
#3 得分:10 回复于: 2002-10-25 11:35:20
转载:
下面的例子代码演示了怎样创建一个快捷方式。在这个例子里,这个快捷方式保存在C:Drive目录下。

//------------------------------------------------------------------include <shlobj.h>

void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(OpenDialog1->Execute())
CreateShortCut(OpenDialog1->FileName);
}
//------------------------------------------------------------------void TForm1::CreateShortCut(const AnsiString &file)
{
IShellLink* pLink;
IPersistFile* pPersistFile;
if(SUCCEEDED(CoInitialize(NULL)))
{
if(SUCCEEDED(CoCreateInstance(CLSID_ShellLink, NULL,
CLSCTX_INPROC_SERVER,
IID_IShellLink, (void **) &pLink)))
{
pLink->SetPath(file.c_str());
pLink->SetDescription("Woo hoo, look at Homer's shortcut");
pLink->SetShowCmd(SW_SHOW);
if(SUCCEEDED(pLink->QueryInterface(IID_IPersistFile,
(void **)&pPersistFile)))
{
WideString strShortCutLocation("C:\bcbshortcut.lnk");
pPersistFile->Save(strShortCutLocation.c_bstr(), TRUE);
pPersistFile->Release();
}
pLink->Release();
}

CoUninitialize();
}
}
//------------------------------------------------------------------
与她合影留念,致终将逝去的青春对我有用[0] 丢个板砖[0] 引用 | 举报 | 管理

mynameis007
mynameis007
等级:
#4 得分:40 回复于: 2002-10-25 12:32:53
如果你不懂COM,可以用一下的代码:
TShellLind *Link;
TPersistFile *PersistFile;

Link=new TShellLink;
Link->SetPath(file.c_str());

PersistFile=dynamic_cast<TPersistFile*>(Link);
PersistFile->Save("d:\ff\");

delete PersistFile;
delete Link;
当然如果你要保存的桌面,要知道桌面的目录:
用一下的代码可以实现(因为NT的桌面目录不再C:desktop下)
void TForm1::CreateShortCut(const AnsiString &file)
{
IShellLink* pLink;
IPersistFile* pPersistFile;
LPMALLOC ShellMalloc;
LPITEMIDLIST DesktopPidl;
char DesktopDir[MAX_PATH];

if(FAILED(SHGetMalloc(&ShellMalloc)))
return;

if(FAILED(SHGetSpecialFolderLocation(NULL,
CSIDL_DESKTOPDIRECTORY,
&DesktopPidl)))
return;

if(!SHGetPathFromIDList(DesktopPidl, DesktopDir))
{
ShellMalloc->Free(DesktopPidl);
ShellMalloc->Release();
return;
}

ShellMalloc->Free(DesktopPidl);
ShellMalloc->Release();

if(SUCCEEDED(CoInitialize(NULL)))
{
if(SUCCEEDED(CoCreateInstance(CLSID_ShellLink, NULL,
CLSCTX_INPROC_SERVER,
IID_IShellLink, (void **) &pLink)))
{
pLink->SetPath(file.c_str());
pLink->SetDescription("Woo hoo, look at Homer's shortcut");
pLink->SetShowCmd(SW_SHOW);

if(SUCCEEDED(pLink->QueryInterface(IID_IPersistFile,
(void **)&pPersistFile)))
{

WideString strShortCutLocation(DesktopDir);
strShortCutLocation += "\bcbshortcut.lnk";
pPersistFile->Save(strShortCutLocation.c_bstr(), TRUE);
pPersistFile->Release();
}
pLink->Release();
}
CoUninitialize();
}
}

原文地址:https://www.cnblogs.com/blogpro/p/11446058.html