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