473,761 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

API Callbacks into Objects?

I'm a self-taught programmer, so this might be a pretty dumb question.
If it is, please point me in the right direction and I shall
apologize profusely.

I have a question regarding C++ and object members. Can anyone help?

I'm writing a C++ wrapper for a fairly old programming interface to a
document editing program that has no OOP whatsoever; only tons of
structs. This program has different callbacks I'm supposed to
implement for different kinds of events, such as open a file, user
changed something, user clicked a custom command button, etcetera.
For structured programming, it's easy. However, I'm having some
trouble with persistence of objects across the different callbacks.

How do I do that?

I had two ideas. One was to create an object that implemented the
callbacks as member functions. That didn't work -- the controlling
program didn't even see the callbacks. (If I could create some sort
of 'alias', could it work?).

Another idea was to create a global static object that I could refer
to within the callbacks, but that didn't really work out at all.

I don't want to go recreating object structures in every callback,
that get recreated every time the callback is executed -- that seems
inefficient to me.

Anybody have any ideas? Hints? Suggestions?

Thanks!

( btw, don't reply to the mailinator.com address.
If you must reply via email, my email addr is:
My email is cej AT i n t e c h . c o m )

Christopher Jastram
Programmer, Technical Instructor
Integrated Technologies, Inc.
Jul 22 '05 #1
5 3246
Christopher Jastram wrote:
I'm writing a C++ wrapper for a fairly old programming interface to a
document editing program that has no OOP whatsoever; only tons of
structs. This program has different callbacks I'm supposed to
implement for different kinds of events, such as open a file, user
changed something, user clicked a custom command button, etcetera.
For structured programming, it's easy. However, I'm having some
trouble with persistence of objects across the different callbacks.

How do I do that?
Totally unclear so far what your problem is. Which direction are the
callbacks? Who's calling those callbacks? The library? How does it
do that? Does it store the function pointers somewhere? Does it use
the pointers provided in another function call as an argument? Why
do you think you need persistence?
I had two ideas. One was to create an object that implemented the
callbacks as member functions. That didn't work -- the controlling
program didn't even see the callbacks. (If I could create some sort
of 'alias', could it work?).
I don't understand what you mean by "didn't even see the callbacks".
Another idea was to create a global static object that I could refer
to within the callbacks, but that didn't really work out at all.
Again, what do you mean by "didn't really work out at all"?
I don't want to go recreating object structures in every callback,
that get recreated every time the callback is executed -- that seems
inefficient to me.
You're probably right. So, if they are not global, the only way to
make them persistent is to make them dynamic and place them into some
kind of a collection, which probably needs to be global anyway. It
might not "work out at all", of course. Whatever that means.

Anybody have any ideas? Hints? Suggestions?


Here is a suggestion: be more specific. Give an example of your
library function and your attempt at a wrapper. Give a _good_ example
of how that function _would_ be used and how you propose to use your
wrapper. Perhaps while trying to describe it to us you will find some
suitable way in which this whole thing is going to work... If not, do
post your interface and we could talk implementation (given that you
attempt to create one first).

Victor
Jul 22 '05 #2

"Christophe r Jastram" <ce*@mailinator .com> wrote in message
news:38******** *************** ***@posting.goo gle.com...
I'm a self-taught programmer, so this might be a pretty dumb question.
If it is, please point me in the right direction and I shall
apologize profusely.

I have a question regarding C++ and object members. Can anyone help?

I'm writing a C++ wrapper for a fairly old programming interface to a
document editing program that has no OOP whatsoever; only tons of
structs. This program has different callbacks I'm supposed to
implement for different kinds of events, such as open a file, user
changed something, user clicked a custom command button, etcetera.
For structured programming, it's easy. However, I'm having some
trouble with persistence of objects across the different callbacks.

How do I do that?

I had two ideas. One was to create an object that implemented the
callbacks as member functions. That didn't work -- the controlling
program didn't even see the callbacks. (If I could create some sort
of 'alias', could it work?).


Perhaps it would be helpful if you illustrated what you mean by 'the
controlling program didn't even see the callbacks' with some sort of code
snippet.

It should be possible to get member functions to work providing the API you
are working with has been well designed. So give us an sample of what you
are working with.

John
Jul 22 '05 #3
Christopher Jastram wrote:
I'm a self-taught programmer, so this might be a pretty dumb question.
If it is, please point me in the right direction and I shall
apologize profusely.

I have a question regarding C++ and object members. Can anyone help?

I'm writing a C++ wrapper for a fairly old programming interface to a
document editing program that has no OOP whatsoever; only tons of
structs. This program has different callbacks I'm supposed to
implement for different kinds of events, such as open a file, user
changed something, user clicked a custom command button, etcetera.
For structured programming, it's easy. However, I'm having some
trouble with persistence of objects across the different callbacks.

How do I do that?

I had two ideas. One was to create an object that implemented the
callbacks as member functions. That didn't work -- the controlling
program didn't even see the callbacks. (If I could create some sort
of 'alias', could it work?).

Another idea was to create a global static object that I could refer
to within the callbacks, but that didn't really work out at all.

I don't want to go recreating object structures in every callback,
that get recreated every time the callback is executed -- that seems
inefficient to me.

Anybody have any ideas? Hints? Suggestions?


I'm going to assume you want to call back a method of an object, and
that the callback function allows you to specify a void* as a parameter.
Given those assumptions, try something like the following:

