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 CreateLnkOnDesktop(const LPWSTR connTitle) { IShellLink *SLink; IPersistFile *PF; HRESULT HRes; TCHAR desktop_path[MAX_PATH] = TEXT(""); TCHAR pszFullLnkPath[MAX_PATH]; CoInitialize(NULL); ITEMIDLIST* pidl1 = NULL; SHGetFolderLocation(NULL, CSIDL_CONNECTIONS, NULL, 0, &pidl1); IShellFolder *desktop, *ncfolder; SHGetDesktopFolder(&desktop); desktop->BindToObject(pidl1, NULL, IID_IShellFolder, (void**)&ncfolder); IEnumIDList *items; ncfolder->EnumObjects(NULL, SHCONTF_NONFOLDERS, &items); ITEMIDLIST* pidl2 = NULL; while (S_OK == items->Next(1, &pidl2, NULL)) { STRRET sr = {STRRET_WSTR}; ncfolder->GetDisplayNameOf(pidl2, SHGDN_NORMAL, &sr); TCHAR buf[MAX_PATH] = TEXT(""); StrRetToBuf(&sr, pidl2, buf, MAX_PATH); if (0 == StrCmpI(buf, connTitle)) { ITEMIDLIST* pidl3 = ILCombine(pidl1, pidl2); HRESULT HRes = CoCreateInstance(CLSID_ShellLink, 0, CLSCTX_INPROC_SERVER, IID_IShellLink, ( LPVOID*)&SLink); SLink->SetIDList(pidl3); SHGetFolderPath(NULL, CSIDL_DESKTOP, NULL, 0, desktop_path); StringCbPrintf(pszFullLnkPath, MAX_PATH * sizeof(TCHAR), TEXT("%s\\%s.lnk"), desktop_path, connTitle); HRes = SLink->QueryInterface(IID_IPersistFile, (LPVOID*)&PF); HRes = PF->Save((LPCOLESTR)pszFullLnkPath, TRUE); PF->Release(); SLink->Release(); ILFree(pidl3); ILFree(pidl2); break; } ILFree(pidl2); pidl2 = NULL; } ncfolder->Release(); desktop->Release(); ILFree(pidl1); CoUninitialize(); }
void UiPlayer::onFileCreateProjectShortcut(void) { WCHAR shortcutPathBuff[MAX_PATH + 1] = {0}; OPENFILENAME ofn = {0}; ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = m_hwnd; ofn.lpstrFilter = L"Shortcut (*.lnk)\0*.lnk\0"; ofn.lpstrTitle = L"Create Project Shortcut"; ofn.Flags = OFN_DONTADDTORECENT | OFN_ENABLESIZING | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST; ofn.lpstrFile = shortcutPathBuff; ofn.nMaxFile = MAX_PATH; if (!GetSaveFileName(&ofn)) return; // Get a pointer to the IShellLink interface. It is assumed that CoInitialize // has already been called. IShellLink* psl; HRESULT hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl); if (SUCCEEDED(hres)) { IPersistFile* ppf; // args string args = m_project.makeCommandLine(); // Set the path to the shortcut target and add the description. psl->SetPath(__wargv[0]); wstring wargs; wargs.assign(args.begin(), args.end()); psl->SetArguments(wargs.c_str()); psl->SetDescription(L"UiPlayer"); // 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. size_t len = wcslen(shortcutPathBuff); if (_wcsicmp(shortcutPathBuff + len - 4, L".lnk") != 0) { wcscat_s(shortcutPathBuff, L".lnk"); } hres = ppf->Save(shortcutPathBuff, TRUE); ppf->Release(); } psl->Release(); } }
BOOL install_util::CreateLnkPath(std::wstring wsSourceFilePath, std::wstring wsDestLnkPath, std::wstring wsArgument, std::wstring wsAppId) { IShellLink *pisl = NULL; HRESULT hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&pisl); if (FAILED(hr)) { return FALSE; } pisl->SetPath(wsSourceFilePath.c_str()); pisl->SetArguments(wsArgument.c_str()); int nStart = wsSourceFilePath.find_last_of(_T("/\\")); pisl->SetWorkingDirectory(wsSourceFilePath.substr(0,nStart).c_str()); IPersistFile *plPF = NULL; hr = pisl->QueryInterface(IID_IPersistFile, (void**)&plPF); bool shortcut_existed = false; if (SUCCEEDED(hr)) { if (PathExists(wsDestLnkPath)) { shortcut_existed = true; install_util::DeleteFile(wsDestLnkPath.c_str()); } if (Win7OrLater() && !wsAppId.empty() && wsAppId.length() < 64) { IPropertyStore *piPS = NULL; if (SUCCEEDED(pisl->QueryInterface(IID_IPropertyStore, (void**)&piPS))) { PROPVARIANT property_value; if (SUCCEEDED(InitPropVariantFromString(wsAppId.c_str(), &property_value))) { if (piPS->SetValue(PKEY_AppUserModel_ID, property_value) == S_OK) piPS->Commit(); PropVariantClear(&property_value); } piPS->Release(); } } hr = plPF->Save(wsDestLnkPath.c_str(), TRUE); plPF->Release(); } pisl->Release(); SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL); return SUCCEEDED(hr); }
//----------------------------------------------------------------------------- // Purpose: Creates a shortcut file //----------------------------------------------------------------------------- bool CSystem::CreateShortcut(const char *linkFileName, const char *targetPath, const char *arguments, const char *workingDirectory, const char *iconFile) { #ifndef _X360 bool bSuccess = false; char temp[MAX_PATH]; strcpy(temp, linkFileName); // make sure it doesn't already exist struct _stat statBuf; if (_stat(linkFileName, &statBuf) != -1) return false; // Create the ShellLink object IShellLink *psl; HRESULT hres = ::CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*) &psl); if (SUCCEEDED(hres)) { // Set the target information from the link object psl->SetPath(targetPath); psl->SetArguments(arguments); if (workingDirectory && *workingDirectory) { psl->SetWorkingDirectory(workingDirectory); } if (iconFile && *iconFile) { psl->SetIconLocation(iconFile, 0); } // Bind the ShellLink object to the Persistent File IPersistFile *ppf; hres = psl->QueryInterface( IID_IPersistFile, (LPVOID *) &ppf); if (SUCCEEDED(hres)) { wchar_t wsz[MAX_PATH]; // Get a UNICODE wide string wsz from the link path MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, temp, -1, wsz, MAX_PATH); hres = ppf->Save(wsz, TRUE); if (SUCCEEDED(hres)) { bSuccess = true; } ppf->Release(); } psl->Release(); } return bSuccess; #else return false; #endif }
BOOL AFXAPI AfxResolveShortcut(CWnd* pWnd, LPCTSTR lpszFileIn, LPTSTR lpszFileOut, int cchPath) { USES_CONVERSION; AFX_COM com; IShellLink* psl; *lpszFileOut = 0; // assume failure // WINBUG: Win32s versions prior to Win32s 1.3b do not restore the // stack pointer correctly in SHGetFileInfo. All it does is return // failure anyway on Win32s, so here we just avoid calling it. if (afxData.bWin31) return FALSE; SHFILEINFO info; if ((SHGetFileInfo(lpszFileIn, 0, &info, sizeof(info), SHGFI_ATTRIBUTES) == 0) || !(info.dwAttributes & SFGAO_LINK)) { return FALSE; } if (FAILED(com.CreateInstance(CLSID_ShellLink, NULL, IID_IShellLink, (LPVOID*)&psl))) { return FALSE; } IPersistFile *ppf; if (SUCCEEDED(psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf))) { if (SUCCEEDED(ppf->Load(T2COLE(lpszFileIn), STGM_READ))) { /* Resolve the link, this may post UI to find the link */ if (SUCCEEDED(psl->Resolve(pWnd->GetSafeHwnd(), SLR_ANY_MATCH))) { //#ifndef _UNICODE psl->GetPath(lpszFileOut, cchPath, NULL, 0); /*#else char szTemp[_MAX_PATH]; psl->GetPath(szTemp, _MAX_PATH, NULL, 0); _mbstowcsz(lpszFileOut, szTemp, cchPath); #endif*/ return TRUE; } } ppf->Release(); } psl->Release(); return FALSE; }
//Taken from: http://www.cplusplus.com/forum/windows/64088/ bool ResolveShortcut(HWND hwnd, const wchar_t* szShortcutPath, char* szResolvedPath, size_t nSize) { if(szResolvedPath == NULL) return SUCCEEDED(E_INVALIDARG); //Initialize COM stuff CoInitialize(NULL); //Get a pointer to the IShellLink interface. IShellLink* psl = NULL; HRESULT hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl); if(SUCCEEDED(hres)) { //Get a pointer to the IPersistFile interface. IPersistFile* ppf = NULL; hres = psl->QueryInterface(IID_IPersistFile, (void**)&ppf); if(SUCCEEDED(hres)) { //Load the shortcut. hres = ppf->Load(szShortcutPath, STGM_READ); if(SUCCEEDED(hres)) { //Resolve the link. hres = psl->Resolve(hwnd, 0); if(SUCCEEDED(hres)) { //Get the path to the link target. char szGotPath[MAX_PATH] = {0}; hres = psl->GetPath(szGotPath, _countof(szGotPath), NULL, SLGP_SHORTPATH); if(SUCCEEDED(hres)) { strcpy_s(szResolvedPath, nSize, szGotPath); } } } //Release the pointer to the IPersistFile interface. ppf->Release(); } //Release the pointer to the IShellLink interface. psl->Release(); } //Uninitialize COM stuff CoUninitialize(); return SUCCEEDED(hres); }
HRESULT ResolveLink(HWND hwnd,DWORD fFlags,TCHAR *LinkFile,TCHAR *LinkPath,int nBufferSize) { IShellLink *pShellLink = NULL; IPersistFile *pPersistFile = NULL; SHFILEINFO shfi; WCHAR LinkFileW[MAX_PATH]; TCHAR ResolvedFilePath[MAX_PATH]; HRESULT hr; SHGetFileInfo(LinkFile,NULL,&shfi,sizeof(shfi),SHGFI_ATTRIBUTES); if(!(shfi.dwAttributes & SFGAO_LINK)) { StringCchCopy(LinkPath,nBufferSize,LinkFile); return E_UNEXPECTED; } hr = CoCreateInstance(CLSID_ShellLink,NULL,CLSCTX_INPROC_SERVER, IID_IShellLink,(LPVOID*)&pShellLink); if(hr == S_OK) { hr = pShellLink->QueryInterface(IID_IPersistFile,(LPVOID *)&pPersistFile); if(hr == S_OK) { #ifndef UNICODE MultiByteToWideChar(CP_ACP,0,LinkFile,-1,LinkFileW,MAX_PATH); #else StringCchCopy(LinkFileW,SIZEOF_ARRAY(LinkFileW),LinkFile); #endif hr = pPersistFile->Load(LinkFileW,STGM_READ); if(hr == S_OK) { pShellLink->Resolve(hwnd,fFlags); pShellLink->GetPath(ResolvedFilePath,MAX_PATH,NULL,SLGP_UNCPRIORITY); StringCchCopy(LinkPath,nBufferSize,ResolvedFilePath); } pPersistFile->Release(); } pShellLink->Release(); } return hr; }
// ショートカット作成 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; }
NS_IMETHODIMP nsFileProtocolHandler::ReadURLFile(nsIFile* aFile, nsIURI** aURI) { // IUniformResourceLocator isn't supported by VC5 (bless its little heart) #if _MSC_VER < 1200 || defined (WINCE) return NS_ERROR_NOT_AVAILABLE; #else nsAutoString path; nsresult rv = aFile->GetPath(path); if (NS_FAILED(rv)) return rv; if (path.Length() < 4) return NS_ERROR_NOT_AVAILABLE; if (!StringTail(path, 4).LowerCaseEqualsLiteral(".url")) return NS_ERROR_NOT_AVAILABLE; HRESULT result; rv = NS_ERROR_NOT_AVAILABLE; IUniformResourceLocator* urlLink = nsnull; result = ::CoCreateInstance(CLSID_InternetShortcut, NULL, CLSCTX_INPROC_SERVER, IID_IUniformResourceLocator, (void**)&urlLink); if (SUCCEEDED(result) && urlLink) { IPersistFile* urlFile = nsnull; result = urlLink->QueryInterface(IID_IPersistFile, (void**)&urlFile); if (SUCCEEDED(result) && urlFile) { result = urlFile->Load(path.get(), STGM_READ); if (SUCCEEDED(result) ) { LPSTR lpTemp = nsnull; // The URL this method will give us back seems to be already // escaped. Hence, do not do escaping of our own. result = urlLink->GetURL(&lpTemp); if (SUCCEEDED(result) && lpTemp) { rv = NS_NewURI(aURI, lpTemp); // free the string that GetURL alloc'd CoTaskMemFree(lpTemp); } } urlFile->Release(); } urlLink->Release(); } return rv; #endif //_MSC_VER < 1200 || defined (WINCE) }
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; }
void createLinks(const std::string name, const std::string &exe) { CoInitialize(NULL); HRESULT res; IShellLink *lnk = NULL; res = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, reinterpret_cast<void**>(&lnk)); if(!SUCCEEDED(res)) fail("Couldn't create shortcut links"); lnk->SetPath(exe.c_str()); lnk->SetDescription(name.c_str()); //lnk->SetIconLocation("where", 0); IPersistFile *pf = NULL; res = lnk->QueryInterface(IID_IPersistFile, reinterpret_cast<void**>(&pf)); if(!SUCCEEDED(res)) { lnk->Release(); fail("Couldn't create shortcut links"); } // Use this for links you don't want to highlight, ie. everything // except the main program link. May need some rewriting. /* PROPVARIANT pvar; pvar.vt = VT_BOOL; pvar.boolVal = VARIANT_TRUE; pf->SetValue(PKEY_AppUserModel_ExcludeFromShowInNewInstall, pvar); */ std::string lname = name + ".lnk"; // Save desktop link fs::path link = getPathCSIDL(CSIDL_DESKTOPDIRECTORY) / lname; pf->Save(toWide(link.string()), TRUE); // Create start menu directory link = getPathCSIDL(CSIDL_PROGRAMS) / name; fs::create_directories(link); // Save the start menu link link /= lname; pf->Save(toWide(link.string()), TRUE); pf->Release(); lnk->Release(); }
/*** @brief [Windows限定]新しいショートカットを作成します。 @param lnkpath: 作成するショートカットのパス reqpath: ショートカット先のパス @return true...成功 false...失敗 */ bool newshortcut(const std::string & lnkpath, const std::string & reqpath) { bool result = false; //COMの初期化 CoInitialize(NULL); //IShellLink インターフェースを取得 IShellLink* psl; HRESULT did_get_link = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, reinterpret_cast<void **>(&psl)); if (SUCCEEDED(did_get_link)) { //IPersistFile インターフェースを取得 IPersistFile* ppf; HRESULT did_get_file = psl->QueryInterface(IID_IPersistFile, reinterpret_cast<void **>(&ppf)); if (SUCCEEDED(did_get_file)) { //リンク元のパスを設定 HRESULT did_set_path = psl->SetPath(reqpath.c_str()); if (SUCCEEDED(did_set_path)) { #ifdef UNICODE HRESULT did_save = ppf->Save(lnkpath.c_str(), TRUE); if (SUCCEEDED(did_save)) { result = true; } #else WCHAR shortcut_file_path[MAX_PATH]; if (MultiByteToWideChar(CP_ACP, 0, lnkpath.c_str(), -1, shortcut_file_path, MAX_PATH) > 0) { //ショートカットファイルの保存 HRESULT did_save = ppf->Save(shortcut_file_path, TRUE); if (SUCCEEDED(did_save)) { result = true; } } #endif } ppf->Release(); } psl->Release(); } CoUninitialize(); return result; }
HRESULT CWipeFree::CreateIt(LPCTSTR pszShortcutFile, LPTSTR pszLink) { HRESULT hres; IShellLink* psl; // Get a pointer to the IShellLink interface. CoInitialize(NULL); 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)) { //WORD wsz[MAX_PATH]; // Set the path to the shell link target. hres = psl->SetPath(pszShortcutFile); if (!SUCCEEDED(hres)) AfxMessageBox(_T("SetPath failed!")); // Set the description of the shell link. hres = psl->SetWorkingDirectory(m_szCurDir); if (!SUCCEEDED(hres)) AfxMessageBox(_T("SetDescription failed!")); //// 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); hres = ppf->Save(pszLink, TRUE); // Release pointer to IPersistFile. ppf->Release(); } // Release pointer to IShellLink. psl->Release(); } CoUninitialize(); return hres; }
HRESULT CreateLink(LPCSTR lpszPathObj, LPCSTR lpszPathLink, LPCSTR lpszDesc) { HRESULT hres; IShellLink* psl; // Get a pointer to the IShellLink interface. 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->SetDescription(lpszDesc); // Query IShellLink for the IPersistFile interface for saving the // shortcut in persistent storage. hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf); if (SUCCEEDED(hres)) { WCHAR wsz[MAX_PATH]; // Ensure that the string is Unicode. MultiByteToWideChar(CP_ACP, 0, lpszPathLink, -1, wsz, MAX_PATH); // TODO: Check return value from MultiByteWideChar to ensure /* * if(!IconLocation.IsEmpty()) { hr = psl->SetIconLocation(IconLocation, IconIndex); #ifdef _DEBUG if(FAILED(hr)) TRACE("IconLocation not changed!\n"); #endif } */ // Save the link by calling IPersistFile::Save. hres = ppf->Save(wsz, TRUE); ppf->Release(); } psl->Release(); } return hres; }
CString CDropEdit::ExpandShortcut(CString &inFile) { CString outFile = ""; // Make sure we have a path ASSERT(inFile != _T("")); IShellLink* psl; HRESULT hres; LPTSTR lpsz = inFile.GetBuffer(MAX_PATH); // Create instance for shell link hres = ::CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*) &psl); if (SUCCEEDED(hres)) { // Get a pointer to the persist file interface IPersistFile* ppf; hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*) &ppf); if (SUCCEEDED(hres)) { // Make sure it's ANSI WCHAR wsz[MAX_PATH]; ::MultiByteToWideChar(CP_ACP, 0, lpsz, -1, wsz, MAX_PATH); // Load shortcut hres = ppf->Load(wsz, STGM_READ); if (SUCCEEDED(hres)) { WIN32_FIND_DATA wfd; // find the path from that HRESULT hres = psl->GetPath(outFile.GetBuffer(MAX_PATH), MAX_PATH, &wfd, SLGP_UNCPRIORITY); outFile.ReleaseBuffer(); } ppf->Release(); } psl->Release(); } inFile.ReleaseBuffer(); // if this fails, outFile == "" return outFile; }
/* #FN# Uses the shell's IShellLink and IPersistFile interfaces to create and store a shortcut to the specified object */ HRESULT /* #AS# Result of calling member functions of the interfaces */ CWizardStep1:: CreateLink( LPCSTR lpszPathObj, /* #IN# Address of a buffer containing the path of the object */ LPCSTR lpszPathLink, /* #IN# Address of a buffer containing the path where the shell link is to be stored */ LPCSTR lpszDesc /* #IN# Address of a buffer containing the description of the shell link */ ) { HRESULT hResult; IShellLink* psl; // Get a pointer to the IShellLink interface. hResult = CoCreateInstance( CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID *)&psl ); if( SUCCEEDED(hResult) ) { IPersistFile *ppf; // Set the path to the shortcut target and add the description. psl->SetPath( lpszPathObj ); psl->SetDescription( lpszDesc ); // Query IShellLink for the IPersistFile interface for saving the // shortcut in persistent storage. hResult = psl->QueryInterface( IID_IPersistFile, (LPVOID *)&ppf ); if( SUCCEEDED(hResult) ) { WCHAR wsz[ MAX_PATH + 1 ]; // Ensure that the string is Unicode. MultiByteToWideChar( CP_ACP, 0, lpszPathLink, -1, wsz, MAX_PATH ); // Save the link by calling IPersistFile::Save. hResult = ppf->Save( wsz, TRUE ); ppf->Release(); } psl->Release(); } return hResult; } /* #OF# CWizardStep1::CreateLink */
void MarkFileUnsafe(WCHAR *wFileName) { HRESULT hr; IZoneIdentifier *pizone = NULL; IPersistFile *piper = NULL; hr = CoInitializeEx( NULL, COINIT_APARTMENTTHREADED ); if (FAILED(hr)) { printf("[-] Failed CoInitializeEx - pEnroll [%x]\n", hr); goto error; } hr = CoCreateInstance(CLSID_PersistentZoneIdentifier, NULL, CLSCTX_INPROC_SERVER, IID_IZoneIdentifier, (void **)&pizone); if (FAILED(hr)) { printf("[-] Failed CoCreateInstance - pEnroll [%x]\n", hr); goto error; } if (pizone->SetId(URLZONE_INTERNET) != S_OK) { printf("[-] SetId failed\n"); goto error; } hr = pizone->QueryInterface(IID_IPersistFile, (void**)&piper); if (FAILED(hr)) { printf("[-] QueryInterface failed\n"); goto error; } hr = piper->Save(wFileName, TRUE); if (FAILED(hr)) { printf("[-] Failed Save\n"); goto error; } error: if (pizone != NULL) pizone->Release(); if (piper != NULL) piper->Release(); CoUninitialize(); }
//获得快捷方式文件所指向的路径 //lpwsLinkName:lnk文件的路径 //lpwsLinkPath:用于存放所指程序路径的缓冲区 //返回:HRESULT HRESULT common_getLnkPath(IN LPWSTR lpwsLinkName,OUT LPWSTR lpwsLinkPath) { HRESULT hResult; IShellLink *pIShellLink; WIN32_FIND_DATA wfd; //初始化 CoInitialize(NULL); //创建实例 hResult = CoCreateInstance((REFIID)CLSID_ShellLink,NULL,CLSCTX_INPROC_SERVER,(REFIID)IID_IShellLink,(LPVOID *)&pIShellLink); //如果成功 if (SUCCEEDED(hResult)) { IPersistFile *pIPersistFile; //查询相关信息 hResult = pIShellLink->QueryInterface((REFIID)IID_IPersistFile,(LPVOID *)&pIPersistFile); //如果查询成功 if (SUCCEEDED(hResult)) { //加载快捷方式文件 hResult = pIPersistFile->Load(lpwsLinkName, STGM_READ); //如果成功 if (SUCCEEDED(hResult)) { //解析 hResult = pIShellLink->Resolve(NULL,SLR_ANY_MATCH | SLR_NO_UI); if (SUCCEEDED(hResult)) { //获得快捷方式的制定的路径 hResult = pIShellLink->GetPath(lpwsLinkPath,MAX_PATH,&wfd,SLGP_SHORTPATH); } } pIPersistFile->Release(); } pIShellLink->Release(); } return hResult; }
// @pymethod |PyIPersistFile|IsDirty|Checks an object for changes since it was last saved to its current file. PyObject *PyIPersistFile::IsDirty(PyObject *self, PyObject *args) { IPersistFile *pIPF = GetI(self); if ( pIPF == NULL ) return NULL; if ( !PyArg_ParseTuple(args, ":IsDirty") ) return NULL; HRESULT hr; PY_INTERFACE_PRECALL; hr = pIPF->IsDirty( ); PY_INTERFACE_POSTCALL; // @rdesc This method returns the raw COM error code without raising the normal COM exception. // You should treat any error return codes as an indication that the object has changed. // Unless this method explicitly returns S_FALSE, assume that the object must be saved. return PyInt_FromLong(hr); }
//----------------------------------------------------------------------------- // Purpose: retrieves shortcut (.lnk) information //----------------------------------------------------------------------------- bool CSystem::GetShortcutTarget(const char *linkFileName, char *targetPath, char *arguments, int destBufferSizes) { #ifndef _X360 char temp[MAX_PATH]; strcpy(temp, linkFileName); strlwr(temp); targetPath[0] = 0; arguments[0] = 0; // Create the ShellLink object IShellLink *psl; HRESULT hres = ::CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*) &psl); if (SUCCEEDED(hres)) { IPersistFile *ppf; // Bind the ShellLink object to the Persistent File hres = psl->QueryInterface( IID_IPersistFile, (LPVOID *) &ppf); if (SUCCEEDED(hres)) { wchar_t wsz[MAX_PATH]; // Get a UNICODE wide string wsz from the link path MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, temp, -1, wsz, MAX_PATH); // Read the link into the persistent file hres = ppf->Load(wsz, 0); if (SUCCEEDED(hres)) { //Read the target information from the link object //UNC paths are supported (SLGP_UNCPRIORITY) psl->GetPath(targetPath, destBufferSizes, NULL, SLGP_UNCPRIORITY); //Read the arguments from the link object psl->GetArguments(arguments, destBufferSizes); } ppf->Release(); } psl->Release(); } return (targetPath[0] != 0); #else return false; #endif }
HRESULT CreateLink(LPCTSTR lpszPathObj, LPCTSTR lpszPathLink, LPCTSTR lpszDesc) { HRESULT hres; IShellLink* psl; // Get a pointer to the IShellLink interface. 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->SetDescription(lpszDesc); // Query IShellLink for the IPersistFile interface for saving the // shortcut in persistent storage. hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf); if (SUCCEEDED(hres)) { #if !( defined (_UNICODE) || defined(UNICODE) ) size_t length = wxConvFile.ToWChar(NULL, 0, lpszPathLink); wchar_t * UnicodePath = new wchar_t [length]; size_t res = wxConvFile.ToWChar(UnicodePath, length, lpszPathLink); (void)res; // warning fix when unicode=0 debug=0 assert(res != wxCONV_FAILED); #else const wchar_t *UnicodePath = lpszPathLink; #endif // Save the link by calling IPersistFile::Save. hres = ppf->Save(UnicodePath, TRUE); ppf->Release(); #if !( defined (_UNICODE) || defined(UNICODE) ) delete [] UnicodePath; #endif } psl->Release(); } return hres; }
NTSTATUS GetPathFromLinkFile(PCWSTR LinkFilePath, PWCHAR FullPath, ULONG BufferCount) { HRESULT hResult; IShellLinkW *ShellLink; IPersistFile *PersistFile; CoInitialize(NULL); hResult = S_OK; ShellLink = NULL; PersistFile = NULL; LOOP_ONCE { hResult = CoCreateInstance( CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLinkW, (PVOID *)&ShellLink ); if (FAILED(hResult)) break; hResult = ShellLink->QueryInterface(IID_IPersistFile, (PVOID *)&PersistFile); if (FAILED(hResult)) break; hResult = PersistFile->Load(LinkFilePath, 0); if (FAILED(hResult)) break; hResult = ShellLink->GetPath(FullPath, BufferCount, NULL, SLGP_UNCPRIORITY); } if (PersistFile != NULL) PersistFile->Release(); if (ShellLink != NULL) ShellLink->Release(); CoUninitialize(); return hResult; }
void AGSWin32::create_shortcut(const char *pathToEXE, const char *workingFolder, const char *arguments, const char *shortcutPath) { IShellLink* pShellLink = NULL; HRESULT hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&pShellLink); if ((SUCCEEDED(hr)) && (pShellLink != NULL)) { IPersistFile *pPersistFile = NULL; if (FAILED(pShellLink->QueryInterface(IID_IPersistFile, (void**)&pPersistFile))) { this->DisplayAlert("Unable to add game tasks: QueryInterface for IPersistFile failed"); pShellLink->Release(); return; } // Set the path to the shortcut target and add the description if (FAILED(pShellLink->SetPath(pathToEXE))) { this->DisplayAlert("Unable to add game tasks: SetPath failed"); } else if (FAILED(pShellLink->SetWorkingDirectory(workingFolder))) { this->DisplayAlert("Unable to add game tasks: SetWorkingDirectory failed"); } else if ((arguments != NULL) && (FAILED(pShellLink->SetArguments(arguments)))) { this->DisplayAlert("Unable to add game tasks: SetArguments failed"); } else { WCHAR wstrTemp[MAX_PATH] = {0}; MultiByteToWideChar(CP_ACP, 0, shortcutPath, -1, wstrTemp, MAX_PATH); if (FAILED(pPersistFile->Save(wstrTemp, TRUE))) { this->DisplayAlert("Unable to add game tasks: IPersistFile::Save failed"); } } pPersistFile->Release(); } if (pShellLink) pShellLink->Release(); }
//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; }
int create_shortcut(string src_path, string dst_path, string working_directory) { #ifdef _WIN32 CoInitialize(NULL); IShellLink* pShellLink = NULL; HRESULT hres; hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_ALL, IID_IShellLink, (void**)&pShellLink); if (SUCCEEDED(hres)) { pShellLink->SetPath(src_path.c_str()); pShellLink->SetDescription(""); pShellLink->SetIconLocation(src_path.c_str(), 0); pShellLink->SetWorkingDirectory(working_directory.c_str()); IPersistFile *pPersistFile; hres = pShellLink->QueryInterface(IID_IPersistFile, (void**)&pPersistFile); if (SUCCEEDED(hres)) { hres = pPersistFile->Save(CA2W(dst_path.c_str()), TRUE); pPersistFile->Release(); } else { console_log("Error 2"); return 2; } pShellLink->Release(); } else { console_log("Error 1"); return 1; } #elif __APPLE__ //todo: port to OSX #endif return 0; }
//----------------------------------------------------------------------------- // Purpose: sets shortcut (.lnk) information //----------------------------------------------------------------------------- bool CSystem::ModifyShortcutTarget(const char *linkFileName, const char *targetPath, const char *arguments, const char *workingDirectory) { #ifndef _X360 bool bSuccess = false; char temp[MAX_PATH]; strcpy(temp, linkFileName); strlwr(temp); // Create the ShellLink object IShellLink *psl; HRESULT hres = ::CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*) &psl); if (SUCCEEDED(hres)) { IPersistFile *ppf; // Bind the ShellLink object to the Persistent File hres = psl->QueryInterface( IID_IPersistFile, (LPVOID *) &ppf); if (SUCCEEDED(hres)) { wchar_t wsz[MAX_PATH]; // Get a UNICODE wide string wsz from the link path MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, temp, -1, wsz, MAX_PATH); // Read the link into the persistent file hres = ppf->Load(wsz, 0); if (SUCCEEDED(hres)) { // Set the target information from the link object psl->SetPath(targetPath); psl->SetArguments(arguments); psl->SetWorkingDirectory(workingDirectory); bSuccess = true; ppf->Save(wsz, TRUE); } ppf->Release(); } psl->Release(); } return bSuccess; #else return false; #endif }
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 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; }