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>
> Hi,
>
[quoted text clipped - 32 lines]
> System::Diagnostics::Trace::WriteLine(S"In the static callback");
> }
H.B. - 04 Oct 2005 14:44 GMT
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
> 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:
[quoted text clipped - 71 lines]
> > System::Diagnostics::Trace::WriteLine(S"In the static callback");
> > }
Carl Daniel [VC++ MVP] - 04 Oct 2005 15:10 GMT
> I still have a problem. I get the following error :
>
[quoted text clipped - 14 lines]
> (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
H.B. - 05 Oct 2005 16:11 GMT
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
> > I still have a problem. I get the following error :
> >
[quoted text clipped - 30 lines]
>
> -cd