callback

Jedhi

Centurion
Joined
Oct 2, 2003
Messages
127
I have some old c code that use callback functions, actually the callback function calls itself several time . Since I am moving platform from VC6 to .Net I would like to hear what might be the easiest way to solve the problem about callback by only using C++ or C#. As far as I know callback function are not used in OOP like C++ or C#. I am aware of using virtual function, but virtual function can not make recursive calls. Anyone got some ideas ?
 
Callbacks are perfectly usable in C++ (through function pointers or functor classes). In C# we have delegates. What do you mean virtual functions cannot make recursive calls (I don't see why not)?

How is this windows forms related?

-- Pete
 
Are delegates in C# similar to callbacks ?

I do not like function pointer. They force your callback functions to be static, so I can not inherit the function. I rather like virtual function

void CForm::ExOfCallback(BYTE id)
{
while ( id++ < MAX )
if (AcceptedId(Id))
break;

send(id, ExOfCallback);
}

Lets take this example. ExOfCallback is a virtual function. As long as id < MAX is must continue call itself. How do you do that ??
 
You don't like function pointers.. and yet your example seems to be using them? I can't see any other way ExOfCallback could be passed to the send method. I would like to see the signature of the send method to be sure.

Delegates in c# are quite different (there's lots of info on the internet).

It would be a good idea to let us know exactly what you are trying to achieve here. What does the code do?

-- Pete
 
so you mean that a callback function will always end up to be static even that you are NOT using function pointer. Let say you are using function objects instead ?

And callbacks can only call itself by using function pointer ?
 
Delegates are multicast, typesafe function pointers. Once you've defined their signature they can call any method, virtual or not.

There's no problem with virtual methods calling themselves in C#.
 
Could you write an example in C# where you have a virtual function that is calling itself until a condition is met ?
 
Sure. Here's a rather useless expansion of the example you gave earlier in this thread. Since the send function calls the callback function this code will result in a stack overflow exception, but you can see how to define the delegate and pass it to the other function.

C#:
		private bool AcceptedId(byte id)
		{
			return (id >= 10);
		}

		private const int MAX = 100;

		private delegate void MyDelegateType(byte id);

		protected virtual void ExOfCallback(byte id)
		{
			while ( id++ < MAX )
				if (AcceptedId(id))
					break;

			send(id, new MyDelegateType(ExOfCallback)); 
		}

		private void send(byte id, MyDelegateType myCallback)
		{
			// Example of calling the callback function
			myCallback(id);
		}
 
Which language is preferable when writing a program containing many callbacks C++ or C# ?


Could I for example write the API part as a dll in C++, and the GUI part in another language without giving any conflict ?
 
Personally I'd use C#, but that's because I don't like C++.

You could write it in both - writing a standard C dll and using platform invoke to interop with it, or using managed C++ to create a .NET class with it. .NET delegates can interop with unmanaged callbacks just fine. I think there's a sample in the .net sdk of using delegates with the win32 function EnumWindows.
 
why do you prefer C# instead of C++ ?

So you think it could be okay making a dll in C++ and the make the GUI interface in C# ?
 
If you really want to mix C# and C++ you can. Personally I wouldn't bother and just stick with C# unless you really need both languages.
 
Last edited:
You mean writing the dll in C# as well as the GUI. But you have not given any explanation why you prefer C# ?
 
Easier than C++, no need for pointer arithmetic, Type Safe, Delegates, Interfaces, supports strings as a data type. Easier integration with other .Net code for starters.

Are there any reasons why you prefer C++ or need to mix two languages.
 
Most of all because it is the language that I have used for decades. I like the features that C++ have like polymorphism, encapsulation and inheritance. But I heard more people complaining that so many things have to be programmed that is automatically done for you like in BASIC. C++ is just in many ways much more proffessional than BASIC. But I agree that make C++ interact with other interface can be problematic. Also issues about Pointers can make your hair grow before finding out what goes wrong. I am just starting at .NET and of course unconscious
I choose C++ because that is the language I know. I have no idea of what language is the most potential. What do people prefer to code in future C# or C++. As long as I do not have to put myself into alot of new things it does not really matter whether it is C# or C++.
 
C# gives you things like Polymorphism, Encapsulation and inheritance.
Inheritance is currently limited to single inheritance (although MI may make a future version of the framework)
Polymorphism is supported through Inheritance and interfaces (which remove a lot - but not all of the reasons for Multiple Inheritance).
If you are familiar with C++ then a lot of C# will be familiar (if statements, loop constructs, variable declaration etc)
C# can't do everything C++ can but how many people actually need to do everything C++ can?
C# on the other hand gives the developer an easier ride with system provided strings, arrays, collections etc as well as memory management (via Garbage collection)
 
One problem I sometimes see, is when you have several classes
that use same function like ex. InheritTxtFunction, you can no longer inherit the function because of the windows handler is probably not point at the right window. Take for example SetWindowText. To what window should it put the text. The result is that you for every class must copy this function. How annoying. Or are there any trick to solve this problem ?


like:

void CDlg::InheritTxtFunction(BYTE answer)
{
char txt[10];

switch (answer)
case 0: MessageBox("App","Error"); break;
case 1: sprintf(txt, "Ready"); break;
default: SetwindowText("Pause);
break;
}
 
Not sure what you mean with 'Several classes that use same function'
If this is a function common to a group of related classes then declaring it in a base class means it's available to all sub-classes (and can be over ridden if needed).
If it is more generic functionality then it could be a delegate or part of an interface.

Using SetWindowText as an example - this would either take a window handle as a parameter to tell it which window to put the text on, or would be a method in a class that encapsulates a window.

edit: typo (faulty keyboard)
 
Last edited:
I guess you are right that you can make such a function as an inherited one. It only require that the handler is pointing to the right window. But how would would you do that ?
 
Back
Top