Creating a desktop icon from code

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?...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    329

    Question Creating a desktop icon from code

    Anyone know how to do this?

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    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;

    Code:
    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);
    
    
    }
    Type into notepad & save that as link.js

    Then ShellExecute the script and you are away.....

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    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;
    }

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    I get an error code from CoCreateInstance. Where can i find the spec's for the classes, i.e CLSID_ShellLink?

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    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

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    Thx..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-21-2006, 06:52 AM
  2. Creating AVI movies with C Code
    By flamingwitch in forum C Programming
    Replies: 3
    Last Post: 08-06-2004, 07:00 AM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 03:28 AM
  4. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 12:33 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21