Skip to content
Open
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
297 changes: 293 additions & 4 deletions specs/applicationdata/ApplicationData.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,194 @@ for more details.
`GetTempPath2W()` is available on Windows version >= 10.0.20348.0. `ApplicationData.TemporaryPath`
performs the equivalent logic on older systems (aka Polyfill).

### 3.5.1. Unpackaged context

Unpackaged applications request an `ApplicationData` object for a specific context (publisher +
product). This object's context is determined in multiple ways:

1. Explicit API Parameter - Call `ApplicationData.GetForUnpackaged(publisher, product)`.
Comment thread
DrusTheAxe marked this conversation as resolved.
Resolved
2. Implicit via Resource in process' Executable - Call `ApplicationData.GetForUnpackaged()` to get context compiled into the process' executable at build-time (set via earlier call to `ApplicationData.SetContextForUnpackaged()`).
3. Implicit via Set API - Call `ApplicationData.GetForUnpackaged()` to get context set via earlier call to `ApplicationData.SetContextForUnpackaged(publisher, product)`.

#1 is the common case when called by the application developer who creates the executable.

#2 is needed by components used in processes where the component author doesn't know the calling
processes' context at build time, and isn't otherwise able to be handled the context at runtime. The
application sets the context fo the process and component authors use whatever has been defined for
Comment thread
DrusTheAxe marked this conversation as resolved.
Resolved
the process' context.
Comment thread
DrusTheAxe marked this conversation as resolved.
Resolved

This can also be used by application developers who prefer to compile the context into their
executable rather than specify it at runtime.

#3 is needed by components in processes using 'shared' or 'hosting' executables where one executable
is used by multiple applications. For example, `python.exe`, `pwsh.exe`, `

### 3.5.2 SetContextForUnpackaged()

`ApplicationData.SetContextForUnpackaged()` sets the context (publisher + product) for an
unpackaged process. These values follow the same rules as the parameters to
`ApplicationData.GetForUnpackaged(publisher, product)` e.g. they must be strings
with length >0.

`SetContextForUnpackaged(product, publisher)` uses these values regardless if resources are defined in
the executable.

Setting the context fails if the context has already been set and the new values differ from the
current context. For example:

```c#
ApplicationData.SetContextForUnpackaged("Contoso", "PointOfSale");
...
ApplicationData.SetContextForUnpackaged("Fabrikam", "PointOfSale");
```

or

```c#
// EXE resources are Publisher != "Contoso" and/or Product != "PointOfSale"
ApplicationData.SetContextForUnpackaged();
...
ApplicationData.SetContextForUnpackaged("Contoso", "PointOfSale");
```

or other variations.

Multiple calls to set the context only succeed if the new values match the current values. For example:

```c#
ApplicationData.SetContextForUnpackaged("Contoso", "PointOfSale");
...
ApplicationData.SetContextForUnpackaged("Contoso", "PointOfSale");
```

or

```c#
ApplicationData.SetContextForUnpackaged()
...
ApplicationData.SetContextForUnpackaged()
```

or

```c#
// EXE resources are Publisher = "Contoso" and/or Product = "PointOfSale"
ApplicationData.SetContextForUnpackaged();
...
ApplicationData.SetContextForUnpackaged("Contoso", "PointOfSale");
```

or other variations.

### 3.5.3 PublisherContextForUnpackaged and ProductContextForUnpackaged

`PublisherContextForUnpackaged` and `ProductContextForUnpackaged` properties return the context
values if set, or an empty string if `SetContextForUnpackaged` hasn't been called.

Developers can use this to check if the unpackaged process' context has been set via
`SetContextForUnpackaged()`. For example:

```c#
// Set the process' context if it hasn't already been set
if (String.IsNullOrEmpty(ApplicationData.PublisherContextForUnpackaged))
{
ApplicationData.SetContextForUnpackaged();
}
```

### 3.5.4 GetForUnpackaged() context resolution

The algorithm for `GetForUnpackaged()` to determine its context:

```c++
std::wstring g_unpackagedPublisher;
std::wstring g_unpackagedProduct;
std::mutex g_unpackagedLock;

