Example: define the entry point for a console application
- Last UpdatedJul 23, 2024
- 2 minute read
The following example shows the entry point for the console application with ATL support:
Note: This code can be found in C:\Program Files (x86)\ArchestrA\Samples\CreateGalaxyCPP.
// ----------------------------------------------------
// <copyright company="AVEVA Software, LLC" file="CreateGalaxyCPP.cpp">
// © 2022 AVEVA Software, LLC. All rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// </summary>
//-----------------------------------------------------
// CreateGalaxyCPP.cpp : Defines the entry point for the console application with ATL support.
//
#include "stdafx.h"
#include <time.h>
#import "GRAccess111.tlb" no_namespace
class COMINIT
{
public:
COMINIT()
{
// Initialize COM.
comInit = CoInitializeEx(
0,
//COINIT_APARTMENTTHREADED
COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE
);
}
~COMINIT()
{
// Uninitialize COM.
if (SUCCEEDED(comInit))
{
CoUninitialize();
}
}
private:
HRESULT comInit;
};
int _tmain(int argc, _TCHAR* argv[])
{
COMINIT SmartComInitialize;
CComPtr<IGRAccess> spGRApp;
HRESULT hr = spGRApp.CoCreateInstance ( __uuidof(GRAccessApp));
if (spGRApp == NULL)
return -1;
CComPtr<IGalaxies> spGalaxies;
DWORD dwsize = MAX_COMPUTERNAME_LENGTH + 1;
TCHAR szGRNodeComputerName[MAX_COMPUTERNAME_LENGTH + 1];
::GetComputerName(szGRNodeComputerName, &dwsize);
CComBSTR bstrGRNodeComputerName = szGRNodeComputerName ;
spGalaxies = spGRApp->QueryGalaxies (bstrGRNodeComputerName.m_str);
CComPtr<ICommandResult> spCommandResult;
spGRApp->get_CommandResult (&spCommandResult);
if(spCommandResult->Successful == VARIANT_FALSE || spGalaxies == NULL)
{
wprintf(L"%s : %s \n",spCommandResult->CustomMessage.GetBSTR (), spCommandResult->Text.GetBSTR ());
return 1;
}
CComPtr<IGalaxy> spGalaxy;
_variant_t vtGalName = L"Example1";
//OR you can use index if you have only one galaxy use 1. All collections are 1 base
//_variant_t vtGalName = L"1";
spGalaxies->get_Item (vtGalName,&spGalaxy);
if(spGalaxy == NULL)
{
spGRApp->CreateGalaxy (vtGalName.bstrVal ,bstrGRNodeComputerName.m_str,FALSE,galaxyAuthenticationMode,L"");
CComPtr<ICommandResult> spCommandResult;
spGRApp->get_CommandResult (&spCommandResult);
if(spCommandResult->Successful == VARIANT_FALSE || spGalaxies == NULL)
{
wprintf(L"Create Galaxy Named Example1 Failed: %s : %s \n",spCommandResult->CustomMessage.GetBSTR (), spCommandResult->Text.GetBSTR ());
return 1;
}
spGalaxies = spGRApp->QueryGalaxies (bstrGRNodeComputerName.m_str);
spGalaxies->get_Item (vtGalName,&spGalaxy);
}
spGalaxy->Login (L"",L"");
CComPtr<IgObjects> spGObjects;
_variant_t vtTemplateName = L"$UserDefined";
spGObjects = spGalaxy->QueryObjects (gObjectIsTemplate,NameEquals,&vtTemplateName,MatchCondition );
CComPtr<IgObject> spGObject;
spGObjects->get_Item (vtTemplateName,&spGObject);
CComPtr<IInstance> spInstance;
CComQIPtr<ITemplate> spTemplate = spGObject;
time_t ltime;
time( <ime );
WCHAR UTCStr [32];
wsprintf(UTCStr,L"%ld",ltime);
_bstr_t instanceName(L"UD_");
instanceName += UTCStr;
spInstance = spTemplate->CreateInstance (instanceName,VARIANT_FALSE);
spInstance->CheckOut ();
_variant_t vtCount = 5;
spInstance->AddUDA (L"Names",MxString,MxCategoryWriteable_USC_Lockable,MxSecurityOperate,VARIANT_TRUE,vtCount);
CComPtr<IAttributes> spAttributes;
spInstance->get_ConfigurableAttributes (&spAttributes);
CComPtr<IAttribute> spAttrib;
_variant_t vtAttrName = L"Names";
spAttributes->get_Item (vtAttrName,&spAttrib);
//Diplay first 5 attribute names from collection
for (int i = 1; i <= 5; i++)
{
CComPtr<IAttribute> spAttrib1 = spAttributes->GetItem(i);
wprintf(L"%s\n",spAttrib1->GetName().GetBSTR());
}
CComPtr<IMxValue> spNamesValue;
spNamesValue.CoCreateInstance (__uuidof(MxValue));
for(int i=1;i<=5;i++)
{
CComPtr<IMxValue> mxv;
mxv.CoCreateInstance (__uuidof(MxValue));
mxv->PutInteger (i);
// Attribute Data type is string Why Integer ??
// The reason is MxValue (Just Like Variant) is smart which contains logic
// to convert from one type to another.
spNamesValue->PutElement (i,mxv);
}
spAttrib->SetValue (spNamesValue);
spInstance->Save ();
spInstance->CheckIn (L"");
spGalaxy->Logout ();
WCHAR dummy [81];
wprintf (L"\nPress ENTER to quit: ");
_getws_s (dummy, 80);
//char ch = getchar();
return 0;
}