Konsolenanwendung - Geloggte Meldungen via ADS-Proxy-Schnittstelle

 

Systemvoraussetzungen:

Das Beispiel baut eine ADS-Proxy-Verbindung zu einem Remote-EventLogger auf und zeigt in der Konsole alle dort geloggten EventLogger Meldungen.

Konsolenanwendung - Geloggte Meldungen via ADS-Proxy-Schnittstelle 1:

 

#include <vcl.h>
#pragma hdrstop

#include <tchar.h>
#include <stdio.h>
#include <string.h>
#include <iostream.h>
#include <TCEVENTLOGGERLib_TLB.h>
#include <TCEVENTLOGPROXYLib_TLB.h>

//---------------------------------------------------------------------------
#pragma argsused
int _tmain(int argc, _TCHAR* argv[])
{
  // Connect via ADS
 // BSTR netID = SysAllocString( L"10.1.128.220.1.1" );// ToDo: Configure ams net id if connection to remote TwinCAT SystemBSTR netID = SysAllocString( L"" );
    long langID = 1033;
    ITcEventLogAdsProxyPtr spAdsProxy;
    HRESULT hr = spAdsProxy.CreateInstance(CLSID_TcEventLogAdsProxy);
    if (SUCCEEDED(hr))
    {
        hr = spAdsProxy->Connect( netID );// connect to the remote/local TwinCAT system
     if (SUCCEEDED(hr))
        {
            ITcEventLogPtr  spTcEventLog;
            hr = spAdsProxy->QueryInterface( IID_ITcEventLog, (void**)&spTcEventLog);
            if (SUCCEEDED(hr))
            {
                long nLoggedEvents = 0;
                long nActiveEvents = 0;
                hr = spTcEventLog->get_LoggedEvents(&nLoggedEvents);
                hr = spTcEventLog->get_ActiveEvents(&nActiveEvents);
                wprintf( L"Max. number of events: %d logged, %d active.\n", nLoggedEvents, nActiveEvents );

                ITcEnumEventsExPtr spEnumEvts;
                hr = spTcEventLog->EnumLoggedEventsEx(&spEnumEvts);// get collection of logged events
             if (SUCCEEDED(hr))
                {
                    if (spEnumEvts->Count > 0)
                    {
                        for (long i = 0; i < spEnumEvts->Count; i++)
                        {
                            ITcEventPtr spEvent;
                            hr = spEnumEvts->Item(i, &spEvent); // get event
                         if (SUCCEEDED(hr))
                            {

                                BSTR msgString = 0;
                                BSTR fmtProgId = 0;
                                BSTR srcName = 0;
                                DATE dtCreated = 0;
                                DATE dtConfirmed = 0;
                                DATE dtReset= 0;
                                long msCreated = 0;
                                long msConfirmed = 0;
                                long msReset = 0;

                                hr = spEvent->get_Date(&dtCreated);
                                hr = spEvent->get_DateConfirmed(&dtConfirmed);
                                hr = spEvent->get_DateReset(&dtReset);
                                hr = spEvent->get_Ms(&msCreated);
                                hr = spEvent->get_MsConfirmed(&msConfirmed);
                                hr = spEvent->get_MsReset(&msReset);

                                wprintf( L"Event number: %d, Id: %d, SrcId: %d, InvokeId: %d\n", i+1, spEvent->Id, spEvent->SrcId, spEvent->InvokeId );
                                wprintf( L"Class: %d, Priority: %d, Flags: 0x%X\n", spEvent->Class, spEvent->Priority, spEvent->Flags );
                                wprintf( L"State: 0x%X, MustConfirm: %d, UserData: %d\n", spEvent->State, spEvent->MustConfirm, spEvent->UserData );
                                wprintf( L"Date created: %s.%d\n", FormatDateTime( L"dddd, mmmm d, yyyy ' at ' hh:mm:ss", dtCreated ),      msCreated );
                                wprintf( L"Date confirmed: %s.%d\n", FormatDateTime( L"dddd, mmmm d, yyyy ' at ' hh:mm:ss", dtConfirmed ), msConfirmed );
                                wprintf( L"Date reset: %s.%d\n", FormatDateTime( L"dddd, mmmm d, yyyy ' at ' hh:mm:ss", dtReset ), msReset );

                                hr = spEvent->get_FmtProgId(&fmtProgId);
                                if (SUCCEEDED(hr)) {
                                    wprintf( L"FmtProgId: \"%s\"\n", fmtProgId );
                                    SysFreeString( fmtProgId );
                                }

                                hr = spEvent->get_SourceName( langID, &srcName );
                                if (SUCCEEDED(hr)) {
                                    wprintf( L"SourceName: \"%s\"\n", srcName );
                                    SysFreeString( srcName );
                                }

                                hr = spEvent->GetMsgString( langID, &msgString );
                                if (SUCCEEDED(hr)) {
                                    wprintf( L"Message: \"%s\"\n\n", msgString );
                                    SysFreeString( msgString );
                                }
                            }// if (SUCCEEDED(hr)), spEvent
                        }// for (long i ...
                    }// if (spEnumEvts->Count > 0)
                }// if (SUCCEEDED(hr)), spEnumEvts
            }//if (SUCCEEDED(hr)), spTcEventLog

            spAdsProxy->Disconnect();

        }//if (SUCCEEDED(hr)), Connect(...
    }// if (SUCCEEDED(hr)), CreateInstance(..

    SysFreeString(netID);
    wprintf( L"Press ENTER to continue...\n" );
    _gettchar();
    return 0;
}

 

Sprache / IDE

Beispielprogram auspacken

CodeGear C++Builder 2009

Sample02.zip