Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature request] Adding the ability to reuse the AUMID of the current process. #80

Closed
prachtan opened this issue Sep 12, 2022 · 1 comment

Comments

@prachtan
Copy link

In case of using the WinToast lib in extensions, it wold be great to use AUMID from existing parent app shortcut.

bool WinToast::GetAUMIdForCurrentProcess(_Out_ std::wstring& aumid)
{
	if (!isCompatible()) {
		DEBUG_MSG(L"Warning: Your system is not compatible with this library ");
		return(false);
	}

	// Thanks to:
	// https://stackoverflow.com/questions/59926644/listing-of-installed-apps-on-windows-10
	//

	HRESULT hr = E_FAIL;
	std::wstring exePath(MAX_PATH, 0);
	DWORD written = GetModuleFileNameExW(GetCurrentProcess(), nullptr, &exePath[0], MAX_PATH);
	exePath.resize(written);

	CoInitialize(NULL);
	ComPtr<IShellItem> folder;
	if (SUCCEEDED(hr = SHCreateItemFromParsingName(L"shell:appsFolder", nullptr, IID_PPV_ARGS(&folder))))
	{
		ComPtr<IEnumShellItems> enumItems;
		if (SUCCEEDED(hr = folder->BindToHandler(nullptr, BHID_EnumItems, IID_PPV_ARGS(&enumItems))))
		{
			IShellItem* items;
			while (enumItems->Next(1, &items, nullptr) == S_OK)
			{
				// Get executable path.
				ComPtr<IShellItem> item = items;
				ComPtr<IPropertyStore> store;
				if (SUCCEEDED(item->BindToHandler(NULL, BHID_PropertyStore, IID_PPV_ARGS(&store))))
				{
					wchar_t pvString[MAX_PATH] = { 0 };

					PROPVARIANT propVarPath;
					PropVariantInit(&propVarPath);
					if (SUCCEEDED(store->GetValue(PKEY_Link_TargetParsingPath, &propVarPath)))
					{
						hr = DllImporter::PropVariantToString(propVarPath, pvString, MAX_PATH);
						PropVariantClear(&propVarPath);

						// Compare path.
						if (exePath == pvString)
						{
							PROPVARIANT appIdPropVar;
							PropVariantInit(&appIdPropVar);

							if (SUCCEEDED(hr = store->GetValue(PKEY_AppUserModel_ID, &appIdPropVar)))
							{
								hr = DllImporter::PropVariantToString(appIdPropVar, pvString, MAX_PATH);
								PropVariantClear(&appIdPropVar);

								if (SUCCEEDED(hr))
								{
									aumid = pvString;
									break;
								}
							}
						}
					}
				}
			}
		}
	}

	CoUninitialize();

	return(SUCCEEDED(hr));
}

@mohabouje
Copy link
Owner

Thanks for the contribution; I have scheduled this change for version v1.4.0.

Closing this issue and moving it to #86

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants