Re: Delegates and pointers

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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.
>>
>>
>>


.



Relevant Pages

  • Re: Delegates - have I got it right?
    ... My problem is that the breakpoint in the video callback is never reached. ... public delegate bool CallBack; ... public static extern int SendMessageWithLparamDelegate(int hWnd, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: beginners question about UI threading
    ... It is periodically polling another DB for updates and needs to inform my service when interesting changes have taken place. ... The component will call a callback function when a change is detected. ... IMHO the simplest way is to just create a producer/consumer queue, where the thread that should be executing the delegate is the consumer, and the threads that want the delegate to be executed are the producers. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Cant find the DbAsyncResult type. Whats going on?
    ... There I learned that the delegate instance was lurking inside the System.IAsyncResult parameter of the callback method. ... delegate int MyDelegate; ... static void CallBack ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Callbacks and Threads
    ... I DO need to be worried about accessing/modifying ... calling thread. ... > The threadpool - by the time the callback is ready to be called, ... > multicast delegate calls each delegate in turn, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Passing reference of the main thread control
    ... puplic delegate void SimbuttonCaptureStream( ... string client_location, string client_url, uint client_port) ... -- Move the call to Control.Invokeinto your callback method ... Then, the DLL can invoke the delegate directly method), and the method referenced by the delegate will handle the call to Control.Invoke. ...
    (microsoft.public.dotnet.languages.csharp)