473,395 Members | 1,783 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,395 software developers and data experts.

Delegates with non-static callback function

Hi,

I successfully implement a static callback function for my dll usign
delegates. Now, I need to use member function instead of static function. How
can I make that (in Managed C++).

Hugo

// .h

typedef void (CALLBACK *FCallback)(); // From DLL

__delegate void CallBackDelegate();
public: static CallBackDelegate* pTheStaticDelegate;
// .cpp

Void Form1::button1_Click(System::Object * sender, System::EventArgs * e)
{
pTheStaticDelegate = new CallBackDelegate(GetType(),
&Form1::TheStaticCallback);
SetCallback((FCallback) TheStaticCallback); // From DLL
}
Void Form1::button2_Click(System::Object * sender, System::EventArgs * e)
{
UseCallback(); // From DLL
}

void Form1::TheStaticCallback()
{
System::Diagnostics::Trace::WriteLine(S"In the static callback");
}

Nov 17 '05 #1
4 5104
If you want to call a static function, pass 0 and the address of your static
function as the constructor arguments. Your may work, but it is misleading:

pTheStaticDelegate = new CallBackDelegate(GetType(),
&Form1::TheStaticCallback);

Don't pass GetType(), but 0.

To call a nonstatic method pass a reference to the target object and the
address of the constructor arguments:

pTheDelegate = new CallBackDelegate(this, &Form1::TheCallback);

where this is a reference to the form in your case.

Here is some more general code that shows how to call static and nonstatic
functions via delegates:

<code language="MCPP">
#using <mscorlib.dll>
using namespace System;

__delegate void DoSthDelegate();

__gc class Target {
public:
void DoSth() { Console::WriteLine("DoSth called"); }
static void DoSthStatic() { Console::WriteLine("DoSthStatic called"); }
};

int main() {
DoSthDelegate __gc* d1 = __gc new DoSthDelegate(0, &Target::DoSthStatic);
d1->Invoke();
Target __gc* pT = __gc new Target;
DoSthDelegate __gc* d2 = __gc new DoSthDelegate(pT, &Target::DoSth);
d2->Invoke();
}
</code>

"H.B." <HB@discussions.microsoft.com> wrote in message
news:FC**********************************@microsof t.com...
Hi,

I successfully implement a static callback function for my dll usign
delegates. Now, I need to use member function instead of static function.
How
can I make that (in Managed C++).

Hugo

// .h

typedef void (CALLBACK *FCallback)(); // From DLL

__delegate void CallBackDelegate();
public: static CallBackDelegate* pTheStaticDelegate;
// .cpp

Void Form1::button1_Click(System::Object * sender, System::EventArgs *
e)
{
pTheStaticDelegate = new CallBackDelegate(GetType(),
&Form1::TheStaticCallback);
SetCallback((FCallback) TheStaticCallback); // From DLL
}
Void Form1::button2_Click(System::Object * sender, System::EventArgs *
e)
{
UseCallback(); // From DLL
}

void Form1::TheStaticCallback()
{
System::Diagnostics::Trace::WriteLine(S"In the static callback");
}

Nov 17 '05 #2
I still have a problem. I get the following error :

error C2440: 'type cast' : cannot convert from 'overloaded-function' to
'FCallback'

for :

SetCallback((FCallback) TheNonStaticCallback);

---------------

In fact, the DLL needs to callback application to refresh visual components.
That's why I need to supply a non-static member fonction to the DLL whit
SetCallback().

The callback is defined in DLL as FCallback -->> typedef void (CALLBACK
*FCallback)()

------------

Hugo

"Marcus Heege" wrote:
If you want to call a static function, pass 0 and the address of your static
function as the constructor arguments. Your may work, but it is misleading:

pTheStaticDelegate = new CallBackDelegate(GetType(),
&Form1::TheStaticCallback);

Don't pass GetType(), but 0.

To call a nonstatic method pass a reference to the target object and the
address of the constructor arguments:

pTheDelegate = new CallBackDelegate(this, &Form1::TheCallback);

where this is a reference to the form in your case.

Here is some more general code that shows how to call static and nonstatic
functions via delegates:

<code language="MCPP">
#using <mscorlib.dll>
using namespace System;

__delegate void DoSthDelegate();

__gc class Target {
public:
void DoSth() { Console::WriteLine("DoSth called"); }
static void DoSthStatic() { Console::WriteLine("DoSthStatic called"); }
};

int main() {
DoSthDelegate __gc* d1 = __gc new DoSthDelegate(0, &Target::DoSthStatic);
d1->Invoke();
Target __gc* pT = __gc new Target;
DoSthDelegate __gc* d2 = __gc new DoSthDelegate(pT, &Target::DoSth);
d2->Invoke();
}
</code>

"H.B." <HB@discussions.microsoft.com> wrote in message
news:FC**********************************@microsof t.com...
Hi,

I successfully implement a static callback function for my dll usign
delegates. Now, I need to use member function instead of static function.
How
can I make that (in Managed C++).

Hugo

// .h

