file://---------------------------------------------------------------------
// CreateLink - uses the shell's IShellLink and IPersistFile interfaces
// to create and store a shortcut to the specified object.
// Returns the result of calling the member functions of the interfaces.
// lpszPathObj - address of a buffer containing the path of the object.
// lpszPathLink - address of a buffer containing the path where the
// shell link is to be stored.
// lpszDesc - address of a buffer containing the description of the
// shell link.
HRESULT CreateLink(LPCSTR lpszPathObj,
LPCSTR lpszPathLink, LPCSTR lpszDesc)
{
void *pvReserved=NULL;
HRESULT rez=CoInitialize(pvReserved);
if(rez!=S_OK) return false;
HRESULT hres;
IShellLink* psl;
// Get a pointer to the IShellLink interface.
hres = CoCreateInstance(CLSID_ShellLink, NULL,
CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID *) &psl);
if (SUCCEEDED(hres)) {
IPersistFile* ppf;
// Set the path to the shortcut target and add the
// description.
psl->SetPath(lpszPathObj);
psl->SetDescription(lpszDesc);
// Query IShellLink for the IPersistFile interface for saving the
// shortcut in persistent storage.
hres = psl->QueryInterface(IID_IPersistFile,(void**)&ppf);
if (SUCCEEDED(hres))
{
WCHAR wsz[MAX_PATH];
// Ensure that the string is Unicode.
MultiByteToWideChar(CP_ACP, 0, lpszPathLink, -1,
wsz, MAX_PATH);
// Save the link by calling IPersistFile::Save.
hres = ppf->Save(wsz, TRUE);
ppf->Release();
}
psl->Release();
}
return hres;
}
file://---------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
String lpszPathObj="c:\\RunCreate.exe",
lpszPathLink="c:\\11.lnk",
lpszDesc="My Link";
int rez=CreateLink(lpszPathObj.c_str(),
lpszPathLink.c_str(),
lpszDesc.c_str());
if(rez==0)Button1->Caption="TRUE";
else Button1->Caption="FALSE";
}
file://---------------------------------------------------------------------