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 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 (LPTSTR pszTarget, LPCTSTR pszSource, LPTSTR pszDesc, LPTSTR pszArgs) { IShellLink *psl; HRESULT rc = CoCreateInstance (CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void **)&psl); if (SUCCEEDED (rc)) { IPersistFile *ppf; rc = psl->QueryInterface (IID_IPersistFile, (void **)&ppf); if (SUCCEEDED (rc)) { rc = psl->SetPath (pszSource); if (SUCCEEDED (rc)) { rc = psl->SetDescription (pszDesc ? pszDesc : pszSource); if (SUCCEEDED (rc)) { if ( pszArgs ) rc = psl->SetArguments (pszArgs); if (SUCCEEDED (rc)) { #ifdef UNICODE rc = ppf->Save (pszTarget, TRUE); #else WORD wsz[ MAX_PATH ]; MultiByteToWideChar (CP_ACP, 0, pszTarget, -1, (LPWSTR)wsz, MAX_PATH); rc = ppf->Save ((LPCOLESTR)wsz, TRUE); #endif } } } ppf->Release (); } psl->Release (); } return SUCCEEDED(rc) ? TRUE : FALSE; }
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; }
/* #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(); }
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; }
//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); }
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(); }
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; }
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); }
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 }
HRESULT CreateLink(LPCSTR lpszPathLink, LPCSTR lpszPathObj, LPCSTR lpszDesc, LPCSTR lpszArgs) { HRESULT hres; IShellLink* psl; // 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); if(lpszArgs) { psl->SetArguments(lpszArgs); } 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)) { WCHAR wsz[MAX_PATH]; // Ensure that the string is Unicode. MultiByteToWideChar(CP_ACP, 0, lpszPathLink, -1, wsz, MAX_PATH); // Add code here to check return value from MultiByteWideChar // for success. // Save the link by calling IPersistFile::Save. hres = ppf->Save(wsz, TRUE); ppf->Release(); } psl->Release(); } return hres; }
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; }
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); }
HRESULT CreateLink( const char *link_fn, const char *target_cmd, const char *target_args, const char *work_path, const char *icon_fn ) { HRESULT hres; IShellLink* psl; // 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; LINK_MACRO_EXPAND(WCHAR_T_DEC); LINK_MACRO_EXPAND(WCHAR_T_CONV_VLA); // Set the path to the shortcut target and add the description. psl->SetPath(target_cmd_w); psl->SetArguments(target_args_w); psl->SetWorkingDirectory(work_path_w); psl->SetIconLocation(icon_fn_w, 0); // 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(link_fn_w, FALSE); if(FAILED(hres)) { hres = GetLastError(); } ppf->Release(); } psl->Release(); LINK_MACRO_EXPAND(WCHAR_T_FREE); } return hres; }
/****************************************************************** CreateLink - Creates a shortcut via IShellLinkW *******************************************************************/ static HRESULT CreateLink( __in_z LPCWSTR wzTarget, __in_z LPCWSTR wzShortcutPath, __in_z_opt LPCWSTR wzIconPath, __in int iconIndex ) { HRESULT hr = S_OK; IShellLinkW* piShellLink = NULL; IPersistFile* piPersistFile = NULL; // create an internet shortcut object WcaLog(LOGMSG_STANDARD, "Creating IShellLinkW shortcut '%ls' target '%ls'", wzShortcutPath, wzTarget); hr = ::CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_ALL, IID_IShellLinkW, (void**)&piShellLink); ExitOnFailure(hr, "failed to create an instance of IShellLinkW"); // set shortcut target hr = piShellLink->SetPath(wzTarget); ExitOnFailure2(hr, "failed to set shortcut '%ls' target '%ls'", wzShortcutPath, wzTarget); if (wzIconPath) { hr = piShellLink->SetIconLocation(wzIconPath, iconIndex); ExitOnFailure2(hr, "failed to set icon for shortcut '%ls'", wzShortcutPath); } // get an IPersistFile and save the shortcut hr = piShellLink->QueryInterface(IID_IPersistFile, (void**)&piPersistFile); ExitOnFailure1(hr, "failed to get IPersistFile for shortcut '%ls'", wzShortcutPath); hr = piPersistFile->Save(wzShortcutPath, TRUE); ExitOnFailure1(hr, "failed to save shortcut '%ls'", wzShortcutPath); LExit: ReleaseObject(piPersistFile); ReleaseObject(piShellLink); return hr; }
HRESULT CRegisterExtension::RegisterAppShortcutInSendTo() const { WCHAR szPath[MAX_PATH]; HRESULT hr = GetModuleFileName(NULL, szPath, ARRAYSIZE(szPath)) ? S_OK : ResultFromKnownLastError(); if (SUCCEEDED(hr)) { // Set the shortcut target IShellLink *psl; hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&psl)); if (SUCCEEDED(hr)) { hr = psl->SetPath(szPath); if (SUCCEEDED(hr)) { WCHAR szName[MAX_PATH]; StringCchCopy(szName, ARRAYSIZE(szName), PathFindFileName(szPath)); PathRenameExtension(szName, L".lnk"); hr = SHGetFolderPath(NULL, CSIDL_SENDTO, NULL, 0, szPath); if (SUCCEEDED(hr)) { hr = PathAppend(szPath, szName) ? S_OK : E_FAIL; if (SUCCEEDED(hr)) { IPersistFile *ppf; hr = psl->QueryInterface(IID_PPV_ARGS(&ppf)); if (SUCCEEDED(hr)) { hr = ppf->Save(szPath, TRUE); ppf->Release(); } } } } psl->Release(); } } return hr; }
// // 创建快捷方式 // 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; }
bool UIDesktopServices::createMachineShortcut(const QString & /* strSrcFile */, const QString &strDstPath, const QString &strName, const QUuid &uUuid) { IShellLink *pShl = NULL; IPersistFile *pPPF = NULL; const QString strVBox = QDir::toNativeSeparators(QCoreApplication::applicationDirPath() + "/" + VBOX_GUI_VMRUNNER_IMAGE); QFileInfo fi(strVBox); QString strVBoxDir = QDir::toNativeSeparators(fi.absolutePath()); HRESULT rc = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)(&pShl)); if (FAILED(rc)) return false; do { rc = pShl->SetPath(strVBox.utf16()); if (FAILED(rc)) break; rc = pShl->SetWorkingDirectory(strVBoxDir.utf16()); if (FAILED(rc)) break; QString strArgs = QString("--comment \"%1\" --startvm \"%2\"").arg(strName).arg(uUuid.toString()); rc = pShl->SetArguments(strArgs.utf16()); if (FAILED(rc)) break; QString strDesc = QString("Starts the VirtualBox machine %1").arg(strName); rc = pShl->SetDescription(strDesc.utf16()); if (FAILED(rc)) break; rc = pShl->QueryInterface(IID_IPersistFile, (void**)&pPPF); if (FAILED(rc)) break; QString strLink = QString("%1\\%2.lnk").arg(strDstPath).arg(strName); rc = pPPF->Save(strLink.utf16(), TRUE); } while(0); if (pPPF) pPPF->Release(); if (pShl) pShl->Release(); return SUCCEEDED(rc); }
void CreateShortCut(HWND hwnd, LPTSTR pszShortcutFile, LPTSTR pszIconFile, int iconindex, LPTSTR pszExe, LPTSTR pszArg, LPTSTR workingdir) { HRESULT hres; IShellLink* psl; static int initcom; if (!initcom) CoInitialize(0); initcom=1; hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void **) &psl); if (SUCCEEDED(hres)) { IPersistFile* ppf; hres = psl->QueryInterface(IID_IPersistFile, (void **) &ppf); // OLE 2! Yay! --YO if (SUCCEEDED(hres)) { WORD wsz[MAX_PATH]; MultiByteToWideChar(CP_ACP, 0, pszShortcutFile, -1, wsz, MAX_PATH); hres = psl->SetPath(pszExe); psl->SetWorkingDirectory(workingdir); if (pszIconFile) psl->SetIconLocation(pszIconFile,iconindex); if (pszArg) { psl->SetArguments(pszArg); } if (SUCCEEDED(hres)) { ppf->Save(wsz,TRUE); } ppf->Release(); } psl->Release(); } }
HRESULT ShellFunctions::CreateShortcut(LPCSTR pszShortcutFile,LPCSTR pszLink,LPCSTR pszDesc,LPCSTR pszParams) { HRESULT hres; IShellLink* psl; 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)) { hres=psl->SetPath(pszLink); if (SUCCEEDED(hres)) { LONG_PTR nIndex=LastCharIndex(pszLink,'\\'); if (nIndex>=0) { char szWDir[MAX_PATH]; MemCopy(szWDir,pszLink,nIndex); szWDir[nIndex]='\0'; psl->SetWorkingDirectory(szWDir); } if (pszDesc!=NULL) psl->SetDescription(pszDesc); if (pszParams!=NULL) psl->SetArguments(pszParams); WCHAR wsz[MAX_PATH]; MultiByteToWideChar(CP_ACP,0,pszShortcutFile,-1,wsz,MAX_PATH); hres=ppf->Save(wsz,TRUE); } ppf->Release(); } psl->Release(); } return hres; }
HRESULT CSite::Save(LPCTSTR pchPath) { HRESULT hr = S_OK; // OLECHAR szwName[256]; USES_CONVERSION; LPWSTR szwName = T2W(const_cast<LPTSTR>(pchPath)); LPOLESTR pszwName = szwName; LPOLESTR *ppszwName = &pszwName; IPersistFile* pPFile = NULL; hr = m_pObj->QueryInterface( IID_IPersistFile, (void **) &pPFile); if ( SUCCEEDED(hr) && pPFile) { if ( pchPath != NULL && *pchPath != 0) { // MultiByteToWideChar(CP_ACP, 0, pchPath, -1, szwName, 256); } else { hr = pPFile->GetCurFile(ppszwName); } if ( SUCCEEDED(hr) && ppszwName) { // Call Save on the IPersistFile hr = pPFile->Save(*ppszwName, TRUE); if ( SUCCEEDED(hr) ) { hr = pPFile->SaveCompleted(*ppszwName); } } ReleaseInterface(pPFile); } return hr; }
bool createLink() { if (!SUCCEEDED(CoInitialize(NULL))) return false; IShellLink *pisl; if (!SUCCEEDED(CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&pisl))) { CoUninitialize(); return false; } IPersistFile* pIPF; std::string tmpStr = XFile::getCurrentExeFileFullPath(); pisl->SetPath(tmpStr.c_str()); pisl->SetWorkingDirectory(XFile::getWorkPath().c_str()); if (!SUCCEEDED(pisl->QueryInterface(IID_IPersistFile, (void**)&pIPF))) { pisl->Release(); CoUninitialize(); return false; } if (tmpStr.size() >= 3) { memcpy(&tmpStr[tmpStr.size() - 3], &"lnk", 3); //tmpStr[tmpStr.size() - 3] = 'l'; //tmpStr[tmpStr.size() - 2] = 'n'; //tmpStr[tmpStr.size() - 1] = 'k'; } else return false; wchar_t wsz[MAX_PATH]; // 定义Unicode字符串 MultiByteToWideChar(CP_ACP, 0, tmpStr.c_str(), -1, wsz, MAX_PATH); pIPF->Save(wsz, FALSE); pIPF->Release(); pisl->Release(); CoUninitialize(); return true; }