Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions dev/DynamicDependency/PackageGraphManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ HRESULT MddCore::PackageGraphManager::GetCurrentPackageInfo2(
UINT32* count,
GetCurrentPackageInfo2Function getCurrentPackageInfo2) noexcept try
{
// Check parameter(s) per GetCurrentPackageInfo2 compatibility
// * bufferLength is required
// * buffer is required if bufferLength >0
// * count is required if buffer is not nullptr
RETURN_HR_IF(E_INVALIDARG, (bufferLength == nullptr) || (*bufferLength > 0 && buffer == nullptr) || ((count == nullptr) && (buffer != nullptr)));
Comment thread
DrusTheAxe marked this conversation as resolved.

if (count)
{
*count = 0;
Expand All @@ -75,14 +81,15 @@ HRESULT MddCore::PackageGraphManager::GetCurrentPackageInfo2(
// Is the package graph empty?
if (s_packageGraph.PackageGraphNodes().empty())
{
// No dynamic package data. Is there a static package graph?
// No dynamic package data. Is there a static package graph? Regardless of the outcome our work here is done
UINT32 length{};
const LONG rc{ getCurrentPackageInfo2(flags, packagePathType, &length, nullptr, nullptr) };
if (rc == APPMODEL_ERROR_NO_PACKAGE)
if ((rc == ERROR_SUCCESS) || (rc == APPMODEL_ERROR_NO_PACKAGE) || (rc == ERROR_INSUFFICIENT_BUFFER))
{
// No static package data either
RETURN_WIN32(APPMODEL_ERROR_NO_PACKAGE);
// Don't log expected outcomes (Success, AppmodelErrorNoPackage, ErrorInsufficientBuffer)
return HRESULT_FROM_WIN32(rc);
}
RETURN_WIN32_MSG(rc, "GetCurrentPackageInfo2(): %d (0x%X)", rc, rc);
}

// We manage the package graph as a list of nodes, where each contain contains information about 1+ package.
Expand Down
20 changes: 20 additions & 0 deletions dev/ProjectReunion_DLL/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,22 @@

#include <../Detours/detours.h>

static bool IsPackagedProcess()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this like the 4th or 5th copy of this we've made?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lol 2nd or maybe 3rd. I'm planning to centralize this (and a few other things) but waiting on a couple of naming decisions first.

There's a laundry list of refactoring I've got my eye on. Planning to put some time in after I finish some critical path work.

{
UINT32 n{};
const auto rc = ::GetCurrentPackageFullName(&n, nullptr);
(void) LOG_HR_IF_MSG(HRESULT_FROM_WIN32(rc), (rc != APPMODEL_ERROR_NO_PACKAGE) && (rc != ERROR_INSUFFICIENT_BUFFER), "GetCurrentPackageFullName rc=%d", rc);
return rc == ERROR_INSUFFICIENT_BUFFER;
}

static HRESULT DetoursInitialize()
{
// Only detour APIs for not-packaged processes
if (IsPackagedProcess())
{
return S_OK;
}

// Do we need to detour APIs?
if (DetourIsHelperProcess())
{
Expand All @@ -29,6 +43,12 @@ static HRESULT DetoursInitialize()

static HRESULT DetoursShutdown()
{
// Only detour APIs for not-packaged processes
if (IsPackagedProcess())
{
return S_OK;
}

// Did we detour APIs?
if (DetourIsHelperProcess())
{
Expand Down
90 changes: 90 additions & 0 deletions test/DynamicDependency/Test_Win32/TestMddBootstrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,96 @@ namespace Test::DynamicDependency
MddBootstrapShutdown();
}

TEST_METHOD(GetCurrentPackageInfo_NotPackaged_InvalidParameter)
{
const UINT32 c_filter{ PACKAGE_FILTER_HEAD | PACKAGE_FILTER_DIRECT | PACKAGE_INFORMATION_BASIC };

{
Assert::AreEqual(E_INVALIDARG, HRESULT_FROM_WIN32(::GetCurrentPackageInfo(c_filter, nullptr, nullptr, nullptr)));
}
{
UINT32 count{};
Assert::AreEqual(E_INVALIDARG, HRESULT_FROM_WIN32(::GetCurrentPackageInfo(c_filter, nullptr, nullptr, &count)));
}

{
UINT32 bufferLength{ 1 };
Assert::AreEqual(E_INVALIDARG, HRESULT_FROM_WIN32(::GetCurrentPackageInfo(c_filter, &bufferLength, nullptr, nullptr)));
}
{
UINT32 bufferLength{ 1 };
UINT32 count{};
Assert::AreEqual(E_INVALIDARG, HRESULT_FROM_WIN32(::GetCurrentPackageInfo(c_filter, &bufferLength, nullptr, &count)));
}

{
BYTE buffer[1]{};
UINT32 bufferLength{ static_cast<UINT32>(ARRAYSIZE(buffer)) };
Assert::AreEqual(E_INVALIDARG, HRESULT_FROM_WIN32(::GetCurrentPackageInfo(c_filter, &bufferLength, buffer, nullptr)));
}
}

TEST_METHOD(GetCurrentPackageInfo_NotPackaged)
{
VerifyGetCurrentPackageInfo();

winrt::hstring packageFamilyName{ Test::Packages::DynamicDependencyLifetimeManager::c_PackageFamilyName };
auto applicationData{ winrt::Windows::Management::Core::ApplicationDataManager::CreateForPackageFamily(packageFamilyName) };

Assert::AreEqual(S_OK, MddBootstrapTestInitialize(Test::Packages::DynamicDependencyLifetimeManager::c_PackageNamePrefix, Test::Packages::DynamicDependencyLifetimeManager::c_PackagePublisherId));

// Version <major>.0.0.0 to find any framework package for this major version
PACKAGE_VERSION minVersion{ static_cast<UINT64>(Test::Packages::DynamicDependencyLifetimeManager::c_Version.Major) << 48 };
Assert::AreEqual(S_OK, MddBootstrapInitialize(minVersion));

VerifyGetCurrentPackageInfo(HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER), 1, 700);

winrt::Windows::ApplicationModel::AppExtensions::AppExtensionCatalog::Open(L"Does.Not.Exist");

MddBootstrapShutdown();

VerifyGetCurrentPackageInfo();
}

#if defined(TODO_EnableAfterConvertingToTAEF)
TEST_METHOD(GetCurrentPackageInfo_Packaged)
{
VerifyGetCurrentPackageInfo();

winrt::hstring packageFamilyName{ Test::Packages::DynamicDependencyLifetimeManager::c_PackageFamilyName };
auto applicationData{ winrt::Windows::Management::Core::ApplicationDataManager::CreateForPackageFamily(packageFamilyName) };

Assert::AreEqual(S_OK, MddBootstrapTestInitialize(Test::Packages::DynamicDependencyLifetimeManager::c_PackageNamePrefix, Test::Packages::DynamicDependencyLifetimeManager::c_PackagePublisherId));

// Version <major>.0.0.0 to find any framework package for this major version
PACKAGE_VERSION minVersion{ static_cast<UINT64>(Test::Packages::DynamicDependencyLifetimeManager::c_Version.Major) << 48 };
Assert::AreEqual(HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED), MddBootstrapInitialize(minVersion));

VerifyGetCurrentPackageInfo();
}
#endif

private:
static void VerifyGetCurrentPackageInfo(
const HRESULT expectedHR = HRESULT_FROM_WIN32(APPMODEL_ERROR_NO_PACKAGE),
const UINT32 expectedCount = 0,
const UINT32 minExpectedBufferLength = 0)
{
UINT32 bufferLength{};
UINT32 count{};
Assert::AreEqual(expectedHR, HRESULT_FROM_WIN32(::GetCurrentPackageInfo(PACKAGE_FILTER_HEAD | PACKAGE_FILTER_DIRECT | PACKAGE_INFORMATION_BASIC, &bufferLength, nullptr, &count)));
Assert::AreEqual(expectedCount, count);
if (minExpectedBufferLength > 0)
{
auto message{ wil::str_printf<wil::unique_process_heap_string>(L"GetCurrentPackageInfo2() expectedBufferLength>=%u bufferLength=%u", minExpectedBufferLength, bufferLength) };
Assert::IsTrue(bufferLength >= minExpectedBufferLength, message.get());
}
else
{
Assert::AreEqual(0u, bufferLength);
}
}

private:
static wil::unique_hmodule m_bootstrapDll;
};
Expand Down
2 changes: 2 additions & 0 deletions test/DynamicDependency/Test_Win32/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include <winrt/Windows.Foundation.Collections.h>

#include <winrt/Windows.ApplicationModel.h>
#include <winrt/Windows.ApplicationModel.AppExtensions.h>
#include <winrt/Windows.Management.Core.h>
#include <winrt/Windows.Management.Deployment.h>

#include <filesystem>
Expand Down