Re: Delegates and pointers
- From: "Dave" <NOSPAM-dave@xxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 26 Apr 2005 16:02:43 -0400
No problem.
The declaration for your interop call (external method) should also declare the paramter type as the delegate your passing it:
> [DllImport("patsapi.dll")]
> public static extern int ptRegisterLinkStateCallback(int callbackID, callbackEvent CallBack);
This, of course, means that you have to declare the delegate in your library and not the consuming program.
--
Dave Sexton
dave@xxxxxxxxxxxxxxxxxxx
-----------------------------------------------------------------------
"Wayne Weeks" <WayneWeeks@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:A05B408C-E89D-4B96-B8CD-C9F4753158F5@xxxxxxxxxxxxxxxx
> Thanks Dave,
>
> I was confused on how to use delegates, but I think I understand how they
> are used now. I have used your code as the basis for correcting what I had
> done. I still have one final issue that perhaps you may know how to fix.
> Here is the code:
>
> delegate void callbackEvent();
>
> void MyCallback()
> {
> MessageBox.Show("Callback Event Received");
> }
>
> private void ptRegisterLinkStateCallback_Click(object sender, EventArgs e)
> {
> callbackEvent cb = new callbackEvent(MyCallback);
> status = ptAPI.ptRegisterLinkStateCallback(ptHostLinkStateChange, cb);
> MessageBox.Show("Status = " + status.ToString());
> }
>
> So when the "ptRegisterLink...." button is clicked a pointer, cb, is created
> to MyCallback. The "ptAPI.ptRegister..." function call is to an external DLL
> using DLLImport and I am expecting the 'cb' argument to be the pointer to the
> MyCallback method. The code in the DLL is:
>
> public class ptAPI
> {
> [DllImport("patsapi.dll")]
> public static extern int ptRegisterLinkStateCallback(int callbackID, int
> mtdAddress);
> }
>
> The compiler complains that there are invalid arguments and that argument 2
> 'cannot convert from "...Form1.callbackEvent to int". Clearly I still have
> something wrong with my code.
>
> Any ideas as to why this is wrong? Thanks for your help with this, much
> appreciated.
>
> Wayne
>
> "Dave" wrote:
>
>> > callbackEvent Event = delegate()
>> > {
>> > MessageBox.Show("Callback Event Received");
>> > }
>>
>> The above syntax is incorrect.
>> Treat your callback as a method:
>>
>> delegate void callbackEvent();
>>
>> void MyCallBack()
>> {
>> MessageBox.Show("Callback Event Received");
>> }
>>
>> void ConsumeDelegate()
>> {
>> // Create a pointer to MyCallBack using the callbackEvent signature:
>> callbackEvent cb = new callbackEvent(MyCallBack);
>>
>> // Use the method pointer in your interop call:
>> myAPIregister(cb);
>>
>> // > The parameter in myAPIregister(parameter) is
>> // > expecting a pointer to the callback handler.
>> //
>> // .NET Interop will automattically Marshal the delegate you pass to the external method as a pointer to the method
>> }
>>
>> --
>> Dave Sexton
>> dave@xxxxxxxxxxxxxxxxxxx
>> -----------------------------------------------------------------------
>> "Wayne Weeks" <Wayne Weeks@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:35A6F85C-A4DF-4F05-AED1-AC9667C96F4F@xxxxxxxxxxxxxxxx
>> >I am trying to use delegates for the first time and I am probably missing
>> > something. I have a legacy DLL and I have used DllImport to get to the
>> > functions that it contains...the normal function calls work fine, but I can't
>> > get callbacks to work. The delegate declaration etc is below.
>> >
>> > public delegate void callbackEvent();
>> >
>> > callbackEvent Event = delegate()
>> > {
>> > MessageBox.Show("Callback Event Received");
>> > }
>> >
>> >
>> > The function in the DLL to register the callback is
>> >
>> > myAPIregister(Event);
>> >
>> > So I am expecting that myAPIregister(Event) will provide a pointer to the
>> > Event delegate. When the callback event occurs it should simply display the
>> > MessageBox with the text. The parameter in myAPIregister(parameter) is
>> > expecting a pointer to the callback handler.
>> >
>> > I have done a lot of reading about delegates over the past few days to get
>> > this far but I appear to be missing something. The Compiler complains that
>> > the argument is invalid.
>> >
>> > Thanks for any help and guidance.
>>
>>
>>
.
- Follow-Ups:
- Re: Delegates and pointers
- From: Wayne Weeks
- Re: Delegates and pointers
- References:
- Delegates and pointers
- From: Wayne Weeks
- Re: Delegates and pointers
- From: Dave
- Re: Delegates and pointers
- From: Wayne Weeks
- Delegates and pointers
- Prev by Date: Re: HResult in c#
- Next by Date: Re: Hi all
- Previous by thread: Re: Delegates and pointers
- Next by thread: Re: Delegates and pointers
- Index(es):
Relevant Pages
|