S
Sid Price
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 "GetIPForwardTable" 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.AllocHGlobal(2)
Try
'
' First do a read, the buffer size is too small, this
' will return the needed size in the buffer size buffer
'
Result = GetIpForwardTable(lpDataBuffer, ByteCount, True)
'
' Free the unmanged data buffer
'
Marshal.FreeHGlobal(lpDataBuffer)
'
' Create a new buffer for the real data
'
lpDataBuffer = Marshal.AllocHGlobal(5000)
'
' Read the table
'
Result = GetIpForwardTable(lpDataBuffer, ByteCount, True)
Catch ex As Exception
MsgBox("OOPS!")
End Try
Thanks for any pointers,
Sid.
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 "GetIPForwardTable" 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.AllocHGlobal(2)
Try
'
' First do a read, the buffer size is too small, this
' will return the needed size in the buffer size buffer
'
Result = GetIpForwardTable(lpDataBuffer, ByteCount, True)
'
' Free the unmanged data buffer
'
Marshal.FreeHGlobal(lpDataBuffer)
'
' Create a new buffer for the real data
'
lpDataBuffer = Marshal.AllocHGlobal(5000)
'
' Read the table
'
Result = GetIpForwardTable(lpDataBuffer, ByteCount, True)
Catch ex As Exception
MsgBox("OOPS!")
End Try
Thanks for any pointers,
Sid.