Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > communicating through callback

Reply
Thread Tools

communicating through callback

 
 
andthen
Guest
Posts: n/a
 
      07-16-2004
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?


 
Reply With Quote
 
 
 
 
Bernhard
Guest
Posts: n/a
 
      07-16-2004
andthen wrote:

> 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?
>
>


I think it depends on the information you like to get from the
EnumWindows. If you would like to process all the windows, do it in the
callback function. If you would like to search a special window handle,
you can send the window-handle by a user-defined message.
By the way, not all global variables are messy. if you label them, is
easy to know, which variable is global.

best regards
Bernhard
 
Reply With Quote
 
 
 
 
Volker Bartheld (SPAM only)
Guest
Posts: n/a
 
      07-16-2004
Hi!

On Fri, 16 Jul 2004 13:24:50 GMT, "andthen" <> wrote :
>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,



struct WNDSTRUCT_t
{
HWND m_hFirst;
};

BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam)
{
assert(lParam);
WNDSTRUCT_t* pWndStruct=(WNDSTRUCT_t*)lParam;
pWndStruct->m_hFirst=hWnd;
return FALSE;
}

void main(void)
{
WNDSTRUCT_t Result;
if(EnumWindows(EnumWindowsProc, &Result)) printf("First: %X\n", (unsigned long)Result.m_hFirst);
}


HTH,
V.

 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      07-16-2004

"andthen" <> wrote in
>
> 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?
>

When you are specifying an interface involving callbacks, the callback
should always be passed a void * to avoid this problem. You then modify a
structure local to the top-level function. If the library provider has
neglected to do this, there is not much you can except to use a global.


 
Reply With Quote
 
andthen
Guest
Posts: n/a
 
      07-16-2004
Thanks for the help... I did not realize that the purpose of the second
parameter of EnumWindows was for a typecasted pointer. Using it as such
works fine.


 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      07-17-2004
On Fri, 16 Jul 2004 17:19:17 +0200, "Volker Bartheld (SPAM only)"
<> wrote in comp.lang.c:

> Hi!
>
> On Fri, 16 Jul 2004 13:24:50 GMT, "andthen" <> wrote :
> >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,

>
>
> struct WNDSTRUCT_t
> {
> HWND m_hFirst;
> };
>
> BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam)


In the absence of supporting macro definitions, the above is a nothing
but a syntax error in C.

> {
> assert(lParam);
> WNDSTRUCT_t* pWndStruct=(WNDSTRUCT_t*)lParam;
> pWndStruct->m_hFirst=hWnd;
> return FALSE;
> }
>
> void main(void)

^^^^

Oh, you're not programming in C, although you might think that you
are. You're in the newsgroup. In C, main() returns int in a hosted
environment.

PS: The Windows API is not part of the C language or library and is
severely off-topic here.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      07-17-2004

"Jack Klein" <> wrote in message
> > >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,

> >

[ example windows program snipped ]
>
> PS: The Windows API is not part of the C language or library and is
> severely off-topic here.
>

To clarify, the OP was on topic because he was asking how to use the C
language effectively given a certain API. The reply wasn't on topic, because
it posts a non-portable program whose only purpose was to illustrate how to
use the details of that API. Just mentioning Windows doesn't in itself
destroy topicality, though you should check to see whether your question
really is a C language question.


 
Reply With Quote
 
Peter Slootweg
Guest
Posts: n/a
 
      07-20-2004

"andthen" <> wrote in message
news:CGQJc.4567$ 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]


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to hook a callback through inheritance Satsou Sa Ruby 5 12-31-2010 10:32 AM
way for a function to understand whether it's being run through aOnCreate callback or not slamdunk Python 3 07-10-2009 07:30 AM
Handeing Session and authentication timeout through callback scrip =?Utf-8?B?TmFocmlu?= ASP .Net 3 01-27-2006 01:35 PM
Communicating between 2 VLANs Norman Zhang Cisco 13 08-13-2004 07:24 AM
problem communicating with static nat machines behind PIX Tony Cisco 2 12-18-2003 07:55 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57