473,398 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

communicating through callback

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?
Nov 14 '05 #1
7 1644
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
Nov 14 '05 #2
Hi!

On Fri, 16 Jul 2004 13:24:50 GMT, "andthen" <a@n.d> 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.

Nov 14 '05 #3

"andthen" <a@n.d> 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.
Nov 14 '05 #4
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.
Nov 14 '05 #5
On Fri, 16 Jul 2004 17:19:17 +0200, "Volker Bartheld (SPAM only)"
<dr********@freenet.de> wrote in comp.lang.c:
Hi!

On Fri, 16 Jul 2004 13:24:50 GMT, "andthen" <a@n.d> 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
Nov 14 '05 #6

"Jack Klein" <ja*******@spamcop.net> 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 questionis, 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.
Nov 14 '05 #7

"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]
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: scott ocamb | last post by:
hello I have implemented a solution using async methods. There is one async method that can be invoked multiple times, ie there are multiple async "threads" running at a time. When these...
0
by: vijaya | last post by:
I've to invoke a unmanaged dll fucntion in C# which uses a callback fucntion.The unmanaged dll fucntion is as follows **************************************** The Original Fucntion in the dll...
4
by: ma740988 | last post by:
// file sltest.h #ifndef SLTEST_H #define SLTEST_H class CallbackBase // herb shutters gotW source .. { public: virtual void operator()() const { }; virtual ~CallbackBase() = 0; };
15
by: Felix Kater | last post by:
Hi, in a given library I register callback functions with this function: bool set_callback(int index, int (*callback_function)(long)); I need the callback function to also pass the index...
2
by: MR | last post by:
help! I have an unmanaged DLL that I do not have the source code, so i can't recompile or make changes. the DLL requires a callback function. I would like to implement the callback method in a...
6
by: smmk25 | last post by:
Before I state the problem, I just want to let the readers know, I am knew to C++\CLI and interop so please forgive any newbie questions. I have a huge C library which I want to be able to use in...
10
by: SQACPP | last post by:
Hi, I try to figure out how to use Callback procedure in a C++ form project The following code *work* perfectly on a console project #include "Windows.h" BOOL CALLBACK...
0
by: Tim Spens | last post by:
--- On Fri, 6/27/08, Tim Spens <t_spens@yahoo.comwrote: I think I know where the problem is but I'm unsure how to fix it. When I call Register_Handler(...) from python via...
5
by: Jef Driesen | last post by:
I have a C DLL that I want to use from a C# project. The C header file contains these declarations: typedef void (*callback_t) (const unsigned char *data, unsigned int size, void *userdata);...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.