Ejemplo n.º 1
1
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;
}
Ejemplo n.º 2
0
// @pymethod str|PyIShellLink|GetWorkingDirectory|Retrieves the working directory for the link
PyObject *PyIShellLink::GetWorkingDirectory(PyObject *self, PyObject *args)
{
	IShellLink *pISL = GetI(self);
	if ( pISL == NULL )
		return NULL;
	// @pyparm int|cchMaxName|1024|Number of characters to allocate for returned text
	int cchMaxName = 1024;
	if ( !PyArg_ParseTuple(args, "|i:GetWorkingDirectory", &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->GetWorkingDirectory( pszName, cchMaxName );
	PY_INTERFACE_POSTCALL;

	PyObject *ret;
	if ( FAILED(hr) )
		ret = OleSetOleError(hr);
	else
		ret = PyWinObject_FromTCHAR(pszName);
	free(pszName);
	return ret;
}
Ejemplo n.º 3
0
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();
}
Ejemplo n.º 4
0
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; 
} 
Ejemplo n.º 5
0
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;
}
Ejemplo n.º 6
0
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);
}