View Single Post
Old 15th October 2007, 16:32   #5
grzech
Junior Member
 
grzech's Avatar
 
Join Date: Jul 2006
Posts: 31
I have found an official way to modify link's attributes.

Here is the final function I used (it allows setting attributes only, for clearing attributes it would need to be modified a bit):

code:

HRESULT SetLinkAttributes(wchar_t* path, DWORD myflags)
{
HRESULT result;
IShellLink* link;
IPersistFile* file;
IShellLinkDataList* pdl;
DWORD flags=0;

CoInitializeEx(NULL,COINIT_APARTMENTTHREADED);

result = CoCreateInstance(CLSID_ShellLink,NULL,CLSCTX_INPROC_SERVER,IID_IShellLink,(void**)(&link)); // Create IShellLink object
if(result==S_OK){
result = link->QueryInterface(IID_IPersistFile,(void**)(&file)); // retreive the IPersistFile
if(result==S_OK){
result = file->Load(path,STGM_READ); // load the link data from the file
if(result==S_OK){
result = link->QueryInterface(IID_IShellLinkDataList,(void**)(&pdl)); // look for IShellLinkDataList interface
if(result==S_OK){
result = pdl->GetFlags(&flags);
if(result==S_OK){
if((myflags & flags)!=myflags){ // only set myflags if they are not already set
result=pdl->SetFlags(myflags|flags);
if(result==S_OK){
result=file->Save(NULL,true);
if(result==S_OK){
result=file->SaveCompleted(NULL);
}
}
}
}
pdl->Release();
}
}
file->Release();
link->Release();
}
CoUninitialize();
}
return result;
}

void CALLBACK DllSetConfigLinkAttributes(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow) //set that "Run as administrator" attribute
{
wchar_t path[_MAX_PATH];

if(LOBYTE(LOWORD(GetVersion()))<6) // we want to change it only on Windows Vista or later
return;

if(SHGetSpecialFolderPath(NULL,path,CSIDL_PROGRAMS,FALSE)){
wcscat(path, L"\\My Folder in Start Menu\\My Link File to Change Attributes to.lnk");
if(SetLinkAttributes(path,SLDF_RUNAS_USER)==S_OK)
;//success
else
;//failure
}
}




It was beyond me to write it in NSIS script so I put it into the dll, and called via rundll32 upon sucessful install.
grzech is offline   Reply With Quote