Delegates and pointers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Wayne Weeks

    Delegates and pointers

    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(E vent);

    So I am expecting that myAPIregister(E vent) 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(p arameter) 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.
  • Dave

    #2
    Re: Delegates and pointers

    > callbackEvent Event = delegate()[color=blue]
    > {
    > MessageBox.Show ("Callback Event Received");
    > }[/color]

    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(M yCallBack);

    // Use the method pointer in your interop call:
    myAPIregister(c b);

    // > The parameter in myAPIregister(p arameter) 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@www..jwaon line..com
    -----------------------------------------------------------------------
    "Wayne Weeks" <Wayne Weeks@discussio ns.microsoft.co m> wrote in message news:35A6F85C-A4DF-4F05-AED1-AC9667C96F4F@mi crosoft.com...[color=blue]
    >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(E vent);
    >
    > So I am expecting that myAPIregister(E vent) 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(p arameter) 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.[/color]


    Comment

    • Wayne Weeks

      #3
      Re: Delegates and pointers

      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 ptRegisterLinkS tateCallback_Cl ick(object sender, EventArgs e)
      {
      callbackEvent cb = new callbackEvent(M yCallback);
      status = ptAPI.ptRegiste rLinkStateCallb ack(ptHostLinkS tateChange, cb);
      MessageBox.Show ("Status = " + status.ToString ());
      }

      So when the "ptRegisterLink ...." button is clicked a pointer, cb, is created
      to MyCallback. The "ptAPI.ptRegist er..." 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("pats api.dll")]
      public static extern int ptRegisterLinkS tateCallback(in t callbackID, int
      mtdAddress);
      }

      The compiler complains that there are invalid arguments and that argument 2
      'cannot convert from "...Form1.callb ackEvent 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:
      [color=blue][color=green]
      > > callbackEvent Event = delegate()
      > > {
      > > MessageBox.Show ("Callback Event Received");
      > > }[/color]
      >
      > 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(M yCallBack);
      >
      > // Use the method pointer in your interop call:
      > myAPIregister(c b);
      >
      > // > The parameter in myAPIregister(p arameter) 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@www..jwaon line..com
      > -----------------------------------------------------------------------
      > "Wayne Weeks" <Wayne Weeks@discussio ns.microsoft.co m> wrote in message news:35A6F85C-A4DF-4F05-AED1-AC9667C96F4F@mi crosoft.com...[color=green]
      > >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(E vent);
      > >
      > > So I am expecting that myAPIregister(E vent) 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(p arameter) 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.[/color]
      >
      >
      >[/color]

      Comment

      • Dave

        #4
        Re: Delegates and pointers

        No problem.

        The declaration for your interop call (external method) should also declare the paramter type as the delegate your passing it:
        [color=blue]
        > [DllImport("pats api.dll")]
        > public static extern int ptRegisterLinkS tateCallback(in t callbackID, callbackEvent CallBack);[/color]

        This, of course, means that you have to declare the delegate in your library and not the consuming program.

        --
        Dave Sexton
        dave@www..jwaon line..com
        -----------------------------------------------------------------------
        "Wayne Weeks" <WayneWeeks@dis cussions.micros oft.com> wrote in message news:A05B408C-E89D-4B96-B8CD-C9F4753158F5@mi crosoft.com...[color=blue]
        > 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 ptRegisterLinkS tateCallback_Cl ick(object sender, EventArgs e)
        > {
        > callbackEvent cb = new callbackEvent(M yCallback);
        > status = ptAPI.ptRegiste rLinkStateCallb ack(ptHostLinkS tateChange, cb);
        > MessageBox.Show ("Status = " + status.ToString ());
        > }
        >
        > So when the "ptRegisterLink ...." button is clicked a pointer, cb, is created
        > to MyCallback. The "ptAPI.ptRegist er..." 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("pats api.dll")]
        > public static extern int ptRegisterLinkS tateCallback(in t callbackID, int
        > mtdAddress);
        > }
        >
        > The compiler complains that there are invalid arguments and that argument 2
        > 'cannot convert from "...Form1.callb ackEvent 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:
        >[color=green][color=darkred]
        >> > callbackEvent Event = delegate()
        >> > {
        >> > MessageBox.Show ("Callback Event Received");
        >> > }[/color]
        >>
        >> 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(M yCallBack);
        >>
        >> // Use the method pointer in your interop call:
        >> myAPIregister(c b);
        >>
        >> // > The parameter in myAPIregister(p arameter) 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@www..jwaon line..com
        >> -----------------------------------------------------------------------
        >> "Wayne Weeks" <Wayne Weeks@discussio ns.microsoft.co m> wrote in message news:35A6F85C-A4DF-4F05-AED1-AC9667C96F4F@mi crosoft.com...[color=darkred]
        >> >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(E vent);
        >> >
        >> > So I am expecting that myAPIregister(E vent) 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(p arameter) 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.[/color]
        >>
        >>
        >>[/color][/color]


        Comment

        • Wayne Weeks

          #5
          Re: Delegates and pointers

          OK, thankyou, yes that makes sense, need to make the parameter types the
          same. Just one final issue and I hope I can move on with the fun part of
          this application. As you note, I added the declaration of the delegate to
          the library code as follows:

          public class ptAPI
          {
          delegate void callbackEvent() ;

          [DllImport("pats api.dll")]
          public static extern int ptRegisterLinkS tateCallback(in t callbackID,
          callbackEvent Callback);
          }

          However when I try to recompile the library the compiler complains that
          there is "Inconsiste nt accessibility", parameter type ptAPI.callbackE vent is
          less accessibile than ptAPI.ptRegiste rLinkStateCallb ack. Am I missing a
          switch for the compiler? Something wrong with declaration of the delegate in
          the library. This delegate construct is a tricky thing to grasp....thanks
          again, hopefully I can get past this soon.

          Wayne



          "Dave" wrote:
          [color=blue]
          > No problem.
          >
          > The declaration for your interop call (external method) should also declare the paramter type as the delegate your passing it:
          >[color=green]
          > > [DllImport("pats api.dll")]
          > > public static extern int ptRegisterLinkS tateCallback(in t callbackID, callbackEvent CallBack);[/color]
          >
          > This, of course, means that you have to declare the delegate in your library and not the consuming program.
          >
          > --
          > Dave Sexton
          > dave@www..jwaon line..com
          > -----------------------------------------------------------------------
          > "Wayne Weeks" <WayneWeeks@dis cussions.micros oft.com> wrote in message news:A05B408C-E89D-4B96-B8CD-C9F4753158F5@mi crosoft.com...[color=green]
          > > 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 ptRegisterLinkS tateCallback_Cl ick(object sender, EventArgs e)
          > > {
          > > callbackEvent cb = new callbackEvent(M yCallback);
          > > status = ptAPI.ptRegiste rLinkStateCallb ack(ptHostLinkS tateChange, cb);
          > > MessageBox.Show ("Status = " + status.ToString ());
          > > }
          > >
          > > So when the "ptRegisterLink ...." button is clicked a pointer, cb, is created
          > > to MyCallback. The "ptAPI.ptRegist er..." 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("pats api.dll")]
          > > public static extern int ptRegisterLinkS tateCallback(in t callbackID, int
          > > mtdAddress);
          > > }
          > >
          > > The compiler complains that there are invalid arguments and that argument 2
          > > 'cannot convert from "...Form1.callb ackEvent 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:
          > >[color=darkred]
          > >> > 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(M yCallBack);
          > >>
          > >> // Use the method pointer in your interop call:
          > >> myAPIregister(c b);
          > >>
          > >> // > The parameter in myAPIregister(p arameter) 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@www..jwaon line..com
          > >> -----------------------------------------------------------------------
          > >> "Wayne Weeks" <Wayne Weeks@discussio ns.microsoft.co m> wrote in message news:35A6F85C-A4DF-4F05-AED1-AC9667C96F4F@mi crosoft.com...
          > >> >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(E vent);
          > >> >
          > >> > So I am expecting that myAPIregister(E vent) 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(p arameter) 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.
          > >>
          > >>
          > >>[/color][/color]
          >
          >
          >[/color]

          Comment

          • Dave

            #6
            Re: Delegates and pointers

            you haven't specified an access modifier on the delegate declaration. This error has nothing to do with the fact that your using a
            delegate :) My sample code didn't specify it because it was intended for use within class-scope.

            Try:

            public delegate void callbackEvent() ;



            --
            Dave Sexton
            dave@www..jwaon line..com
            -----------------------------------------------------------------------
            "Wayne Weeks" <WayneWeeks@dis cussions.micros oft.com> wrote in message news:D0A4B1FC-242F-482C-AEB6-21F437359172@mi crosoft.com...[color=blue]
            > OK, thankyou, yes that makes sense, need to make the parameter types the
            > same. Just one final issue and I hope I can move on with the fun part of
            > this application. As you note, I added the declaration of the delegate to
            > the library code as follows:
            >
            > public class ptAPI
            > {
            > delegate void callbackEvent() ;
            >
            > [DllImport("pats api.dll")]
            > public static extern int ptRegisterLinkS tateCallback(in t callbackID,
            > callbackEvent Callback);
            > }
            >
            > However when I try to recompile the library the compiler complains that
            > there is "Inconsiste nt accessibility", parameter type ptAPI.callbackE vent is
            > less accessibile than ptAPI.ptRegiste rLinkStateCallb ack. Am I missing a
            > switch for the compiler? Something wrong with declaration of the delegate in
            > the library. This delegate construct is a tricky thing to grasp....thanks
            > again, hopefully I can get past this soon.
            >
            > Wayne
            >
            >
            >
            > "Dave" wrote:
            >[color=green]
            >> No problem.
            >>
            >> The declaration for your interop call (external method) should also declare the paramter type as the delegate your passing it:
            >>[color=darkred]
            >> > [DllImport("pats api.dll")]
            >> > public static extern int ptRegisterLinkS tateCallback(in t callbackID, callbackEvent CallBack);[/color]
            >>
            >> This, of course, means that you have to declare the delegate in your library and not the consuming program.
            >>
            >> --
            >> Dave Sexton
            >> dave@www..jwaon line..com
            >> -----------------------------------------------------------------------
            >> "Wayne Weeks" <WayneWeeks@dis cussions.micros oft.com> wrote in message news:A05B408C-E89D-4B96-B8CD-C9F4753158F5@mi crosoft.com...[color=darkred]
            >> > 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 ptRegisterLinkS tateCallback_Cl ick(object sender, EventArgs e)
            >> > {
            >> > callbackEvent cb = new callbackEvent(M yCallback);
            >> > status = ptAPI.ptRegiste rLinkStateCallb ack(ptHostLinkS tateChange, cb);
            >> > MessageBox.Show ("Status = " + status.ToString ());
            >> > }
            >> >
            >> > So when the "ptRegisterLink ...." button is clicked a pointer, cb, is created
            >> > to MyCallback. The "ptAPI.ptRegist er..." 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("pats api.dll")]
            >> > public static extern int ptRegisterLinkS tateCallback(in t callbackID, int
            >> > mtdAddress);
            >> > }
            >> >
            >> > The compiler complains that there are invalid arguments and that argument 2
            >> > 'cannot convert from "...Form1.callb ackEvent 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(M yCallBack);
            >> >>
            >> >> // Use the method pointer in your interop call:
            >> >> myAPIregister(c b);
            >> >>
            >> >> // > The parameter in myAPIregister(p arameter) 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@www..jwaon line..com
            >> >> -----------------------------------------------------------------------
            >> >> "Wayne Weeks" <Wayne Weeks@discussio ns.microsoft.co m> wrote in message
            >> >> news:35A6F85C-A4DF-4F05-AED1-AC9667C96F4F@mi crosoft.com...
            >> >> >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(E vent);
            >> >> >
            >> >> > So I am expecting that myAPIregister(E vent) 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(p arameter) 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.
            >> >>
            >> >>
            >> >>[/color]
            >>
            >>
            >>[/color][/color]


            Comment

            • Wayne Weeks

              #7
              Re: Delegates and pointers

              Dave, a big thank you....finally I can get the code to build and it seems to
              work....thanks again for your support getting this resolved...now on to more
              interesting development.

              "Dave" wrote:
              [color=blue]
              > you haven't specified an access modifier on the delegate declaration. This error has nothing to do with the fact that your using a
              > delegate :) My sample code didn't specify it because it was intended for use within class-scope.
              >
              > Try:
              >
              > public delegate void callbackEvent() ;
              >
              >
              >
              > --
              > Dave Sexton
              > dave@www..jwaon line..com
              > -----------------------------------------------------------------------
              > "Wayne Weeks" <WayneWeeks@dis cussions.micros oft.com> wrote in message news:D0A4B1FC-242F-482C-AEB6-21F437359172@mi crosoft.com...[color=green]
              > > OK, thankyou, yes that makes sense, need to make the parameter types the
              > > same. Just one final issue and I hope I can move on with the fun part of
              > > this application. As you note, I added the declaration of the delegate to
              > > the library code as follows:
              > >
              > > public class ptAPI
              > > {
              > > delegate void callbackEvent() ;
              > >
              > > [DllImport("pats api.dll")]
              > > public static extern int ptRegisterLinkS tateCallback(in t callbackID,
              > > callbackEvent Callback);
              > > }
              > >
              > > However when I try to recompile the library the compiler complains that
              > > there is "Inconsiste nt accessibility", parameter type ptAPI.callbackE vent is
              > > less accessibile than ptAPI.ptRegiste rLinkStateCallb ack. Am I missing a
              > > switch for the compiler? Something wrong with declaration of the delegate in
              > > the library. This delegate construct is a tricky thing to grasp....thanks
              > > again, hopefully I can get past this soon.
              > >
              > > Wayne
              > >
              > >
              > >
              > > "Dave" wrote:
              > >[color=darkred]
              > >> No problem.
              > >>
              > >> The declaration for your interop call (external method) should also declare the paramter type as the delegate your passing it:
              > >>
              > >> > [DllImport("pats api.dll")]
              > >> > public static extern int ptRegisterLinkS tateCallback(in t 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@www..jwaon line..com
              > >> -----------------------------------------------------------------------
              > >> "Wayne Weeks" <WayneWeeks@dis cussions.micros oft.com> wrote in message news:A05B408C-E89D-4B96-B8CD-C9F4753158F5@mi crosoft.com...
              > >> > 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 ptRegisterLinkS tateCallback_Cl ick(object sender, EventArgs e)
              > >> > {
              > >> > callbackEvent cb = new callbackEvent(M yCallback);
              > >> > status = ptAPI.ptRegiste rLinkStateCallb ack(ptHostLinkS tateChange, cb);
              > >> > MessageBox.Show ("Status = " + status.ToString ());
              > >> > }
              > >> >
              > >> > So when the "ptRegisterLink ...." button is clicked a pointer, cb, is created
              > >> > to MyCallback. The "ptAPI.ptRegist er..." 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("pats api.dll")]
              > >> > public static extern int ptRegisterLinkS tateCallback(in t callbackID, int
              > >> > mtdAddress);
              > >> > }
              > >> >
              > >> > The compiler complains that there are invalid arguments and that argument 2
              > >> > 'cannot convert from "...Form1.callb ackEvent 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(M yCallBack);
              > >> >>
              > >> >> // Use the method pointer in your interop call:
              > >> >> myAPIregister(c b);
              > >> >>
              > >> >> // > The parameter in myAPIregister(p arameter) 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@www..jwaon line..com
              > >> >> -----------------------------------------------------------------------
              > >> >> "Wayne Weeks" <Wayne Weeks@discussio ns.microsoft.co m> wrote in message
              > >> >> news:35A6F85C-A4DF-4F05-AED1-AC9667C96F4F@mi crosoft.com...
              > >> >> >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(E vent);
              > >> >> >
              > >> >> > So I am expecting that myAPIregister(E vent) 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(p arameter) 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.
              > >> >>
              > >> >>
              > >> >>
              > >>
              > >>
              > >>[/color][/color]
              >
              >
              >[/color]

              Comment

              Working...