Beispiel #1
0
  HRESULT
  LaunchDefaultMetroBrowser()
  {
    CoInitialize(nullptr);
    HRESULT hr = E_FAIL;
    // The interface that allows us to activate the browser
    IApplicationActivationManager *activateMgr;
    if (FAILED(hr = CoCreateInstance(CLSID_ApplicationActivationManager,
                                     nullptr, CLSCTX_LOCAL_SERVER,
                                     IID_IApplicationActivationManager,
                                     (void**)&activateMgr))) {
      CoUninitialize();
      return hr;
    }

    // Activation is based on the browser's registered app model id
    WCHAR appModelID[256];
    if (!GetDefaultBrowserAppModelID(appModelID, (sizeof(appModelID)/sizeof(WCHAR)))) {
      activateMgr->Release();
      CoUninitialize();
      return hr;
    }

    // Hand off focus rights to the out-of-process activation server. Without
    // this the metro interface won't launch.
    CoAllowSetForegroundWindow(activateMgr, nullptr);

    // Launch default browser in Metro
    DWORD processID;
    hr = activateMgr->ActivateApplication(appModelID, L"", AO_NOERRORUI,
                                          &processID);
    activateMgr->Release();
    CoUninitialize();
    return hr;
  }
bool RegisterQAppAssociation::showNativeDefaultAppSettingsUi()
{
    if (!isVistaOrNewer()) {
        return false;
    }

#ifdef _WIN32_WINNT_WIN8
    IApplicationActivationManager* pActivator;
    HRESULT hr = CoCreateInstance(CLSID_ApplicationActivationManager,
                                  nullptr,
                                  CLSCTX_INPROC,
                                  IID_IApplicationActivationManager,
                                  (void**)&pActivator);

    if (!SUCCEEDED(hr)) {
        return false;
    }

    DWORD pid;
    hr = pActivator->ActivateApplication(
        L"windows.immersivecontrolpanel_cw5n1h2txyewy" // appUserModelId of "Settings"
        L"!microsoft.windows.immersivecontrolpanel",   //  in Windows Store
        L"page=SettingsPageAppsDefaults", AO_NONE, &pid);

    if (!SUCCEEDED(hr)) {
        return false;
    }

    // Do not check error because we could at least open
    // the "Default apps" setting.
    pActivator->ActivateApplication(
        L"windows.immersivecontrolpanel_cw5n1h2txyewy"
        L"!microsoft.windows.immersivecontrolpanel",
        L"page=SettingsPageAppsDefaults"
        L"&target=SystemSettings_DefaultApps_Browser", AO_NONE, &pid);

    pActivator->Release();
#else // Vista or Win7
    IApplicationAssociationRegistrationUI* pAARUI = NULL;

    HRESULT hr = CoCreateInstance(CLSID_ApplicationAssociationRegistrationUI,
                                  NULL, CLSCTX_INPROC, __uuidof(IApplicationAssociationRegistrationUI),
                                  reinterpret_cast< void** > (&pAARUI));

    if (!SUCCEEDED(hr)) {
        return false;
    }

    hr = pAARUI->LaunchAdvancedAssociationUI(reinterpret_cast<LPCWSTR>(_appRegisteredName.utf16()));
    pAARUI->Release();
#endif // _WIN32_WINNT_WIN8

    return true;
}
int _tmain(UINT uArgumentsCount, _TCHAR* asArguments[]) {
  HRESULT hResult;
  NtSuspendProcess = (tNtSuspendProcess)GetProcAddress(GetModuleHandle(_T("ntdll")), "NtSuspendProcess");
  if (!NtSuspendProcess) {
    _tprintf(_T("Cannot open process\r\n"));
    hResult = E_NOTIMPL;
  } else if (uArgumentsCount < 2) {
    _tprintf(_T("Usage: ProjectSpartanDbg <url> <debugger command line>\r\n"));
    hResult = E_INVALIDARG;
  } else  {
    hResult = CoInitialize(NULL);
    if (!SUCCEEDED(hResult)) {
      _tprintf(L"Failed to initialize\r\n");
    } else {
      IApplicationActivationManager* pAAM;
      hResult = CoCreateInstance(CLSID_ApplicationActivationManager, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pAAM));
      if (!SUCCEEDED(hResult)) {
        _tprintf(_T("Failed to create Application Activation Manager\r\n"));
      } else {
        DWORD dwProcessId;
        hResult = pAAM->ActivateApplication(sSpartanAUMID, asArguments[1], AO_NONE, &dwProcessId);
        if (!SUCCEEDED(hResult)) {
          _tprintf(_T("Failed to launch Project Spartan\r\n"));
        } else {
          _tprintf(_T("Project Spartan process id = %d\r\n"), dwProcessId);
          hResult = fSuspendThreadsInProcessById(dwProcessId);
          if (SUCCEEDED(hResult) && uArgumentsCount > 2) {
            hResult = fStartDebugger(dwProcessId, uArgumentsCount - 2, asArguments + 2);
          }
        }
      }
      pAAM->Release();
    }
    CoUninitialize();
  }
  return hResult;
}
bool WindowsPlatformIntegration::setAsDefaultBrowser()
{
	if (!isBrowserRegistered() && !registerToSystem())
	{
		return false;
	}

	QSettings registry(QLatin1String("HKEY_CURRENT_USER\\Software"), QSettings::NativeFormat);

	for (int i = 0; i < m_registrationPairs.count(); ++i)
	{
		if (m_registrationPairs.at(i).second == ProtocolType)
		{
			registry.setValue(QLatin1String("Classes/") + m_registrationPairs.at(i).first + QLatin1String("/DefaultIcon/."), m_applicationFilePath + QLatin1String(",1"));
			registry.setValue(QLatin1String("Classes/") + m_registrationPairs.at(i).first + QLatin1String("/shell/open/command/."), QLatin1String("\"") + m_applicationFilePath + QLatin1String("\" \"%1\""));
		}
		else
		{
			registry.setValue(QLatin1String("Classes/") + m_registrationPairs.at(i).first + QLatin1String("/."), m_registrationIdentifier);
		}
	}

	registry.setValue(QLatin1String("Clients/StartmenuInternet/."), m_registrationIdentifier);
	registry.sync();

	if (QSysInfo::windowsVersion() >= QSysInfo::WV_10_0)
	{
		DWORD pid = 0;
		IApplicationActivationManager *activationManager = NULL;
		HRESULT result = CoCreateInstance(CLSID_ApplicationActivationManager, NULL, CLSCTX_INPROC_SERVER, IID_IApplicationActivationManager, (LPVOID*)&activationManager);

		if (result == S_OK)
		{
			result = activationManager->ActivateApplication(QString("windows.immersivecontrolpanel_cw5n1h2txyewy!microsoft.windows.immersivecontrolpanel").toStdWString().c_str(), QString("page=SettingsPageAppsDefaults").toStdWString().c_str(), AO_NONE, &pid);

			activationManager->Release();

			if (result == S_OK)
			{
				return true;
			}
		}

		Console::addMessage(QCoreApplication::translate("main", "Failed to run File Associations Manager, error code: %1\nApplication ID: %2").arg(result).arg(pid), Otter::OtherMessageCategory, ErrorMessageLevel);
	}
	else if (QSysInfo::windowsVersion() >= QSysInfo::WV_VISTA)
	{
		IApplicationAssociationRegistrationUI *applicationAssociationRegistrationUI = NULL;
		HRESULT result = CoCreateInstance(CLSID_ApplicationAssociationRegistrationUI, NULL, CLSCTX_INPROC_SERVER, IID_IApplicationAssociationRegistrationUI, (LPVOID*)&applicationAssociationRegistrationUI);

		if (result == S_OK && applicationAssociationRegistrationUI)
		{
			result = applicationAssociationRegistrationUI->LaunchAdvancedAssociationUI(m_registrationIdentifier.toStdWString().c_str());

			applicationAssociationRegistrationUI->Release();

			if (result == S_OK)
			{
				return true;
			}
		}

		Console::addMessage(QCoreApplication::translate("main", "Failed to run File Associations Manager, error code: %1").arg(result), Otter::OtherMessageCategory, ErrorMessageLevel);
	}
	else
	{
		SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_DWORD | SHCNF_FLUSH, NULL, NULL);
		Sleep(1000);
	}

	return true;
}