Board index » cppbuilder » Calling/executing a method from another class

Calling/executing a method from another class


2005-10-02 03:04:19 AM
cppbuilder104
Hello, I have two classes defined in a single "h" file: TWmiEvent and
EventSink. I also have the implementation for both classes in a single
"cpp" file. In the cpp, I want to be able to call
TWmiEvent::CheckNICStatus() from EventSink. Something like:
//--------------------------------------------------------------------------
-
void__stdcall EventSink::FireEvent(void)
{
TWmiEvent::CheckNICStatus()
}
How do I do that?
Thanks
//--------------------------------------------------------------------------
-
#include <vcl.h>
#pragma hdrstop
#include "TWmiEvent.h"
#include "..\\Test\\Unit1.h"
#pragma link "..\\TWmi\\TWmi"
#pragma package(smart_init)
//--------------------------------------------------------------------------
-
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//
static inline void ValidCtrCheck(TWmiEvent *)
{
new TWmiEvent(NULL);
}
//--------------------------------------------------------------------------
-
__fastcall TWmiEvent::TWmiEvent(TComponent* Owner)
: TComponent(Owner)
{
FActive = false;
}
//--------------------------------------------------------------------------
-
namespace Twmievent
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TWmiEvent)};
RegisterComponents("Maurice", classes, 0);
}
}
//--------------------------------------------------------------------------
-
__fastcall TWmiEvent::~TWmiEvent(void)
{
Cleanup();
}
//--------------------------------------------------------------------------
-
ULONG __stdcall EventSink::AddRef()
{
return InterlockedIncrement(&m_lRef);
}
//--------------------------------------------------------------------------
-
ULONG __stdcall EventSink::Release()
{
LONG lRef = InterlockedDecrement(&m_lRef);
if(lRef == 0)
delete this;
return lRef;
}
//--------------------------------------------------------------------------
-
// EventSink Class
#ifndef EVENTSINK_H
#define EVENTSINK_H
#define _WIN32_DCOM
#include <iostream>
using namespace std;
#include <Wbemidl.h>
# pragma comment(lib, "..\TWmi\wbemuuid.lib")
class EventSink : public IWbemObjectSink
{
LONG m_lRef;
bool bDone;
public:
EventSink() { m_lRef = 0; }
~EventSink() { bDone = true; }
ULONG __stdcall AddRef();
ULONG __stdcall Release();
virtual HRESULT __stdcall QueryInterface(REFIID riid, void** ppv);
virtual HRESULT __stdcall Indicate(
LONG lObjectCount,
IWbemClassObject __RPC_FAR *__RPC_FAR *apObjArray
);
virtual HRESULT __stdcall SetStatus(
/* [in] */ LONG lFlags,
/* [in] */ HRESULT hResult,
/* [in] */ BSTR strParam,
/* [in] */ IWbemClassObject __RPC_FAR *pObjParam
);
// void __fastcall CheckNICStatus();// On network Connect/Disconnect
event
};
#endif // end of EventSink Class
//--------------------------------------------------------------------------
-
#ifndef TWmiEventH
#define TWmiEventH
//--------------------------------------------------------------------------
-
#include <SysUtils.hpp>
#include <Controls.hpp>
#include <Classes.hpp>
#include <Forms.hpp>
#include "..\\TWmi\\TWmi.h"
//--------------------------------------------------------------------------
-
class PACKAGE TWmiEvent : public TComponent
{
private:
TWmi* FWmi;
int TimerCounter;
IUnsecuredApartment* pUnsecApp;
EventSink* pSink;
IUnknown* pStubUnk;
IWbemObjectSink* pStubSink;
IWbemObjectSink* pDestSink;
bool FActive;
TTimer *Timer1;
unsigned int __fastcall Initialize(void);
void __fastcall WaitForEvent(void);
void __fastcall Cleanup(void);
void __fastcall SetActive(bool value);
void __fastcall SetWmi(TWmi* value);
void __fastcall Timer1Timer(TObject *Sender);
protected:
typedef void __fastcall (__closure* TUpdateEvent)(TObject* Sender,
int NicStatus);
TUpdateEvent FOnUpdate;
TUpdateEvent FOnCableConnect;
TUpdateEvent FOnCableDisConnect;
// void __fastcall CheckNICStatus();// On network Connect/Disconnect
event
IWbemServices *pSvc;
public:
__fastcall TWmiEvent(TComponent* Owner);
__fastcall ~TWmiEvent(void);
__property TWmi* Wmi = {write=SetWmi };
void __fastcall CheckNICStatus();// On network Connect/Disconnect
event
__published:
__property TUpdateEvent OnUpdate = {read = FOnUpdate, write =
FOnUpdate};
__property TUpdateEvent OnCableConnect = {read = FOnUpdate, write =
FOnCableConnect};
__property TUpdateEvent OnCableDisConnect = {read = FOnUpdate,
write = FOnCableDisConnect};
__property bool Active = { read=FActive, write=SetActive,
default=false };
};
//--------------------------------------------------------------------------
-
#endif
want to know how to
 
 

Re:Calling/executing a method from another class

Maurice Anderson wrote:
Quote
... In the cpp, I want to be able to call
TWmiEvent::CheckNICStatus() from EventSink. Something like:
void__stdcall EventSink::FireEvent(void)
{
TWmiEvent::CheckNICStatus()
}

How do I do that?
You need a pointer to an instance of the TWmiEvent class in order
to call a memberfunction of it.
TWmiEvent *WmiEvent = new TWmiEvent();
WmiEvent->CheckNICStatus()
delete WmiEvent;
Hans.
 

Re:Calling/executing a method from another class

Quote

You need a pointer to an instance of the TWmiEvent class in order
to call a memberfunction of it.

TWmiEvent *WmiEvent = new TWmiEvent();

WmiEvent->CheckNICStatus()

delete WmiEvent;

Hans.
Unfortunetly I can't do it that way because EventSink does not know about
TWmiEvent being that in terms of placement in the h file, EventSink is
first. And EventSink needs to be first because TWmiEvent needs to know
about it in order to create an instance of EventSink. As in:
unsigned int __fastcall TWmiEvent::Initialize(void)
{
pSink = new EventSink;
}
So you see it's a recursive thing. And I am I a bind unless there is a way
to call a function from another class if the calling class is 'above' the
called class in the h file.
 

{smallsort}

Re:Calling/executing a method from another class

"Maurice Anderson" < XXXX@XXXXX.COM >wrote:
Quote

[...] EventSink does not know about TWmiEvent being that in
terms of placement in the h file,
Forward-declare TWmiEvent so that it will compile and the
linker will clear up any dangling references. For example:
//-------------------------------------------------------------
#ifndef EventSinkH
#define EventSinkH
//-------------------------------------------------------------
class TWmiEvent;
//-------------------------------------------------------------
class TEventSink
{
....
};
//-------------------------------------------------------------
#endif
~ JD
 

Re:Calling/executing a method from another class

Quote
Forward-declare TWmiEvent so that it will compile and the
linker will clear up any dangling references. For example:
Thanks it works!!