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; }
void CGUISetupFrame::writeDeskTop(const wxString& name, const wxString& dir) { TCHAR folder[MAX_PATH]; BOOL res = ::SHGetSpecialFolderPath(NULL, folder, CSIDL_DESKTOP, FALSE); if (!res) { ::wxMessageBox(_("Cannot get the Desktop folder from Windows"), _("GUISetup Error"), wxICON_ERROR); return; } wxString linkName = name + wxT(".lnk"); wxString linkPath = wxString(folder) + wxT("\\") + linkName; wxString exePath = dir + wxT("\\UWSDR.exe"); wxString args = name; HRESULT hRes = ::CoInitialize(NULL); if (!SUCCEEDED(hRes)) { ::wxMessageBox(_("Cannot initialise the COM interface"), _("GUISetup Error"), wxICON_ERROR); return; } IShellLink* pShellLink; hRes = ::CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&pShellLink); if (!SUCCEEDED(hRes)) { ::CoUninitialize(); ::wxMessageBox(_("Cannot create a COM interface"), _("GUISetup Error"), wxICON_ERROR); return; } pShellLink->SetPath(exePath.c_str()); pShellLink->SetArguments(args.c_str()); pShellLink->SetWorkingDirectory(dir.c_str()); TCHAR wszLinkfile[MAX_PATH]; ::MultiByteToWideChar(CP_ACP, 0, linkPath.mb_str(), -1, wszLinkfile, MAX_PATH); IPersistFile* pPersistFile; hRes = pShellLink->QueryInterface(IID_IPersistFile, (void**)&pPersistFile); if (!SUCCEEDED(hRes)) { pShellLink->Release(); ::CoUninitialize(); ::wxMessageBox(_("Cannot query the COM interface"), _("GUISetup Error"), wxICON_ERROR); return; } hRes = pPersistFile->Save(LPCOLESTR(wszLinkfile), TRUE); if (!SUCCEEDED(hRes)) ::wxMessageBox(_("Cannot save the shortcut file"), _("GUISetup Error"), wxICON_ERROR); pPersistFile->Release(); pShellLink->Release(); ::CoUninitialize(); }
//creates a shell link...in this example a Start Group item HRESULT CreateShellLink(LPCSTR pszShortcutFile, LPSTR pszLink, LPSTR pszDesc) { HRESULT hres; IShellLink* psl; // Get a pointer to the IShellLink interface. hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&psl); if (SUCCEEDED(hres)) { IPersistFile* ppf; // Query IShellLink for the IPersistFile interface for // saving the shell link in persistent storage. hres = psl->QueryInterface(IID_IPersistFile,(void**)&ppf); if (SUCCEEDED(hres)) { WCHAR wsz[MAX_PATH]; // Set the path to the shell link target. hres = psl->SetPath(pszShortcutFile); if (!SUCCEEDED(hres)) MessageBox(NULL,"SetPath failed!","ERROR",MB_OK); // Set the description of the shell link. hres = psl->SetDescription(pszDesc); if (!SUCCEEDED(hres)) MessageBox(NULL,"Set Description failed!","ERROR",MB_OK); // Ensure string is ANSI. MultiByteToWideChar(CP_ACP, 0, pszLink, -1, wsz, MAX_PATH); // Save the link via the IPersistFile::Save method. hres = ppf->Save(wsz, TRUE); // Release pointer to IPersistFile. ppf->Release(); } // Release pointer to IShellLink. psl->Release(); } return (hres==S_OK); }
//---------------------------------------------------------------------------------------- PRBool nsFileSpec::IsSymlink() const //---------------------------------------------------------------------------------------- { HRESULT hres; IShellLink* psl; PRBool isSymlink = PR_FALSE; CoInitialize(NULL); // Get a pointer to the IShellLink interface. hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&psl); if (SUCCEEDED(hres)) { IPersistFile* ppf; // Get a pointer to the IPersistFile interface. hres = psl->QueryInterface(IID_IPersistFile, (void**)&ppf); if (SUCCEEDED(hres)) { WCHAR wsz[MAX_PATH]; // Ensure that the string is Unicode. MultiByteToWideChar(CP_ACP, 0, mPath, -1, wsz, MAX_PATH); // Load the shortcut. hres = ppf->Load(wsz, STGM_READ); if (SUCCEEDED(hres)) { isSymlink = PR_TRUE; } // Release the pointer to the IPersistFile interface. ppf->Release(); } // Release the pointer to the IShellLink interface. psl->Release(); } CoUninitialize(); return isSymlink; }
BOOL CreateShortcut(TCHAR *file, TCHAR *shortcut) { IShellLink *psl = NULL; HRESULT hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void **) &psl); if (SUCCEEDED(hr)) { psl->SetPath(file); IPersistFile *ppf = NULL; hr = psl->QueryInterface(IID_IPersistFile, (void **) &ppf); if (SUCCEEDED(hr)) { hr = ppf->Save(shortcut, TRUE); ppf->Release(); } psl->Release(); } return SUCCEEDED(hr); }
BOOL ResolveShortcut(TCHAR *shortcut, TCHAR *file) { CoInitialize(NULL); IShellLink* psl = NULL; HRESULT hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void **) &psl); if (SUCCEEDED(hr)) { IPersistFile* ppf = NULL; hr = psl->QueryInterface(IID_IPersistFile, (void **) &ppf); if (SUCCEEDED(hr)) { #ifdef _UNICODE hr = ppf->Load(shortcut, STGM_READ); #else WCHAR tmp[MAX_PATH]; MultiByteToWideChar(CP_ACP, 0, shortcut, -1, tmp, MAX_PATH); hr = ppf->Load(tmp, STGM_READ); #endif if (SUCCEEDED(hr)) { hr = psl->Resolve(NULL, SLR_UPDATE); if (SUCCEEDED(hr)) { WIN32_FIND_DATA wfd; hr = psl->GetPath(file, MAX_PATH, &wfd, SLGP_RAWPATH); } } ppf->Release(); } psl->Release(); } if(FAILED(hr)) ErrorExit(NULL,_T("CreateShortcut")); return SUCCEEDED(hr); }
HRESULT CTaskbar7::CreateShellLink(Destination destination, IShellLink **ppShellLink) { USES_CONVERSION; IShellLink *pShellLink = NULL; IPropertyStore *pPropertyStore = NULL; PROPVARIANT propVariant; HRESULT hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pShellLink)); EXIT_ON_ERROR(hr); // Path hr = pShellLink->SetPath(CW2A(destination.path.c_str())); EXIT_ON_ERROR(hr); // Arguments hr = pShellLink->SetArguments(CW2A(destination.arguments.c_str())); EXIT_ON_ERROR(hr); // Working Directory if (!destination.workingFolder.empty()) { hr = pShellLink->SetWorkingDirectory(CW2A(destination.workingFolder.c_str())); EXIT_ON_ERROR(hr); } // Icon Location if (!destination.icon.empty()) { hr = pShellLink->SetIconLocation(CW2A(destination.icon.c_str()), destination.iconIndex); EXIT_ON_ERROR(hr); } hr = pShellLink->QueryInterface(IID_PPV_ARGS(&pPropertyStore)); EXIT_ON_ERROR(hr); // Name hr = InitPropVariantFromString(destination.name.c_str(), &propVariant); EXIT_ON_ERROR(hr); hr = pPropertyStore->SetValue(PKEY_Title, propVariant); EXIT_ON_ERROR(hr); hr = pPropertyStore->Commit(); EXIT_ON_ERROR(hr); hr = pShellLink->QueryInterface(IID_PPV_ARGS(ppShellLink)); Exit: PropVariantClear(&propVariant); SAFE_RELEASE(pPropertyStore); SAFE_RELEASE(pShellLink); return hr; }
HRESULT create_shortcut(const string& target, const string& arguments, const string& save_as) { IShellLink* sl = NULL; HRESULT hres; if (SUCCEEDED(hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, reinterpret_cast<void**>(&sl)))) { sl->SetPath(target.c_str()); sl->SetArguments(arguments.c_str()); IPersistFile* pf = NULL; if (SUCCEEDED(hres = sl->QueryInterface(IID_IPersistFile, reinterpret_cast<void**>(&pf)))) { wstring name = mbyte_to_wchar(save_as.c_str()); hres = pf->Save(name.c_str(), true); pf->Release(); } sl->Release(); } return hres; }
// @pymethod |PyIShellLink|Resolve|Resolves a shell link by searching for the shell link object and updating the // shell link path and its list of identifiers (if necessary) PyObject *PyIShellLink::Resolve(PyObject *self, PyObject *args) { IShellLink *pISL = GetI(self); if ( pISL == NULL ) return NULL; // @pyparm HWND|hwnd||The parent window of a dialog which will pop up if resolution fails. // @pyparm int|fFlags||One of the following constants: // @flagh Value|Description // @flag SLR_INVOKE_MSI|Call the Microsoft Windows Installer. // @flag SLR_NOLINKINFO |Disable distributed link tracking. By default, distributed // link tracking tracks removable media across multiple devices based on the // volume name. It also uses the UNC path to track remote file systems whose // drive letter has changed. Setting SLR_NOLINKINFO disables both types of tracking. // @flag SLR_NO_UI|Do not display a dialog box if the link cannot be resolved. When // SLR_NO_UI is set, the high-order word of fFlags can be set to a time-out value // that specifies the maximum amount of time to be spent resolving the link. The // function returns if the link cannot be resolved within the time-out duration. // If the high-order word is set to zero, the time-out duration will be set to the // default value of 3,000 milliseconds (3 seconds). To specify a value, set the high // word of fFlags to the desired time-out duration, in milliseconds. // @flag SLR_NOUPDATE|Do not update the link information. // @flag SLR_NOSEARCH|Do not execute the search heuristics. // @flag SLR_NOTRACK|Do not use distributed link tracking. // @flag SLR_UPDATE|If the link object has changed, update its path and list of identifiers. If SLR_UPDATE is set, you do not need to call IPersistFile::IsDirty to determine whether or not the link object has changed. HWND hwnd; PyObject *obhwnd; DWORD fFlags; if ( !PyArg_ParseTuple(args, "Ol:Resolve", &obhwnd, &fFlags) ) return NULL; if (!PyWinObject_AsHANDLE(obhwnd, (HANDLE *)&hwnd)) return NULL; HRESULT hr; PY_INTERFACE_PRECALL; hr = pISL->Resolve( hwnd, fFlags ); PY_INTERFACE_POSTCALL; if ( FAILED(hr) ) return OleSetOleError(hr); Py_INCREF(Py_None); return Py_None; }
// @pymethod int|PyIShellLink|GetHotkey|Retrieves the hot key for a shell link object. PyObject *PyIShellLink::GetHotkey(PyObject *self, PyObject *args) { IShellLink *pISL = GetI(self); if ( pISL == NULL ) return NULL; HRESULT hr; WORD hotkey; PY_INTERFACE_PRECALL; hr = pISL->GetHotkey( &hotkey ); PY_INTERFACE_POSTCALL; if ( FAILED(hr) ) return OleSetOleError(hr); return PyInt_FromLong(hotkey); // @comm Note: My tests do not seem to be working. at least, the values returned // seem not to match what the documentation says should be returned. // I would expect with a Hotkey of CTRL-ALT-T, to get an integer where // integer & 256 == ord('T'), i.e. 116 or 84, instead I get 1620 }
// @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 CreateLink(LPCSTR linkPath, LPCWSTR targetPath, LPCWSTR args) { HRESULT hres; if (!called_coinit) { hres = CoInitialize(NULL); called_coinit = true; if (!SUCCEEDED(hres)) { wxLogError(_("Failed to initialize COM. Error 0x%08X"), hres); return hres; } } IShellLink* link; hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&link); if (SUCCEEDED(hres)) { IPersistFile* persistFile; link->SetPath(targetPath); link->SetArguments(args); hres = link->QueryInterface(IID_IPersistFile, (LPVOID*)&persistFile); if (SUCCEEDED(hres)) { WCHAR wstr[MAX_PATH]; MultiByteToWideChar(CP_ACP, 0, linkPath, -1, wstr, MAX_PATH); hres = persistFile->Save(wstr, TRUE); persistFile->Release(); } link->Release(); } return hres; }
// Get the target PIDL for a folder PIDL. This deals with cases where a folder // is an alias to a real folder, folder shortcuts, etc. STDAPI SHGetTargetFolderIDList(LPITEMIDLIST pidlFolder, LPITEMIDLIST *ppidl) { IShellLink *psl; *ppidl = NULL; HRESULT hr = SHGetUIObjectFromFullPIDL(pidlFolder, NULL, IID_IShellLink, (void**)(&psl)); if (SUCCEEDED(hr)) { hr = psl->GetIDList(ppidl); psl->Release(); } // It's not a folder shortcut so get the PIDL normally. if (FAILED(hr)) { *ppidl = pidlFolder; return S_OK; } return hr; }
bool CreateShortcutFile(const WCHAR* filePath, const WCHAR* targetPath) { IShellLink* psl; HRESULT hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&psl); if (SUCCEEDED(hr)) { IPersistFile* ppf; hr = psl->QueryInterface(IID_IPersistFile, (void**)&ppf); if (SUCCEEDED(hr)) { psl->SetPath(filePath); hr = ppf->Save(targetPath, TRUE); ppf->Release(); } psl->Release(); } return SUCCEEDED(hr); }
//----------------------------------------------------------------------------- bool WinDragContainer::checkResolveLink (const TCHAR* nativePath, TCHAR* resolved) { const TCHAR* ext = VSTGUI_STRRCHR (nativePath, '.'); if (ext && VSTGUI_STRICMP (ext, TEXT(".lnk")) == 0) { IShellLink* psl; IPersistFile* ppf; WIN32_FIND_DATA wfd; HRESULT hres; // Get a pointer to the IShellLink interface. hres = CoCreateInstance (CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&psl); if (SUCCEEDED (hres)) { // Get a pointer to the IPersistFile interface. hres = psl->QueryInterface (IID_IPersistFile, (void**)&ppf); if (SUCCEEDED (hres)) { // Load the shell link. hres = ppf->Load (nativePath, STGM_READ); if (SUCCEEDED (hres)) { hres = psl->Resolve (0, MAKELONG (SLR_ANY_MATCH | SLR_NO_UI, 500)); if (SUCCEEDED (hres)) { // Get the path to the link target. hres = psl->GetPath (resolved, 2048, &wfd, SLGP_SHORTPATH); } } // Release pointer to IPersistFile interface. ppf->Release (); } // Release pointer to IShellLink interface. psl->Release (); } return SUCCEEDED(hres); } return false; }
/* リンク あらかじめ、CoInitialize(NULL); を実行しておくこと src ... old_path dest ... new_path */ BOOL SymLinkV(void *src, void *dest, void *arg) { IShellLink *shellLink; IPersistFile *persistFile; WCHAR wbuf[MAX_PATH], *ps_dest = (WCHAR *)dest; BOOL ret = FALSE; WCHAR buf[MAX_PATH]; if (SUCCEEDED(CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLinkV, (void **)&shellLink))) { shellLink->SetPath((char *)src); shellLink->SetArguments((char *)arg); GetParentDirV(src, buf); shellLink->SetWorkingDirectory((char *)buf); if (SUCCEEDED(shellLink->QueryInterface(IID_IPersistFile, (void **)&persistFile))) { if (!IS_WINNT_V) { MultiByteToWideChar(CP_ACP, 0, (char *)dest, -1, wbuf, MAX_PATH); ps_dest = wbuf; } if (SUCCEEDED(persistFile->Save(ps_dest, TRUE))) { ret = TRUE; GetParentDirV(dest, buf); ::SHChangeNotify(SHCNE_UPDATEDIR, SHCNF_PATHV|SHCNF_FLUSH, buf, NULL); } persistFile->Release(); } shellLink->Release(); } return ret; }
// ショートカット作成 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; }
void Shortcut_Run(const char* szLink, const char* szWorkdir) { wchar_t wszLink[MAX_PATH] = {}; ::MultiByteToWideChar(CP_ACP, NULL, szLink, int(strlen(szLink)), wszLink, MAX_PATH); IShellLink *pShellLink; HRESULT hRes; ::CoInitialize(NULL); hRes = CoCreateInstance(CLSID_ShellLink,NULL,CLSCTX_INPROC_SERVER,IID_IShellLink,(void **)&pShellLink); if(SUCCEEDED(hRes)) { IPersistFile *ppf; hRes = pShellLink->QueryInterface(IID_IPersistFile,(void **)&ppf); if(SUCCEEDED(hRes)) { hRes = ppf->Load(wszLink, TRUE); if(SUCCEEDED(hRes)) { int show = SW_SHOW; char path[MAX_PATH] = {}; char workdir[MAX_PATH] = {}; pShellLink->GetShowCmd(&show); pShellLink->GetPath(path, MAX_PATH, NULL, 0); pShellLink->GetWorkingDirectory(workdir, MAX_PATH); if(path[0] != 0) { ShellExecute(NULL, "open", path, NULL, workdir, show); } } ppf->Release(); } pShellLink->Release(); } ::CoUninitialize(); }
long CreateLink(LPCSTR lpszPathObj,LPSTR lpszPathLink,LPSTR lpszDesc) { HRESULT hres; IShellLink* psl; CoInitialize(NULL); hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink,(void**)&psl); if (SUCCEEDED(hres)) { IPersistFile *ppf; hres = psl->QueryInterface (IID_IPersistFile, (void **)&ppf); if (SUCCEEDED(hres)) { WCHAR wsz[MAX_PATH]; ZeroMemory(wsz,sizeof(WORD)*MAX_PATH); hres = psl->SetPath(lpszPathObj); hres = psl->SetDescription(lpszDesc); hres = psl->SetIconLocation(lpszPathObj,0); MultiByteToWideChar(CP_ACP, 0, lpszPathLink, -1, wsz, MAX_PATH); hres = ppf->Save(wsz, TRUE); ppf->Release(); if(!SUCCEEDED(hres)) return false; } else return false; psl->Release(); } else return false; CoUninitialize(); return true; }
/* リンク あらかじめ、CoInitialize(NULL); を実行しておくこと */ BOOL SymLink(LPCSTR src, LPSTR dest, LPCSTR arg) { IShellLink *shellLink; IPersistFile *persistFile; Wstr wsrc(src), wdest(dest), warg(arg); BOOL ret = FALSE; char buf[MAX_PATH_U8]; if (SUCCEEDED(CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLinkW, (void **)&shellLink))) { shellLink->SetPath((char *)wsrc.Buf()); shellLink->SetArguments((char *)warg.Buf()); GetParentDirU8(src, buf); shellLink->SetWorkingDirectory((char *)U8toWs(buf)); if (SUCCEEDED(shellLink->QueryInterface(IID_IPersistFile, (void **)&persistFile))) { if (SUCCEEDED(persistFile->Save(wdest.s(), TRUE))) { ret = TRUE; GetParentDirU8(WtoU8(wdest.s()), buf); ::SHChangeNotify(SHCNE_UPDATEDIR, SHCNF_PATH|SHCNF_FLUSH, U8toAs(buf), NULL); } persistFile->Release(); } shellLink->Release(); } return ret; }
HRESULT CreateLink(LPCWSTR aTargetPath, LPCWSTR aShortcutPath, LPCWSTR aDescription) { HRESULT hres; IShellLink* psl; wprintf(L"creating shortcut: '%s'\n", aShortcutPath); CoInitialize(nullptr); hres = CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl); if (FAILED(hres)) { CoUninitialize(); return hres; } psl->SetPath(aTargetPath); if (aDescription) { psl->SetDescription(aDescription); } else { psl->SetDescription(L""); } IPersistFile* ppf = nullptr; hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf); if (SUCCEEDED(hres)) { hres = ppf->Save(aShortcutPath, TRUE); ppf->Release(); } psl->Release(); CoUninitialize(); return hres; }
bool CreateShellLink(const char *filepath, const char *linkpath, const char *desc, int icon) { HRESULT hres; IShellLink* psl; IPersistFile* ppf; hres = CoInitialize(NULL); hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (PVOID *) &psl); if(SUCCEEDED(hres)) { psl->SetPath(filepath); psl->SetDescription(desc); if(icon >= 0) psl->SetIconLocation(filepath, icon); hres = psl->QueryInterface(IID_IPersistFile, (PVOID *) &ppf); if (SUCCEEDED(hres)) { WCHAR szPath[_MAX_PATH] = { 0 }; MultiByteToWideChar(CP_ACP, 0, linkpath, strlen(linkpath), szPath, _MAX_PATH); hres = ppf->Save(szPath, TRUE); ppf->Release(); } } psl->Release(); CoUninitialize(); return SUCCEEDED(hres); }
bool CShortCut::Create(void) { bool results; HRESULT hres; IShellLink *psl; IPersistFile *ppf; wchar_t wsz[MAX_PATH]; CoInitialize(NULL); hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&psl); results=false; if (SUCCEEDED(hres)) { psl->SetPath(LinkFile.c_str()); psl->SetWorkingDirectory(LinkPath.c_str()); psl->SetDescription(LinkDescription.c_str()); hres = psl->QueryInterface(IID_IPersistFile, (void**)&ppf); if (SUCCEEDED(hres)) { MultiByteToWideChar(CP_ACP, 0, FileName.c_str(), -1, wsz, MAX_PATH); hres = ppf->Save(wsz, true); ppf->Release(); results=true; } psl->Release(); } CoUninitialize(); return results; }
HRESULT ResolveShortcut(const TCHAR* LnkFile, TCHAR* FilePath, TCHAR* LnkDesc, TCHAR* WorkDir) { CoInitialize(NULL); HRESULT hres; IShellLink* psl; WIN32_FIND_DATA wfd; TCHAR strfilepath[MAX_PATH]; TCHAR strlnkdesc[INFOTIPSIZE]; TCHAR strworkdir[MAX_PATH]; USES_CONVERSION; hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID *) &psl); if (SUCCEEDED(hres)) { IPersistFile* ppf; hres = psl->QueryInterface(IID_IPersistFile, (LPVOID *)&ppf); if (SUCCEEDED(hres)) { hres = ppf->Load(LnkFile, STGM_READ); if (SUCCEEDED(hres)) { hres = psl->Resolve(GetDesktopWindow(), 0); if (SUCCEEDED(hres)) { hres = psl->GetPath(strfilepath,MAX_PATH, &wfd, SLGP_UNCPRIORITY ); if (SUCCEEDED(hres)) { _tcscpy(FilePath, strfilepath); hres = psl->GetDescription(strlnkdesc,INFOTIPSIZE); } if (SUCCEEDED(hres)) { _tcscpy(LnkDesc,strlnkdesc); hres = psl->GetWorkingDirectory(strworkdir,MAX_PATH); } if (SUCCEEDED(hres)) { _tcscpy(WorkDir,strworkdir); } } } ppf->Release(); } psl->Release(); } CoUninitialize(); return hres; }
// http://msdn.microsoft.com/en-us/library/aa969393.aspx HRESULT CreateLink(LPCWSTR lpszPathObj, LPCWSTR lpszArguments, LPCWSTR lpszPathLink, LPCWSTR lpszDesc) { HRESULT hres; IShellLink* psl; CoInitialize(0); // Get a pointer to the IShellLink interface. It is assumed that CoInitialize // has already been called. hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl); if (SUCCEEDED(hres)) { IPersistFile* ppf; // Set the path to the shortcut target and add the description. psl->SetPath(lpszPathObj); psl->SetArguments(lpszArguments); psl->SetDescription(lpszDesc); // Query IShellLink for the IPersistFile interface, used for saving the // shortcut in persistent storage. hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf); if (SUCCEEDED(hres)) { // Save the link by calling IPersistFile::Save. hres = ppf->Save(lpszPathLink, TRUE); ppf->Release(); } psl->Release(); } CoUninitialize(); return hres; }
bool ShellIO::CreateLink(CString fileName, LinkInfo *linkInfo, int flags) { IShellLink *psl; IPersistFile *ppf; HRESULT hRes; bool ret = FALSE; hRes = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&psl); if(SUCCEEDED(hRes)) { hRes = psl->QueryInterface(IID_IPersistFile, (void**)&ppf); if(SUCCEEDED(hRes)) { if((flags & LI_DESCRIPTION) == LI_DESCRIPTION) { psl->SetDescription(linkInfo->description.GetBuffer()); } if((flags & LI_PATH) == LI_PATH) { psl->SetPath(linkInfo->path.GetBuffer()); } if((flags & LI_ARGUMENTS) == LI_ARGUMENTS) { psl->SetArguments(linkInfo->arguments.GetBuffer()); } if((flags & LI_WORKDIRECTORY) == LI_WORKDIRECTORY) { psl->SetWorkingDirectory(linkInfo->workDirectory.GetBuffer()); } if((flags & LI_ICONLOCATION) == LI_ICONLOCATION) { psl->SetIconLocation(linkInfo->iconLocation.GetBuffer(), linkInfo->iconIndex); } hRes = ppf->Save(fileName.GetBuffer(), FALSE); ret = SUCCEEDED(hRes); ppf->Release(); } psl->Release(); } return ret; }
// // 创建快捷方式 // bool Shortcut_Create(const char* szPath, const char* szWorkingPath, const char* szLink) { HRESULT hres ; IShellLink * psl ; IPersistFile* ppf ; WCHAR wsz[MAX_PATH]= {L""}; ////初始化COM CoInitialize (NULL); //创建一个IShellLink实例 hres = CoCreateInstance( CLSID_ShellLink, NULL,CLSCTX_INPROC_SERVER, IID_IShellLink, (void **)&psl); if( FAILED( hres)) { CoUninitialize (); return false ; } //设置目标应用程序 psl->SetPath( szPath); psl->SetWorkingDirectory(szWorkingPath); //从IShellLink获取其IPersistFile接口 //用于保存快捷方式的数据文件 (*.lnk) hres = psl->QueryInterface(IID_IPersistFile, (void**)&ppf) ; if( FAILED( hres)) { CoUninitialize (); return false ; } // 确保数据文件名为ANSI格式 MultiByteToWideChar( CP_ACP, 0, szLink, -1, wsz, MAX_PATH) ; //调用IPersistFile::Save //保存快捷方式的数据文件 (*.lnk) hres = ppf->Save(wsz, STGM_READWRITE); //释放IPersistFile和IShellLink接口 ppf->Release() ; psl->Release() ; CoUninitialize(); return true; }
HRESULT _CreateShellLink(PCSTR pszArguments, PCWSTR pszTitle, LPCSTR szDescription, IShellLink **ppsl) { IShellLink *psl; HRESULT hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&psl)); if (SUCCEEDED(hr)) { // Determine our executable's file path so the task will execute this application CHAR szAppPath[MAX_PATH]; if (GetModuleFileName(NULL, szAppPath, ARRAYSIZE(szAppPath))) { hr = psl->SetPath(szAppPath); if (SUCCEEDED(hr)) { hr = psl->SetArguments(pszArguments); if (SUCCEEDED(hr)) { hr = psl->SetDescription( szDescription ); if (SUCCEEDED(hr)) { // The title property is required on Jump List items provided as an IShellLink // instance. This value is used as the display name in the Jump List. IPropertyStore *pps; hr = psl->QueryInterface(IID_PPV_ARGS(&pps)); if (SUCCEEDED(hr)) { PROPVARIANT propvar; hr = InitPropVariantFromString(pszTitle, &propvar); if (SUCCEEDED(hr)) { hr = pps->SetValue(PKEY_Title, propvar); if (SUCCEEDED(hr)) { hr = pps->Commit(); if (SUCCEEDED(hr)) { hr = psl->QueryInterface(IID_PPV_ARGS(ppsl)); } } PropVariantClear(&propvar); } pps->Release(); } } } } } else { hr = HRESULT_FROM_WIN32(GetLastError()); } psl->Release(); } return hr; }
// @pymethod |PyIShellLink|SetHotkey|Sets the hot key for a shell link object. PyObject *PyIShellLink::SetHotkey(PyObject *self, PyObject *args) { IShellLink *pISL = GetI(self); if ( pISL == NULL ) return NULL; // @pyparm int|wHotkey||The virtual key code is in the low-order byte, and the modifier // flags are in the high-order byte. The modifier flags can be a combination of the // values specified in the description of the <om PyIShellLink::GetHotkey> method. WORD wHotkey; if ( !PyArg_ParseTuple(args, "i:SetHotkey", &wHotkey) ) return NULL; HRESULT hr; PY_INTERFACE_PRECALL; hr = pISL->SetHotkey( wHotkey ); PY_INTERFACE_POSTCALL; if ( FAILED(hr) ) return OleSetOleError(hr); Py_INCREF(Py_None); return Py_None; }
// @pymethod |PyIShellLink|SetPath|Sets the path and file name of a shell link object. PyObject *PyIShellLink::SetPath(PyObject *self, PyObject *args) { IShellLink *pISL = GetI(self); if ( pISL == NULL ) return NULL; PyObject *obName; if ( !PyArg_ParseTuple(args, "O:SetDescription", &obName) ) return NULL; TCHAR *pszName; if (!PyWinObject_AsTCHAR(obName, &pszName)) return NULL; // @pyparm string|path||The path and filename of the link. HRESULT hr; PY_INTERFACE_PRECALL; hr = pISL->SetPath( pszName ); PY_INTERFACE_POSTCALL; if ( FAILED(hr) ) return OleSetOleError(hr); Py_INCREF(Py_None); return Py_None; }