From 822e88c6e246b25116bb2b36522210f7e2636b4b Mon Sep 17 00:00:00 2001 From: "Chris Guzak (WINDOWS)" Date: Thu, 20 Sep 2018 11:04:54 -0700 Subject: [PATCH 01/10] update to latest SDK 17134 --- .../ActivatableOptionalPackage1.vcxproj | 408 ++++++++--------- .../ActivatableOptionalPackage2.vcxproj | 410 ++++++++--------- MyMainApp/MyMainApp.vcxproj | 398 ++++++++-------- OptionalPackageDLL/OptionalPackageDLL.vcxproj | 424 +++++++++--------- 4 files changed, 820 insertions(+), 820 deletions(-) diff --git a/ActivatableOptionalPackage1/ActivatableOptionalPackage1.vcxproj b/ActivatableOptionalPackage1/ActivatableOptionalPackage1.vcxproj index acf8037..83e7bc4 100644 --- a/ActivatableOptionalPackage1/ActivatableOptionalPackage1.vcxproj +++ b/ActivatableOptionalPackage1/ActivatableOptionalPackage1.vcxproj @@ -1,205 +1,205 @@ - - - - {1c0199ca-1a35-460b-848a-a1e50227f5a1} - ActivatableOptionalPackage1 - en-US - 14.0 - true - Windows Store - 10.0.15063.0 - 10.0.15063.0 - 10.0 - 29270depappf.ActivatableOptional1 - Application - - - - - Debug - ARM - - - Debug - Win32 - - - Debug - x64 - - - Release - ARM - - - Release - Win32 - - - Release - x64 - - - - Application - true - v141 - - - Application - true - v141 - - - Application - true - v141 - - - Application - false - true - v141 - true - - - Application - false - true - v141 - true - - - Application - false - true - v141 - true - - - - - - - - - - - - - - - - - - - - - - - - - ActivatableOptionalPackage1_TemporaryKey.pfx - 9D9742B2E8CD105986131CD5859BB8F404653AE7 - False - Always - x86 - False - - - - /bigobj %(AdditionalOptions) - 4453;28204 - - - - - /bigobj %(AdditionalOptions) - 4453;28204 - - - - - /bigobj %(AdditionalOptions) - 4453;28204 - - - - - /bigobj %(AdditionalOptions) - 4453;28204 - - - - - /bigobj %(AdditionalOptions) - 4453;28204 - - - - - /bigobj %(AdditionalOptions) - 4453;28204 - - - - - - App.xaml - - - MainPage.xaml - - - - - Designer - - - Designer - - - - - Designer - - - - - - - - - - - - - - - - App.xaml - - - MainPage.xaml - - - Create - Create - Create - Create - Create - Create - - - - - {5887cd9c-428e-4151-914b-14b7ed6e0301} - - - - - - - - + + + + {1c0199ca-1a35-460b-848a-a1e50227f5a1} + ActivatableOptionalPackage1 + en-US + 14.0 + true + Windows Store + 10.0.17134.0 + 10.0.15063.0 + 10.0 + 29270depappf.ActivatableOptional1 + Application + + + + + Debug + ARM + + + Debug + Win32 + + + Debug + x64 + + + Release + ARM + + + Release + Win32 + + + Release + x64 + + + + Application + true + v141 + + + Application + true + v141 + + + Application + true + v141 + + + Application + false + true + v141 + true + + + Application + false + true + v141 + true + + + Application + false + true + v141 + true + + + + + + + + + + + + + + + + + + + + + + + + + ActivatableOptionalPackage1_TemporaryKey.pfx + 9D9742B2E8CD105986131CD5859BB8F404653AE7 + False + Always + x86 + False + + + + /bigobj %(AdditionalOptions) + 4453;28204 + + + + + /bigobj %(AdditionalOptions) + 4453;28204 + + + + + /bigobj %(AdditionalOptions) + 4453;28204 + + + + + /bigobj %(AdditionalOptions) + 4453;28204 + + + + + /bigobj %(AdditionalOptions) + 4453;28204 + + + + + /bigobj %(AdditionalOptions) + 4453;28204 + + + + + + App.xaml + + + MainPage.xaml + + + + + Designer + + + Designer + + + + + Designer + + + + + + + + + + + + + + + + App.xaml + + + MainPage.xaml + + + Create + Create + Create + Create + Create + Create + + + + + {5887cd9c-428e-4151-914b-14b7ed6e0301} + + + + + + + + \ No newline at end of file diff --git a/ActivatableOptionalPackage2/ActivatableOptionalPackage2.vcxproj b/ActivatableOptionalPackage2/ActivatableOptionalPackage2.vcxproj index 7fd68d8..3473cab 100644 --- a/ActivatableOptionalPackage2/ActivatableOptionalPackage2.vcxproj +++ b/ActivatableOptionalPackage2/ActivatableOptionalPackage2.vcxproj @@ -1,206 +1,206 @@ - - - - {b287e6f8-905d-49bd-afb4-b49c371b742f} - ActivatableOptionalPackage2 - en-US - 14.0 - true - Windows Store - 10.0.15063.0 - 10.0.15063.0 - 10.0 - 29270depappf.ActivatableOptional2 - Application - - - - - Debug - ARM - - - Debug - Win32 - - - Debug - x64 - - - Release - ARM - - - Release - Win32 - - - Release - x64 - - - - Application - true - v141 - - - Application - true - v141 - - - Application - true - v141 - - - Application - false - true - v141 - true - - - Application - false - true - v141 - true - - - Application - false - true - v141 - true - - - - - - - - - - - - - - - - - - - - - - - - - ActivatableOptionalPackage2_TemporaryKey.pfx - 043C19AB9AAD6D8131C6BF16507FE557AA9C079D - False - Always - x86 - False - - - - /bigobj %(AdditionalOptions) - 4453;28204 - - - - - /bigobj %(AdditionalOptions) - 4453;28204 - - - - - /bigobj %(AdditionalOptions) - 4453;28204 - - - - - /bigobj %(AdditionalOptions) - 4453;28204 - - - - - /bigobj %(AdditionalOptions) - 4453;28204 - - - - - /bigobj %(AdditionalOptions) - 4453;28204 - - - - - - App.xaml - - - MainPage.xaml - - - - - Designer - - - Designer - - - - - Designer - - - - - - - - - - - - - - App.xaml - - - MainPage.xaml - - - Create - Create - Create - Create - Create - Create - - - - - - - - {5887cd9c-428e-4151-914b-14b7ed6e0301} - - - - - - - - + + + + {b287e6f8-905d-49bd-afb4-b49c371b742f} + ActivatableOptionalPackage2 + en-US + 14.0 + true + Windows Store + 10.0.17134.0 + 10.0.15063.0 + 10.0 + 29270depappf.ActivatableOptional2 + Application + + + + + Debug + ARM + + + Debug + Win32 + + + Debug + x64 + + + Release + ARM + + + Release + Win32 + + + Release + x64 + + + + Application + true + v141 + + + Application + true + v141 + + + Application + true + v141 + + + Application + false + true + v141 + true + + + Application + false + true + v141 + true + + + Application + false + true + v141 + true + + + + + + + + + + + + + + + + + + + + + + + + + ActivatableOptionalPackage2_TemporaryKey.pfx + 043C19AB9AAD6D8131C6BF16507FE557AA9C079D + False + Always + x86 + False + + + + /bigobj %(AdditionalOptions) + 4453;28204 + + + + + /bigobj %(AdditionalOptions) + 4453;28204 + + + + + /bigobj %(AdditionalOptions) + 4453;28204 + + + + + /bigobj %(AdditionalOptions) + 4453;28204 + + + + + /bigobj %(AdditionalOptions) + 4453;28204 + + + + + /bigobj %(AdditionalOptions) + 4453;28204 + + + + + + App.xaml + + + MainPage.xaml + + + + + Designer + + + Designer + + + + + Designer + + + + + + + + + + + + + + App.xaml + + + MainPage.xaml + + + Create + Create + Create + Create + Create + Create + + + + + + + + {5887cd9c-428e-4151-914b-14b7ed6e0301} + + + + + + + + \ No newline at end of file diff --git a/MyMainApp/MyMainApp.vcxproj b/MyMainApp/MyMainApp.vcxproj index c37eb58..00b5ab0 100644 --- a/MyMainApp/MyMainApp.vcxproj +++ b/MyMainApp/MyMainApp.vcxproj @@ -1,200 +1,200 @@ - - - - {eaf0bdee-941c-4fc0-af41-b6e425c55636} - MyMainApp - en-US - 14.0 - true - Windows Store - 10.0.15063.0 - 10.0.15063.0 - 10.0 - true - - - - - Debug - ARM - - - Debug - Win32 - - - Debug - x64 - - - Release - ARM - - - Release - Win32 - - - Release - x64 - - - - Application - true - v141 - - - Application - true - v141 - - - Application - true - v141 - - - Application - false - true - v141 - true - - - Application - false - true - v141 - true - - - Application - false - true - v141 - true - - - - - - - - - - - - - - - - - - - - - - - - - MyMainApp_TemporaryKey.pfx - False - Always - x86|x64|arm - 6878E566494B0A1367FDBA240BD4C772B97F5976 - False - - - - /bigobj %(AdditionalOptions) - 4453;28204 - - - - - /bigobj %(AdditionalOptions) - 4453;28204 - - - - - /bigobj %(AdditionalOptions) - 4453;28204 - - - - - /bigobj %(AdditionalOptions) - 4453;28204 - - - - - /bigobj %(AdditionalOptions) - 4453;28204 - - - - - /bigobj %(AdditionalOptions) - 4453;28204 - - - - - - App.xaml - - - MainPage.xaml - - - - - Designer - - - Designer - - - - - Designer - - - - - - - - - - - - - - App.xaml - - - MainPage.xaml - - - Create - Create - Create - Create - Create - Create - - - - - - - - - - - + + + + {eaf0bdee-941c-4fc0-af41-b6e425c55636} + MyMainApp + en-US + 14.0 + true + Windows Store + 10.0.17134.0 + 10.0.15063.0 + 10.0 + true + + + + + Debug + ARM + + + Debug + Win32 + + + Debug + x64 + + + Release + ARM + + + Release + Win32 + + + Release + x64 + + + + Application + true + v141 + + + Application + true + v141 + + + Application + true + v141 + + + Application + false + true + v141 + true + + + Application + false + true + v141 + true + + + Application + false + true + v141 + true + + + + + + + + + + + + + + + + + + + + + + + + + MyMainApp_TemporaryKey.pfx + False + Always + x86|x64|arm + 6878E566494B0A1367FDBA240BD4C772B97F5976 + False + + + + /bigobj %(AdditionalOptions) + 4453;28204 + + + + + /bigobj %(AdditionalOptions) + 4453;28204 + + + + + /bigobj %(AdditionalOptions) + 4453;28204 + + + + + /bigobj %(AdditionalOptions) + 4453;28204 + + + + + /bigobj %(AdditionalOptions) + 4453;28204 + + + + + /bigobj %(AdditionalOptions) + 4453;28204 + + + + + + App.xaml + + + MainPage.xaml + + + + + Designer + + + Designer + + + + + Designer + + + + + + + + + + + + + + App.xaml + + + MainPage.xaml + + + Create + Create + Create + Create + Create + Create + + + + + + + + + + + \ No newline at end of file diff --git a/OptionalPackageDLL/OptionalPackageDLL.vcxproj b/OptionalPackageDLL/OptionalPackageDLL.vcxproj index e3a816b..77e5e7a 100644 --- a/OptionalPackageDLL/OptionalPackageDLL.vcxproj +++ b/OptionalPackageDLL/OptionalPackageDLL.vcxproj @@ -1,213 +1,213 @@ - - - - - Debug - ARM - - - Debug - Win32 - - - Debug - x64 - - - Release - ARM - - - Release - Win32 - - - Release - x64 - - - - {5887cd9c-428e-4151-914b-14b7ed6e0301} - DynamicLibrary - OptionalPackageDLL - CoffeeOptionalPackageDLL - en-US - 14.0 - true - Windows Store - 10.0.15063.0 - 10.0.15063.0 - 10.0 - - - - DynamicLibrary - true - v141 - - - DynamicLibrary - true - v141 - - - DynamicLibrary - true - v141 - - - DynamicLibrary - false - true - v141 - - - DynamicLibrary - false - true - v141 - - - DynamicLibrary - false - true - v141 - - - - - - - - - - - - - - - - - - - - - - - - - - - - false - false - - - false - false - $(SolutionDir)$(Platform)\$(Configuration)\$(MSBuildProjectName)\ - - - false - false - - - false - false - - - false - false - $(SolutionDir)$(Platform)\$(Configuration)\$(MSBuildProjectName)\ - - - false - false - - - - Use - false - - - Console - false - false - - - - - Use - false - - - Console - false - false - - - - - Use - false - - - Console - false - false - - - - - Use - false - - - Console - false - false - - - - - Use - false - - - Console - false - false - - - - - Use - false - - - Console - false - false - - - - - - - - - - - - Create - Create - Create - Create - Create - Create - - - - - + + + + + Debug + ARM + + + Debug + Win32 + + + Debug + x64 + + + Release + ARM + + + Release + Win32 + + + Release + x64 + + + + {5887cd9c-428e-4151-914b-14b7ed6e0301} + DynamicLibrary + OptionalPackageDLL + CoffeeOptionalPackageDLL + en-US + 14.0 + true + Windows Store + 10.0.17134.0 + 10.0.15063.0 + 10.0 + + + + DynamicLibrary + true + v141 + + + DynamicLibrary + true + v141 + + + DynamicLibrary + true + v141 + + + DynamicLibrary + false + true + v141 + + + DynamicLibrary + false + true + v141 + + + DynamicLibrary + false + true + v141 + + + + + + + + + + + + + + + + + + + + + + + + + + + + false + false + + + false + false + $(SolutionDir)$(Platform)\$(Configuration)\$(MSBuildProjectName)\ + + + false + false + + + false + false + + + false + false + $(SolutionDir)$(Platform)\$(Configuration)\$(MSBuildProjectName)\ + + + false + false + + + + Use + false + + + Console + false + false + + + + + Use + false + + + Console + false + false + + + + + Use + false + + + Console + false + false + + + + + Use + false + + + Console + false + false + + + + + Use + false + + + Console + false + false + + + + + Use + false + + + Console + false + false + + + + + + + + + + + + Create + Create + Create + Create + Create + Create + + + + + \ No newline at end of file From e2f1d3a109069d4f063c2a50e82dbbc6f829a7e6 Mon Sep 17 00:00:00 2001 From: "Chris Guzak (WINDOWS)" Date: Thu, 20 Sep 2018 11:05:10 -0700 Subject: [PATCH 02/10] latest SDK --- OptionalPackage/OptionalPackage.vcxproj | 341 ++++++++++-------------- 1 file changed, 144 insertions(+), 197 deletions(-) diff --git a/OptionalPackage/OptionalPackage.vcxproj b/OptionalPackage/OptionalPackage.vcxproj index 81c8c9e..7b6ee00 100644 --- a/OptionalPackage/OptionalPackage.vcxproj +++ b/OptionalPackage/OptionalPackage.vcxproj @@ -1,198 +1,145 @@ - - - - {15195583-7e99-4f0e-b15f-4e37c06def87} - OptionalPackage - en-US - 14.0 - true - Windows Store - 10.0.15063.0 - 10.0.15063.0 - 10.0 - - - - - Debug - ARM - - - Debug - Win32 - - - Debug - x64 - - - Release - ARM - - - Release - Win32 - - - Release - x64 - - - - Application - true - v141 - - - Application - true - v141 - - - Application - true - v141 - - - Application - false - true - v141 - true - - - Application - false - true - v141 - true - - - Application - false - true - v141 - true - - - - - - - - - - - - - - - - - - - - - - - - - OptionalPackage_TemporaryKey.pfx - True - Always - x86|x64|arm - 36614B54BB478456A8B9E92D1CEFDF21DD4D84EB - - - - /bigobj %(AdditionalOptions) - 4453;28204 - - - - - /bigobj %(AdditionalOptions) - 4453;28204 - - - - - /bigobj %(AdditionalOptions) - 4453;28204 - - - - - /bigobj %(AdditionalOptions) - 4453;28204 - - - - - /bigobj %(AdditionalOptions) - 4453;28204 - - - - - /bigobj %(AdditionalOptions) - 4453;28204 - - - - - - App.xaml - - - MainPage.xaml - - - - - Designer - - - Designer - - - - - Designer - - - - - - - - - - - - - - App.xaml - - - MainPage.xaml - - - Create - Create - Create - Create - Create - Create - - - - - - - - - - - + + + + {15195583-7e99-4f0e-b15f-4e37c06def87} + OptionalPackage + en-US + 14.0 + true + Windows Store + 10.0.17134.0 + 10.0.15063.0 + 10.0 + + + + + Debug + ARM + + + Debug + Win32 + + + Debug + x64 + + + Release + ARM + + + Release + Win32 + + + Release + x64 + + + + Application + true + v141 + + + Application + true + v141 + + + Application + true + v141 + + + Application + false + true + v141 + true + + + Application + false + true + v141 + true + + + Application + false + true + v141 + true + + + + + + + OptionalPackage_TemporaryKey.pfx + True + Always + x86|x64|arm + 36614B54BB478456A8B9E92D1CEFDF21DD4D84EB + + + + /bigobj %(AdditionalOptions) + 4453;28204 + + + + + + App.xaml + + + MainPage.xaml + + + + + Designer + + + Designer + + + + + Designer + + + + + + + + + + + + + + App.xaml + + + MainPage.xaml + + + Create + + + + + + + + + + + \ No newline at end of file From 77b43bc5fdcdf0304e96c09f6ed25d130ca2867c Mon Sep 17 00:00:00 2001 From: "Chris Guzak (WINDOWS)" Date: Thu, 20 Sep 2018 11:05:23 -0700 Subject: [PATCH 03/10] tabs -> spaces, minor code cleanup --- MyMainApp/App.xaml.cpp | 2 +- MyMainApp/MainPage.xaml.cpp | 400 +++++++++++----------- OptionalPackage/MainPage.xaml.cpp | 2 +- OptionalPackageDLL/OptionalPackageDLL.cpp | 4 +- OptionalPackageDLL/OptionalPackageDLL.h | 2 +- 5 files changed, 195 insertions(+), 215 deletions(-) diff --git a/MyMainApp/App.xaml.cpp b/MyMainApp/App.xaml.cpp index 56b1c09..80f3e4e 100644 --- a/MyMainApp/App.xaml.cpp +++ b/MyMainApp/App.xaml.cpp @@ -39,7 +39,7 @@ App::App() /// Details about the launch request and process. void App::OnLaunched(Windows::ApplicationModel::Activation::LaunchActivatedEventArgs^ e) { -#if _DEBUG +#if 0 // Show graphics profiling information while debugging. if (IsDebuggerPresent()) { diff --git a/MyMainApp/MainPage.xaml.cpp b/MyMainApp/MainPage.xaml.cpp index 878473f..3fe0dd3 100644 --- a/MyMainApp/MainPage.xaml.cpp +++ b/MyMainApp/MainPage.xaml.cpp @@ -22,248 +22,230 @@ using namespace Windows::UI::Core; // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 - - MainPage::MainPage() { - InitializeComponent(); - HookupCatalog(); - + InitializeComponent(); + HookupCatalog(); } void MainPage::ReadOptionalPackageContent() { - concurrency::create_task([this]() - { - WriteToTextBox("Enumerating Packages"); - auto optionalPackages = EnumerateInstalledPackages(); - for (auto package : optionalPackages) - { - Platform::String^ packageName = ref new String(package->Id->FullName->Data()); - WriteToTextBox(packageName); - - LoadTextFromPackage(package); - LoadDLLFromPackage(package); - WriteToTextBox("+++++++++++++++++++"); - WriteToTextBox("\n"); - } - DebugPrint(L" Finished loading all optional packages\n"); - DebugPrint(L"\n"); - }); + concurrency::create_task([this]() + { + WriteToTextBox("Enumerating Packages"); + auto optionalPackages = EnumerateInstalledPackages(); + for (auto package : optionalPackages) + { + Platform::String^ packageName = ref new String(package->Id->FullName->Data()); + WriteToTextBox(packageName); + + LoadTextFromPackage(package); + LoadDLLFromPackage(package); + WriteToTextBox("+++++++++++++++++++"); + WriteToTextBox("\n"); + } + DebugPrint(L" Finished loading all optional packages\n"); + DebugPrint(L"\n"); + }); } -std::vector< Windows::ApplicationModel::Package^ > MainPage::EnumerateInstalledPackages() +std::vector MainPage::EnumerateInstalledPackages() { - DebugPrint(L" Searching for optional packages...\n"); - - // Obtain the app's package first to then find all related packages - auto currentAppPackage = Windows::ApplicationModel::Package::Current; - - // The dependencies list is where the list of optional packages (OP) can be determined - auto dependencies = currentAppPackage->Dependencies; - std::vector< Windows::ApplicationModel::Package^ > optionalPackages; - - for (auto package : dependencies) - { - // If it is optional, then add it to our results vector - if (package->IsOptional) - { - DebugPrint(L" Optional Package found - %ws\n", package->Id->FullName->Data()); - optionalPackages.push_back(package); - } - } - - return optionalPackages; // Return the resulting vector + DebugPrint(L" Searching for optional packages...\n"); + + // The dependencies list is where the list of optional packages (OP) can be determined + std::vector optionalPackages; + + for (auto package : Windows::ApplicationModel::Package::Current->Dependencies) + { + // If it is optional, then add it to our results vector + if (package->IsOptional) + { + DebugPrint(L" Optional Package found - %ws\n", package->Id->FullName->Data()); + optionalPackages.push_back(package); + } + } + + return optionalPackages; } void MainPage::HookupCatalog() { - catalog = Windows::ApplicationModel::PackageCatalog::OpenForCurrentPackage(); - catalog->PackageInstalling += ref new Windows::Foundation::TypedEventHandler(this, &MyMainApp::MainPage::OnPackageInstalling); + catalog = Windows::ApplicationModel::PackageCatalog::OpenForCurrentPackage(); + catalog->PackageInstalling += ref new Windows::Foundation::TypedEventHandler(this, &MyMainApp::MainPage::OnPackageInstalling); } void MainPage::OnPackageInstalling(Windows::ApplicationModel::PackageCatalog ^sender, Windows::ApplicationModel::PackageInstallingEventArgs ^args) { - if (args->IsComplete) - { - WriteToTextBox("PackageCatalog - Done installing package"); - WriteToTextBox(args->Package->Id->FamilyName->ToString()); - } + if (args->IsComplete) + { + WriteToTextBox("PackageCatalog - Done installing package"); + WriteToTextBox(args->Package->Id->FamilyName->ToString()); + } } void MainPage::LoadTextFromPackage(Windows::ApplicationModel::Package^ package) { - try - { - Windows::Storage::StorageFolder^ installFolder = package->InstalledLocation; - - concurrency::create_task(installFolder->GetFolderAsync(L"Content")) - .then([this](concurrency::task< Windows::Storage::StorageFolder^ > result) - { - try - { - auto assetsFolder = result.get(); - - auto fileAsyncOp = assetsFolder->GetFileAsync(L"SampleFile.txt"); - - concurrency::create_task(fileAsyncOp) - .then([this](concurrency::task< Windows::Storage::StorageFile^ > task) - { - try - { - auto targetFile = task.get(); - - if (targetFile->IsAvailable) - { - WriteToTextBox("Found SampleFile.txt - loading contents"); - DebugPrint(L" %ws is available:\n", targetFile->Name->Data()); - concurrency::create_task(targetFile->OpenAsync(Windows::Storage::FileAccessMode::Read)) - .then([this, targetFile](concurrency::task< Windows::Storage::Streams::IRandomAccessStream^ > task) - { - try - { - auto readStream = task.get(); - UINT64 const size = readStream->Size; - if (size <= MAXUINT32) - { - auto dataReader = ref new Windows::Storage::Streams::DataReader(readStream); - concurrency::create_task(dataReader->LoadAsync(static_cast(size))) - .then([this, targetFile, dataReader](unsigned int numBytesLoaded) - { - Platform::String^ fileContent = dataReader->ReadString(numBytesLoaded); - - WriteToTextBox(fileContent); - - delete dataReader; // As a best practice, explicitly close the dataReader resource as soon as it is no longer needed. - DebugPrint(L" %ws\n", fileContent->Data()); - }); - } - else - { - delete readStream; // As a best practice, explicitly close the readStream resource as soon as it is no longer needed. - DebugPrint(L" File %ws is too big for LoadAsync to load in a single chunk. Files larger than 4GB need to be broken into multiple chunks to be loaded by LoadAsync.", targetFile->Name->Data()); - } - } - catch (Platform::Exception^ ex) - { - DebugPrint(L" Error 0x%x reading text file\n", ex->HResult); - } - }).wait(); - } - else - { - DebugPrint(L" File not available\n"); - } - } - catch (Platform::Exception^ ex) - { - DebugPrint(L" Error 0x%x getting text file\n", ex->HResult); - } - }).wait(); - } - catch (Platform::Exception^ ex) - { - DebugPrint(L" Error 0x%x getting Contents folder\n", ex->HResult); - } - - }).wait(); - - return; - } - catch (Platform::Exception^ ex) - { - DebugPrint(L" Error 0x%x\n", ex->HResult); - } + try + { + concurrency::create_task(package->InstalledLocation->GetFolderAsync(L"Content")) + .then([this](concurrency::task< Windows::Storage::StorageFolder^ > result) + { + try + { + auto assetsFolder = result.get(); + + auto fileAsyncOp = assetsFolder->GetFileAsync(L"SampleFile.txt"); + + concurrency::create_task(fileAsyncOp) + .then([this](concurrency::task task) + { + try + { + auto targetFile = task.get(); + + if (targetFile->IsAvailable) + { + WriteToTextBox("Found SampleFile.txt - loading contents"); + DebugPrint(L" %ws is available:\n", targetFile->Name->Data()); + concurrency::create_task(targetFile->OpenAsync(Windows::Storage::FileAccessMode::Read)) + .then([this, targetFile](concurrency::task< Windows::Storage::Streams::IRandomAccessStream^ > task) + { + try + { + auto readStream = task.get(); + UINT64 const size = readStream->Size; + if (size <= MAXUINT32) + { + auto dataReader = ref new Windows::Storage::Streams::DataReader(readStream); + concurrency::create_task(dataReader->LoadAsync(static_cast(size))) + .then([this, targetFile, dataReader](unsigned int numBytesLoaded) + { + Platform::String^ fileContent = dataReader->ReadString(numBytesLoaded); + + WriteToTextBox(fileContent); + + delete dataReader; // As a best practice, explicitly close the dataReader resource as soon as it is no longer needed. + DebugPrint(L" %ws\n", fileContent->Data()); + }); + } + else + { + delete readStream; // As a best practice, explicitly close the readStream resource as soon as it is no longer needed. + DebugPrint(L" File %ws is too big for LoadAsync to load in a single chunk. Files larger than 4GB need to be broken into multiple chunks to be loaded by LoadAsync.", targetFile->Name->Data()); + } + } + catch (Platform::Exception^ ex) + { + DebugPrint(L" Error 0x%x reading text file\n", ex->HResult); + } + }).wait(); + } + else + { + DebugPrint(L" File not available\n"); + } + } + catch (Platform::Exception^ ex) + { + DebugPrint(L" Error 0x%x getting text file\n", ex->HResult); + } + }).wait(); + } + catch (Platform::Exception^ ex) + { + DebugPrint(L" Error 0x%x getting Contents folder\n", ex->HResult); + } + + }).wait(); + } + catch (Platform::Exception^ ex) + { + DebugPrint(L" Error 0x%x\n", ex->HResult); + } } void MainPage::LoadDLLFromPackage(Windows::ApplicationModel::Package^ package) { - Windows::Storage::StorageFolder^ Folder = package->InstalledLocation; - auto asyncOp = Folder->GetFileAsync(L"OptionalPackageDLL.dll"); - - Concurrency::create_task(asyncOp) - .then([this, package](concurrency::task< Windows::Storage::StorageFile^ > task) - { - try - { - auto targetFile = task.get(); - - if (targetFile->IsAvailable) - { - DebugPrint(L" %ws is available:\n", targetFile->Name->Data()); - auto DllModule = LoadPackagedLibrary(targetFile->Name->Data(), 0); - - if (DllModule != NULL) - { - WriteToTextBox("Contains dll - loading code"); - try - { - auto procAddress = GetProcAddress(DllModule, "ExampleAPIExport"); - - if (procAddress != NULL) - { - DebugPrint(L" Value returned from ExampleAPIExport: %i\n", procAddress()); - auto ret = procAddress(); - WriteToTextBox(ret.ToString()); - } - else - { - DWORD error = GetLastError(); - error = error; - } - } - catch (Platform::Exception^ ex) - { - DebugPrint(L" Error getting address for ExampleAPIExport 0x%x\n", GetLastError()); - } - FreeLibrary(DllModule); - } - else - { - HRESULT hr = HRESULT_FROM_WIN32(GetLastError()); - if (hr == 0x8007007e) //ERROR_MOD_NOT_FOUND - { - DebugPrint(L" Could not load dll from OP - ERROR_MOD_NOT_FOUND. Make sure that your op is in a related set.\n"); - WriteToTextBox("Could not load dll from OP - ERROR_MOD_NOT_FOUND. Make sure that your op is in a related set."); - } - else - { - DebugPrint(L" Error getting DLL file in package 0x%x\n", hr); - WriteToTextBox("Error getting DLL file in package"); - } - } - } - } - catch (Platform::Exception^ ex) - { - if (ex->HResult == 0x80070002) //E_FILE_NOT_FOUND - { - DebugPrint(L" No DLL to load in package\n"); - } - else - { - DebugPrint(L" Error 0x%x loading DLL file from OP package\n %ws\n", ex->HResult, ex->Message->Data()); - } - - } - }).wait(); - - return; - + auto asyncOp = package->InstalledLocation->GetFileAsync(L"OptionalPackageDLL.dll"); + + concurrency::create_task(asyncOp) + .then([this, package](concurrency::task< Windows::Storage::StorageFile^ > task) + { + try + { + auto targetFile = task.get(); + if (targetFile->IsAvailable) + { + DebugPrint(L" %ws is available:\n", targetFile->Name->Data()); + auto dllModule = LoadPackagedLibrary(targetFile->Name->Data(), 0); + if (dllModule) + { + WriteToTextBox("Contains dll - loading code"); + try + { + auto procAddress = GetProcAddress(dllModule, "ExampleAPIExport"); + if (procAddress) + { + DebugPrint(L" Value returned from ExampleAPIExport: %i\n", procAddress()); + auto ret = procAddress(); + WriteToTextBox(ret.ToString()); + } + else + { + DWORD error = GetLastError(); + error = error; + } + } + catch (Platform::Exception^ ex) + { + DebugPrint(L" Error getting address for ExampleAPIExport 0x%x\n", GetLastError()); + } + FreeLibrary(dllModule); + } + else + { + HRESULT hr = HRESULT_FROM_WIN32(GetLastError()); + if (hr == HRESULT_FROM_WIN32(ERROR_MOD_NOT_FOUND)) + { + DebugPrint(L" Could not load dll from OP - ERROR_MOD_NOT_FOUND. Make sure that your op is in a related set.\n"); + WriteToTextBox("Could not load dll from OP - ERROR_MOD_NOT_FOUND. Make sure that your op is in a related set."); + } + else + { + DebugPrint(L" Error getting DLL file in package 0x%x\n", hr); + WriteToTextBox("Error getting DLL file in package"); + } + } + } + } + catch (Platform::Exception^ ex) + { + if (ex->HResult == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) + { + DebugPrint(L" No DLL to load in package\n"); + } + else + { + DebugPrint(L" Error 0x%x loading DLL file from OP package\n %ws\n", ex->HResult, ex->Message->Data()); + } + + } + }).wait(); } void MainPage::WriteToTextBox(Platform::String^ str) { - auto dispatcher = Windows::ApplicationModel::Core::CoreApplication::MainView->Dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, - ref new Windows::UI::Core::DispatchedHandler([this, str]() - { - ApptextBox->Text += str +"\n"; - })); + auto dispatcher = Windows::ApplicationModel::Core::CoreApplication::MainView->Dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, + ref new Windows::UI::Core::DispatchedHandler([this, str]() + { + ApptextBox->Text += str + "\n"; + })); } void MyMainApp::MainPage::button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e) { - ReadOptionalPackageContent(); + ReadOptionalPackageContent(); } diff --git a/OptionalPackage/MainPage.xaml.cpp b/OptionalPackage/MainPage.xaml.cpp index d86fee5..a75e08a 100644 --- a/OptionalPackage/MainPage.xaml.cpp +++ b/OptionalPackage/MainPage.xaml.cpp @@ -23,5 +23,5 @@ using namespace Windows::UI::Xaml::Navigation; MainPage::MainPage() { - InitializeComponent(); + InitializeComponent(); } diff --git a/OptionalPackageDLL/OptionalPackageDLL.cpp b/OptionalPackageDLL/OptionalPackageDLL.cpp index f36f036..a4faf45 100644 --- a/OptionalPackageDLL/OptionalPackageDLL.cpp +++ b/OptionalPackageDLL/OptionalPackageDLL.cpp @@ -4,7 +4,5 @@ // Optional Package Code __declspec(dllexport) int __cdecl ExampleAPIExport() { - return 40; + return 40; } - - diff --git a/OptionalPackageDLL/OptionalPackageDLL.h b/OptionalPackageDLL/OptionalPackageDLL.h index 754fe38..d960f58 100644 --- a/OptionalPackageDLL/OptionalPackageDLL.h +++ b/OptionalPackageDLL/OptionalPackageDLL.h @@ -1,5 +1,5 @@ #pragma once extern "C" { - __declspec(dllexport) int __cdecl ExampleAPIExport(); + __declspec(dllexport) int __cdecl ExampleAPIExport(); } From 1ce2c757fdb83a2e759e6ebf0c38d7ec72226a0e Mon Sep 17 00:00:00 2001 From: "Chris Guzak (WINDOWS)" Date: Thu, 20 Sep 2018 16:05:44 -0700 Subject: [PATCH 04/10] use __stdcall for dll export --- OptionalPackageDLL/OptionalPackageDLL.cpp | 2 +- OptionalPackageDLL/OptionalPackageDLL.h | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/OptionalPackageDLL/OptionalPackageDLL.cpp b/OptionalPackageDLL/OptionalPackageDLL.cpp index a4faf45..53aaeff 100644 --- a/OptionalPackageDLL/OptionalPackageDLL.cpp +++ b/OptionalPackageDLL/OptionalPackageDLL.cpp @@ -2,7 +2,7 @@ #include "OptionalPackageDLL.h" // Optional Package Code -__declspec(dllexport) int __cdecl ExampleAPIExport() +EXTERN_C __declspec(dllexport) int __stdcall ExampleAPIExport() { return 40; } diff --git a/OptionalPackageDLL/OptionalPackageDLL.h b/OptionalPackageDLL/OptionalPackageDLL.h index d960f58..06f2668 100644 --- a/OptionalPackageDLL/OptionalPackageDLL.h +++ b/OptionalPackageDLL/OptionalPackageDLL.h @@ -1,5 +1,2 @@ #pragma once -extern "C" -{ - __declspec(dllexport) int __cdecl ExampleAPIExport(); -} +EXTERN_C __declspec(dllexport) int __stdcall ExampleAPIExport(); From d2242d8e6bc8465c7632634ad5507efdefcf8127 Mon Sep 17 00:00:00 2001 From: "Chris Guzak (WINDOWS)" Date: Thu, 20 Sep 2018 16:06:01 -0700 Subject: [PATCH 05/10] tabs -> spaces --- ActivatableOptionalPackage2/MainPage.xaml.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ActivatableOptionalPackage2/MainPage.xaml.cpp b/ActivatableOptionalPackage2/MainPage.xaml.cpp index d0cad6d..5b86ab0 100644 --- a/ActivatableOptionalPackage2/MainPage.xaml.cpp +++ b/ActivatableOptionalPackage2/MainPage.xaml.cpp @@ -23,5 +23,5 @@ using namespace Windows::UI::Xaml::Navigation; MainPage::MainPage() { - InitializeComponent(); + InitializeComponent(); } From e7943ce91413fda05c927f35d997a4b08174b17a Mon Sep 17 00:00:00 2001 From: "Chris Guzak (WINDOWS)" Date: Thu, 20 Sep 2018 17:04:06 -0700 Subject: [PATCH 06/10] use __stdcall, proper calling convention used by the client use .def file to export function remove unneeded targetver.h --- OptionalPackageDLL/OptionalPackageDLL.cpp | 5 ++--- OptionalPackageDLL/OptionalPackageDLL.h | 2 -- OptionalPackageDLL/OptionalPackageDLL.vcxproj | 11 +++++++++-- OptionalPackageDLL/exports.def | 2 ++ OptionalPackageDLL/pch.h | 7 ------- OptionalPackageDLL/targetver.h | 8 -------- 6 files changed, 13 insertions(+), 22 deletions(-) delete mode 100644 OptionalPackageDLL/OptionalPackageDLL.h create mode 100644 OptionalPackageDLL/exports.def delete mode 100644 OptionalPackageDLL/targetver.h diff --git a/OptionalPackageDLL/OptionalPackageDLL.cpp b/OptionalPackageDLL/OptionalPackageDLL.cpp index 53aaeff..8d1a5aa 100644 --- a/OptionalPackageDLL/OptionalPackageDLL.cpp +++ b/OptionalPackageDLL/OptionalPackageDLL.cpp @@ -1,8 +1,7 @@ #include "pch.h" -#include "OptionalPackageDLL.h" -// Optional Package Code -EXTERN_C __declspec(dllexport) int __stdcall ExampleAPIExport() +int __stdcall ExampleAPIExport() { + // Optional Package Code goes here return 40; } diff --git a/OptionalPackageDLL/OptionalPackageDLL.h b/OptionalPackageDLL/OptionalPackageDLL.h deleted file mode 100644 index 06f2668..0000000 --- a/OptionalPackageDLL/OptionalPackageDLL.h +++ /dev/null @@ -1,2 +0,0 @@ -#pragma once -EXTERN_C __declspec(dllexport) int __stdcall ExampleAPIExport(); diff --git a/OptionalPackageDLL/OptionalPackageDLL.vcxproj b/OptionalPackageDLL/OptionalPackageDLL.vcxproj index 77e5e7a..9397cf5 100644 --- a/OptionalPackageDLL/OptionalPackageDLL.vcxproj +++ b/OptionalPackageDLL/OptionalPackageDLL.vcxproj @@ -133,6 +133,7 @@ Console false false + exports.def @@ -144,6 +145,7 @@ Console false false + exports.def @@ -155,6 +157,7 @@ Console false false + exports.def @@ -166,6 +169,7 @@ Console false false + exports.def @@ -177,6 +181,7 @@ Console false false + exports.def @@ -188,12 +193,11 @@ Console false false + exports.def - - @@ -207,6 +211,9 @@ Create + + + diff --git a/OptionalPackageDLL/exports.def b/OptionalPackageDLL/exports.def new file mode 100644 index 0000000..e30dce3 --- /dev/null +++ b/OptionalPackageDLL/exports.def @@ -0,0 +1,2 @@ +EXPORTS + ExampleAPIExport diff --git a/OptionalPackageDLL/pch.h b/OptionalPackageDLL/pch.h index 529bbb1..b416959 100644 --- a/OptionalPackageDLL/pch.h +++ b/OptionalPackageDLL/pch.h @@ -1,9 +1,2 @@ #pragma once - -#include "targetver.h" - -#ifndef WIN32_LEAN_AND_MEAN -#define WIN32_LEAN_AND_MEAN -#endif - #include diff --git a/OptionalPackageDLL/targetver.h b/OptionalPackageDLL/targetver.h deleted file mode 100644 index a66ecb0..0000000 --- a/OptionalPackageDLL/targetver.h +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once - -// Including SDKDDKVer.h defines the highest available Windows platform. - -// If you wish to build your application for a previous Windows platform, include WinSDKVer.h and -// set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. - -#include From 5f75b814ecc02fb3fedb29795d83f90b7489e9dc Mon Sep 17 00:00:00 2001 From: "Chris Guzak (WINDOWS)" Date: Thu, 20 Sep 2018 17:32:28 -0700 Subject: [PATCH 07/10] use the TryGetItemAsync to find files that may not be present, avoid exceptions --- MyMainApp/MainPage.xaml.cpp | 180 +++++++++++++----------------------- MyMainApp/MainPage.xaml.h | 70 +++++++------- 2 files changed, 99 insertions(+), 151 deletions(-) diff --git a/MyMainApp/MainPage.xaml.cpp b/MyMainApp/MainPage.xaml.cpp index 3fe0dd3..90b8e07 100644 --- a/MyMainApp/MainPage.xaml.cpp +++ b/MyMainApp/MainPage.xaml.cpp @@ -20,7 +20,6 @@ using namespace Windows::UI::Xaml::Media; using namespace Windows::UI::Xaml::Navigation; using namespace Windows::UI::Core; - // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 MainPage::MainPage() { @@ -28,12 +27,12 @@ MainPage::MainPage() HookupCatalog(); } -void MainPage::ReadOptionalPackageContent() +void MainPage::ReadOptionalPackageContentAsync() { concurrency::create_task([this]() { WriteToTextBox("Enumerating Packages"); - auto optionalPackages = EnumerateInstalledPackages(); + auto optionalPackages = GetOptionalPackages(); for (auto package : optionalPackages) { Platform::String^ packageName = ref new String(package->Id->FullName->Data()); @@ -49,7 +48,7 @@ void MainPage::ReadOptionalPackageContent() }); } -std::vector MainPage::EnumerateInstalledPackages() +std::vector MainPage::GetOptionalPackages() { DebugPrint(L" Searching for optional packages...\n"); @@ -58,7 +57,6 @@ std::vector MainPage::EnumerateInstalledPac for (auto package : Windows::ApplicationModel::Package::Current->Dependencies) { - // If it is optional, then add it to our results vector if (package->IsOptional) { DebugPrint(L" Optional Package found - %ws\n", package->Id->FullName->Data()); @@ -86,104 +84,79 @@ void MainPage::OnPackageInstalling(Windows::ApplicationModel::PackageCatalog ^se void MainPage::LoadTextFromPackage(Windows::ApplicationModel::Package^ package) { - try + concurrency::create_task(package->InstalledLocation->TryGetItemAsync(L"Content")) + .then([this](concurrency::task result) { - concurrency::create_task(package->InstalledLocation->GetFolderAsync(L"Content")) - .then([this](concurrency::task< Windows::Storage::StorageFolder^ > result) + auto contentItem = result.get(); + if (contentItem) { - try - { - auto assetsFolder = result.get(); - - auto fileAsyncOp = assetsFolder->GetFileAsync(L"SampleFile.txt"); + auto contentFolder = safe_cast(contentItem); - concurrency::create_task(fileAsyncOp) - .then([this](concurrency::task task) + concurrency::create_task(contentFolder->TryGetItemAsync(L"SampleFile.txt")) + .then([this](concurrency::task result) + { + if (result.get()) { - try + auto sampleFile = safe_cast(result.get()); + if (sampleFile->IsAvailable) { - auto targetFile = task.get(); - - if (targetFile->IsAvailable) + WriteToTextBox("Found SampleFile.txt - loading contents"); + DebugPrint(L" %ws is available:\n", sampleFile->Name->Data()); + concurrency::create_task(sampleFile->OpenAsync(Windows::Storage::FileAccessMode::Read)) + .then([this, sampleFile](concurrency::task task) { - WriteToTextBox("Found SampleFile.txt - loading contents"); - DebugPrint(L" %ws is available:\n", targetFile->Name->Data()); - concurrency::create_task(targetFile->OpenAsync(Windows::Storage::FileAccessMode::Read)) - .then([this, targetFile](concurrency::task< Windows::Storage::Streams::IRandomAccessStream^ > task) + auto readStream = task.get(); + UINT64 const size = readStream->Size; + if (size <= MAXUINT32) { - try - { - auto readStream = task.get(); - UINT64 const size = readStream->Size; - if (size <= MAXUINT32) - { - auto dataReader = ref new Windows::Storage::Streams::DataReader(readStream); - concurrency::create_task(dataReader->LoadAsync(static_cast(size))) - .then([this, targetFile, dataReader](unsigned int numBytesLoaded) - { - Platform::String^ fileContent = dataReader->ReadString(numBytesLoaded); - - WriteToTextBox(fileContent); - - delete dataReader; // As a best practice, explicitly close the dataReader resource as soon as it is no longer needed. - DebugPrint(L" %ws\n", fileContent->Data()); - }); - } - else - { - delete readStream; // As a best practice, explicitly close the readStream resource as soon as it is no longer needed. - DebugPrint(L" File %ws is too big for LoadAsync to load in a single chunk. Files larger than 4GB need to be broken into multiple chunks to be loaded by LoadAsync.", targetFile->Name->Data()); - } - } - catch (Platform::Exception^ ex) + auto dataReader = ref new Windows::Storage::Streams::DataReader(readStream); + concurrency::create_task(dataReader->LoadAsync(static_cast(size))) + .then([this, sampleFile, dataReader](unsigned int numBytesLoaded) { - DebugPrint(L" Error 0x%x reading text file\n", ex->HResult); - } - }).wait(); - } - else - { - DebugPrint(L" File not available\n"); - } - } - catch (Platform::Exception^ ex) - { - DebugPrint(L" Error 0x%x getting text file\n", ex->HResult); - } - }).wait(); - } - catch (Platform::Exception^ ex) - { - DebugPrint(L" Error 0x%x getting Contents folder\n", ex->HResult); - } + auto fileContent = dataReader->ReadString(numBytesLoaded); - }).wait(); - } - catch (Platform::Exception^ ex) - { - DebugPrint(L" Error 0x%x\n", ex->HResult); - } + WriteToTextBox(fileContent); + + delete dataReader; // As a best practice, explicitly close the dataReader resource as soon as it is no longer needed. + DebugPrint(L" %ws\n", fileContent->Data()); + }); + } + }).wait(); + } + } + else + { + DebugPrint(L" SampleFile.txt not available\n"); + } + }).wait(); + } + else + { + DebugPrint(L" Content folder not available\n"); + } + }).wait(); } void MainPage::LoadDLLFromPackage(Windows::ApplicationModel::Package^ package) { - auto asyncOp = package->InstalledLocation->GetFileAsync(L"OptionalPackageDLL.dll"); - - concurrency::create_task(asyncOp) - .then([this, package](concurrency::task< Windows::Storage::StorageFile^ > task) + concurrency::create_task(package->InstalledLocation->TryGetItemAsync(L"OptionalPackageDLL.dll")) + .then([this, package](concurrency::task task) { - try + if (task.get()) { - auto targetFile = task.get(); - if (targetFile->IsAvailable) + auto targetFile = safe_cast(task.get()); + if (targetFile) { - DebugPrint(L" %ws is available:\n", targetFile->Name->Data()); - auto dllModule = LoadPackagedLibrary(targetFile->Name->Data(), 0); - if (dllModule) + if (targetFile->IsAvailable) { - WriteToTextBox("Contains dll - loading code"); - try + DebugPrint(L" %ws is available:\n", targetFile->Name->Data()); + auto path = targetFile->Path->Data(); + auto name = targetFile->Name->Data(); + auto dllModule = LoadPackagedLibrary(name, 0); + if (dllModule) { + WriteToTextBox("Contains dll - loading code"); + auto procAddress = GetProcAddress(dllModule, "ExampleAPIExport"); if (procAddress) { @@ -191,45 +164,20 @@ void MainPage::LoadDLLFromPackage(Windows::ApplicationModel::Package^ package) auto ret = procAddress(); WriteToTextBox(ret.ToString()); } - else - { - DWORD error = GetLastError(); - error = error; - } - } - catch (Platform::Exception^ ex) - { - DebugPrint(L" Error getting address for ExampleAPIExport 0x%x\n", GetLastError()); - } - FreeLibrary(dllModule); - } - else - { - HRESULT hr = HRESULT_FROM_WIN32(GetLastError()); - if (hr == HRESULT_FROM_WIN32(ERROR_MOD_NOT_FOUND)) - { - DebugPrint(L" Could not load dll from OP - ERROR_MOD_NOT_FOUND. Make sure that your op is in a related set.\n"); - WriteToTextBox("Could not load dll from OP - ERROR_MOD_NOT_FOUND. Make sure that your op is in a related set."); + FreeLibrary(dllModule); } else { - DebugPrint(L" Error getting DLL file in package 0x%x\n", hr); - WriteToTextBox("Error getting DLL file in package"); + DWORD error = GetLastError(); + error = error; } } } - } - catch (Platform::Exception^ ex) - { - if (ex->HResult == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) - { - DebugPrint(L" No DLL to load in package\n"); - } else { - DebugPrint(L" Error 0x%x loading DLL file from OP package\n %ws\n", ex->HResult, ex->Message->Data()); + DebugPrint(L" Could not load dll from the optional package, make sure it is installed.\n"); + WriteToTextBox("Could not load dll from the optional package, make sure it is installed."); } - } }).wait(); } @@ -245,7 +193,7 @@ void MainPage::WriteToTextBox(Platform::String^ str) void MyMainApp::MainPage::button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e) { - ReadOptionalPackageContent(); + ReadOptionalPackageContentAsync(); } diff --git a/MyMainApp/MainPage.xaml.h b/MyMainApp/MainPage.xaml.h index 639a535..bb71d07 100644 --- a/MyMainApp/MainPage.xaml.h +++ b/MyMainApp/MainPage.xaml.h @@ -9,39 +9,39 @@ namespace MyMainApp { - /// - /// An empty page that can be used on its own or navigated to within a Frame. - /// - public ref class MainPage sealed - { - public: - MainPage(); - private: - Windows::ApplicationModel::PackageCatalog^ catalog; - std::vector < Windows::ApplicationModel::Package^ > EnumerateInstalledPackages(); - void MainPage::OnPackageInstalling(Windows::ApplicationModel::PackageCatalog ^sender, Windows::ApplicationModel::PackageInstallingEventArgs ^args); - void MainPage::HookupCatalog(); - void LoadTextFromPackage(Windows::ApplicationModel::Package^ package); - void LoadDLLFromPackage(Windows::ApplicationModel::Package^ package); - void MainPage::WriteToTextBox(Platform::String^ str); - void MainPage::ReadOptionalPackageContent(); - - void DebugPrint(const wchar_t* msg, ...) - { - va_list ap; - va_start(ap, msg); - - int len = _vscwprintf(msg, ap) + 1; - wchar_t * buf = (wchar_t*)malloc(len * sizeof(wchar_t)); - vswprintf_s(buf, len, msg, ap); - buf[len - 1] = 0; - - va_end(ap); - - OutputDebugString(buf); - - free(buf); - }; - void button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e); - }; + /// + /// An empty page that can be used on its own or navigated to within a Frame. + /// + public ref class MainPage sealed + { + public: + MainPage(); + private: + Windows::ApplicationModel::PackageCatalog^ catalog; + std::vector GetOptionalPackages(); + void MainPage::OnPackageInstalling(Windows::ApplicationModel::PackageCatalog ^sender, Windows::ApplicationModel::PackageInstallingEventArgs ^args); + void MainPage::HookupCatalog(); + void LoadTextFromPackage(Windows::ApplicationModel::Package^ package); + void LoadDLLFromPackage(Windows::ApplicationModel::Package^ package); + void MainPage::WriteToTextBox(Platform::String^ str); + void MainPage::ReadOptionalPackageContentAsync(); + + void DebugPrint(const wchar_t* msg, ...) + { + va_list ap; + va_start(ap, msg); + + int len = _vscwprintf(msg, ap) + 1; + wchar_t * buf = (wchar_t*)malloc(len * sizeof(wchar_t)); + vswprintf_s(buf, len, msg, ap); + buf[len - 1] = 0; + + va_end(ap); + + OutputDebugString(buf); + + free(buf); + }; + void button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e); + }; } From 64d9ea3f2c711ee231334f9473990e587c648659 Mon Sep 17 00:00:00 2001 From: "Chris Guzak (WINDOWS)" Date: Fri, 21 Sep 2018 09:32:04 -0700 Subject: [PATCH 08/10] user relative path to content file, report error on LoadPackagedLibrary failure --- MyMainApp/MainPage.xaml.cpp | 67 ++++++++++++++----------------------- 1 file changed, 26 insertions(+), 41 deletions(-) diff --git a/MyMainApp/MainPage.xaml.cpp b/MyMainApp/MainPage.xaml.cpp index 90b8e07..2684c61 100644 --- a/MyMainApp/MainPage.xaml.cpp +++ b/MyMainApp/MainPage.xaml.cpp @@ -84,55 +84,41 @@ void MainPage::OnPackageInstalling(Windows::ApplicationModel::PackageCatalog ^se void MainPage::LoadTextFromPackage(Windows::ApplicationModel::Package^ package) { - concurrency::create_task(package->InstalledLocation->TryGetItemAsync(L"Content")) + concurrency::create_task(package->InstalledLocation->TryGetItemAsync(LR"(Content\SampleFile.txt)")) .then([this](concurrency::task result) { - auto contentItem = result.get(); - if (contentItem) + if (result.get()) { - auto contentFolder = safe_cast(contentItem); - - concurrency::create_task(contentFolder->TryGetItemAsync(L"SampleFile.txt")) - .then([this](concurrency::task result) + auto sampleFile = safe_cast(result.get()); + if (sampleFile->IsAvailable) { - if (result.get()) + WriteToTextBox("Found SampleFile.txt - loading contents"); + DebugPrint(L" %ws is available:\n", sampleFile->Name->Data()); + concurrency::create_task(sampleFile->OpenAsync(Windows::Storage::FileAccessMode::Read)) + .then([this, sampleFile](concurrency::task task) { - auto sampleFile = safe_cast(result.get()); - if (sampleFile->IsAvailable) + auto readStream = task.get(); + UINT64 const size = readStream->Size; + if (size <= MAXUINT32) { - WriteToTextBox("Found SampleFile.txt - loading contents"); - DebugPrint(L" %ws is available:\n", sampleFile->Name->Data()); - concurrency::create_task(sampleFile->OpenAsync(Windows::Storage::FileAccessMode::Read)) - .then([this, sampleFile](concurrency::task task) + auto dataReader = ref new Windows::Storage::Streams::DataReader(readStream); + concurrency::create_task(dataReader->LoadAsync(static_cast(size))) + .then([this, sampleFile, dataReader](unsigned int numBytesLoaded) { - auto readStream = task.get(); - UINT64 const size = readStream->Size; - if (size <= MAXUINT32) - { - auto dataReader = ref new Windows::Storage::Streams::DataReader(readStream); - concurrency::create_task(dataReader->LoadAsync(static_cast(size))) - .then([this, sampleFile, dataReader](unsigned int numBytesLoaded) - { - auto fileContent = dataReader->ReadString(numBytesLoaded); - - WriteToTextBox(fileContent); - - delete dataReader; // As a best practice, explicitly close the dataReader resource as soon as it is no longer needed. - DebugPrint(L" %ws\n", fileContent->Data()); - }); - } - }).wait(); + auto fileContent = dataReader->ReadString(numBytesLoaded); + + WriteToTextBox(fileContent); + + delete dataReader; // As a best practice, explicitly close the dataReader resource as soon as it is no longer needed. + DebugPrint(L" %ws\n", fileContent->Data()); + }); } - } - else - { - DebugPrint(L" SampleFile.txt not available\n"); - } - }).wait(); + }).wait(); + } } else { - DebugPrint(L" Content folder not available\n"); + DebugPrint(L" SampleFile.txt not available\n"); } }).wait(); } @@ -150,7 +136,6 @@ void MainPage::LoadDLLFromPackage(Windows::ApplicationModel::Package^ package) if (targetFile->IsAvailable) { DebugPrint(L" %ws is available:\n", targetFile->Name->Data()); - auto path = targetFile->Path->Data(); auto name = targetFile->Name->Data(); auto dllModule = LoadPackagedLibrary(name, 0); if (dllModule) @@ -168,8 +153,8 @@ void MainPage::LoadDLLFromPackage(Windows::ApplicationModel::Package^ package) } else { - DWORD error = GetLastError(); - error = error; + const DWORD error = GetLastError(); + DebugPrint(L" LoadPackagedLibrary failed, make sure package certificates are configured: %i\n", error); } } } From 788d7b89e2fa88778582aefe982f5f39a7d1d065 Mon Sep 17 00:00:00 2001 From: "Chris Guzak (WINDOWS)" Date: Fri, 21 Sep 2018 09:34:52 -0700 Subject: [PATCH 09/10] use dynamic_cast to differentate file/folder/null, simpler code --- MyMainApp/MainPage.xaml.cpp | 104 +++++++++++++++++------------------- 1 file changed, 48 insertions(+), 56 deletions(-) diff --git a/MyMainApp/MainPage.xaml.cpp b/MyMainApp/MainPage.xaml.cpp index 2684c61..ad1f969 100644 --- a/MyMainApp/MainPage.xaml.cpp +++ b/MyMainApp/MainPage.xaml.cpp @@ -20,7 +20,6 @@ using namespace Windows::UI::Xaml::Media; using namespace Windows::UI::Xaml::Navigation; using namespace Windows::UI::Core; -// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 MainPage::MainPage() { InitializeComponent(); @@ -35,7 +34,7 @@ void MainPage::ReadOptionalPackageContentAsync() auto optionalPackages = GetOptionalPackages(); for (auto package : optionalPackages) { - Platform::String^ packageName = ref new String(package->Id->FullName->Data()); + auto packageName = ref new String(package->Id->FullName->Data()); WriteToTextBox(packageName); LoadTextFromPackage(package); @@ -84,41 +83,40 @@ void MainPage::OnPackageInstalling(Windows::ApplicationModel::PackageCatalog ^se void MainPage::LoadTextFromPackage(Windows::ApplicationModel::Package^ package) { - concurrency::create_task(package->InstalledLocation->TryGetItemAsync(LR"(Content\SampleFile.txt)")) - .then([this](concurrency::task result) + auto sampleFileRelPath = ref new Platform::String(LR"(Content\SampleFile.txt)"); + concurrency::create_task(package->InstalledLocation->TryGetItemAsync(sampleFileRelPath)) + .then([this, sampleFileRelPath](concurrency::task result) { - if (result.get()) + // maybe null (try failed) or might be a folder, not a file. + auto sampleFile = dynamic_cast(result.get()); + if (sampleFile && sampleFile->IsAvailable) { - auto sampleFile = safe_cast(result.get()); - if (sampleFile->IsAvailable) + WriteToTextBox("Found SampleFile.txt - loading contents"); + DebugPrint(L" %ws is available:\n", sampleFile->Name->Data()); + concurrency::create_task(sampleFile->OpenAsync(Windows::Storage::FileAccessMode::Read)) + .then([this, sampleFile](concurrency::task task) { - WriteToTextBox("Found SampleFile.txt - loading contents"); - DebugPrint(L" %ws is available:\n", sampleFile->Name->Data()); - concurrency::create_task(sampleFile->OpenAsync(Windows::Storage::FileAccessMode::Read)) - .then([this, sampleFile](concurrency::task task) + auto readStream = task.get(); + UINT64 const size = readStream->Size; + if (size <= MAXUINT32) { - auto readStream = task.get(); - UINT64 const size = readStream->Size; - if (size <= MAXUINT32) + auto dataReader = ref new Windows::Storage::Streams::DataReader(readStream); + concurrency::create_task(dataReader->LoadAsync(static_cast(size))) + .then([this, sampleFile, dataReader](unsigned int numBytesLoaded) { - auto dataReader = ref new Windows::Storage::Streams::DataReader(readStream); - concurrency::create_task(dataReader->LoadAsync(static_cast(size))) - .then([this, sampleFile, dataReader](unsigned int numBytesLoaded) - { - auto fileContent = dataReader->ReadString(numBytesLoaded); - - WriteToTextBox(fileContent); - - delete dataReader; // As a best practice, explicitly close the dataReader resource as soon as it is no longer needed. - DebugPrint(L" %ws\n", fileContent->Data()); - }); - } - }).wait(); - } + auto fileContent = dataReader->ReadString(numBytesLoaded); + + WriteToTextBox(fileContent); + + delete dataReader; // As a best practice, explicitly close the dataReader resource as soon as it is no longer needed. + DebugPrint(L" %ws\n", fileContent->Data()); + }); + } + }).wait(); } else { - DebugPrint(L" SampleFile.txt not available\n"); + DebugPrint(L" %ws not available\n", sampleFileRelPath->Data()); } }).wait(); } @@ -128,42 +126,36 @@ void MainPage::LoadDLLFromPackage(Windows::ApplicationModel::Package^ package) concurrency::create_task(package->InstalledLocation->TryGetItemAsync(L"OptionalPackageDLL.dll")) .then([this, package](concurrency::task task) { - if (task.get()) + auto targetFile = dynamic_cast(task.get()); + if (targetFile && targetFile->IsAvailable) { - auto targetFile = safe_cast(task.get()); - if (targetFile) + DebugPrint(L" %ws is available:\n", targetFile->Name->Data()); + auto name = targetFile->Name->Data(); + auto dllModule = LoadPackagedLibrary(name, 0); + if (dllModule) { - if (targetFile->IsAvailable) + WriteToTextBox("Contains dll - loading code"); + + auto procAddress = GetProcAddress(dllModule, "ExampleAPIExport"); + if (procAddress) { - DebugPrint(L" %ws is available:\n", targetFile->Name->Data()); - auto name = targetFile->Name->Data(); - auto dllModule = LoadPackagedLibrary(name, 0); - if (dllModule) - { - WriteToTextBox("Contains dll - loading code"); - - auto procAddress = GetProcAddress(dllModule, "ExampleAPIExport"); - if (procAddress) - { - DebugPrint(L" Value returned from ExampleAPIExport: %i\n", procAddress()); - auto ret = procAddress(); - WriteToTextBox(ret.ToString()); - } - FreeLibrary(dllModule); - } - else - { - const DWORD error = GetLastError(); - DebugPrint(L" LoadPackagedLibrary failed, make sure package certificates are configured: %i\n", error); - } + const auto ret = procAddress(); + DebugPrint(L" Value returned from ExampleAPIExport: %i\n", ret); + WriteToTextBox(ret.ToString()); } + FreeLibrary(dllModule); } else { - DebugPrint(L" Could not load dll from the optional package, make sure it is installed.\n"); - WriteToTextBox("Could not load dll from the optional package, make sure it is installed."); + const DWORD error = GetLastError(); + DebugPrint(L" LoadPackagedLibrary failed, make sure package certificates are configured: %i\n", error); } } + else + { + DebugPrint(L" Could not load dll from the optional package, make sure it is installed.\n"); + WriteToTextBox("Could not load dll from the optional package, make sure it is installed."); + } }).wait(); } From 15876334bf28dc044dff5855673f7194fa8874cb Mon Sep 17 00:00:00 2001 From: "Chris Guzak (WINDOWS)" Date: Tue, 25 Sep 2018 12:35:27 -0700 Subject: [PATCH 10/10] c++17, /await, cert thumbprint updates --- .../ActivatableOptionalPackage1.vcxproj | 2 +- .../ActivatableOptionalPackage2.vcxproj | 2 +- MyMainApp/MyMainApp.vcxproj | 25 +++++++++++++------ OptionalPackage/OptionalPackage.vcxproj | 2 +- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/ActivatableOptionalPackage1/ActivatableOptionalPackage1.vcxproj b/ActivatableOptionalPackage1/ActivatableOptionalPackage1.vcxproj index 83e7bc4..a354265 100644 --- a/ActivatableOptionalPackage1/ActivatableOptionalPackage1.vcxproj +++ b/ActivatableOptionalPackage1/ActivatableOptionalPackage1.vcxproj @@ -100,7 +100,7 @@ ActivatableOptionalPackage1_TemporaryKey.pfx - 9D9742B2E8CD105986131CD5859BB8F404653AE7 + 41664B9D04F5F72A9EEC997ABF13EF192F790CA6 False Always x86 diff --git a/ActivatableOptionalPackage2/ActivatableOptionalPackage2.vcxproj b/ActivatableOptionalPackage2/ActivatableOptionalPackage2.vcxproj index 3473cab..bbe80e5 100644 --- a/ActivatableOptionalPackage2/ActivatableOptionalPackage2.vcxproj +++ b/ActivatableOptionalPackage2/ActivatableOptionalPackage2.vcxproj @@ -100,7 +100,7 @@ ActivatableOptionalPackage2_TemporaryKey.pfx - 043C19AB9AAD6D8131C6BF16507FE557AA9C079D + 360F3CB0B474121921745C51C0472412B93E9BD0 False Always x86 diff --git a/MyMainApp/MyMainApp.vcxproj b/MyMainApp/MyMainApp.vcxproj index 00b5ab0..dc23fc0 100644 --- a/MyMainApp/MyMainApp.vcxproj +++ b/MyMainApp/MyMainApp.vcxproj @@ -101,44 +101,53 @@ MyMainApp_TemporaryKey.pfx False Always - x86|x64|arm - 6878E566494B0A1367FDBA240BD4C772B97F5976 + x86 + 6A905D6D6322A2DEA8063BB5BAE93EF3AAC33A7A False + False + 1 + OnApplicationRun - /bigobj %(AdditionalOptions) + /bigobj %(AdditionalOptions) /await 4453;28204 + stdcpp17 - /bigobj %(AdditionalOptions) + /bigobj %(AdditionalOptions) /await 4453;28204 + stdcpp17 - /bigobj %(AdditionalOptions) + /bigobj %(AdditionalOptions) /await 4453;28204 + stdcpp17 - /bigobj %(AdditionalOptions) + /bigobj %(AdditionalOptions) /await 4453;28204 + stdcpp17 - /bigobj %(AdditionalOptions) + /bigobj %(AdditionalOptions) /await 4453;28204 + stdcpp17 - /bigobj %(AdditionalOptions) + /bigobj %(AdditionalOptions) /await 4453;28204 + stdcpp17 diff --git a/OptionalPackage/OptionalPackage.vcxproj b/OptionalPackage/OptionalPackage.vcxproj index 7b6ee00..ab3905f 100644 --- a/OptionalPackage/OptionalPackage.vcxproj +++ b/OptionalPackage/OptionalPackage.vcxproj @@ -83,7 +83,7 @@ True Always x86|x64|arm - 36614B54BB478456A8B9E92D1CEFDF21DD4D84EB + A9963FE83D5A7F5BFBB500638055422C28DB24D2