Пример #1
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;
}
Пример #2
0
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();
    }
}
Пример #3
0
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);
}
Пример #4
0
//-----------------------------------------------------------------------------
// 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
}
Пример #5
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;
}
// Creates a CLSID_ShellLink to insert into the Tasks section of the Jump List.  This type of Jump
// List item allows the specification of an explicit command line to execute the task.
HRESULT _CreateShellLink(PCWSTR pszArguments, PCWSTR pszTitle, IShellLink **ppsl)
{
    IShellLink *psl;
    HRESULT hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&psl));
    if (SUCCEEDED(hr))
    {
        // Determine our executable's file path so the task will execute this application
        WCHAR szAppPath[MAX_PATH];
        if (GetModuleFileName(NULL, szAppPath, ARRAYSIZE(szAppPath)))
        {
            hr = psl->SetPath(szAppPath);
            if (SUCCEEDED(hr))
            {
                hr = psl->SetArguments(pszArguments);
                if (SUCCEEDED(hr))
                {
                    // The title property is required on Jump List items provided as an IShellLink
                    // instance.  This value is used as the display name in the Jump List.
                    IPropertyStore *pps;
                    hr = psl->QueryInterface(IID_PPV_ARGS(&pps));
                    if (SUCCEEDED(hr))
                    {
                        PROPVARIANT propvar;
                        hr = InitPropVariantFromString(pszTitle, &propvar);
                        if (SUCCEEDED(hr))
                        {
                            hr = pps->SetValue(PKEY_Title, propvar);
                            if (SUCCEEDED(hr))
                            {
                                hr = pps->Commit();
                                if (SUCCEEDED(hr))
                                {
                                    hr = psl->QueryInterface(IID_PPV_ARGS(ppsl));
                                }
                            }
                            PropVariantClear(&propvar);
                        }
                        pps->Release();
                    }
                }
            }
        }
        else
        {
            hr = HRESULT_FROM_WIN32(GetLastError());
        }
        psl->Release();
    }
    return hr;
}
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;
}
Пример #8
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();
}
Пример #9
0
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;
}
Пример #10
0
	/***
	@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;
	}
/* #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 */
Пример #12
0
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; 
}
Пример #13
0
HRESULT CreateShortcut(LPCSTR pszPathObj,LPSTR pszParam,LPSTR pszPath,LPSTR pszPathLink,LPSTR pszDesc)
{
	HRESULT hres; //调用 COM 接口方法之后的返回值
	IShellLink *pShellLink;
	IPersistFile *pPersistFile;
	WCHAR wsz[MAX_PATH]; //UNICODE串, 用来存放快捷方式文件名

	CoInitialize(NULL); //初始化 COM 库
	//创建 COM 对象并获取其实现的接口
	hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,IID_IShellLink,(void **)&pShellLink);
	if(FAILED(hres))
	{
		pShellLink ->Release(); //释放 IShellLink 接口
		CoUninitialize(); //关闭 COM 库, 释放所有 COM 资源
		return FALSE;
	}
	//设置快捷方式的各种属性
	pShellLink->SetPath(pszPathObj); //快捷方式所指的应用程序名
	pShellLink->SetArguments(pszParam); //参数
	pShellLink->SetDescription(pszDesc); //描述
	pShellLink->SetWorkingDirectory(pszPath); //设置工作目录
	//pShellLink->SetIconLocation("C:\\Icon.ico",0); //快捷方式的图标
	//pShellLink->SetHotkey(热键); //启动快捷方式的热键(只能是Ctrl+Alt+_)
	//pShellLink->SetShowCmd(SW_MAXIMIZE); //运行方式(常规窗口,最大化,最小化)
	//查询 IShellLink 接口从而得到 IPersistFile 接口来保存快捷方式
	hres = pShellLink->QueryInterface(IID_IPersistFile,(void **)&pPersistFile);
	if(FAILED(hres))
	{
		pPersistFile ->Release(); //释放 IPersistFile 接口
		pShellLink ->Release(); //释放 IShellLink 接口
		CoUninitialize(); //关闭 COM 库, 释放所有 COM 资源
		return(FALSE);
	}
	//转换 ANSI 串为 UNICODE 串(COM 内部使用 NUICODE)
	MultiByteToWideChar(CP_ACP, 0, pszPathLink, -1, wsz, MAX_PATH);
	//使用 IPersistFile 接口的 Save() 方法保存快捷方式
	hres = pPersistFile ->Save(wsz, TRUE);

	//释放 IPersistFile 接口
	pPersistFile ->Release();
	//释放 IShellLink 接口
	pShellLink ->Release();
	//关闭 COM 库, 释放所有 COM 资源
	CoUninitialize();
	return(hres);
}
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; 
}
Пример #15
0
//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);
}
Пример #16
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();
}
Пример #17
0
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);
}
Пример #18
0
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;
}
Пример #19
0
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;
}
Пример #20
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
}
Пример #21
0
BOOL CreateShortcut(LPCTSTR linkPath, LPCTSTR targetPath, LPCTSTR comment, LPCTSTR arguments,
                    LPCTSTR workDir, LPCTSTR iconFile, int iconIndex, int showCommand)
{
   // Create a shortcut file
   HRESULT hRes; 
   IShellLink* psl; 

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

      psl->SetPath(targetPath); 
      if (arguments)
         psl->SetArguments(arguments);
      if (workDir)
         psl->SetWorkingDirectory(workDir);
      if (iconFile || iconIndex)
         psl->SetIconLocation(iconFile ? iconFile : targetPath, iconIndex);
      if (comment)
         psl->SetDescription(comment);

      psl->SetShowCmd(showCommand);

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

      if (SUCCEEDED(hRes))
      {
#ifndef UNICODE
         WCHAR wsz[BUFF_SIZE]; 
         MultiByteToWideChar(CP_ACP, 0, linkPath, -1, wsz, BUFF_SIZE); 

         hRes = ppf->Save(wsz, TRUE);
#else
         hRes = ppf->Save(linkPath, TRUE);
#endif
         ppf->Release(); 
      } 
      psl->Release(); 
   } 
   return TRUE; 
} 
Пример #22
0
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;
}
Пример #23
0
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; 
}
Пример #24
0
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);
}
Пример #25
0
	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;
	}
Пример #26
0
// @pymethod |PyIShellLink|SetPath|Sets the path and file name of a shell link object.
PyObject *PyIShellLink::SetPath(PyObject *self, PyObject *args)
{
	IShellLink *pISL = GetI(self);
	if ( pISL == NULL )
		return NULL;
	PyObject *obName;
	if ( !PyArg_ParseTuple(args, "O:SetDescription", &obName) )
		return NULL;
	TCHAR *pszName;
	if (!PyWinObject_AsTCHAR(obName, &pszName))
		return NULL;
	// @pyparm string|path||The path and filename of the link.
	HRESULT hr;
	PY_INTERFACE_PRECALL;
	hr = pISL->SetPath( pszName );
	PY_INTERFACE_POSTCALL;

	if ( FAILED(hr) )
		return OleSetOleError(hr);
	Py_INCREF(Py_None);
	return Py_None;
}
Пример #27
0
//
//	创建快捷方式
//
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;
}
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;
}
Пример #29
0
	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;
	}
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);
}