Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

callbacks in c++

Posted on 2003-10-31
4
Medium Priority
?
475 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 750 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

Industry Leaders: 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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

730 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