Пример #1
1
long GetShortCut( char *linkpath, char *newpath )
{
	HRESULT hres;
	IShellLink* psl;
	WIN32_FIND_DATA fd; 
    
	CoInitialize(NULL);
	hres = CoCreateInstance(CLSID_ShellLink, NULL, 
        CLSCTX_INPROC_SERVER, IID_IShellLink,(void**)&psl); 
    if (SUCCEEDED(hres)) {
		IPersistFile *ppf;
		hres = psl->QueryInterface (IID_IPersistFile, (void **)&ppf);		//IID_IPersistFile
		if (SUCCEEDED(hres)) { 
            WCHAR wsz[MAX_PATH];

            MultiByteToWideChar(CP_ACP, 0, linkpath, -1, wsz,  MAX_PATH);              // Load the shortcut. 
			hres = ppf->Load(wsz, TRUE); 
			if (SUCCEEDED(hres)) {
				psl->GetPath(newpath ,256, &fd, SLGP_SHORTPATH );
			}
            ppf->Release();
			if(!SUCCEEDED(hres))
				return false;
		} else
			return false;
        psl->Release();
	} else
		return false;
	CoUninitialize();
	return true;
}
Пример #2
0
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;
}
Пример #3
0
// ショートカット作成
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;
}
Пример #4
0
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();
}
Пример #5
0
// http://msdn.microsoft.com/en-us/library/aa969393.aspx
bool my_createshortcut(const TCHAR *source, const TCHAR *target, const TCHAR *description) 
{ 
    HRESULT hres; 
    IShellLink* psl;
	TCHAR tmp[MAX_DPATH];
 
    // 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(target); 
        psl->SetDescription(description); 
 
        // 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. 
			_tcscpy (tmp, source);
			const TCHAR *ext = _tcsrchr (tmp, '.');
			if (!ext || _tcsicmp (ext, _T(".lnk")) != 0)
				_tcscat (tmp, _T(".lnk"));
            hres = ppf->Save(tmp, TRUE); 
            ppf->Release(); 
        } 
        psl->Release(); 
    }
	return SUCCEEDED(hres);
}
Пример #6
0
HRESULT CreateLinkToFile(TCHAR *PathToFile,TCHAR *PathToLink,TCHAR *LinkDescription)
{
	IShellLink		*pShellLink = NULL;
	IPersistFile	*pPersistFile = NULL;
	WCHAR			PathToLinkW[MAX_PATH];
	HRESULT			hr;

	hr = CoCreateInstance(CLSID_ShellLink,NULL,CLSCTX_INPROC_SERVER,
	IID_IShellLink,(LPVOID*)&pShellLink);

	if(SUCCEEDED(hr))
	{
		pShellLink->SetPath(PathToFile);
		pShellLink->SetDescription(LinkDescription);

		hr = pShellLink->QueryInterface(IID_IPersistFile,(LPVOID*)&pPersistFile);

		if(SUCCEEDED(hr))
		{
			#ifndef UNICODE
			MultiByteToWideChar(CP_ACP,0,PathToLink,-1,PathToLinkW,MAX_PATH);
			#else
			StringCchCopy(PathToLinkW,SIZEOF_ARRAY(PathToLinkW),PathToLink);
			#endif

			pPersistFile->Save(PathToLinkW,TRUE);

			pPersistFile->Release();
		}

		pShellLink->Release();
	}

	return hr;
}
Пример #7
0
long CreateLink(LPCSTR lpszPathObj,LPSTR lpszPathLink,LPSTR lpszDesc)
{
	HRESULT hres;
	IShellLink* psl;
    
	CoInitialize(NULL);
	hres = CoCreateInstance(CLSID_ShellLink, NULL, 
        CLSCTX_INPROC_SERVER, IID_IShellLink,(void**)&psl); 
    if (SUCCEEDED(hres)) 
	{
		IPersistFile *ppf;
		hres = psl->QueryInterface (IID_IPersistFile, (void **)&ppf);
		if (SUCCEEDED(hres)) 
		{ 
            WCHAR wsz[MAX_PATH];
			ZeroMemory(wsz,sizeof(WORD)*MAX_PATH);
			hres = psl->SetPath(lpszPathObj); 
			hres = psl->SetDescription(lpszDesc);  
       		hres = psl->SetIconLocation(lpszPathObj,0);
			MultiByteToWideChar(CP_ACP, 0, lpszPathLink, -1, wsz, MAX_PATH);
			hres = ppf->Save(wsz, TRUE); 
            ppf->Release();
			if(!SUCCEEDED(hres))
				return false;
		} 
		else
			return false;
        psl->Release();
	}
	else
		return false;
	CoUninitialize();
	return true;
}
Пример #8
0
HRESULT ShellFunctions::ResolveShortcut(HWND hWnd,LPCSTR pszShortcutFile,LPSTR pszPath)
{
	HRESULT hres;  
	IShellLink* psl;
	pszPath[0]='\0';

	hres=CoCreateInstance(CLSID_ShellLink,NULL,CLSCTX_INPROC_SERVER,IID_IShellLink,(void**)&psl);
	if (SUCCEEDED(hres))
	{
		IPersistFile* ppf;
		hres=psl->QueryInterface(IID_IPersistFile,(void**)&ppf);
		if (SUCCEEDED(hres))
		{
			WCHAR wsz[MAX_PATH];
			MultiByteToWideChar(CP_ACP,0,pszShortcutFile,-1,wsz,MAX_PATH);
			hres=ppf->Load(wsz,STGM_READ);
			if (SUCCEEDED(hres))
			{
				hres=psl->Resolve(hWnd,SLR_ANY_MATCH);
				if (pszPath!=NULL && SUCCEEDED(hres))
					hres=psl->GetPath(pszPath,MAX_PATH,NULL,0);
			}
			ppf->Release();
		}
		psl->Release();  
	}
	return hres;
}
Пример #9
0
/*
	リンク
	あらかじめ、CoInitialize(NULL); を実行しておくこと
*/
BOOL SymLink(LPCSTR src, LPSTR dest, LPCSTR arg)
{
	IShellLink		*shellLink;
	IPersistFile	*persistFile;
	Wstr			wsrc(src), wdest(dest), warg(arg);
	BOOL			ret = FALSE;
	char			buf[MAX_PATH_U8];

	if (SUCCEEDED(CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLinkW, (void **)&shellLink))) {
		shellLink->SetPath((char *)wsrc.Buf());
		shellLink->SetArguments((char *)warg.Buf());
		GetParentDirU8(src, buf);
		shellLink->SetWorkingDirectory((char *)U8toWs(buf));

		if (SUCCEEDED(shellLink->QueryInterface(IID_IPersistFile, (void **)&persistFile))) {
			if (SUCCEEDED(persistFile->Save(wdest.s(), TRUE))) {
				ret = TRUE;
				GetParentDirU8(WtoU8(wdest.s()), buf);
				::SHChangeNotify(SHCNE_UPDATEDIR, SHCNF_PATH|SHCNF_FLUSH, U8toAs(buf), NULL);
			}
			persistFile->Release();
		}
		shellLink->Release();
	}
	return	ret;
}
Пример #10
0
HRESULT CSysDialog::CreateLink(LPCSTR lpszPathObj, LPCSTR lpszPathLink, LPCSTR lpszDesc)
{
    HRESULT hres;
    IShellLink* psl;

    hres = CoInitialize(NULL);
    hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID *) &psl);
    
    if (SUCCEEDED(hres)) {
        IPersistFile *ppf;

        psl->SetPath(lpszPathObj);
        psl->SetDescription(lpszDesc);
        hres = psl->QueryInterface(IID_IPersistFile, (void **)&ppf);
        if (SUCCEEDED(hres)) {
            WCHAR wsz[MAX_PATH];
            
            MultiByteToWideChar(CP_ACP, 0, lpszPathLink, -1, wsz, MAX_PATH);
            hres = ppf->Save(wsz, TRUE);
            ppf->Release();
        }
        psl->Release();
    }
    return hres;
}
Пример #11
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();
}
Пример #12
0
BOOL ResolveShortcut(TCHAR *shortcut, TCHAR *file)
{
	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)) {
			hr = ppf->Load(shortcut, STGM_READ); 
			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(); 
	}

	return SUCCEEDED(hr);
}
Пример #13
0
CString FileMisc::ResolveShortcut(LPCTSTR szShortcut)
{
	// start by checking its a valid file
	if (!FileExists(szShortcut))
	{
		return _T("");
	}

	CoInitialize(NULL);

	HRESULT hResult;
	IShellLink* psl;
	CString sTarget(szShortcut);

	hResult = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl);

	if (SUCCEEDED(hResult))
	{
		LPPERSISTFILE ppf;

		hResult = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf);

		if (SUCCEEDED(hResult))
		{
			hResult = ppf->Load(ATL::CT2OLE(szShortcut), STGM_READ);

			if (SUCCEEDED(hResult))
			{
				hResult = psl->Resolve(NULL, SLR_ANY_MATCH | SLR_NO_UI);

				if (SUCCEEDED(hResult))
				{
					TCHAR szPath[MAX_PATH];
					WIN32_FIND_DATA wfd;

					//fabio_2005
#if _MSC_VER >= 1400
					_tcscpy_s(szPath, szShortcut);
#else
					_tcscpy(szPath, szShortcut);
#endif
					hResult = psl->GetPath(szPath, MAX_PATH, (WIN32_FIND_DATA*)&wfd, SLGP_SHORTPATH);

					if (SUCCEEDED(hResult))
					{
						sTarget = CString(szPath);
					}
				}
			}

			ppf->Release();
		}

		psl->Release();
	}

	CoUninitialize();

	return sTarget;
}
Пример #14
0
/*
	リンクの解決
	あらかじめ、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;
}
Пример #15
0
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;
}
Пример #16
0
/*
	リンク
	あらかじめ、CoInitialize(NULL); を実行しておくこと
	src  ... old_path
	dest ... new_path
*/
BOOL SymLinkV(void *src, void *dest, void *arg)
{
	IShellLink		*shellLink;
	IPersistFile	*persistFile;
	WCHAR			wbuf[MAX_PATH], *ps_dest = (WCHAR *)dest;
	BOOL			ret = FALSE;
	WCHAR			buf[MAX_PATH];

	if (SUCCEEDED(CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLinkV,
			(void **)&shellLink))) {
		shellLink->SetPath((char *)src);
		shellLink->SetArguments((char *)arg);
		GetParentDirV(src, buf);
		shellLink->SetWorkingDirectory((char *)buf);
		if (SUCCEEDED(shellLink->QueryInterface(IID_IPersistFile, (void **)&persistFile))) {
			if (!IS_WINNT_V) {
				MultiByteToWideChar(CP_ACP, 0, (char *)dest, -1, wbuf, MAX_PATH);
				ps_dest = wbuf;
			}
			if (SUCCEEDED(persistFile->Save(ps_dest, TRUE))) {
				ret = TRUE;
				GetParentDirV(dest, buf);
				::SHChangeNotify(SHCNE_UPDATEDIR, SHCNF_PATHV|SHCNF_FLUSH, buf, NULL);
			}
			persistFile->Release();
		}
		shellLink->Release();
	}
	return	ret;
}
Пример #17
0
static bool sGetSymLinkPath0(const char *linkpath, String *path)
{
	bool ret = false;
	HRESULT hres;
	IShellLink* psl;
	IPersistFile* ppf;
	CoInitialize(NULL);
	hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink,
	                        (PVOID *) &psl);
	if(SUCCEEDED(hres)) {
		hres = psl->QueryInterface(IID_IPersistFile, (PVOID *) &ppf);
		if(SUCCEEDED(hres)) {
			hres = ppf->Load(ToSystemCharsetW(linkpath), STGM_READ);
			if(SUCCEEDED(hres))
				if(path) {
					char fileW[_MAX_PATH] = {0};
					psl->GetPath(fileW, _MAX_PATH, NULL, 0); 
					*path = FromSystemCharset(fileW);
				}
				else
					ret = true;
			ppf->Release();
		}
		psl->Release();
	}
	CoUninitialize();
	return ret;
}
Пример #18
0
std::string	Shortcut_GetTarget(const char* szLink) {
    wchar_t	wszLink[MAX_PATH]	= {};
    ::MultiByteToWideChar(CP_ACP, NULL, szLink, int(strlen(szLink)), wszLink, MAX_PATH);

    char	buf[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)) {
                pShellLink->GetPath(buf, MAX_PATH, NULL, 0);
            }
            ppf->Release();
        }
        pShellLink->Release();
    }
    ::CoUninitialize();

    return	std::string(buf);
}
Пример #19
0
bool CShortCut::Create(void)
{
    bool results;
    HRESULT hres;
    IShellLink *psl;
    IPersistFile *ppf;
    wchar_t wsz[MAX_PATH];
    CoInitialize(NULL);
    hres = CoCreateInstance(CLSID_ShellLink, NULL,
    CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&psl);
    results=false;
    if (SUCCEEDED(hres))
    {
        psl->SetPath(LinkFile.c_str());
        psl->SetWorkingDirectory(LinkPath.c_str());
        psl->SetDescription(LinkDescription.c_str());
        hres = psl->QueryInterface(IID_IPersistFile, (void**)&ppf);
        if (SUCCEEDED(hres))
        {
            MultiByteToWideChar(CP_ACP, 0, FileName.c_str(), -1,
            wsz, MAX_PATH);
            hres = ppf->Save(wsz, true);
            ppf->Release();
            results=true;
        }
        psl->Release();
    }
    CoUninitialize();
    return results;
}
Пример #20
0
/**
 * @brief Create link files for registry menu.
 * CreateLink - uses the shell's IShellLink and IPersistFile interfaces to
 *  create and store a shortcut to the specified object.
 * @param [in] lpszPathObj Address of a buffer containing the path of the
 *   object.
 * @param [in] lpszPathLink Address of a buffer containing the path where the
 *   shell link is to be stored.
 * @return The result of calling the member functions of the interfaces.
 */
