Anyone know how to do this?
This is a discussion on Creating a desktop icon from code within the Windows Programming forums, part of the Platform Specific Boards category; Anyone know how to do this?...
Anyone know how to do this?
You need COM.....I posted an example quite a while ago if you want to search..
An easier way is to use Windows Scripting Host like this;
Type into notepad & save that as link.jsCode:try{ var oShell = new ActiveXObject("WScript.Shell"); var oDesktop = oShell.SpecialFolders("Desktop"); var oShortcut = oShell.CreateShortcut(oDesktop + "\\Calc.lnk"); oShortcut.TargetPath = "calc.exe"; oShortcut.Save(); oShell = null; oDesktop = null; oShortcut = null; WScript.Echo("Shortcut Created"); } catch(e){ WScript.Echo("Error " + e); }
Then ShellExecute the script and you are away.....
To do something similar in VC++
Code:#include <windows.h>//Essential stuff #include <objbase.h>//get COM up and running #include <Shlobj.h>//for interfaces #include <comdef.h> inline void TESTIT(HRESULT hRes){ if(FAILED(hRes)) throw _com_error(hRes); } struct COM_INIT{//setup com COM_INIT(){CoInitialize(0);} ~COM_INIT(){CoUninitialize();} }COM_INIT_; int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int Show){ char *lpstrTargetPath = "C:\\WINDOWS\\system32\\calc.exe"; WCHAR *lpwstrLinkPath = L"C:\\Documents and Settings\\All Users\\Desktop\\CalcLink.lnk"; try{ IShellLink* ISL = 0; IPersistFile* IPF = 0; TESTIT(CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID *) &ISL)); TESTIT(ISL->SetPath(lpstrTargetPath)); TESTIT(ISL->QueryInterface(IID_IPersistFile,(void**)&IPF)); TESTIT(IPF->Save(lpwstrLinkPath, TRUE)); ISL->Release(); IPF->Release(); MessageBox(0,"Success!","",MB_OK); } catch(_com_error& e){ MessageBox(0,e.Description().length() ? e.Description() : "COM Error","",MB_OK); } return 0; }
I get an error code from CoCreateInstance. Where can i find the spec's for the classes, i.e CLSID_ShellLink?
All the CLSID info and interfaces are defined in Shlobj.h or shobjidl.h....the library you need for that is shell32.lib I think
That code compiles on VC++6 under Windows XP
Thx..