Calling unmanaged DLL issue

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

    Calling unmanaged DLL issue

    Hello,
    I am trying to call an unmanaged API (IP helper) and having problems getting
    started. I have the following code to read an IP Forward table. It appears
    my first call to "GetIPForwardTa ble" appears to work, it returns the
    required buffer size in the ByteCount variable. However when I try the
    second call, the one that should actually read the table the application
    simply exits. This suggests a stack corruption or similar. Can anyone see
    what is wrong with the code?
    Dim lpDataBuffer As IntPtr

    Dim BufferSize As Int32

    Dim ByteCount As Int32 = Marshal.SizeOf( BufferSize)

    Dim Result As Int32

    '

    ' create unmanged data buffer that is too small for real data,

    ' this will force the method to return the actual byte count needed

    '

    lpDataBuffer = Marshal.AllocHG lobal(2)

    Try

    '

    ' First do a read, the buffer size is too small, this

    ' will return the needed size in the buffer size buffer

    '

    Result = GetIpForwardTab le(lpDataBuffer , ByteCount, True)

    '

    ' Free the unmanged data buffer

    '

    Marshal.FreeHGl obal(lpDataBuff er)

    '

    ' Create a new buffer for the real data

    '

    lpDataBuffer = Marshal.AllocHG lobal(5000)

    '

    ' Read the table

    '

    Result = GetIpForwardTab le(lpDataBuffer , ByteCount, True)

    Catch ex As Exception

    MsgBox("OOPS!")

    End Try



    Thanks for any pointers,

    Sid.


  • Tom Shelton

    #2
    Re: Calling unmanaged DLL issue

    On 2008-05-02, Sid Price <sid@nowhere.co mwrote:
    Hello,
    I am trying to call an unmanaged API (IP helper) and having problems getting
    started. I have the following code to read an IP Forward table. It appears
    my first call to "GetIPForwardTa ble" appears to work, it returns the
    required buffer size in the ByteCount variable. However when I try the
    second call, the one that should actually read the table the application
    simply exits. This suggests a stack corruption or similar. Can anyone see
    what is wrong with the code?
    Dim lpDataBuffer As IntPtr
    >
    Dim BufferSize As Int32
    >
    Dim ByteCount As Int32 = Marshal.SizeOf( BufferSize)
    >
    Dim Result As Int32
    >
    '
    >
    ' create unmanged data buffer that is too small for real data,
    >
    ' this will force the method to return the actual byte count needed
    >
    '
    >
    lpDataBuffer = Marshal.AllocHG lobal(2)
    >
    Try
    >
    '
    >
    ' First do a read, the buffer size is too small, this
    >
    ' will return the needed size in the buffer size buffer
    >
    '
    >
    Result = GetIpForwardTab le(lpDataBuffer , ByteCount, True)
    >
    '
    >
    ' Free the unmanged data buffer
    >
    '
    >
    Marshal.FreeHGl obal(lpDataBuff er)
    >
    '
    >
    ' Create a new buffer for the real data
    >
    '
    >
    lpDataBuffer = Marshal.AllocHG lobal(5000)
    >
    '
    >
    ' Read the table
    >
    '
    >
    Result = GetIpForwardTab le(lpDataBuffer , ByteCount, True)
    >
    Catch ex As Exception
    >
    MsgBox("OOPS!")
    >
    End Try
    >
    >
    >
    Thanks for any pointers,
    >
    Sid.
    >
    >
    Sid,

    It would really be nice to have a complete working example... That means
    it helps to see your declare statements :)

    --
    Tom Shelton

    Comment

    • Armin Zingler

      #3
      Re: Calling unmanaged DLL issue

      "Sid Price" <sid@nowhere.co mschrieb
      Hello,
      I am trying to call an unmanaged API (IP helper) and having problems
      getting started. I have the following code to read an IP Forward
      table. It appears my first call to "GetIPForwardTa ble" appears to
      work, it returns the required buffer size in the ByteCount variable.
      However when I try the second call, the one that should actually
      read the table the application simply exits. This suggests a stack
      corruption or similar. Can anyone see what is wrong with the code?

      Most important: What's your declaration of GetIPForwardTab le? The rest
      looks ok at first sight.



      Armin

      Comment

      Working...