Click here to Skip to main content
11,431,483 members (67,022 online)
Click here to Skip to main content

Yet another approach to Delegates in unmanaged C++

, 5 Mar 2002
Rate this:
Please Sign up or sign in to vote.
Asynchronous delegates in unamanaged C++ using the C# syntax
<!-- Download Links --> <!-- Article image -->

Sample Image - cppDelegate.gif

Introduction

Ben Chan introduced a way of simulating delegates in unmanaged code. Although his method works I wanted to have a syntax similar found in the C# language. I also wanted to be able to use async delegates.

Using my solution

Defining a Delegate is simple as this:

Delegate OnBtnClick;
AsyncDelegate OnNewMail;

To add a new delegate handler to those defined above you have to choose whether you want to use a static/global function or a member function. To subscribe a delegate you use a syntax similar to C#'s:

class MyClass
{
public:
	void Handler1(LPVOID source, DelegateArgs* args)
	{
	...
	}

	static void StaticHandler(LPVOID source, DelegateArgs* args)
	{
	...
	}
};

void main()
{
	MyClass instance;
	Delegate OnBtnClick;

	OnBtnClick += NewClassDelegateHandler(MyClass, instance, Handler1);
	OnBtnClick += NewDelegateHandler(&MyClass::StaticHandler);


	LPVOID source = 0;
	DelegateArgs* args = 0;

	// you can also remove a delegate handler from the invocation list
	// OnBtnClick -= NewClassDelegateHandler(MyClass, instance, Handler1);

	OnBtnClick.Invoke(source, args);
}

NewClassDelegateHandler and NewDelegateHandler are defines that create the appropriate DelegateHandler class. It can be the StaticDelegateHandler for static functions or ClassDelegateHandler for member functions.

The async delegate only overrides the Delegate::Invoke method by creating a thread to invoke the handlers. You can make your delegate thread safe (AsyncDelegate already is) by defining a Delegate like this Delegate ThreadSafeDelegate(true);

IMPORTANT: To use my solution you have to enable runtime type information in your project.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

xicoloko
Architect VisionOne AG
Switzerland Switzerland
XicoLoko is a brazilian developer based in Switzerland.

Comments and Discussions

 
QuestionDownload link is down... Pin
GhostEx28-Jan-13 0:58
memberGhostEx28-Jan-13 0:58 
GeneralStraightforward and useful Pin
Yves Tkaczyk21-Oct-05 11:50
memberYves Tkaczyk21-Oct-05 11:50 
GeneralRe: Straightforward and useful Pin
Mizan Rahman24-Sep-13 2:51
memberMizan Rahman24-Sep-13 2:51 
GeneralRe: Straightforward and useful Pin
Yves Tkaczyk7-Oct-13 8:14
memberYves Tkaczyk7-Oct-13 8:14 
GeneralRe: Straightforward and useful Pin
Mizan Rahman7-Oct-13 23:21
memberMizan Rahman7-Oct-13 23:21 
GeneralRe: Straightforward and useful Pin
Yves Tkaczyk8-Oct-13 9:13
memberYves Tkaczyk8-Oct-13 9:13 
GeneralExcellent work Pin
Verifier19-Nov-03 4:08
memberVerifier19-Nov-03 4:08 
GeneralShould not mix C/C++ with .NET Pin
TW23-May-03 1:04
memberTW23-May-03 1:04 
Generalthreads.. Pin
saltynuts200225-Feb-03 19:32
membersaltynuts200225-Feb-03 19:32 
Generalget the code to run under VS.NET Pin
Tweety30-Jan-03 7:54
memberTweety30-Jan-03 7:54 
GeneralRe: get the code to run under VS.NET Pin
YuetKent25-Aug-04 0:01
memberYuetKent25-Aug-04 0:01 
GeneralRe: get the code to run under VS.NET Pin
Tweety26-Aug-04 4:05
memberTweety26-Aug-04 4:05 
GeneralFairly named "Standard C++" instead biased ads "unmanged C++" ! Pin
Anonymous10-Apr-02 14:24
memberAnonymous10-Apr-02 14:24 
MS's advertising is biased, so called "managed C++" or C#
is only better to develop for .Net platform only, especially
C# is Not a better C++, it just another programming language,
if C# is so good, why not MS just develop SQL server, IE or MSOffice
in pure C# ?!!

Standard C++ is powerful,
through "resources acquisition in initialization"
it's much safe and simple then so-called "managed C++".
by the way, "managed C++" should be called "Unstandard C++"!!

.Net is good, because MS will force it become build-in framework
of every devices he could or will conquer.

Advertising is good, but should not biased.
Please let's call "Standard C++" instead biased ads like "unmanaged C++"

Could editers of codeproject.com fairly change the title of
"managed C++" to "MS managed C++" or ".Net managed C++"
or "unstandard C++" ?
GeneralRe: Fairly named "Standard C++" instead biased ads "unmanged C++" ! Pin
Cachivache15-May-02 18:20
memberCachivache15-May-02 18:20 
GeneralRe: Fairly named "Standard C++" instead biased ads "unmanged C++" ! Pin
M Aamir Maniar10-Mar-03 18:32
memberM Aamir Maniar10-Mar-03 18:32 
GeneralRe: Fairly named "Standard C++" instead biased ads "unmanged C++" ! Pin
Cholo15-Mar-03 5:53
memberCholo15-Mar-03 5:53 
GeneralRe: Fairly named "Standard C++" instead biased ads "unmanged C++" ! Pin
Cholo Lennon15-Mar-03 5:53
sussCholo Lennon15-Mar-03 5:53 
GeneralWell I don't agree! Pin
Chryler21-Jul-03 22:29
memberChryler21-Jul-03 22:29 
GeneralRe: Well I don't agree! Pin
Paul Selormey4-Sep-03 4:09
memberPaul Selormey4-Sep-03 4:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150428.2 | Last Updated 6 Mar 2002
Article Copyright 2002 by xicoloko
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid