How to set up a call back function ... 
Author Message
 How to set up a call back function ...

Hi!

I've got to solve the following problem:

* My MFC-App has got a member function for receiving data
* ... and another fct to pass a pointer to this function down to a DLL
* In my DLL is a fct which dispatches a fct pointer to the app function
* ... and a thread is waiting for data
* When data arrives the thread calls the app fct via the pointer to this
fct

My PROBLEM:

How can I pass the pointer to the member fct down to the DLL ?

I've got no problem with 'normal' functions (non class members).

THX in advance,

Thomas

Pls answer at least to my email address ...



Mon, 03 Dec 2001 03:00:00 GMT  
 How to set up a call back function ...
Hallo Thomas,

you should declare your member function as static and CALLBACK:

class MyClass
{
protected:
static void CALLBACK GetSomeData(LPVOID lpSomeThing);
...

Quote:
}

and implement it like this:
void CALLBACK CMyClass::GetSomeData(LPVOID lpSomeThing)
{
        pMyClass->m_bSomeOtherThing = true; // see bad news for explanation
        ...

Quote:
}


your class from inside a static member function!
Therefore you must declare a global pointer at the beginning of your cpp
file:
CMyClass* pMyClass = NULL;
and initialize it in your constructor:
pMyClass = this;

If you are a suspicious nature you can check for pMyClass != NULL in
your callback function ;-)

Hope this helps, Jens

Thomas Reiners schrieb:

Quote:

> Hi!

> I've got to solve the following problem:

> * My MFC-App has got a member function for receiving data
> * ... and another fct to pass a pointer to this function down to a DLL
> * In my DLL is a fct which dispatches a fct pointer to the app function
> * ... and a thread is waiting for data
> * When data arrives the thread calls the app fct via the pointer to this
> fct

> My PROBLEM:

> How can I pass the pointer to the member fct down to the DLL ?

> I've got no problem with 'normal' functions (non class members).

> THX in advance,

> Thomas

> Pls answer at least to my email address ...

--
Dipl.-Ing. Spie? COMPUTERSYSTEMS GmbH
http://www.spiess.com
Tel. 089 / 544 178-0

PS: K?nnen Sie im Kopf DM in EURO umrechnen?
Mit unserem kostenlosen Produkt EuroExchange, dem
universalen EURO-Rechner fr alle Programme, ist
die W?hrungsumrechnung mit einem Mausklick erledigt!
Einfach downloaden ber http://www.spiess.com



Mon, 03 Dec 2001 03:00:00 GMT  
 How to set up a call back function ...
Hi,

I solved this one a while ago so from memory...

The DLL can only callback a 'static' function, where there is only one
function for all instances of the class. Therefore when you
load/initialise the dll parse a pointer to the class instance (ie
'(DWORD)this')  to be stored by the dll. When the dll calls back the
static function the pointer it can be used to call a 'normal' function
in the class. Note: The dll will load only once when you might have
multiple instances of the class, so it must store & track multiple
pointers to be safe.

HTH
-Chris

On Thu, 17 Jun 1999 13:01:43 +0200, Thomas Reiners

Quote:

>Hi!

>I've got to solve the following problem:

>* My MFC-App has got a member function for receiving data
>* ... and another fct to pass a pointer to this function down to a DLL
>* In my DLL is a fct which dispatches a fct pointer to the app function
>* ... and a thread is waiting for data
>* When data arrives the thread calls the app fct via the pointer to this
>fct

>My PROBLEM:

>How can I pass the pointer to the member fct down to the DLL ?

>I've got no problem with 'normal' functions (non class members).

>THX in advance,

>Thomas

>Pls answer at least to my email address ...

_______________________________________
AntiSpam email address, change oz to au


Mon, 03 Dec 2001 03:00:00 GMT  
 How to set up a call back function ...
What Jens says is very true, except that you don't need a global
pointer to your class, you can use a static data member of the
class.  That provides for better encapsulation -- everything is
contained within the class.

Quote:

>Hallo Thomas,

>you should declare your member function as static and CALLBACK:

>class MyClass
>{
>protected:
>static void CALLBACK GetSomeData(LPVOID lpSomeThing);
>...
>}

>and implement it like this:
>void CALLBACK CMyClass::GetSomeData(LPVOID lpSomeThing)
>{
> pMyClass->m_bSomeOtherThing = true; // see bad news for explanation
> ...
>}


>your class from inside a static member function!
>Therefore you must declare a global pointer at the beginning of your
cpp
>file:
>CMyClass* pMyClass = NULL;
>and initialize it in your constructor:
>pMyClass = this;

>If you are a suspicious nature you can check for pMyClass != NULL in
>your callback function ;-)

