RE: [Dev-C++] Static members for callbacks
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Ioannis V. <no...@ho...> - 2002-07-29 08:04:18
|
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf
> Of Ioannis Vranos
> Sent: Monday, July 29, 2002 10:42 AM
> To: 'Abhijit Shylanath'
> Cc: 'Dev-C++ Mailing List'
> Subject: RE: [Dev-C++] Static members for callbacks
>
> >
> > > -----Original Message-----
> > > From: Abhijit Shylanath [mailto:mu...@gm...]
> > > Sent: Monday, July 29, 2002 9:50 AM
> > > To: Ioannis Vranos
> > > Cc: Dev-C++ Mailing List
> > > Subject: Re: [Dev-C++] Static members for callbacks
> > >
> > >
> > > > > You can't use non-static methods for windows
> callbacks (like in
> > > > > CreateThread(), SetWindowsHookEx()) which need you to suppply
> > > > > a call-back
> > > > > function.
> > > >
> > > >
> > > > This is too Win32 to me. What do you mean by call-back
> > > function. Can you
> > > > provide a small concrete example?
> > >
> > >
> > > Basically, typedef function_pointer call_back;
> > >
> > > For example, in CreateThread(), the args are:
> > >
> > > HANDLE CreateThread(
> > > LPSECURITY_ATTRIBUTES lpThreadAttributes, // address of
> > > thread security
> > > attributes
> > > DWORD dwStackSize, // initial thread stack size, in bytes
> > > LPTHREAD_START_ROUTINE lpStartAddress, // address of
> > > thread function
> > > LPVOID lpParameter, // argument for new thread
> > > DWORD dwCreationFlags, // creation flags
> > > LPDWORD lpThreadId // address of returned thread identifier
> > > );
> > >
> > > Here, LPTHREAD_START_ROUTINE should be a function pointer
> > to a 32-bit
> > > returning (void *) accepting function, as in
> > >
> > > DWORD my_thread_proc(void *some_arg);
> > >
> > > where DWORD is, on Win32, unsigned long.
> >
> >
> >
> > Ok then you can use a regular function to call the
> > appropriate member function. This regular function will
> > accept also your object as a parameter.
>
>
> In general use an additional regular function. For example if your
> function must have a specific return type and specific arguments like:
>
> int s(void *);
>
>
> You can use a third function *& a global variable e.g.
>
>
> // The value of the data member of class A
>
> int x;
>
> inline void loadvalue(const A &a)
> {
> x=a.getvalue();
> }
>
>
> // s now can read the global variable x
>
>
> Of course this way sucks. But this is not a C++ problem, but Win32
> problem which after all is C (or perhaps even pre-ANSI C).
>
> In C there is some use of global variables like errno,
> although the use
> of global variables is not recommended there too.
Sorry for the many messages but i have to be perfect. :)
The best way to do it given the Win32 API limitations is to do the above
using the modular programming paradigm (which was the ancestor of OO but
is a concrete paradigm of its own). That is define those things in a
namespace:
namespace dirty_work
{
// Values of the object's data members
int x, y, z;
inline void loadvalue(const A &a)
{
x=a.getvalue();
}
}
int s(void *);
// ...
A a(3,2,1);
// ...
dirty_work::loadvalue(a);
// s can now read the values of a from dirty_work::x etc.
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.cjb.net
* Alternative URL: http://run.to/noicys
|