void SetContextForUnpackaged(const std::wstring& publisher, const std::wstring& product)
{
auto lock{ std::unique_lock<std::mutex>(g_unpackagedLock) };
THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_ALREADY_ASSIGNED), !g_unpackagedPublisher.empty());
THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_ALREADY_ASSIGNED), !g_unpackagedProduct.empty());
g_unpackagedPublisher = publisher;
g_unpackagedProduct = product;
}

ApplicationData GetForUnpackaged()
{
std::wstring publisher;
std::wstring product;

{
auto lock{ std::unique_lock<std::mutex>(g_unpackagedLock) };
publisher = g_unpackagedPublisher;
product = g_unpackagedProduct;
}

return GetForUnpackaged(publisher, product);
}

void GetUnpackagedContext(std::wstring& publisher, std::wstring& product)
{
publisher.clear();
product.clear();

{
auto lock{ std::unique_lock<std::mutex>(g_unpackagedLock) };
if (!g_unpackagedPublisher.empty() && !g_unpackagedProduct.empty())
{
publisher = g_unpackagedPublisher;
product = g_unpackagedProduct;
return;
}
}
THROW_WIN32_ERROR_IF(ERROR_RESOURCE_NOT_PRESENT, !GetUnpackagedContextFromResource(publisher, product));
}

bool GetUnpackagedContextFromResource(std::wstring& publisher, std::wstring& product)
{
HMODULE module{ ::GetModuleHandleW(nullptr) };

std::wstring publisherFromResource{ GetStringFromResource( module.get(), L"ApplicationData.Publisher", L"ApplicationDataContext") };
if (!publisherFromResource.empty())
{
std::wstring productFromResource{ GetStringFromResource( module.get(), L"ApplicationData.Product", L"ApplicationDataContext") };
if (!productFromResource.empty())
{
publisher = publisherFromResource;
product = productFromResource;
return true;
}
}
return false;
}

std::wstring GetStringFromResource(HMODULE module, PCWSTR resourceName, PCSTR resourceType)
{
HRSRC resourceInformationBlock{ ::FindResourceW(module, resourceName, resourceType) };
if (!resourceInformationBlock)
{
const auto lastError{ GetLastError() };
if (lastError == ERROR_RESOURCE_NAME_NOT_FOUND)
{
return false;
}
THROW_WIN32_ERROR(lastError);
}
HGLOBAL resourceValue{ ::LoadResource(module, resourceInformationBlock) };
if (!resourceValue)
{
const auto lastError{ GetLastError() };
if ((lastError == ERROR_RESOURCE_DATA_NOT_FOUND) || (lastError == ERROR_RESOURCE_TYPE_NOT_FOUND))
{
return false;
}
THROW_WIN32_ERROR(lastError);
}
return std::wstring{ reinterpret_cast<PCWSTR>(::LockResource(resourceValue)) };
}
```
Comment thread
DrusTheAxe marked this conversation as resolved.
Resolved

# 4. Examples

## 4.1. Packaged app using LocalPath
Expand All @@ -214,7 +402,7 @@ void SaveData(string data)

## 4.2. Unpackaged app using package'd app's LocalPath

An unpackaged app app creating a file in the Fabrikam app's LocalPath data store.
An unpackaged app creating a file in the Fabrikam app's LocalPath data store.

```c#
void SaveData(string data)
Expand All @@ -241,7 +429,7 @@ void SaveData(string data)

## 4.4. Unpackaged app using LocalPath

An unpackaged app app creating a file its LocalPath data store.
An unpackaged app creating a file in its LocalPath data store.

```c#
void SaveData(string data)
Expand Down Expand Up @@ -278,6 +466,81 @@ HRESULT SaveData(DWORD dataLength, void* data)
}
```

## 4.6. Component in Unpackaged app using LocalPath - Context defined at build-time

A component in an unpackaged app creating a file in the app's LocalPath data store when the app
has defined the unpackaged context in resources at build-time.

