bool ShellIO::GetLinkInfo(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)) { hRes = ppf->Load(fileName.GetBuffer(), NULL); if(SUCCEEDED(hRes)) { wchar_t buf[1000]; if((flags & LI_DESCRIPTION) == LI_DESCRIPTION) { memset(buf, 0, sizeof(buf)); hRes = psl->GetDescription(buf, 1000); if(SUCCEEDED(hRes) && (wcslen(buf) > 0)) { linkInfo->description = buf; ret = true; } else { linkInfo->description.Empty(); } } if((flags & LI_PATH) == LI_PATH) { LPITEMIDLIST pidl; hRes = psl->GetIDList(&pidl); if(SUCCEEDED(hRes) && pidl) { linkInfo->path = PIDLToString(pidl, NULL, SHGDN_FORPARSING); ILFree(pidl); ret = !linkInfo->path.IsEmpty(); } else { linkInfo->path.Empty(); } } if((flags & LI_ARGUMENTS) == LI_ARGUMENTS) { memset(buf, 0, sizeof(buf)); hRes = psl->GetArguments(buf, 1000); if(SUCCEEDED(hRes) && (wcslen(buf) > 0)) { linkInfo->arguments = buf; ret = true; } else { linkInfo->arguments.Empty(); } } if((flags & LI_WORKDIRECTORY) == LI_WORKDIRECTORY) { memset(buf, 0, sizeof(buf)); hRes = psl->GetWorkingDirectory(buf, 1000); if(SUCCEEDED(hRes) && (wcslen(buf) > 0)) { linkInfo->workDirectory = buf; ret = true; } else { linkInfo->workDirectory.Empty(); } } if((flags & LI_ICONLOCATION) == LI_ICONLOCATION) { int iIcon; memset(buf, 0, sizeof(buf)); hRes = psl->GetIconLocation(buf, 1000, &iIcon); if(SUCCEEDED(hRes) && (wcslen(buf) > 0)) { linkInfo->iconLocation = buf; linkInfo->iconIndex = iIcon; ret = true; } else { linkInfo->iconLocation.Empty(); linkInfo->iconIndex = 0; } } } ppf->Release(); } psl->Release(); } return ret; }
// @pymethod str|PyIShellLink|GetArguments|Retrieves the command-line arguments associated with a shell link object. PyObject *PyIShellLink::GetArguments(PyObject *self, PyObject *args) { IShellLink *pISL = GetI(self); if ( pISL == NULL ) return NULL; // @pyparm int|cchMaxName|1024|Number of characters to fetch. int cchMaxName = 1024; if ( !PyArg_ParseTuple(args, "|i:GetArguments", &cchMaxName) ) return NULL; HRESULT hr; TCHAR *pszName = (TCHAR *)malloc(cchMaxName * sizeof(TCHAR) ); if (pszName==NULL) { PyErr_SetString(PyExc_MemoryError, "allocating string buffer"); return NULL; } PY_INTERFACE_PRECALL; hr = pISL->GetArguments( pszName, cchMaxName ); PY_INTERFACE_POSTCALL; PyObject *ret; if ( FAILED(hr) ) ret = OleSetOleError(hr); else ret = PyWinObject_FromTCHAR(pszName); free(pszName); return ret; }
/* リンクの解決 あらかじめ、CoInitialize(NULL); を実行しておくこと */ BOOL ReadLinkU8(LPCSTR src, LPSTR dest, LPSTR arg) { IShellLink *shellLink; IPersistFile *persistFile; WCHAR wbuf[MAX_PATH]; BOOL ret = FALSE; if (SUCCEEDED(CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLinkW, (void **)&shellLink))) { if (SUCCEEDED(shellLink->QueryInterface(IID_IPersistFile, (void **)&persistFile))) { U8toW(src, wbuf, wsizeof(wbuf)); if (SUCCEEDED(persistFile->Load(wbuf, STGM_READ))) { if (SUCCEEDED(shellLink->GetPath((char *)wbuf, MAX_PATH, NULL, SLGP_SHORTPATH))) { WtoU8(wbuf, dest, MAX_PATH_U8); shellLink->GetArguments((char *)wbuf, MAX_PATH); WtoU8(wbuf, arg, MAX_PATH_U8); ret = TRUE; } } persistFile->Release(); } shellLink->Release(); } return ret; }
BOOL ReadLinkV(void *src, void *dest, void *arg) { IShellLink *shellLink; // 実際は IShellLinkA or IShellLinkW IPersistFile *persistFile; WCHAR wbuf[MAX_PATH]; BOOL ret = FALSE; if (SUCCEEDED(CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLinkV, (void **)&shellLink))) { if (SUCCEEDED(shellLink->QueryInterface(IID_IPersistFile, (void **)&persistFile))) { if (!IS_WINNT_V) { ::MultiByteToWideChar(CP_ACP, 0, (char *)src, -1, wbuf, MAX_PATH); src = wbuf; } if (SUCCEEDED(persistFile->Load((WCHAR *)src, STGM_READ))) { if (SUCCEEDED(shellLink->GetPath((char *)dest, MAX_PATH, NULL, 0))) { if (arg) { shellLink->GetArguments((char *)arg, MAX_PATH); } ret = TRUE; } } persistFile->Release(); } shellLink->Release(); } return ret; }
//----------------------------------------------------------------------------- // 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 }
bool FileSystemManager::getShortcutTarget(QString shortcutFileName, QString * targetOut, QString * argsOut, QString * workingDirOut) { assert(targetOut); // return true if _any_ of the attributes can be resolved bool targetResolved = false; bool result = false; IShellLink * psl = NULL; // Accounting for the newer "Advertised Shortcuts" which refer to MSI operations which may pre-empt the // launching of the specified shortcut DWORD targetPathLen = MAX_PATH; TCHAR targetPath[MAX_PATH]; TCHAR productCode[MAX_PATH]; TCHAR featureId[MAX_PATH]; TCHAR componentCode[MAX_PATH]; if (pfnMsiGetShortcutTarget && pfnMsiGetComponentPath) { if (ERROR_SUCCESS == pfnMsiGetShortcutTarget((LPCTSTR) shortcutFileName.utf16(), productCode, featureId, componentCode)) { if (INSTALLSTATE_LOCAL == pfnMsiGetComponentPath(productCode, componentCode, targetPath, &targetPathLen)) { *targetOut = QString::fromUtf16((const ushort *) targetPath); targetResolved = true; result = true; } } } // Get a pointer to the IShellLink interface. TCHAR args[MAX_PATH]; TCHAR workingDir[MAX_PATH]; HRESULT hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID *) &psl); if (SUCCEEDED(hres)) { IPersistFile * ppf = NULL; // Get a pointer to the IPersistFile interface. if (SUCCEEDED(psl->QueryInterface(IID_IPersistFile, (LPVOID *) &ppf))) { // Load the shortcut. if (SUCCEEDED(ppf->Load((LPCOLESTR) shortcutFileName.utf16(), STGM_READ))) { // Resolve the link. if (SUCCEEDED(psl->Resolve(winOS->GetWindowsHandle(), SLR_NOUPDATE | SLR_NO_UI))) { // Get the path to the link target. if (!targetResolved && SUCCEEDED(psl->GetPath(targetPath, MAX_PATH, NULL, 0))) { *targetOut = QString::fromUtf16((const ushort *) targetPath); result = true; } if (argsOut && SUCCEEDED(psl->GetArguments(args, MAX_PATH))) { *argsOut = QString::fromUtf16((const ushort *) args); result = true; } if (workingDirOut && SUCCEEDED(psl->GetWorkingDirectory(workingDir, MAX_PATH))) { *workingDirOut = QString::fromUtf16((const ushort *) workingDir); result = true; } } } // Release the pointer to the IPersistFile interface. ppf->Release(); } // Release the pointer to the IShellLink interface. psl->Release(); } return result; }
BOOL CInstall::CreateShellLink(LPCSTR description, LPCSTR program, LPCSTR arguments, LPCSTR icon, int nIconIndex) { HRESULT hres; IShellLink* psl; CHAR szLink[MAXSTR]; strcpy(szLink, m_szPrograms); strcat(szLink, "\\"); strcat(szLink, m_szTargetGroup); strcat(szLink, "\\"); strcat(szLink, description); strcat(szLink, ".LNK"); AddMessage("Adding shell link\n "); AddMessage(szLink); AddMessage("\n"); // Ensure string is UNICODE. WCHAR wsz[MAX_PATH]; MultiByteToWideChar(CP_ACP, 0, szLink, -1, wsz, MAX_PATH); // Save old shell link // 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. hres = psl->QueryInterface(IID_IPersistFile, (void **)&ppf); if (SUCCEEDED(hres)) { // Load the shell link. hres = ppf->Load(wsz, STGM_READ); if (SUCCEEDED(hres)) { // Resolve the link. hres = psl->Resolve(HWND_DESKTOP, SLR_ANY_MATCH); if (SUCCEEDED(hres)) { // found it, so save details CHAR szTemp[MAXSTR]; WIN32_FIND_DATA wfd; int i; fprintf(m_fLogOld, "Name=%s\n", szLink); hres = psl->GetPath(szTemp, MAXSTR, (WIN32_FIND_DATA *)&wfd, SLGP_SHORTPATH ); if (SUCCEEDED(hres)) fprintf(m_fLogOld, "Path=%s\n", szTemp); hres = psl->GetDescription(szTemp, MAXSTR); if (SUCCEEDED(hres)) fprintf(m_fLogOld, "Description=%s\n", szTemp); hres = psl->GetArguments(szTemp, MAXSTR); if (SUCCEEDED(hres) && (szTemp[0] != '\0')) fprintf(m_fLogOld, "Arguments=%s\n", szTemp); hres = psl->GetWorkingDirectory(szTemp, MAXSTR); if (SUCCEEDED(hres) && (szTemp[0] != '\0')) fprintf(m_fLogOld, "Directory=%s\n", szTemp); hres = psl->GetIconLocation(szTemp, MAXSTR, &i); if (SUCCEEDED(hres) && (szTemp[0] != '\0')) { fprintf(m_fLogOld, "IconLocation=%s\n", szTemp); fprintf(m_fLogOld, "IconIndex=%d\n", i); } fprintf(m_fLogOld, "\n"); } } // Release pointer to IPersistFile. ppf->Release(); } // Release pointer to IShellLink. psl->Release(); } // Save new shell link // 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)) { fprintf(m_fLogNew, "Name=%s\n", szLink); // Set the path to the shell link target. hres = psl->SetPath(program); if (!SUCCEEDED(hres)) AddMessage("SetPath failed!"); fprintf(m_fLogNew, "Path=%s\n", program); // Set the description of the shell link. hres = psl->SetDescription(description); if (!SUCCEEDED(hres)) AddMessage("SetDescription failed!"); fprintf(m_fLogNew, "Description=%s\n", description); if (arguments != (LPCSTR)NULL) { // Set the arguments of the shell link target. hres = psl->SetArguments(arguments); if (!SUCCEEDED(hres)) AddMessage("SetArguments failed!"); fprintf(m_fLogNew, "Arguments=%s\n", arguments); } if (icon != (LPCSTR)NULL) { // Set the arguments of the shell link target. hres = psl->SetIconLocation(icon, nIconIndex); if (!SUCCEEDED(hres)) AddMessage("SetIconLocation failed!"); fprintf(m_fLogNew, "IconLocation=%s\n", icon); fprintf(m_fLogNew, "IconIndex=%d\n", nIconIndex); } // 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(); fprintf(m_fLogNew, "\n"); } return (hres == 0); }
BOOL TInstDlg::UnInstall(void) { char buf[MAX_PATH]; char setupDir[MAX_PATH] = ""; ::GetModuleFileName(NULL, setupDir, sizeof(setupDir)); GetParentDir(setupDir, setupDir); BOOL is_shext = FALSE; is_shext = ShellExtFunc(setupDir, CHECK_SHELLEXT); if (is_shext && IsWinVista() && !TIsUserAnAdmin()) { RunAsAdmin(TRUE); return TRUE; } if (MessageBox(GetLoadStr(IDS_START), UNINSTALL_STR, MB_OKCANCEL|MB_ICONINFORMATION) != IDOK) return FALSE; // スタートメニュー&デスクトップから削除 TRegistry reg(HKEY_CURRENT_USER, BY_MBCS); if (reg.OpenKey(REGSTR_SHELLFOLDERS)) { char *regStr[] = { REGSTR_PROGRAMS, REGSTR_DESKTOP, NULL }; for (int cnt=0; regStr[cnt] != NULL; cnt++) { if (reg.GetStr(regStr[cnt], buf, sizeof(buf))) { if (cnt == 0) RemoveSameLink(buf); ::wsprintf(buf + strlen(buf), "\\%s", FASTCOPY_SHORTCUT); if (IS_WINNT_V) { Wstr w_buf(buf, BY_MBCS); DeleteLinkV(w_buf.Buf()); } else { DeleteLinkV(buf); } } } reg.CloseKey(); } ShellExtFunc(setupDir, UNREGISTER_SHELLEXT); #ifdef _WIN64 if (IS_WINNT_V) { #else if (IS_WINNT_V && TIsWow64()) { #endif SHELLEXECUTEINFO sei = { sizeof(sei) }; char arg[1024]; sprintf(arg, "\"%s\\%s\",%s", setupDir, CURRENT_SHEXTDLL_EX, "DllUnregisterServer"); sei.lpFile = "rundll32.exe"; sei.lpParameters = arg; ShellExecuteEx(&sei); } // レジストリからアンインストール情報を削除 if (reg.OpenKey(REGSTR_PATH_UNINSTALL)) { if (reg.OpenKey(FASTCOPY)) { if (reg.GetStr(REGSTR_VAL_UNINSTALLER_COMMANDLINE, setupDir, sizeof(setupDir))) GetParentDir(setupDir, setupDir); reg.CloseKey(); } reg.DeleteKey(FASTCOPY); reg.CloseKey(); } // 終了メッセージ MessageBox(is_shext ? GetLoadStr(IDS_UNINSTSHEXTFIN) : GetLoadStr(IDS_UNINSTFIN)); // インストールディレクトリを開く if (GetFileAttributes(setupDir) != 0xffffffff) { ::ShellExecute(NULL, NULL, setupDir, 0, 0, SW_SHOW); } // AppDataディレクトリを開く if (IsWinVista()) { WCHAR wbuf[MAX_PATH] = L"", upath[MAX_PATH] = L""; WCHAR fastcopy_dir[MAX_PATH] = L"", *fastcopy_dirname = NULL; Wstr w_setup(setupDir); if (TIsVirtualizedDirV(w_setup.Buf())) { if (TSHGetSpecialFolderPathV(NULL, wbuf, CSIDL_APPDATA, FALSE)) { GetFullPathNameW(w_setup.Buf(), MAX_PATH, fastcopy_dir, &fastcopy_dirname); MakePathV(upath, wbuf, fastcopy_dirname); if (GetFileAttributesV(upath) != 0xffffffff) { ::ShellExecuteW(NULL, NULL, upath, 0, 0, SW_SHOW); } } } } ::PostQuitMessage(0); return TRUE; } BOOL ReadLink(char *src, char *dest, char *arg=NULL) { IShellLink *shellLink; // 実際は IShellLinkA or IShellLinkW IPersistFile *persistFile; WCHAR wbuf[MAX_PATH]; BOOL ret = FALSE; if (SUCCEEDED(CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void **)&shellLink))) { if (SUCCEEDED(shellLink->QueryInterface(IID_IPersistFile, (void **)&persistFile))) { AtoW(src, wbuf, MAX_PATH); if (SUCCEEDED(persistFile->Load(wbuf, STGM_READ))) { if (SUCCEEDED(shellLink->GetPath(dest, MAX_PATH, NULL, 0))) { if (arg) { shellLink->GetArguments(arg, MAX_PATH); } ret = TRUE; } } persistFile->Release(); } shellLink->Release(); } return ret; }