typedef void (CALLBACK *FCallback)(); // From DLL

__delegate void CallBackDelegate();
public: static CallBackDelegate* pTheStaticDelegate;
// .cpp

Void Form1::button1_Click(System::Object * sender, System::EventArgs *
e)
{
pTheStaticDelegate = new CallBackDelegate(GetType(),
&Form1::TheStaticCallback);
SetCallback((FCallback) TheStaticCallback); // From DLL
}
Void Form1::button2_Click(System::Object * sender, System::EventArgs *
e)
{
UseCallback(); // From DLL
}

void Form1::TheStaticCallback()
{
System::Diagnostics::Trace::WriteLine(S"In the static callback");
}


Nov 17 '05 #3
H.B. wrote:
I still have a problem. I get the following error :

error C2440: 'type cast' : cannot convert from
'overloaded-function' to 'FCallback'

for :

SetCallback((FCallback) TheNonStaticCallback);

---------------

In fact, the DLL needs to callback application to refresh visual
components. That's why I need to supply a non-static member fonction
to the DLL whit SetCallback().

The callback is defined in DLL as FCallback -->> typedef void
(CALLBACK
*FCallback)()


What is the definition of SetCallback? (You didn't show any such function
in your original example).

The root of your problem is this:

A pointer to a static member function IsA pointer, but a pointer to a
non-static member function isn't. While a delegate can be constructed from
either, they are completely incompatible types.

You should be able to accomodate that fact (from ISO C++, nothing special to
managedf C++) by passing the delegate type to SetCallback instead of the
function pointer type. Then you'd have "new
CallbackDelegate(object,&class::member)" each place you call SetCallback.

-cd
Nov 17 '05 #4
Ok,

Now I'm usign a wrapping struct for my delegate and I use marshalling to get
the pointer and it works.

The only problem is that for one callback call in DLL it seems to have
multiple call of the member fonction in the main application ??? Weird !!!

Hugo
"Carl Daniel [VC++ MVP]" wrote:
H.B. wrote:
I still have a problem. I get the following error :

error C2440: 'type cast' : cannot convert from
'overloaded-function' to 'FCallback'

for :

SetCallback((FCallback) TheNonStaticCallback);

---------------

In fact, the DLL needs to callback application to refresh visual
components. That's why I need to supply a non-static member fonction
to the DLL whit SetCallback().

The callback is defined in DLL as FCallback -->> typedef void
(CALLBACK
*FCallback)()


What is the definition of SetCallback? (You didn't show any such function
in your original example).

The root of your problem is this:

A pointer to a static member function IsA pointer, but a pointer to a
non-static member function isn't. While a delegate can be constructed from
either, they are completely incompatible types.

You should be able to accomodate that fact (from ISO C++, nothing special to
managedf C++) by passing the delegate type to SetCallback instead of the
function pointer type. Then you'd have "new
CallbackDelegate(object,&class::member)" each place you call SetCallback.

-cd

Nov 17 '05 #5

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

Similar topics

6
by: Jeffrey T. Smith | last post by:
Back when the new J2SE1.5 features were announced, there was a JavaLive community chat (http://java.sun.com/developer/community/chat/JavaLive/2003/jl0729.html) in which Neal Gafter explains the...
1
by: Pablo Salazar | last post by:
Hi people. I found a book, it talked about oop, in a chapter i found a topic called "DELEGATES", I read it , but I didn't Understand. Somebody can tell me where I can find a tutorial or book that...
4
by: Phil G. | last post by:
I was recently struggling to adapt an example I have using delegate methods, IasynResult and AsynCallback. Doing a little research I came across an example, which in fact was being used to...
2
by: Amit Bansal \(MCT, MCSD.NET\) | last post by:
delegates are type safe. what exacly is meant by type safe ?? any good document explaining the benefits of delegates.
5
by: GVN | last post by:
Hi All, I recently worked on delegates. But I have a question regarding Multicast delegates. The scenario is as follows: I have a delegate myDelegate with object dlgt, and two methods...
6
by: =?Utf-8?B?Sko=?= | last post by:
I have a logger component that logs to multiple sources, ie textfile, eventlog etc. and I have two methods that depending on where I call up my logger comp. one of them will be called. For ex. if...
2
by: jehugaleahsa | last post by:
Hello: I was reading another post and saw an argument (sort of) that brought up a good question. I have written a fairly large library of algorithms that occur in programming all the time. It...
3
by: sherifffruitfly | last post by:
I'm looking for a tutorial/sample specifically aimed at showing why life is better with them than without them. Ideally, what I'd like to see in the tutorial I'm looking for: 1) No discussion of...
10
by: Roger Frost | last post by:
Since we are currently on the subject of multi-threading in a different thread, I have a question about delegates. When I use delegates, I just create one for each different parameter set that I...
9
by: raylopez99 | last post by:
Hello all— I’m trying to get the below to work and cannot get the format right. It’s from this example: http://msdn.microsoft.com/en-us/library/8627sbea(VS.71).aspx What it is: I’m trying...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.