```rc
// ContosoApp.rc

L" ApplicationData.Publisher" L"ApplicationDataContext" L"Fabrikam"
L" ApplicationData.Product" L"ApplicationDataContext" L"Contoso"
Comment thread
DrusTheAxe marked this conversation as resolved.
Resolved
```

```c#
void SaveData(string data)
{
var applicationData = ApplicationData.GetForUnpackaged();
Comment thread
DrusTheAxe marked this conversation as resolved.
Resolved
var filename = Path.Combine(applicationData.LocalPath, "stuff.dat");
File.WriteAllText(filename, data, System.Text.UTF8Encoding);
}
```

## 4.7. Component in Unpackaged app using LocalPath - Context defined at run-time

A component in an unpackaged app creating a file in the app's LocalPath data store after the app
has defined the unpackaged context at run-time.

```c#
class Program
{
static void Main()
{
string publisher = "Fabrikam";
string product = "Contoso";
ApplicationData.SetContextForUnpackaged(publisher, product)
...
}
}
```

```c#
void SaveData(string data)
{
var applicationData = ApplicationData.GetForUnpackaged();
var filename = Path.Combine(applicationData.LocalPath, "stuff.dat");
File.WriteAllText(filename, data, System.Text.UTF8Encoding);
}
```

## 4.8. Component in Unpackaged app conditionally using LocalPath - Context defined at run-time

A component in an unpackaged app creating a file in the app's LocalPath data store after the app
has defined the unpackaged context at run-time.

```c#
class Program
{
static void Main()
{
string publisher = "Fabrikam";
string product = "Contoso";
ApplicationData.SetContextForUnpackaged(publisher, product)
...
}
}
```

```c#
void SaveData(string data)
{
var applicationData = ApplicationData.GetForUnpackaged();
var filename = Path.Combine(applicationData.LocalPath, "stuff.dat");
File.WriteAllText(filename, data, System.Text.UTF8Encoding);
}
```

# 5. Remarks

## 5.1. Platform Support
Expand Down Expand Up @@ -307,7 +570,7 @@ If you need this functionality use the OS APIs in `Windows.Storage.ApplicationDa
```c# (but really MIDL3)
namespace Microsoft.Windows.Storage
{
[contractversion(2)]
[contractversion(3)]
apicontract ApplicationDataContract{};

/// Specifies the type of data store.
Expand Down Expand Up @@ -387,10 +650,36 @@ namespace Microsoft.Windows.Storage
static ApplicationData GetForPackageFamily(String packageFamilyName);

/// Get an instance of ApplicationData for the specified unpackaged app for the current user.
[feature(Feature_ApplicationData)]
/// @see GetForUnpackaged()
[contract(ApplicationDataContract, 2)]
static ApplicationData GetForUnpackaged(String publisher, String product);

/// Get an instance of ApplicationData for the current unpackaged app for the current user, per the current context.
/// @see GetForUnpackaged(String, String)
/// @see SetForUnpackaged(String, String)
[contract(ApplicationDataContract, 3)]
[method_name("GetDefaultForUnpackaged")]
static ApplicationData GetForUnpackaged();

/// Set the context (product and publisher) for the current unpackaged app.
/// @see GetForUnpackaged()
/// @see PublisherContextForUnpackaged
/// @see ProductContextForUnpackaged
[contract(ApplicationDataContract, 3)]
static void SetContextForUnpackaged(String publisher, String product);

/// Get the context's publisher for the current unpackaged app.
/// @see SetContextForUnpackaged()
/// @see ProductContextForUnpackaged
[contract(ApplicationDataContract, 3)]
static String PublisherContextForUnpackaged { get; }

/// Get the context's product for the current unpackaged app.
/// @see SetContextForUnpackaged()
/// @see PublisherContextForUnpackaged
[contract(ApplicationDataContract, 3)]
static String ProductContextForUnpackaged { get; }

/// Return true if the package family supports the machine data store.
Boolean IsMachinePathSupported { get; };

Expand Down