Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / October 2005

Tip: Looking for answers? Try searching our database.

Delegates with non-static callback function

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
H.B. - 03 Oct 2005 16:29 GMT
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");
}
Marcus Heege - 03 Oct 2005 18:10 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.