Solved

callbacks in c++

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ get user from AD  (VS6) 7 93
Getting IP address 8 99
GUI: DIalog Stacking and Popping in MS C++ 4 87
What is sub-make ? 2 87
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

685 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