在 C++ 中建立應用程式物件
授權原則存放區包含一或多個應用程式的授權原則資訊。 針對使用該原則存放區的每個應用程式,您必須建立 IAzApplication 物件,並將它儲存至原則存放區。
下列範例示範如何建立代表應用程式的 IAzApplication 物件,以及如何將 IAzApplication 物件新增至應用程式所使用的授權原則存放區。 此範例假設磁片磁碟機 C 根目錄中有名為 MyStore.xml 的現有 XML 原則存放區。
#pragma comment(lib, "duser.lib")
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0502
#endif
#include <windows.h>
#include <stdio.h>
#include <azroles.h>
#include <objbase.h>
void main(void){
IAzAuthorizationStore* pStore = NULL;
IAzApplication* pApp = NULL;
HRESULT hr;
void MyHandleError(char *s);
BSTR storeName = NULL;
BSTR appName = NULL;
// Initialize COM.
hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (!(SUCCEEDED(hr)))
MyHandleError("Could not initialize COM.");
// Create the AzAuthorizationStore object.
hr = CoCreateInstance(
/*"b2bcff59-a757-4b0b-a1bc-ea69981da69e"*/
__uuidof(AzAuthorizationStore),
NULL,
CLSCTX_ALL,
/*"edbd9ca9-9b82-4f6a-9e8b-98301e450f14"*/
__uuidof(IAzAuthorizationStore),
(void**)&pStore);
if (!(SUCCEEDED(hr)))
MyHandleError("Could not create AzAuthorizationStore object.");
// Create null VARIANT for parameters.
VARIANT myVar;
VariantInit(&myVar);
// Allocate a string for the name of the store.
if(!(storeName = SysAllocString(L"msxml://c:\\MyStore.xml")))
MyHandleError("Could not allocate string.");
// Initialize the existing store.
hr = pStore->Initialize(AZ_AZSTORE_FLAG_MANAGE_STORE_ONLY,
storeName, myVar);
if (!(SUCCEEDED(hr)))
MyHandleError("Could not initialize store.");
// Create an application object.
if (!(appName = SysAllocString(L"Expense")))
MyHandleError("Could not allocate application name string");
hr = pStore->CreateApplication(appName, myVar, &pApp);
if (!(SUCCEEDED(hr)))
MyHandleError("Could not create application.");
// Save changes to the store.
hr = pApp->Submit(0, myVar);
if (!(SUCCEEDED(hr)))
MyHandleError("Could not save changes to store.");
// Clean up resources.
pStore->Release();
pApp->Release();
SysFreeString(storeName);
SysFreeString(appName);
VariantClear(&myVar);
CoUninitialize();
}
void MyHandleError(char *s)
{
printf("An error occurred in running the program.\n");
printf("%s\n",s);
printf("Error number %x\n.",GetLastError());
printf("Program terminating.\n");
exit(1);
}