"andthen" <a@n.d> wrote in message
news:CG********************@news4.srv.hcvlny.cv.ne t...
I'm using a windows library function which does not return anything useful
(EnumWindows... returns a BOOL), I give it a pointer to a callback
function and it sends the data I want to the callback function. My question is,
what is the best way to get that data into the initial function, which called
the library function? The only way I can think of is to have the callback
function modify a global variable and then have the initial function
access that. But that seems messy and I'm wondering if there is a better way. Any
ideas?
the nice way to implement callbacks is to allow the caller to pass in a
context pointer that is then passed into the callback function
e.g.
/* from handle.h */
typedef unsigned long handle;
#define handleisvalid(h) (h!=0UL)
typedef int (*cbfunctype)(handle h, void * context);
int enumhandles(cbfunctype cbfunc, void * context);
/* from x.c */
#include "handle.h"
#include <stdio.h>
struct indata
{
int i;
};
struct outdata
{
int o;
};
struct cbcontextdata {
struct indata in;
struct outdata out;
};
int mycbfunc(handle h, void *context)
{
cbdata * pcbd = context;
pcbd->out.o += pcbd->in.i + pcbd->in.i ; /* or whatever else you want to
do with h */
return !0; /* 0 if you want to stop processing */
}
int main(void)
{
int rc;
cbdata cbd;
cbd.in.i = 1;
cbd.out.o = 0;
rc = enumhandles(mycbfunc,&cbd);
if (rc != 0)
{
printf("%d\n",cbd.out.o);
}
return 0;
}
/* from handle.c */
#include "handle.h"
int enumhandles(cbfunctype cbfunc, void * context)
{
handle somehandle;
int rc = 1;
somehandle = /* firsthandle() */ 100;
while(handleisvalid(somehandle) && ((*cbfunc)(somehandle,context) != 0))
somehandle = /* nexthandle() */ somehandle - 1;
return rc;
}
[OT]
EnumWindows has a context parameter of type LPARAM - which is large enough
to hold a void pointer.
e.g.
EnumWindows(mywinenumcallback,(LPARAM)(void *)&mycallbackdata);
IMHO They really should have done it as a LPVOID (void *).
[/OT]