Solved

callbacks in c++

Posted on 2003-10-31
4
471 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

738 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