Solved

callbacks in c++

Posted on 2003-10-31
4
462 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

762 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

20 Experts available now in Live!

Get 1:1 Help Now