Solved

callbacks in c++

Posted on 2003-10-31
4
467 Views
Last Modified: 2008-02-26
Hello, someone can to get me samples of callbacks in c++.How I define an interface callback?
How I use the interface .
Thanks.

0
Comment
Question by:dmf9000
  • 2
4 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 9657631
Your question is difficult to address.  It's clearly OFF TOPIC since "callbacks" are NOT a part of the C++ language.  Further, while many operating systems support things called callbacks, the way they do that is varied.

I suggest you clarify your question and explain what it is you're trying to de in DETAIL.  Another suggestion would be to ask the EE moderators to move this question to a more appropriate topic area.  The best choice would be the PROGRAMMING topic area for your specific operating system.
0
 
LVL 9

Expert Comment

by:_ys_
ID: 9657632
If by interface you're talking COM interfaces. then read on.

----------------------------
Reproduced from http://www-tcsn.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20751731.html
----------------------------

*All* that is needed is an interface pointer exchanged and held by each side of the *conversation*.

-- you do know IDL don't you --

[
     object,
     uuid(...),
     helpstring("IMyClient Interface")
]
interface IMyClient : IUnknown
{
    [helpstring("method ClientMethod")]
    HRESULT ClientMethod(...);

...
};

[
     object,
     uuid(...),
     helpstring("IMyServer Interface")
]
interface IMyServer : IUnknown
{
    [helpstring("method AdviseClient")]
    HRESULT AdviseClient([in] IMyClient*);

...
};


All that is required is for the client to instantiate the server:

IMyServer* pMyServer = 0;
CoCreateInstance(..., &pMyServer);  // assuming the AppId registry value is set-up to instruct DCOM usage
pMyServer->AdviseClient(static_cast<IMyClient*>(this));

At which point the IMyServer implementation can invoke the client method ClientMethod:

m_pMyClient->ClientMethod(...);       // assuming m_pMyClient is typed as IMyClient*

as and when it feels necessary to do so.

Simple as that. No MFC. No IConnectionPoint etc.

[This example assumes that standard marshalling is employed. Also that IMyClient is implemented on the client class represented by 'this' - otherwise the normal QI mechanism should be employed instead.]
0
 
LVL 19

Accepted Solution

by:
Dexstar earned 250 total points
ID: 9658932
dmf9000:

> Hello, someone can to get me samples of callbacks in c++.How I define an interface callback?
> How I use the interface .

I didn't get the impression that you were talking about COM, and I don't think that your question is off topic.  Here is a set of classes that demonstrate callback interfaces in C++.  In this case, the callback interface is called "CCallback".

If you don't understand any of it, just ask and I'll clarify:
      // Callback Interface Class
      class CCallback
      {
      public:
            virtual void OnEvent( int nEventID ) = 0;
      };

      class CSomeEventClass
      {
      public:
            CSomeEventClass() : m_pHandler(NULL) { };

      public:
            // Store the value of the callback interface object
            void SetCallback( CCallback* pHandler ) { m_pHandler = pHandler; };

            void DoSomething()
            {
                  if ( m_pHandler != NULL )
                  {
                        // Use the call back interface
                        m_pHandler->OnEvent( 100 );
                  }
            }
      };

      class CSomeOtherClass : public CCallback
      {
      public:
            // Implement the callback interface
            virtual void OnEvent( int nEventID )
            {
                  cout << "It happened!" << endl;
            }

            void Whatever()
            {
                  CSomeEventClass      sec;

                  // Set the callback interface
                  sec.SetCallback( this );

                  // Do something with it
                  sec.DoSomething();
            }
      };


There is also another way to do callbacks in C++ using templates.  I won't go into detail on that here because there is an excellent site that tells you all about it:
     http://www.tutok.sk/fastgl/callback.html

Hope that helps,
Dex*
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9658952
dmf9000:

> Hello, someone can to get me samples of callbacks in c++.How I define an interface callback?
> How I use the interface .

Actually, I found a good site that describes an example similiar to the one I gave you.
     http://www.gmonline.demon.co.uk/cscene/topics/misc/cs7-04.xml.html

Dex*
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now