>Hope this helps, Jens

>Thomas Reiners schrieb:

>> Hi!

>> I've got to solve the following problem:

>> * My MFC-App has got a member function for receiving data
>> * ... and another fct to pass a pointer to this function down to a
DLL
>> * In my DLL is a fct which dispatches a fct pointer to the app
function
>> * ... and a thread is waiting for data
>> * When data arrives the thread calls the app fct via the pointer to
this
>> fct

>> My PROBLEM:

>> How can I pass the pointer to the member fct down to the DLL ?

>> I've got no problem with 'normal' functions (non class members).

>> THX in advance,

>> Thomas

>> Pls answer at least to my email address ...

>--
>Dipl.-Ing. Spie? COMPUTERSYSTEMS GmbH
>http://www.spiess.com
>Tel. 089 / 544 178-0

>PS: K?nnen Sie im Kopf DM in EURO umrechnen?
>Mit unserem kostenlosen Produkt EuroExchange, dem
>universalen EURO-Rechner fr alle Programme, ist
>die W?hrungsumrechnung mit einem Mausklick erledigt!
>Einfach downloaden ber http://www.spiess.com



Mon, 03 Dec 2001 03:00:00 GMT  
 How to set up a call back function ...


Quote:
>What Jens says is very true, except that you don't need a global
>pointer to your class, you can use a static data member of the
>class.  That provides for better encapsulation -- everything is
>contained within the class.

Actually, the way I solve this problem is to pass in the this pointer from
the class to the callback at some point.  If the only time you can do it is
right at the beginning, then I declare a static pointer to the class within
the callback function, and then I usually like to call other methods to do the
real dirty work.


Sat, 08 Dec 2001 03:00:00 GMT  
 How to set up a call back function ...
Also consider using the ->* and .* operators which can work on non-static
member functions (far more useful than using statics, IMHO)

Scott Tunstall

www.kwikrite.clara.net/bdash/


Quote:
>On Thu, 17 Jun 1999 14:51:24 -0400, Richard Norman


Quote:

>>What Jens says is very true, except that you don't need a global
>>pointer to your class, you can use a static data member of the
>>class.  That provides for better encapsulation -- everything is
>>contained within the class.

>Actually, the way I solve this problem is to pass in the this pointer from
>the class to the callback at some point.  If the only time you can do it is
>right at the beginning, then I declare a static pointer to the class within
>the callback function, and then I usually like to call other methods to do
the
>real dirty work.



Sat, 08 Dec 2001 03:00:00 GMT  
 How to set up a call back function ...


Quote:
>Also consider using the ->* and .* operators which can work on non-static
>member functions (far more useful than using statics, IMHO)

How exactly would these operators work in setting a callback that isn't
static?


Sat, 08 Dec 2001 03:00:00 GMT  
 How to set up a call back function ...
That is the best way to do it!



Quote:
>On Thu, 17 Jun 1999 14:51:24 -0400, Richard Norman


Quote:

>>What Jens says is very true, except that you don't need a global
>>pointer to your class, you can use a static data member of the
>>class.  That provides for better encapsulation -- everything is
>>contained within the class.

>Actually, the way I solve this problem is to pass in the this pointer
from
>the class to the callback at some point.  If the only time you can do
it is
>right at the beginning, then I declare a static pointer to the class
within
>the callback function, and then I usually like to call other methods
to do the
>real dirty work.



Sat, 08 Dec 2001 03:00:00 GMT  
 How to set up a call back function ...
Can I refer you to this page:

http://www.*-*-*.com/

Look at the heading "Functions". At my place of work, we are successfully
using this sort of code to implement callbacks.

