Skip to content

Commit

Permalink
Start to implement the sponsor button implementation in the main window.
Browse files Browse the repository at this point in the history
  • Loading branch information
MouriNaruto committed May 9, 2024
1 parent d628cbc commit df9015a
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 30 deletions.
127 changes: 104 additions & 23 deletions NanaBox/MainWindowControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@

#include <ShObjIdl_core.h>

namespace
{
DWORD GetShellProcessId()
{
HWND ShellWindowHandle = ::GetShellWindow();
if (!ShellWindowHandle)
{
return static_cast<DWORD>(-1);
}

DWORD ShellProcessId = static_cast<DWORD>(-1);
if (!::GetWindowThreadProcessId(ShellWindowHandle, &ShellProcessId))
{
return static_cast<DWORD>(-1);
}
return ShellProcessId;
}
}

namespace winrt
{
using Windows::Foundation::IAsyncAction;
Expand Down Expand Up @@ -211,29 +230,91 @@ namespace winrt::NanaBox::implementation

winrt::handle(Mile::CreateThread([=]()
{
// Commented out for waiting some issue when using that under Admin.

//winrt::StoreContext Context = winrt::StoreContext::GetDefault();
//if (Context)
//{
// winrt::check_hresult(
// Context.as<IInitializeWithWindow>()->Initialize(
// this->m_WindowHandle));

// winrt::StoreProductQueryResult ProductQueryResult =
// co_await Context.GetStoreProductsAsync(
// { L"Durable" },
// { L"9P3KMWM424WK" });
// for (auto Item : ProductQueryResult.Products())
// {
// winrt::StoreProduct Product = Item.Value();

// if (!Product.IsInUserCollection())
// {
// co_await Product.RequestPurchaseAsync();
// }
// }
//}
DWORD ShellProcessId = ::GetShellProcessId();
if (static_cast<DWORD>(-1) == ShellProcessId)
{
return;
}

HANDLE ShellProcessHandle = nullptr;

auto Handler = Mile::ScopeExitTaskHandler([&]()
{
if (ShellProcessHandle)
{
::CloseHandle(ShellProcessHandle);
}
});

ShellProcessHandle = ::OpenProcess(
PROCESS_CREATE_PROCESS,
FALSE,
ShellProcessId);
if (!ShellProcessHandle)
{
return;
}

SIZE_T AttributeListSize = 0;
::InitializeProcThreadAttributeList(
nullptr,
1,
0,
&AttributeListSize);

std::vector<std::uint8_t> AttributeListBuffer =
std::vector<std::uint8_t>(AttributeListSize);

PPROC_THREAD_ATTRIBUTE_LIST AttributeList =
reinterpret_cast<PPROC_THREAD_ATTRIBUTE_LIST>(
&AttributeListBuffer[0]);

if (!::InitializeProcThreadAttributeList(
AttributeList,
1,
0,
&AttributeListSize))
{
return;
}

if (!::UpdateProcThreadAttribute(
AttributeList,
0,
PROC_THREAD_ATTRIBUTE_PARENT_PROCESS,
&ShellProcessHandle,
sizeof(ShellProcessHandle),
nullptr,
nullptr))
{
return;
}

STARTUPINFOEXW StartupInfoEx = { 0 };
PROCESS_INFORMATION ProcessInformation = { 0 };
StartupInfoEx.StartupInfo.cb = sizeof(STARTUPINFOEXW);
StartupInfoEx.lpAttributeList = AttributeList;

std::wstring ApplicationName = ::GetCurrentProcessModulePath();

if (!::CreateProcessW(
ApplicationName.c_str(),
const_cast<LPWSTR>(L"NanaBox --AcquireSponsorEdition"),
nullptr,
nullptr,
TRUE,
CREATE_UNICODE_ENVIRONMENT | EXTENDED_STARTUPINFO_PRESENT,
nullptr,
nullptr,
&StartupInfoEx.StartupInfo,
&ProcessInformation))
{
return;
}

::CloseHandle(ProcessInformation.hThread);
::WaitForSingleObjectEx(ProcessInformation.hProcess, INFINITE, FALSE);
::CloseHandle(ProcessInformation.hProcess);

this->RefreshSponsorButtonContent();
}));
Expand Down
8 changes: 1 addition & 7 deletions NanaBox/MainWindowControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@

#include <Windows.h>

#include <Mile.Helpers.CppBase.h>
#include <Mile.Helpers.CppWinRT.h>

namespace winrt::Mile
{
using namespace ::Mile;
}
#include "Utils.h"

namespace winrt
{
Expand Down
16 changes: 16 additions & 0 deletions NanaBox/NanaBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,22 @@ int WINAPI wWinMain(
OptionsAndParameters,
UnresolvedCommandLine);

bool AcquireSponsorEdition = false;

for (auto& Current : OptionsAndParameters)
{
if (0 == _wcsicmp(Current.first.c_str(), L"AcquireSponsorEdition"))
{
AcquireSponsorEdition = true;
}
}

if (AcquireSponsorEdition)
{
::MessageBoxW(nullptr, L"Sponsor Edition", L"NanaBox", 0);
::ExitProcess(0);
}

bool PackagedMode = Mile::WinRT::IsPackagedMode();
std::wstring TargetBinaryPath;

Expand Down

0 comments on commit df9015a

Please sign in to comment.