//---------------------------------------------------------------------------
#define NO_WIN32_LEAN_AND_MEAN
#include <vcl.h>
#include <shlobj.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
//Para elegir el archivo destino del acceso directo
if(OpenDialog1->Execute())
CreaAcceso(OpenDialog1->FileName);
}
//---------------------------------------------------------------------------
void TForm1::CreaAcceso(const AnsiString &file)
{
IShellLink* pLink;
IPersistFile* pPersistFile;
//Inicializamos la libreria COM
if(SUCCEEDED(CoInitialize(NULL)))
{
if(SUCCEEDED(CoCreateInstance(CLSID_ShellLink, NULL,
CLSCTX_INPROC_SERVER,
IID_IShellLink, (void **) &pLink)))
{
//Asignamos los atributos del Acceso directo
pLink->SetPath(file.c_str());
pLink->SetDescription("Descripcion para probar");
pLink->SetShowCmd(SW_SHOW);
if(SUCCEEDED(pLink->QueryInterface(IID_IPersistFile,
(void **)&pPersistFile)))
{
/*Obtenemos la ruta del Escritorio*/
LPITEMIDLIST pidl;
LPMALLOC pShellMalloc;
char Ruta[MAX_PATH];
if(SUCCEEDED(SHGetMalloc(&pShellMalloc)))
{
if(SUCCEEDED(SHGetSpecialFolderLocation(NULL,
CSIDL_DESKTOPDIRECTORY,
&pidl)))
{
//Creamos el Acceso.
if(SHGetPathFromIDList(pidl, Ruta))
{
strcat(Ruta,"\\Acceso.lnk");
WideString strShortCutLocation(Ruta);
pPersistFile->Save(strShortCutLocation.c_bstr(), TRUE);
pPersistFile->Release();
}
pShellMalloc->Free(pidl);
}
pShellMalloc->Release();
}
}
pLink->Release();
}
CoUninitialize();
}
}
//---------------------------------------------------------------------------