Of course, for Win32 callbacks, static functions can be used but I had a
small problem with them: as static member functions can only work with
static member data (if I'm wrong about this, feel free to correct me) the
callback couldn't work with non-static member data ({*filter*}!) and so limited
what work could be done with the class.

Therefore I created a global function to be the callback handler, and
declared it as a friend to all the classes it should access. Hacky, yes, but
there was no limitations on the data I could access.

For the record tho', does anyone know of a better way?

Scott Tunstall

www.kwikrite.clara.net/bdash/

Quote:



>>Also consider using the ->* and .* operators which can work on non-static
>>member functions (far more useful than using statics, IMHO)

>How exactly would these operators work in setting a callback that isn't
>static?



Sun, 09 Dec 2001 03:00:00 GMT  
 How to set up a call back function ...
I thought the standard way was to save the "this" pointer of the class that
has your static declared callback in a global pointer variable, then you can
use any method of the class from within your callback function.
Mel

Quote:

>Can I refer you to this page:

> http://www.*-*-*.com/

>Look at the heading "Functions". At my place of work, we are successfully
>using this sort of code to implement callbacks.

>Of course, for Win32 callbacks, static functions can be used but I had a
>small problem with them: as static member functions can only work with
>static member data (if I'm wrong about this, feel free to correct me) the
>callback couldn't work with non-static member data ({*filter*}!) and so limited
>what work could be done with the class.

>Therefore I created a global function to be the callback handler, and
>declared it as a friend to all the classes it should access. Hacky, yes,
but
>there was no limitations on the data I could access.

>For the record tho', does anyone know of a better way?

>Scott Tunstall

>www.kwikrite.clara.net/bdash/


>>On Tue, 22 Jun 1999 19:55:18 +0100, Scott Tunstall


- Show quoted text -

Quote:

>>>Also consider using the ->* and .* operators which can work on non-static
>>>member functions (far more useful than using statics, IMHO)

>>How exactly would these operators work in setting a callback that isn't
>>static?



Sun, 09 Dec 2001 03:00:00 GMT  
 How to set up a call back function ...

Quote:

>I thought the standard way was to save the "this" pointer of the class that
>has your static declared callback in a global pointer variable, then you
can
>use any method of the class from within your callback function.

There is no "standard way"; you can use any way that works. I too often use
global "friend" functions as callbacks.

Chris
-----------------------------------------------------------------------

Visit our web site at http://www.skymap.com
Astronomy software written by astronomers, for astronomers



Mon, 10 Dec 2001 03:00:00 GMT  
 How to set up a call back function ...
Callback functions can be static members of a class.  Put a
static data member into the class which holds a pointer to the
class.  The callback function can access this value and use it
to access non-static data and functions.

If you need more than one instance of the class, you might be
able to pass the callback function a pointer to the instance of
the class where it can find the appropriate data.

Quote:

>Can I refer you to this page:

> http://www.*-*-*.com/

>Look at the heading "Functions". At my place of work, we are
successfully
>using this sort of code to implement callbacks.

>Of course, for Win32 callbacks, static functions can be used but I
had a
>small problem with them: as static member functions can only work
with
>static member data (if I'm wrong about this, feel free to correct me)
the
>callback couldn't work with non-static member data ({*filter*}!) and so
limited
>what work could be done with the class.

>Therefore I created a global function to be the callback handler, and
>declared it as a friend to all the classes it should access. Hacky,
yes, but
>there was no limitations on the data I could access.

>For the record tho', does anyone know of a better way?

>Scott Tunstall

>www.kwikrite.clara.net/bdash/


>>On Tue, 22 Jun 1999 19:55:18 +0100, Scott Tunstall


- Show quoted text -

Quote:

>>>Also consider using the ->* and .* operators which can work on
non-static
>>>member functions (far more useful than using statics, IMHO)

>>How exactly would these operators work in setting a callback that
isn't
>>static?



Mon, 10 Dec 2001 03:00:00 GMT  
 How to set up a call back function ...

Quote:

>I thought the standard way was to save the "this" pointer of the class that
>has your static declared callback in a global pointer variable, then you
can
>use any method of the class from within your callback function.
>Mel

You could use that, true enough, but I used a friend function so as NOT to
expose any global pointers! (Amazing how newbies can mess about with em <g>)

In case anyone actually gives a shit what I was doing <g>, the friend
function was used with EnumWindows, and if I remember correctly went
something like this:

BOOL CALLBACK g_EnumWindowsHandler(HWND hWnd, LPARAM lParam)
{
    char xyz[80];        // remember, I've not copied this code, I'm typing
it by hand so no meaningful var names today :) !!!
    ::GetClassName(hWnd, xyz, sizeof(xyz));

    CBasePresent *pBasePresent = (CBasePresent *) lParam;

    if (strcmp(xyz, "SS32x25")==0)
        {
        pBasePresent->OnNewSpreadSheet(hWnd);
        }
    ...

Quote:
}

Where CSpreadsheetEx is derived from the ActiveX wrapper class of Farpoint's
groovy SPREAD control. Note how the lParam app specific parameter that you
would supply to the EnumWindows call is being used to store my "presentation
object" pointer.


Mon, 10 Dec 2001 03:00:00 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. Call Back Functions call from within a DLL

2. Default function call backs in a struct

3. Default function call backs in a struct

4. call-back function

5. pointer problem - passing contents back to calling function.

6. Problems in calling back Lisp functions from C

7. how do you pass in a call back function to a member fnc in C++

8. Fire Event in Class from a Call-back function

9. Call back function

10. Call back functions from DLL to EXE

11. SendMessage() doesn't return back to calling function after its execution

12. Displaying .txt without call back functions???

 

 
Powered by phpBB® Forum Software