/* createLnk Copyright Matthew Ford Forward Computing and Control Pty. Ltd. (www.forward.com.au) NSW Australia. This code is placed in the public domain. You may use it however you wish. There is absolutely NO warranty with this code. Description: See main() below CoInitialize() must be called in order to initialize the Component Object Model (COM) library. It must be called before any COM functions are called. Similarly, CoUninitialize() must be called in order to uninitialize the COM library. Each call to CoInitialize() must be paired up with a call to CoUninitialize(). Notes: Also link: ole32.lib Code: */ #include #include #include #include void CreateLink(LPSTR lpszPathLink, LPCSTR lpszPathObj, LPTSTR pszWorkingDir, LPSTR lpszDesc, LPTSTR pszArgs, LPTSTR pszIconPath ) { HRESULT hres; IShellLink* psl; hres = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, &IID_IShellLink, &psl); if (SUCCEEDED(hres)) { IPersistFile* ppf; psl->lpVtbl->SetPath(psl, lpszPathObj); if (pszWorkingDir != NULL) { psl->lpVtbl->SetWorkingDirectory(psl, pszWorkingDir); } if (pszArgs != NULL) { psl->lpVtbl->SetArguments(psl, pszArgs); } if (lpszDesc != NULL) { psl->lpVtbl->SetDescription(psl, lpszDesc); } if (pszIconPath != NULL) { psl->lpVtbl->SetIconLocation(psl, pszIconPath,0); //nIconIndex); } hres = psl->lpVtbl->QueryInterface(psl, &IID_IPersistFile,&ppf); if (SUCCEEDED(hres)) { WORD wsz[MAX_PATH]; MultiByteToWideChar(CP_ACP, 0, lpszPathLink, -1,wsz, MAX_PATH); hres = ppf->lpVtbl->Save(ppf, wsz, TRUE); ppf->lpVtbl->Release(ppf); } psl->lpVtbl->Release(psl); } return; } /* * args * 0 this program's name * 1 path eg. C:\\MTENG50\\Parallel * 2 file name can be empty if linking to directory Path eg parallel.jar * 3 link name eg Parallel V1.1 * 4 working dir eg C:\\MTENG50\\Parallel * 5 description (optional) * 6 args (optional) * 7 icon path (optional) */ int main(int argc, char **argv) { char *path = NULL; //argv[1]; // "C:\\MTENG50\\Parallel\\" char *filename = NULL; //argv[2]; // "parallel.jar" char *lpszLinkName = NULL; //argv[3]; //"Parallel V1.1"; char *workingDir = NULL; //argv[4]; //"C:\\MTENG50\\Parallel\\"; char *desc = NULL; //argv[5]; //"Shortcut to Parallel.jar"; char *args = NULL; //argv[6]; //""; char *icon = NULL; //argv[7]; //"C:\\MTENG50\\Parallel\\p32.ico"; IShellFolder *psfDeskTop = NULL; ITEMIDLIST *id; char szLinkPath[MAX_PATH]; char szLinkFileName[MAX_PATH]; char szWorkDir[MAX_PATH]; if (argc < 5) { printf("%s usage:\n This program creates a shortcut on the desktop \n" " %s path filename linkFilename workingDir [description args icon]\n" " where path is the path to link to\n" " filename is the filename to link to or "" if linking to a directory\n" " linkFilename is the name of the link file (without the .lnk)\n" " workingDir the working directory\n" " description (optional) the description of this link or \"\"\n" " args (optional) the arguments or \" \" for no args\n" " iconPath (optional) the location of the .ico file for this link icon \n" " EG %s C:\\MTENG50\\Parallel parallel.jar \"Parallel V1.1\" C:\\MTENG50\\Parallel \"Shortcut to Parallel.jar\" \"\" c:\\MTENG50\\Parallel\\p32.ico\n" " (c) Forward Computing and Control Pty. Ltd. (www.forward.com.au)", argv[0],argv[0],argv[0]); exit(1); } path = argv[1]; // "C:\\MTENG50\\Parallel\\" filename = argv[2]; // "parallel.jar" lpszLinkName = argv[3]; //"Parallel V1.1"; workingDir = argv[4]; //"C:\\MTENG50\\Parallel\\"; if (argc >= 5) desc = argv[5]; //"Shortcut to Parallel.jar"; if (argc >= 6) args = argv[6]; //""; if (argc >= 7) icon = argv[7]; //"C:\\MTENG50\\Parallel\\p32.ico"; //printf("Initializing...\n"); CoInitialize(NULL); ZeroMemory(szLinkFileName, sizeof(szLinkFileName)); strcat(szLinkFileName, path); if (szLinkFileName[strlen(szLinkFileName)-1] != '\\') { strcat(szLinkFileName, "\\"); } if (filename != NULL) { strcat(szLinkFileName, filename); } ZeroMemory(szWorkDir, sizeof(szWorkDir)); if (workingDir != NULL) { strcat(szWorkDir, workingDir); if (szWorkDir[strlen(szWorkDir)-1] != '\\') { strcat(szWorkDir, "\\"); } } ZeroMemory(szLinkPath, sizeof(szLinkPath)); SHGetSpecialFolderLocation(NULL, CSIDL_DESKTOP, &id); SHGetPathFromIDList(id, szLinkPath); strcat(szLinkPath, "\\"); strcat(szLinkPath, lpszLinkName); strcat(szLinkPath, ".lnk"); //printf("Creating shortcut at %s.\n", szLinkPath); //printf(" to %s.\n", szLinkFileName); //printf("Creating link to <%s> called <%s> in <%s>...\n", path, desc, szLinkPath); CreateLink(szLinkPath, szLinkFileName, workingDir, desc, args, icon); //printf("Uninitializing...\n"); CoUninitialize(); return 0; }