STDAPI CreateLink(LPCTSTR lpszPathObj, LPCTSTR lpszPathLink)
{
	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;
		// Set the path to the shortcut target
		psl->SetPath(lpszPathObj);
		// Query IShellLink for the IPersistFile interface for saving the
		// shortcut in persistent storage.
		hres = psl->QueryInterface(IID_IPersistFile, (void **)&ppf);
		if (SUCCEEDED(hres))
		{
			// Create the dir before saving the file because IPersistFile::Save won't
			if (LPTSTR tmp = StrRChr(lpszPathLink, 0, _T('\\')))
			{
				*tmp = _T('\0'); //Remove filename
				CreateDirectory(lpszPathLink, 0);
				*tmp = _T('\\');
			}
			// Save the link by calling IPersistFile::Save.
			hres = ppf->Save(static_cast<T2W>(lpszPathLink), TRUE);
			ppf->Release();
		}
		psl->Release();
	}
	return hres;
}
Пример #21
0
 BOOL CreateShortcut(wchar_t *DestPath, wchar_t *LnkPath)
{

	/* THANKS TO KAUSHIK */

	HRESULT hres;
	IShellLink* psl;
	CoInitialize(NULL);
	hres = CoCreateInstance(CLSID_ShellLink, NULL,
	CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID *) &psl);
	if (SUCCEEDED(hres)) {
		IPersistFile* ppf; 
		hres = psl->SetPath(DestPath);
		psl->QueryInterface(IID_IPersistFile,(LPVOID*)&ppf);
		hres = ppf->Save(LnkPath,FALSE);
		ppf->Release();
		psl->Release(); 
		if(hres==S_OK)
		{
			return TRUE;
		}
	}
	return FALSE;

	/* THANKS TO KAUSHIK */
}
Пример #22
0
bool CreateShellLink(const char *filepath, const char *linkpath, const char *desc, int icon)
{
	HRESULT hres;
	IShellLink* psl;
	IPersistFile* ppf;
	hres = CoInitialize(NULL);
	hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink,
	                        (PVOID *) &psl);
	if(SUCCEEDED(hres)) {
		psl->SetPath(filepath);
		psl->SetDescription(desc);
		if(icon >= 0)
			psl->SetIconLocation(filepath, icon);
		hres = psl->QueryInterface(IID_IPersistFile, (PVOID *) &ppf);
		if (SUCCEEDED(hres)) {
			WCHAR szPath[_MAX_PATH] = { 0 };
			MultiByteToWideChar(CP_ACP, 0, linkpath, strlen(linkpath), szPath, _MAX_PATH);
			hres = ppf->Save(szPath, TRUE);
			ppf->Release();
		}
	}
	psl->Release();
	CoUninitialize();
	return SUCCEEDED(hres);
}
Пример #23
0
void FileSystemManager::createShortcut(QString shortcutFilePath, QString pathToTarget, QString desc)
{
	HRESULT hres = NULL;
	IShellLink * psl = NULL;
	IPersistFile * ppf = NULL;
	
	QDir workingDirectory = scnManager->getWorkingDirectory();
	QDir linkPath = workingDirectory / shortcutFilePath;

	// Get a pointer to the IShellLink interface.
	hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void **) &psl);

	if (SUCCEEDED(hres))
	{
		// Set the path to the shortcut target
		psl->SetPath((LPCWSTR) pathToTarget.utf16());
		psl->SetDescription((LPCWSTR) desc.utf16());

		// Query IShellLink for the IPersistFile interface for
		// saving the shortcut in persistent storage.
		hres = psl->QueryInterface(IID_IPersistFile, (void **) &ppf);

		if (SUCCEEDED(hres))
		{
			// Save the link by calling IPersistFile::Save.
			hres = ppf->Save((LPCOLESTR) native(linkPath).utf16(), TRUE);
			ppf->Release();
		}

		psl->Release();
	}
}
Пример #24
0
BOOL CreateShortcut(TCHAR *file, TCHAR *shortcut)
{
	CoInitialize(NULL);

    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))
        {
#ifdef _UNICODE
			hr = ppf->Save(shortcut, TRUE); 
#else
			WCHAR tmp[MAX_PATH]; 
            MultiByteToWideChar(CP_ACP, 0, shortcut, -1, tmp, MAX_PATH); 
            hr = ppf->Save(tmp, TRUE); 
#endif
            ppf->Release(); 
        }

        psl->Release(); 
    } 

	if(FAILED(hr))
		ErrorExit(NULL,_T("CreateShortcut"));
	return SUCCEEDED(hr);
}
Пример #25
0
// http://msdn.microsoft.com/en-us/library/aa969393.aspx
HRESULT CreateLink(LPCWSTR lpszPathObj, LPCWSTR lpszArguments, LPCWSTR lpszPathLink, LPCWSTR lpszDesc) { 
	HRESULT hres; 
	IShellLink* psl; 
	CoInitialize(0);

	// Get a pointer to the IShellLink interface. It is assumed that CoInitialize
	// has already been called.
	hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl); 
	if (SUCCEEDED(hres)) { 
		IPersistFile* ppf; 

		// Set the path to the shortcut target and add the description. 
		psl->SetPath(lpszPathObj); 
		psl->SetArguments(lpszArguments);
		psl->SetDescription(lpszDesc); 

		// Query IShellLink for the IPersistFile interface, used for saving the 
		// shortcut in persistent storage. 
		hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf); 

		if (SUCCEEDED(hres)) { 
			// Save the link by calling IPersistFile::Save. 
			hres = ppf->Save(lpszPathLink, TRUE); 
			ppf->Release(); 
		} 
		psl->Release(); 
	}
	CoUninitialize();

	return hres; 
}
Пример #26
0
static HRESULT CreateLink(LPCTSTR lpszPathObj, LPCTSTR lpszPathLink,
                   LPCTSTR pszArgs, LPCTSTR lpszDesc, LPCTSTR lpszWdir)
{    
   HRESULT h_result;    
   IShellLink* psl;     
   CoInitialize( NULL );    
   h_result = CoCreateInstance(CLSID_ShellLink, NULL,    
      CLSCTX_INPROC_SERVER, 
      IID_IShellLink, 
      (LPVOID *) &psl);    
   if (SUCCEEDED(h_result))
   {        
      IPersistFile* ppf;         
      psl->SetPath(lpszPathObj);        
      psl->SetArguments(pszArgs);        
      psl->SetDescription(lpszDesc);         
      psl->SetWorkingDirectory(lpszWdir);
      h_result = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf);         
      if (SUCCEEDED(h_result))
      {            
         h_result = ppf->Save(lpszPathLink, TRUE);
         ppf->Release();        
      }        
      psl->Release();    
   }    
   return h_result;
}
Пример #27
0
HRESULT
CreateLink(LPCWSTR aTargetPath, LPCWSTR aShortcutPath, LPCWSTR aDescription) 
{ 
    HRESULT hres;
    IShellLink* psl;
 
    wprintf(L"creating shortcut: '%s'\n",  aShortcutPath);

    CoInitialize(nullptr);

    hres = CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER,
                            IID_IShellLink, (LPVOID*)&psl);
    if (FAILED(hres)) {
      CoUninitialize();
      return hres;
    }
    psl->SetPath(aTargetPath);
    if (aDescription) {
      psl->SetDescription(aDescription);
    } else {
      psl->SetDescription(L"");
    }
 
    IPersistFile* ppf = nullptr;
    hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf);
 
    if (SUCCEEDED(hres)) {
        hres = ppf->Save(aShortcutPath, TRUE);
        ppf->Release();
    }
    psl->Release();
    CoUninitialize();
    return hres;
}
Пример #28
0
bool CreateLink(std::string& path, std::string& linkPath, std::string& description) 
{ 
	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; 
		wstring pathW = KrollUtils::UTF8ToWide(path);
		wstring linkPathW = KrollUtils::UTF8ToWide(linkPath);
		wstring descriptionW = KrollUtils::UTF8ToWide(description);
 
		// Set the path to the shortcut target and add the description. 
		psl->SetPath(pathW.c_str()); 
		psl->SetDescription(descriptionW.c_str()); 
 
		// Query IShellLink for the IPersistFile interface 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(linkPathW.c_str(), TRUE); 
			ppf->Release(); 
			return true;
		} 
		psl->Release(); 
	} 
	return false;
}
Пример #29
0
void CEzShortcutDlg::OnDropFiles(HDROP hDropInfo)
{
	TCHAR szPathName[MAX_PATH];

	// 드롭된 파일의 갯수
	int nFiles=DragQueryFile(hDropInfo, 0xFFFFFFFF, szPathName, MAX_PATH);
	if( nFiles != 1)
	{
		AfxMessageBox(_T("This application is not support multi-file drag & drop"));
		return;
	}

	for(int index=0 ; index < nFiles ; index++)
	{
		DragQueryFile(hDropInfo, index, szPathName, MAX_PATH);     // 파일의 경로 얻어옴
		std::wstring strExt = light::get_file_ext(szPathName);

		if( strExt == _T("lnk" ))
		{
			IShellLink *link;
			IPersistFile *pfile;
			BSTR szLinkPath;
			CString szPrePath;
			TCHAR szBuffer[MAX_PATH];

			CString fname = szPathName;
			if(SUCCEEDED(CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void **)&link))) 
			{
				link->QueryInterface(IID_IPersistFile, (void **)&pfile);
				szLinkPath = fname.AllocSysString();
				pfile->Load(szLinkPath, STGM_READ);
				SysFreeString(szLinkPath);
				link->Resolve(NULL, NULL);
				link->GetPath(szBuffer, MAX_PATH, NULL, 0);

				// 리스트 박스나 메세지 박스로 해당 경로 값을 불러온다. (szBuffer)
				_tcsncpy_s(szPathName , szBuffer, _TRUNCATE);
				pfile->Release();
				link->Release();
			}
		}
		
		SHORTCUT_INFO ShortcutInfo(GetSafeHwnd());
		ShortcutInfo.m_strShortcutName = light::get_file_name_without_ext(szPathName);
		ShortcutInfo.m_strProgramPath = szPathName;
		if( true == OnEditShortcutDlg(ShortcutInfo) )
		{
			AddShortCutInfo(ShortcutInfo);
			RebuildShortcutList();
			AutoSave();
		}
	}

	DragFinish(hDropInfo);

	CDialog::OnDropFiles(hDropInfo);
}
Пример #30
0
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();
}