Konsolenanwendung - Geloggte Meldungen via DCOM-Schnittstelle
Systemvoraussetzungen:
- CodeGear C++Builder 2009;
- TwinCAT v2.11 B2228 oder höher;
- Die Typbibiothek der TcEventLogger.exe muss importiert werden (TCEVENTLOGGERLib_TLB.h);
Das Beispiel baut eine DCOM-Verbindung zu einem Remote-EventLogger auf und zeigt in der Konsole alle dort geloggten EventLogger Meldungen.
#include <vcl.h>
#pragma hdrstop
#include <tchar.h>
#include <stdio.h>
#include <string.h>
#include <iostream.h>
#include <TCEVENTLOGGERLib_TLB.h>
//---------------------------------------------------------------------------
#pragma argsused
int _tmain(int argc, _TCHAR* argv[])
{
// Create connection via DCOM
ITcEventLogPtr spTcEventLog;
COSERVERINFO comServerInfo={0};
// comServerInfo.pwszName = SysAllocString( L"172.17.60.234" );// ToDo: Configure the IP address if connecting to remote TwinCAT System
comServerInfo.pwszName = SysAllocString( L"localhost" );
long langID = 1033;// ToDo: Select language ID (e.g. english)
MULTI_QI mQI={&IID_ITcEventLog, NULL, 0};
HRESULT hr = ::CoCreateInstanceEx( CLSID_TcEventLog, NULL, CLSCTX_SERVER, &comServerInfo, 1, &mQI);
if (SUCCEEDED(hr) && mQI.pItf )
{
spTcEventLog = mQI.pItf;
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
SysFreeString(comServerInfo.pwszName);
wprintf( L"Press ENTER to continue...\n" );
_gettchar();
return 0;
}
Sprache / IDE | Beispielprogram auspacken |
---|---|
CodeGear C++Builder 2009 |