If the method succeeds, the return value is a handle to the
storage container. If the
method fails, the return value is INVALID_HANDLE. To get extended error information,
call the GetLastError method.
The OpenStorage method opens the specified
storage container and requests an access token for the application.
This is the first method that must be called prior to accessing any
stored objects.
The application ID is a string that uniquely
identifies the application requesting the access and must have been
previously registered with the server by calling the
RegisterAppId method. If the lpszAppId
parameter is NULL or an empty string, the method will use a default
internal ID that is allocated for each storage account. You can use
this default ID if you wish to share data between all of the
applications you create.
The storage type specifies the type of container that objects will
be stored in. In most cases, we recommend using WEB_STORAGE_GLOBAL
which means that stored objects will be accessible to all users of
your application. However, you can limit access to the stored objects
based on the local domain, local machine ID or the current user SID.
If you specify anything other than global storage, objects can be
orphaned if the system configuration changes. For example, if
WEB_STORAGE_MACHINE is specified the objects that are stored can only
be accessed from that system. If the system is reconfigured (for
example, the boot volume formatted and Windows is reinstalled) the
unique identifier for that system will change and the previous objects
that were stored by your application can no longer be accessed.
It is advisable is to store critical application data and
configuration information using WEB_STORAGE_GLOBAL and use other
non-global storage containers for configuration information that is
unique to that system and/or user which is not critical and can be
easily recreated.
If a storage container was previously opened, calling this method
will automatically close the current container and open the new
container specified by the dwStorageType parameter. This can
change the internal handle value used to reference the container and
the old handle will no longer be valid.
CWebStorage appStorage;
LPCTSTR lpszAppId = _T("MyCompany.WebTest.1");
LPCTSTR lpszLocalFile = _T("TestDocument.pdf");
LPCTSTR lpszObjectLabel = _T("TestDocument.pdf");
WEB_STORAGE_OBJECT webObject = { 0, };
if (!appStorage.RegisterAppId(lpszAppId))
{
tprintf(_T("Unable to register the application ID"));
_exit(0);
}
if (!appStorage.OpenStorage(lpszAppId, WEB_STORAGE_GLOBAL);
{
tprintf(_T("Unable to open global storage for this application"));
_exit(0);
}
if (appStorage.PutFile(lpszLocalFile, lpszObjectLabel, &webObject))
{
_tprintf(_T("Object: %s\n"), webObject.szObjectId);
_tprintf(_T("Label: %s\n"), webObject.szLabel);
_tprintf(_T("Size: %lu\n"), webObject.dwObjectSize);
_tprintf(_T("Digest: %s\n"), webObject.szDigest);
_tprintf(_T("Content: %s\n"), webObject.szContent);
}
else
{
CString strError;
appStorage.GetLastError(strError);
_tprintf(_T("Cannot store \"%s\" (%s)\n"), lpszLocalFile, (LPCTSTR)strError);
}
appStorage.CloseStorage();