Click here to Skip to main content
15,886,095 members
Home / Discussions / C#
   

C#

 
AnswerRe: help Pin
DaveyM6917-Jul-09 13:20
professionalDaveyM6917-Jul-09 13:20 
GeneralRe: help Pin
Michael Schubert17-Jul-09 22:06
Michael Schubert17-Jul-09 22:06 
QuestionMS Word Automation - Combining word documents Pin
venadder17-Jul-09 11:25
venadder17-Jul-09 11:25 
QuestionDelegates - I don't Get It Pin
eddieangel17-Jul-09 7:52
eddieangel17-Jul-09 7:52 
AnswerRe: Delegates - I don't Get It Pin
harold aptroot17-Jul-09 8:14
harold aptroot17-Jul-09 8:14 
AnswerRe: Delegates - I don't Get It Pin
CodingYoshi17-Jul-09 8:20
CodingYoshi17-Jul-09 8:20 
GeneralRe: Delegates - I don't Get It Pin
eddieangel17-Jul-09 8:35
eddieangel17-Jul-09 8:35 
GeneralRe: Delegates - I don't Get It Pin
DaveyM6917-Jul-09 10:47
professionalDaveyM6917-Jul-09 10:47 
This actually isn't as complicated as it sounds!

Instead of a method being called directly, the call can be delegated - to a delegate.

One of the reasons for using a delegate for this (there are many) is methods cannot be passed as parameters to other methods, but delegates can (in C/C++ they pass a pointer [a pointer literaly points to a location in memory, or in otherwords is a value that is a memory address] to the callback function as the parameter).

So in the PInvoke function where the callback (the pointer to the callback function) is the parameter, in C# we pass a delgate instance. This delegate will then be called, which in turn calls the actual method you want to run.

I hope that makes sense!

All you need to do is create a delegate that returns the correct type, and has the correct signature:
private delegate UInt32 CameraCallBackDelegate(UInt32 cameraHandle, UInt32 context, IntPtr pEventData);
Create your delegate instance and set up the method call, and you're done.

You have to be careful that your delegate doesn't go out of scope (i.e the instance gets garbage collected) while it can still be called by the PInvoke function.

Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus)

GeneralRe: Delegates - I don't Get It Pin
eddieangel17-Jul-09 11:00
eddieangel17-Jul-09 11:00 
GeneralRe: Delegates - I don't Get It Pin
DaveyM6917-Jul-09 11:26
professionalDaveyM6917-Jul-09 11:26 
GeneralRe: Delegates - I don't Get It Pin
DaveyM6917-Jul-09 11:39
professionalDaveyM6917-Jul-09 11:39 
AnswerRe: Delegates - I don't Get It Pin
Luc Pattyn17-Jul-09 10:13
sitebuilderLuc Pattyn17-Jul-09 10:13 
GeneralRe: Delegates - I don't Get It Pin
Baeltazor17-Jul-09 22:28
Baeltazor17-Jul-09 22:28 
QuestionCrystalReports Pin
CodingYoshi17-Jul-09 7:06
CodingYoshi17-Jul-09 7:06 
AnswerRe: CrystalReports Pin
Adam R Harris17-Jul-09 9:20
Adam R Harris17-Jul-09 9:20 
GeneralRe: CrystalReports Pin
Adam R Harris17-Jul-09 10:41
Adam R Harris17-Jul-09 10:41 
GeneralRe: CrystalReports Pin
CodingYoshi17-Jul-09 10:54
CodingYoshi17-Jul-09 10:54 
QuestionCommon components in C# and .net? Pin
saxisa17-Jul-09 7:01
saxisa17-Jul-09 7:01 
QuestionNTLM Authentication in C# Pin
joana.simoes17-Jul-09 5:39
joana.simoes17-Jul-09 5:39 
AnswerRe: NTLM Authentication in C# Pin
Adam R Harris17-Jul-09 9:25
Adam R Harris17-Jul-09 9:25 
GeneralRe: NTLM Authentication in C# Pin
joana.simoes19-Jul-09 23:34
joana.simoes19-Jul-09 23:34 
GeneralRe: NTLM Authentication in C# Pin
joana.simoes19-Jul-09 23:50
joana.simoes19-Jul-09 23:50 
AnswerRe: NTLM Authentication in C# Pin
joana.simoes20-Jul-09 3:30
joana.simoes20-Jul-09 3:30 
QuestionRichTextBox- :(( Pin
RongNK17-Jul-09 4:49
RongNK17-Jul-09 4:49 
AnswerRe: RichTextBox- :(( Pin
Manas Bhardwaj17-Jul-09 4:59
professionalManas Bhardwaj17-Jul-09 4:59 

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

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