First published on MSDN on Sep 12, 2017
Desktop Bridge helps developers gradually migrate traditional apps to the Universal Windows Platform (UWP). The In-App Purchase (IAP) in Windows Store is an important scenario to monetize the converted apps. When working with developers on this scenario, I notice certain obstacles are in common, for example:
a. Porting old .Net or unmanaged applications to use the IAP UWP APIs.
b. Didn’t use async calls properly in UI apps and caused hang/deadlock
c. Default purchase windows failed to popup
d. Need accurate guide to follow IAP testing with current Windows Store APIs
Here I introduce the IAPWrapper solution to solve above issues for converted Win32 Desktop Applications, and also show how easily to use the the IAPWrapper in different scenarios:
a. WinForm in old .Net version
b. WPF app with latest .Net version
c. Win32 App in C++
d. Unity Win32 App in old .Net version
Note: The sample code is put on: https://github.com/appconsult/IAPWrapper
Many converted Win32 Desktop apps were developed in unmanaged code or old .NET version which cannot directly call current UWP Store APIs, in order to eliminate this gap, let's create an .Net IAP Wrapper library. This wrapper will finally leverage DLLEXPORT nuget package to export function entries, which can be used by different kinds of Apps easily. Below are detailed steps:
Create an IAP Wrapper Project:
1. Create a Win32 .NET class library project. The target frame work can be 4.6.1. I uses VS2017 as a quick start.
2. Add two references to allow this Win32 dll calls UWP APIs and async/await properly:
C:\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework
3. Add statements to use necessary namespaces:
4. Follow this guide to call a StoreContext object in class library project that will be uses the Desktop Bridge:
In Converted Win32 Desktop App, it needs to configure the StoreContext object to specify which application window is the owner window for modal dialogs. The functional sample code is:
5. Install DLLExport nuget package to the project:
After installed the nuget package, you may see a setting dialog box pups up, can refer to this picture to configure (name space is still IAPWrapper here):
For more details of DLLExport, please refer to: https://github.com/3f/dllexport
6. Add one statement to declare DLLExport before the Purchase (string storeID) function that we need to export from this dll:
7. Use X86 or X64 option to build the library.
Note: AnyCPU option doesn’t give us the expected exported function entries.
If I build it in X64 option, using “dumpbin iapwrapper.dll /exports” can see the “Purchase” is listed in export section
This means we can load the library and function address to call it directly.
If build it in AnyCPU configuration option, the export function will not show up:
8. After above steps, we will get the IAPWRAPPER.dll, now can use it easily in various scenarios.
To move forward, firstly we should avoid below known issues:
1. Don’t call Task.Wait directly, this will cause UI apps deadlock issue. For detailed info, refer to:
Async/Await - Best Practices in Asynchronous Programming
2. Don’t run app with admin privilege, otherwise may not see the purchase diaglog box popup.
3. Make sure the purchase option runs in the UI thread, refer to:
4. The store ID used as parameter is the 12 characters (9pxxxxxxxxxx) of add-on on your developer portal, not product display name 001.
Now we start go through each scenario to see how to call the IAP functions:
Scenario one: Use IAPWRAPPER in .Net 2.0 Winform app
1. Add DLLImport statement and call the Purchase function:
2. Build the app, and put IAPWRAPPER.dll into the output folder.
Run the app, and trigger the purchase, we can see default purchase windows pops up (depends on your account setting, authentication window may occur as well,):
Scenario two: Use IAPWRAPPER in .Net 4.6 WPF app
1. Add DLLImport and call purchase function:
2. Build the app, and put IAPWRAPPER.dll into the output folder. Run the app, it will show up the same purchase window.
Scenario three: Use IAPWRAPPER in C++ Win32 app
1. Use LoadLibrary and GetProcAddress to call the Purchase function:
2. Run the app, it will show up the same purchase window.
Scenario four: Use IAPWRAPPER in UNITY Win32 app
1. Put IAPWrapper into the Assets\Plugins folder
2. Add DLLImport and Purchase function as below:
3. Build the app as Win32 app, and run it.
Here we discussed how to quickly integrate the essential Windows IAP function into different kinds of Win32 Apps. You may expand your IAPWrapper for more specific usage scenarios, checking licensing, trail status, enumerate add-ons, etc. Can refer to:
After completing above tasks, please follow the 5 bullets exactly to ensure your applications are ready for IAP and Store function tests:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.