class C;
extern "C" {
void c_callback(void *);
void api_with_callba ck(void (*)(void*), void*);
}

class C {
public:
// yada yada yada
void set_callback()
{
::api_with_call back(&::c_callb ack, this);
}
void my_callback()
{
}
};

void c_callback(void *param)
{
C* p = static_cast<C*> (param);
p->my_callback( );
}
Jul 22 '05 #4
Christopher Jastram wrote:

I'm a self-taught programmer, so this might be a pretty dumb question.
If it is, please point me in the right direction and I shall
apologize profusely.

I have a question regarding C++ and object members. Can anyone help?

I'm writing a C++ wrapper for a fairly old programming interface to a
document editing program that has no OOP whatsoever; only tons of
structs. This program has different callbacks I'm supposed to
implement for different kinds of events, such as open a file, user
changed something, user clicked a custom command button, etcetera.
For structured programming, it's easy. However, I'm having some
trouble with persistence of objects across the different callbacks.

How do I do that?

I had two ideas. One was to create an object that implemented the
callbacks as member functions. That didn't work -- the controlling
program didn't even see the callbacks. (If I could create some sort
of 'alias', could it work?).

Another idea was to create a global static object that I could refer
to within the callbacks, but that didn't really work out at all.

I don't want to go recreating object structures in every callback,
that get recreated every time the callback is executed -- that seems
inefficient to me.

Anybody have any ideas? Hints? Suggestions?

Thanks!

( btw, don't reply to the mailinator.com address.
If you must reply via email, my email addr is:
My email is cej AT i n t e c h . c o m )

Christopher Jastram
Programmer, Technical Instructor
Integrated Technologies, Inc.

--
Karl Heinz Buchegger, GASCAD GmbH
Teichstrasse 2
A-4595 Waldneukirchen
Tel ++43/7258/7545-0 Fax ++43/7258/7545-99
email: kb******@gascad .at Web: www.gascad.com

Fuer sehr grosse Werte von 2 gilt: 2 + 2 = 5
Jul 22 '05 #5
Christopher Jastram wrote:

If I understand you correctly than the FAQ might
be of some help.
Look up

http://www.parashift.com/c++-faq-lit...o-members.html

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #6

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

Similar topics

1
2297
by: Melissa Wallis | last post by:
I have a class with 5 callbacks. Two of the callbacks work fine but the others don't. The main difference is that the callbacks that don't work are composed of a sequence of structs. I noticed a comment about this same problem on the web but no solution was noted. What makes the callbacks with the sequences so different? It seems that when one of the callbacks with a sequence is called it just hangs. I am talking to a TAO orb from...
4
2272
by: faktujaa | last post by:
Hi, I am having some problem with callback used in socket implementation. private static void Connect(string strPrtrIPAddr, int intPrtrPort, ref Socket rsocClient) { try { // Create remote end point. System.Net.IPAddress IPAddress = System.Net.IPAddress.Parse(strPrtrIPAddr); System.Net.IPEndPoint IPEndPoint = new System.Net.IPEndPoint(IPAddress,
4
3372
by: womanontheinside | last post by:
I have a library which was written in C, you call a function, it provides the result by a callback to specific function names. I am trying to wrap the calls to this inside a class, but this causes a problem with the callbacks, for example: class X { public: add(); };
11
3674
by: Fabien Penso | last post by:
Hi. I am trying to make this work but I got a weird behavior. I got a very basic system, I call a unmanaged "dllimported" function and give it a structure of callback functions. Sometimes, the unmanaged part calls one of the callback functions. But the first one has its pointer changed from its address to "0x00000001". I can't figure out why.
5
2245
by: Adam Clauss | last post by:
Couple questions: 1) I have an application using TCP sockets. When I make a call to BeginReceive(), is the callback I specify called in the current thread or from a new thread? 2) Similar to the first, with regards to events. When I actually call an event (with some number of delegates added to it), is each thread called sequentially in the calling thread, or is each executed asynchronously in its own?
1
2085
by: MichaelY | last post by:
While cruising through the script files that help to implement script callbacks in 2.0, I noticed that there is no code path to handle a return string that doesn't match the parsing logic - it just falls through and nothing happens. My scenario is this: My web app session times out/application error occurs when I make my script callback...my application 'normally' redirects users to the login form and/or error page (HTTP 302,...
4
1699
by: R. MacDonald | last post by:
Hello, all, I have a .NET application (VB) that passes the address of a delegate to unmanaged code in a DLL. The unmanaged code then uses the delegate as a call-back. This seems to work fine, but now I am worried about garbage collection. I am concerned that the location of the delegate might be altered as a result of other (unused) objects being garbage collected. This would probably cause undesirable results when the unmanaged DLL...
9
3345
by: zholthran | last post by:
Hi folks, after reading several threads on this issue (-> subject) I fear that I got a problem that cannot easily be solved by the offered workarounds in an acceptable way, at least not with my limited c & c++ experience. Maybe some of you can help. the problem: I need several instances of a class whose (non-static!) methods should serve as callbacks for a dll (which can' be manipulated/adapted in any
13
3051
by: noone | last post by:
consider the following problem: You have a C style library and API that uses callbacks to implement functionality. Examples of this are X11 API, OpenGL/GLUT...The List goes on. The power of virtuals in C++ leads us to want to implement a framework where those callbacks are simply overriden virtual methods in a derived class. So...
0
9554
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9377
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10136
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9925
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8814
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7358
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5266
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.