bool SetStartOnSystemStartup(bool fAutoStart) { // If the shortcut exists already, remove it for updating boost::filesystem::remove(StartupShortcutPath()); if (fAutoStart) { CoInitialize(NULL); // Get a pointer to the IShellLink interface. IShellLink* psl = NULL; HRESULT hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, reinterpret_cast<void**>(&psl)); if (SUCCEEDED(hres)) { // Get the current executable path TCHAR pszExePath[MAX_PATH]; GetModuleFileName(NULL, pszExePath, sizeof(pszExePath)); TCHAR pszArgs[5] = TEXT("-min"); // Set the path to the shortcut target psl->SetPath(pszExePath); PathRemoveFileSpec(pszExePath); psl->SetWorkingDirectory(pszExePath); psl->SetShowCmd(SW_SHOWMINNOACTIVE); psl->SetArguments(pszArgs); // Query IShellLink for the IPersistFile interface for // saving the shortcut in persistent storage. IPersistFile* ppf = NULL; hres = psl->QueryInterface(IID_IPersistFile, reinterpret_cast<void**>(&ppf)); if (SUCCEEDED(hres)) { WCHAR pwsz[MAX_PATH]; // Ensure that the string is ANSI. MultiByteToWideChar(CP_ACP, 0, StartupShortcutPath().string().c_str(), -1, pwsz, MAX_PATH); // Save the link by calling IPersistFile::Save. hres = ppf->Save(pwsz, TRUE); ppf->Release(); psl->Release(); CoUninitialize(); return true; } psl->Release(); } CoUninitialize(); return false; } return true; }
// ショートカット作成 BOOL CreateShortcut ( LPCTSTR pszTargetPath /* ターゲットパス */, LPCTSTR pszArguments /* 引数 */, LPCTSTR pszWorkPath /* 作業ディレクトリ */, int nCmdShow /* ShowWindowの引数 */, LPCSTR pszShortcutPath /* ショートカットファイル(*.lnk)のパス */ ) { IShellLink *psl = NULL; IPersistFile *ppf = NULL; enum { MY_MAX_PATH = 65536 }; TCHAR wcLink[ MY_MAX_PATH ]=_T(""); // IShellLinkインターフェースの作成 HRESULT result = CoCreateInstance( CLSID_ShellLink, NULL,CLSCTX_INPROC_SERVER, IID_IShellLink, ( void ** ) &psl); if(FAILED(result)) { return result; } // 設定 psl->SetPath ( pszTargetPath ); psl->SetArguments ( pszArguments ); psl->SetWorkingDirectory ( pszWorkPath ); psl->SetShowCmd ( nCmdShow ); // IPersistFileインターフェースの作成 if ( FAILED ( psl->QueryInterface ( IID_IPersistFile, ( void ** ) &ppf ) ) ) { psl->Release (); return FALSE; } // lpszLinkをWCHAR型に変換 MultiByteToWideChar ( CP_ACP, 0, pszShortcutPath, -1, wcLink, MY_MAX_PATH ); if ( FAILED ( ppf->Save ( wcLink, TRUE ) ) ) { ppf->Release (); return FALSE; } result = ppf->Save((LPCOLESTR)pszShortcutPath,TRUE); // 解放 ppf->Release (); psl->Release (); return TRUE; }
BOOL CreateShortcut(LPCTSTR linkPath, LPCTSTR targetPath, LPCTSTR comment, LPCTSTR arguments, LPCTSTR workDir, LPCTSTR iconFile, int iconIndex, int showCommand) { // Create a shortcut file HRESULT hRes; IShellLink* psl; hRes = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl); if (SUCCEEDED(hRes)) { IPersistFile* ppf; psl->SetPath(targetPath); if (arguments) psl->SetArguments(arguments); if (workDir) psl->SetWorkingDirectory(workDir); if (iconFile || iconIndex) psl->SetIconLocation(iconFile ? iconFile : targetPath, iconIndex); if (comment) psl->SetDescription(comment); psl->SetShowCmd(showCommand); hRes = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf); if (SUCCEEDED(hRes)) { #ifndef UNICODE WCHAR wsz[BUFF_SIZE]; MultiByteToWideChar(CP_ACP, 0, linkPath, -1, wsz, BUFF_SIZE); hRes = ppf->Save(wsz, TRUE); #else hRes = ppf->Save(linkPath, TRUE); #endif ppf->Release(); } psl->Release(); } return TRUE; }
// @pymethod |PyIShellLink|SetShowCmd|Sets the show (SW_) command for a shell link object. PyObject *PyIShellLink::SetShowCmd(PyObject *self, PyObject *args) { IShellLink *pISL = GetI(self); if ( pISL == NULL ) return NULL; // @pyparm int|iShowCmd||The new show command value. int iShowCmd; if ( !PyArg_ParseTuple(args, "i:SetShowCmd", &iShowCmd) ) return NULL; HRESULT hr; PY_INTERFACE_PRECALL; hr = pISL->SetShowCmd( iShowCmd ); PY_INTERFACE_POSTCALL; if ( FAILED(hr) ) return OleSetOleError(hr); Py_INCREF(Py_None); return Py_None; }
HRESULT LINK(PTSTR ptzCmd) { // Parse Shortcut,Target,Param,IconPath,IconIndex PTSTR ptzTarget = UStrChr(ptzCmd, CC_SEP); if (ptzTarget == NULL) { return ERROR_PATH_NOT_FOUND; } INT iIcon = 0; PTSTR ptzIcon = NULL; *ptzTarget++ = 0; PTSTR ptzParam = UStrChr(ptzTarget, CC_SEP); if (ptzParam) { *ptzParam++ = 0; ptzIcon = UStrChr(ptzParam, CC_SEP); if (ptzIcon) { *ptzIcon++ = 0; PTSTR ptzIndex = UStrChr(ptzIcon, CC_SEP); if (ptzIndex) { *ptzIndex++ = 0; iIcon = UStrToInt(ptzIndex); } } } // Search target if (*ptzCmd == '*') { ptzCmd++; } else { TCHAR tzTarget[MAX_PATH]; if (SearchPath(NULL, ptzTarget, NULL, MAX_PATH, tzTarget, NULL)) { ptzTarget = tzTarget; } else if (!UDirExist(ptzTarget)) { return ERROR_PATH_NOT_FOUND; } } // Create shortcut IShellLink *pLink; CoInitialize(NULL); HRESULT hResult = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (PVOID *) &pLink); if (hResult == S_OK) { IPersistFile *pFile; hResult = pLink->QueryInterface(IID_IPersistFile, (PVOID *) &pFile); if (hResult == S_OK) { if (*ptzCmd == '!') { ptzCmd++; hResult = pLink->SetShowCmd(SW_SHOWMINIMIZED); } // Shortcut settings hResult = pLink->SetPath(ptzTarget); hResult = pLink->SetArguments(ptzParam); hResult = pLink->SetIconLocation(ptzIcon, iIcon); if (UPathSplit(ptzTarget) != ptzTarget) { hResult = pLink->SetWorkingDirectory(ptzTarget); } // Save link WCHAR wzLink[MAX_PATH]; if (UStrCmpI(ptzCmd + UStrLen(ptzCmd) - 4, TEXT(".LNK"))) { UStrCat(ptzCmd, TEXT(".LNK")); } UStrToWStr(wzLink, ptzCmd, MAX_PATH); UDirCreate(ptzCmd); hResult = pFile->Save(wzLink, FALSE); pFile->Release(); } pLink->Release(); } CoUninitialize(); return hResult; }
//--------------------------------------------------------------------------- IShellLink * __fastcall CreateDesktopShortCut(const UnicodeString & Name, const UnicodeString &File, const UnicodeString & Params, const UnicodeString & Description, int SpecialFolder, int IconIndex, bool Return) { IShellLink* pLink = NULL; if (SpecialFolder < 0) { SpecialFolder = CSIDL_DESKTOPDIRECTORY; } try { if (SUCCEEDED(CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void **) &pLink))) { try { pLink->SetPath(File.c_str()); pLink->SetDescription(Description.c_str()); pLink->SetArguments(Params.c_str()); pLink->SetShowCmd(SW_SHOW); // Explicitly setting icon file, // without this icons are not shown at least in Windows 7 jumplist pLink->SetIconLocation(File.c_str(), IconIndex); IPersistFile* pPersistFile; if (!Return && SUCCEEDED(pLink->QueryInterface(IID_IPersistFile, (void **)&pPersistFile))) { try { LPMALLOC ShellMalloc; LPITEMIDLIST DesktopPidl; wchar_t DesktopDir[MAX_PATH]; OleCheck(SHGetMalloc(&ShellMalloc)); try { OleCheck(SHGetSpecialFolderLocation(NULL, SpecialFolder, &DesktopPidl)); OleCheck(SHGetPathFromIDList(DesktopPidl, DesktopDir)); } __finally { ShellMalloc->Free(DesktopPidl); ShellMalloc->Release(); } WideString strShortCutLocation(DesktopDir); // Name can contain even path (e.g. to create quick launch icon) strShortCutLocation += UnicodeString(L"\\") + Name + L".lnk"; OleCheck(pPersistFile->Save(strShortCutLocation.c_bstr(), TRUE)); } __finally { pPersistFile->Release(); } } // this is necessary for Windows 7 taskbar jump list links IPropertyStore * PropertyStore; if (SUCCEEDED(pLink->QueryInterface(IID_IPropertyStore, (void**)&PropertyStore))) { PROPVARIANT Prop; Prop.vt = VT_LPWSTR; Prop.pwszVal = Name.c_str(); PropertyStore->SetValue(PKEY_Title, Prop); PropertyStore->Commit(); PropertyStore->Release(); } } catch(...) { pLink->Release(); throw; } if (!Return) { pLink->Release(); pLink = NULL; } } } catch(Exception & E) { throw ExtException(&E, LoadStr(CREATE_SHORTCUT_ERROR)); } return pLink; }
int installShortcut(const WORD *shortcutPath, const unsigned short *shortcutNameU, const unsigned short *descriptionU, const unsigned short *pathU, const unsigned short *argumentsU, const unsigned short *workingDirectoryU, const unsigned short *iconPathU) { char *shortcutName = javawsWideCharToMBCS(shortcutNameU); char *description = javawsWideCharToMBCS(descriptionU); char *path = javawsWideCharToMBCS(pathU); char *arguments = javawsWideCharToMBCS(argumentsU); char *workingDirectory = javawsWideCharToMBCS(workingDirectoryU); char *iconPath = javawsWideCharToMBCS(iconPathU); // Initialize COM, stash the result to know if we need to call // CoUnintialize HRESULT comStart = CoInitialize(NULL); HRESULT tempResult; IShellLink *shell; int retValue = 0; // Find IShellLink interface. tempResult = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void **)&shell); if (SUCCEEDED(tempResult)) { IPersistFile* persistFile; // Query IShellLink for the IPersistFile interface for // saving the shell link in persistent storage. tempResult = shell->QueryInterface(IID_IPersistFile, (void **)&persistFile); if (SUCCEEDED(tempResult)) { // Set the path to the shell link target. tempResult = shell->SetPath(path); if (!SUCCEEDED(tempResult)) { // Couldn't set the path retValue = -2; } // Set the description of the shell link. // fix for 4499382 // make sure description length is less than MAX_PATH // else truncate the string before setting description if (retValue == 0 && description != NULL && strlen(description) < MAX_PATH && !SUCCEEDED(shell->SetDescription(description))) { retValue = -3; } else { char *desc = (char*)malloc(sizeof(char)*MAX_PATH); desc = strncpy(desc, description, MAX_PATH - 1); if (!SUCCEEDED(shell->SetDescription(desc))) { retValue = -3; } } // Set the arguments if (retValue == 0 && arguments != NULL && !(SUCCEEDED(shell->SetArguments(arguments)))) { retValue = -4; } // Working directory if (retValue == 0 && workingDirectory != NULL && !(SUCCEEDED(shell->SetWorkingDirectory(workingDirectory)))) { retValue = -5; } // Sets the icon location, default to an icon index of 0. if (retValue == 0 && iconPath != NULL && !(SUCCEEDED(shell->SetIconLocation(iconPath, 0)))) { retValue = -6; } // PENDING: if iconPath == null, should install a link to // the default icon! // Defaults to a normal window. if (retValue == 0) { shell->SetShowCmd(SW_NORMAL); // Save the link via the IPersistFile::Save method. if (!SUCCEEDED(persistFile->Save(shortcutPath, TRUE))) { retValue = -7; } } // Release pointer to IPersistFile. persistFile->Release(); } else { // No persist file retValue = -8; } // Release pointer to IShellLink. shell->Release(); } else { // No shell! retValue = -9; } if (comStart == S_OK) { CoUninitialize(); } free(shortcutName); free(description); free(path); free(arguments); free(workingDirectory); free(iconPath); return retValue; }