Windows API callback, using boost::bind

hi, We have a class and it has a member function. When this function is called, it calls a Windows API. One of the argument of the windows API is a callback function. Naturally this callback function has to be a *non member* function. Is there any way by which I can pass a *member* function in the place of callback function. I heard about boost::bind, with bind is it possible? Thanks, Lloyd ______________________________________ Scanned and protected by Email scanner

On Fri, Oct 29, 2010 at 12:12:06PM +0530, Lloyd wrote:
We have a class and it has a member function. When this function is called, it calls a Windows API. One of the argument of the windows API is a callback function. Naturally this callback function has to be a *non member* function. Is there any way by which I can pass a *member* function in the place of callback function. I heard about boost::bind, with bind is it possible?
No. Boost.Bind does not offer any such functionality, and I do not believe there are any libraries in Boost that have anything like that in their public interfaces. The common approach is to generate code that translates from the thiscall calling convention to stdcall. There might be third party libraries that can do this, but I'm unsure if it's possible at all on x64, if that matters. The other common solutions is to have a global pointer to your instance and use that from a static member function, or pass in a context to the API function that your callback will be invoked with. Other solutions are things like storing data in extra bytes of windows, or other fun target-dependant solutions. -- Lars Viklund | zao@acc.umu.se

The common approach is to generate code that translates from the thiscall calling convention to stdcall.
There might be third party libraries that can do this, but I'm unsure if it's possible at all on x64, if that matters.
I have a free library that does this for 32-bit code. It's fairly easy to translate from "thiscall" calling convention to "stdcall". The same trick won't work for x64. Generating an actual call to re-push the arguments needs to know more details about the number and type of arguments! However, a typed thunk generator could be crafted for the specific signature needed. You also need to allocate the thunk's memory in a page that was VirtualAlloc'ed to hold code (in 32-bit code you can just disable the feature), which complicates management of thunks as value objects. I've not gotten around to it, and don't have an immediate need for it. But I'd be happy to give some pointers if someone were writing such a thing.
The other common solutions is to have a global pointer to your instance and use that from a static member function,
For real things I've encountered, the number of callbacks needed is limited to a small value known at compile time. Using a fixed global or a small pool of individually written global wrappers is much simpler than anything else. Hmm, using a numeric template argument it would be trivial to create a "pool" of any size N, without excessive source code. - TradeStation Group, Inc. is a publicly-traded holding company (NASDAQ GS: TRAD) of three operating subsidiaries, TradeStation Securities, Inc. (Member NYSE, FINRA, SIPC and NFA), TradeStation Technologies, Inc., a trading software and subscription company, and TradeStation Europe Limited, a United Kingdom, FSA-authorized introducing brokerage firm. None of these companies provides trading or investment advice, recommendations or endorsements of any kind. The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from any computer.

On Fri, Oct 29, 2010 at 2:42 AM, Lloyd
it calls a Windows API. One of the argument of the windows API is a callback function. Naturally this callback function has to be a *non member* function. Is there any way by which I can pass a *member* function in the place of callback function. I heard about boost::bind, with bind is it possible?
The key question is: does the Windows function also accept, and pass to your callback, a void* data pointer so you can supply the callback function with arbitrary data? (That idiom is quite common with classic C callbacks.) If so, there are a number of different ways to get what you want here. Please see the attached, which compiles and runs for me.

hi, We have a class and it has a member function. When this function is called, it calls a Windows API. One of the argument of the windows API is a callback function. Naturally this callback function has to be a *non member* function. Is there any way by which I can pass a *member* function in the place of callback function. I heard about boost::bind, with bind is it possible? Thanks, Lloyd Since most Windows callback functions have a user passed in DWORD_PTR (or LPARAM) as one of the arguments, what I typically do is use a static member function of the class as the callback function and pass the "this" pointer as the DWORD_PTR. The static member function now has full access to the class and can either use member variables directly (via the passed in pointer) or forward the callback to a true member function. -- Bill --

For 32-bit code, use this: http://www.dlugosz.com/Repertoire/refman/Classics/closure.html#toptop It will create an actual C-like function pointer that can be called by code expecting such. This specifically includes the WinProc and any of the Enum... API functions in Windows. --John From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Lloyd Sent: Friday, October 29, 2010 1:42 AM To: boost-users@lists.boost.org Subject: [Boost-users] Windows API callback, using boost::bind hi, We have a class and it has a member function. When this function is called, it calls a Windows API. One of the argument of the windows API is a callback function. Naturally this callback function has to be a *non member* function. Is there any way by which I can pass a *member* function in the place of callback function. I heard about boost::bind, with bind is it possible? Thanks, Lloyd ______________________________________ Scanned and protected by Email scanner TradeStation Group, Inc. is a publicly-traded holding company (NASDAQ GS: TRAD) of three operating subsidiaries, TradeStation Securities, Inc. (Member NYSE, FINRA, SIPC and NFA), TradeStation Technologies, Inc., a trading software and subscription company, and TradeStation Europe Limited, a United Kingdom, FSA-authorized introducing brokerage firm. None of these companies provides trading or investment advice, recommendations or endorsements of any kind. The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from any computer.

Lloyd
hi, We have a class and it has a member function. When this function is called, it calls a Windows API. One of the argument of the windows API is a callback function. Naturally this callback function has to be a *non member* function. Is there any way by which I can pass a *member* function in the place of callback function. I heard about boost::bind, with bind is it possible? Thanks, Lloyd
Here are two separate things you can check out... http://www.codeproject.com/KB/winsdk/callback_adapter.aspx http://www.codeproject.com/KB/cpp/thunk32.aspx
participants (6)
-
Bill Buklis
-
Jerry Jeremiah
-
John Dlugosz
-
Lars Viklund
-
Lloyd
-
Nat Linden