Callback problem with cdecl dll!?

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

    #1

    Callback problem with cdecl dll!?

    Hi,

    I have to use a cdecl dll (3 party dll). One of the functions needs a
    callback as a parameter.
    Unfortunately it seems that I'm not able to solve this issue.
    What I have done is:

    Declare the callback as
    Public Delegate Sub CallbackFunctio n(ByVal s1 As structure1, ByVal s2 as
    Structure2)

    the real callback is defined as

    Private Sub myCallBackFunct ion(ByVal s1 As structure1, ByVal s2 as
    Structure2)

    the function which needs the callback as a parameter is declared as

    <DllImport("S.d ll", CharSet:=CharSe t.Auto,
    CallingConventi on:=CallingConv ention.Cdecl)> _

    Public Shared Sub Connect(ByVal s1 as structure1, ByVal callbackFunc As
    CallbackFunctio n, ByVal s2 as Structure2)

    And I call it

    Dim vbs1 As New Structure1

    Dim vbs2 As New Structure2

    Dim cbf As CallbackFunctio n

    cbf = AddressOf myCallBackFunct ion

    Try

    Connect(vbs1, cbf, vbs2)

    Catch ex As Exception

    end try

    But I always get the error Object Reference not set to an instance an object
    ..

    But when I look into the locals window I can't see that either a structure
    nor the callback has the value nothing.

    It looks ok - every variable seems to have the correct value.

    So what am I doing wrong?

    Thank you fro your help and sorry for my bad english

    Reinhard


  • Ken Tucker [MVP]

    #2
    Re: Callback problem with cdecl dll!?

    Hi,

    cbf = new callback(Addres sOf myCallBackFunct ion)


    Ken
    ---------------------------

    "ReinhardH" <reinhard@nospa m.de> wrote in message
    news:d8bfoi$264 $00$1@news.t-online.com...
    Hi,

    I have to use a cdecl dll (3 party dll). One of the functions needs a
    callback as a parameter.
    Unfortunately it seems that I'm not able to solve this issue.
    What I have done is:

    Declare the callback as
    Public Delegate Sub CallbackFunctio n(ByVal s1 As structure1, ByVal s2 as
    Structure2)

    the real callback is defined as

    Private Sub myCallBackFunct ion(ByVal s1 As structure1, ByVal s2 as
    Structure2)

    the function which needs the callback as a parameter is declared as

    <DllImport("S.d ll", CharSet:=CharSe t.Auto,
    CallingConventi on:=CallingConv ention.Cdecl)> _

    Public Shared Sub Connect(ByVal s1 as structure1, ByVal callbackFunc As
    CallbackFunctio n, ByVal s2 as Structure2)

    And I call it

    Dim vbs1 As New Structure1

    Dim vbs2 As New Structure2

    Dim cbf As CallbackFunctio n

    cbf = AddressOf myCallBackFunct ion

    Try

    Connect(vbs1, cbf, vbs2)

    Catch ex As Exception

    end try

    But I always get the error Object Reference not set to an instance an object
    ..

    But when I look into the locals window I can't see that either a structure
    nor the callback has the value nothing.

    It looks ok - every variable seems to have the correct value.

    So what am I doing wrong?

    Thank you fro your help and sorry for my bad english

    Reinhard



    Comment

    • ReinhardH

      #3
      Re: Callback problem with cdecl dll!?

      Hi Ken,

      thanks for your answer but still the same error.
      Another question

      Is there a difference between

      cbf = new callback(Addres sOf myCallBackFunct ion)

      and

      dim cbf as new callback
      cbf = AddressOf myCallBackFunct ion

      Thank you
      Reinhard


      Comment

      • Ken Tucker [MVP]

        #4
        Re: Callback problem with cdecl dll!?

        Hi,

        I prefer the method I posted but i believe both will work. In your
        post you left off the new keyword. Anyway with api calls you will get the
        object not set to reference of an object error when you are passing something
        byval when it should be byref. If you expect to get a value back from vbs1 or
        vbs2 pass them byref.

        I usually declare the variable for the callback function as a form
        level variable instead of in a procedure. I have found that vb.net forgets
        where the function is on the second callback because the procedure level
        callback variable is out of scope. You will get an object not set to a
        reference error on the form's class declaration. Hope that helps.

        Ken
        --------------------------

        "ReinhardH" wrote:
        [color=blue]
        > Hi Ken,
        >
        > thanks for your answer but still the same error.
        > Another question
        >
        > Is there a difference between
        >
        > cbf = new callback(Addres sOf myCallBackFunct ion)
        >
        > and
        >
        > dim cbf as new callback
        > cbf = AddressOf myCallBackFunct ion
        >
        > Thank you
        > Reinhard
        >
        >
        >[/color]

        Comment

        • Nick Hall

          #5
          Re: Callback problem with cdecl dll!?

          Reinhard,

          The problem is that CDecl callbacks are not supported in the current release
          (don't know whether this has changed in VS 2005).

          There is a workaround which involves modifying the IL generated for the
          Delegate definition; see
          http://www.dotnet247.com/247referenc.../17/87210.aspx for more details.

          Good luck,

          Nick Hall

          "ReinhardH" <reinhard@nospa m.de> wrote in message
          news:d8bfoi$264 $00$1@news.t-online.com...[color=blue]
          > Hi,
          >
          > I have to use a cdecl dll (3 party dll). One of the functions needs a
          > callback as a parameter.
          > Unfortunately it seems that I'm not able to solve this issue.
          > What I have done is:
          >
          > Declare the callback as
          > Public Delegate Sub CallbackFunctio n(ByVal s1 As structure1, ByVal s2 as
          > Structure2)
          >
          > the real callback is defined as
          >
          > Private Sub myCallBackFunct ion(ByVal s1 As structure1, ByVal s2 as
          > Structure2)
          >
          > the function which needs the callback as a parameter is declared as
          >
          > <DllImport("S.d ll", CharSet:=CharSe t.Auto,
          > CallingConventi on:=CallingConv ention.Cdecl)> _
          >
          > Public Shared Sub Connect(ByVal s1 as structure1, ByVal callbackFunc As
          > CallbackFunctio n, ByVal s2 as Structure2)
          >
          > And I call it
          >
          > Dim vbs1 As New Structure1
          >
          > Dim vbs2 As New Structure2
          >
          > Dim cbf As CallbackFunctio n
          >
          > cbf = AddressOf myCallBackFunct ion
          >
          > Try
          >
          > Connect(vbs1, cbf, vbs2)
          >
          > Catch ex As Exception
          >
          > end try
          >
          > But I always get the error Object Reference not set to an instance an
          > object .
          >
          > But when I look into the locals window I can't see that either a structure
          > nor the callback has the value nothing.
          >
          > It looks ok - every variable seems to have the correct value.
          >
          > So what am I doing wrong?
          >
          > Thank you fro your help and sorry for my bad english
          >
          > Reinhard
          >
          >[/color]


          Comment

          • ReinhardH

            #6
            Re: Callback problem with cdecl dll!?

            Hi Nick,

            thanks for this information I will give it a try.

            Reinhard
            "Nick Hall" <nickh@aslan.no spam.co.uk> schrieb im Newsbeitrag
            news:eCi5aFbbFH A.3404@TK2MSFTN GP10.phx.gbl...[color=blue]
            > Reinhard,
            >
            > The problem is that CDecl callbacks are not supported in the current
            > release (don't know whether this has changed in VS 2005).
            >
            > There is a workaround which involves modifying the IL generated for the
            > Delegate definition; see
            > http://www.dotnet247.com/247referenc.../17/87210.aspx for more details.
            >
            > Good luck,
            >
            > Nick Hall
            >
            > "ReinhardH" <reinhard@nospa m.de> wrote in message
            > news:d8bfoi$264 $00$1@news.t-online.com...[color=green]
            >> Hi,
            >>
            >> I have to use a cdecl dll (3 party dll). One of the functions needs a
            >> callback as a parameter.
            >> Unfortunately it seems that I'm not able to solve this issue.
            >> What I have done is:
            >>
            >> Declare the callback as
            >> Public Delegate Sub CallbackFunctio n(ByVal s1 As structure1, ByVal s2 as
            >> Structure2)
            >>
            >> the real callback is defined as
            >>
            >> Private Sub myCallBackFunct ion(ByVal s1 As structure1, ByVal s2 as
            >> Structure2)
            >>
            >> the function which needs the callback as a parameter is declared as
            >>
            >> <DllImport("S.d ll", CharSet:=CharSe t.Auto,
            >> CallingConventi on:=CallingConv ention.Cdecl)> _
            >>
            >> Public Shared Sub Connect(ByVal s1 as structure1, ByVal callbackFunc As
            >> CallbackFunctio n, ByVal s2 as Structure2)
            >>
            >> And I call it
            >>
            >> Dim vbs1 As New Structure1
            >>
            >> Dim vbs2 As New Structure2
            >>
            >> Dim cbf As CallbackFunctio n
            >>
            >> cbf = AddressOf myCallBackFunct ion
            >>
            >> Try
            >>
            >> Connect(vbs1, cbf, vbs2)
            >>
            >> Catch ex As Exception
            >>
            >> end try
            >>
            >> But I always get the error Object Reference not set to an instance an
            >> object .
            >>
            >> But when I look into the locals window I can't see that either a
            >> structure nor the callback has the value nothing.
            >>
            >> It looks ok - every variable seems to have the correct value.
            >>
            >> So what am I doing wrong?
            >>
            >> Thank you fro your help and sorry for my bad english
            >>
            >> Reinhard
            >>
            >>[/color]
            >
            >[/color]


            Comment

            • ReinhardH

              #7
              Re: Callback problem with cdecl dll!?

              Thank you.
              But I guess it could be the problem Nick metioned.

              Nevertheless thanks for your help and explanations.

              Reinhard
              "Ken Tucker [MVP]" <KenTuckerMVP@d iscussions.micr osoft.com> schrieb im
              Newsbeitrag news:2FFF4EBA-A1F3-4C7E-A8C0-B7C601916DD1@mi crosoft.com...[color=blue]
              > Hi,
              >
              > I prefer the method I posted but i believe both will work. In your
              > post you left off the new keyword. Anyway with api calls you will get the
              > object not set to reference of an object error when you are passing
              > something
              > byval when it should be byref. If you expect to get a value back from vbs1
              > or
              > vbs2 pass them byref.
              >
              > I usually declare the variable for the callback function as a form
              > level variable instead of in a procedure. I have found that vb.net
              > forgets
              > where the function is on the second callback because the procedure level
              > callback variable is out of scope. You will get an object not set to a
              > reference error on the form's class declaration. Hope that helps.
              >
              > Ken
              > --------------------------
              >
              > "ReinhardH" wrote:
              >[color=green]
              >> Hi Ken,
              >>
              >> thanks for your answer but still the same error.
              >> Another question
              >>
              >> Is there a difference between
              >>
              >> cbf = new callback(Addres sOf myCallBackFunct ion)
              >>
              >> and
              >>
              >> dim cbf as new callback
              >> cbf = AddressOf myCallBackFunct ion
              >>
              >> Thank you
              >> Reinhard
              >>
              >>
              >>[/color][/color]


              Comment

              Working...