Delegates with non-static callback function

G

Guest

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");
}
 
M

Marcus Heege

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>
 
G

Guest

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)()
 
C

Carl Daniel [VC++ MVP]

H.B. said:
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
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top