Calling unmanaged DLL issue

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

Tom Shelton

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.

Sid,

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

Armin Zingler

Sid Price said:
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?


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



Armin
 
S

Sid Price

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

Here you go ...
Declare Function GetIpForwardTable Lib "iphlpapi.DLL" (ByRef IpForwardTable
As System.IntPtr, ByRef Size As Int32, ByVal Order As Boolean) As Int32

'

' Attempt to read the IP forward table

'

Dim lpDataBuffer As IntPtr

Dim BufferSize As Int32

Dim ByteCount As Int32 = Marshal.SizeOf(BufferSize)

Dim Result As Int32

'

' create unmanaged 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)

'

' Adjust the unmanaged data buffer

'

Marshal.ReAllocCoTaskMem(lpDataBuffer, ByteCount)



'

' Read the table

'

Result = GetIpForwardTable(lpDataBuffer, ByteCount, True)

Catch ex As Exception

MsgBox("OOPS!")

End Try

Sid.
 
A

Armin Zingler

Sid Price said:
Here you go ...
Declare Function GetIpForwardTable Lib "iphlpapi.DLL" (ByRef
IpForwardTable As System.IntPtr, ByRef Size As Int32, ByVal Order As
Boolean) As Int32

BYVAL IpForwardTable As System.IntPtr


Armin
 
S

Sid Price

Armin Zingler said:
BYVAL IpForwardTable As System.IntPtr


Armin
Thank you Armin, that resolved that issue. My next problem is that the table
being read has the following structure and I do not understand how to read
the unmanaged buffer data into my managed structures using "PtrToStructure".
Here is what I have following the reading of the table using
"GetIpForwardTable"; first I define the two structures and then the code I
have tried.
Public Structure MIB_IPFORWARDROW

Public ForwardDest As Int32

Public ForwardMask As Int32

Public ForwardPolicy As Int32

Public ForwardNextHop As Int32

Public ForwardIfIndex As Int32

Public ForwardType As Int32

Public ForwardProto As Int32

Public ForwardAge As Int32

Public ForwardNextHopAS As Int32

Public ForwardMetric1 As Int32

Public ForwardMetric2 As Int32

Public ForwardMetric3 As Int32

Public ForwardMetric4 As Int32

Public ForwardMetric5 As Int32

End Structure

<StructLayout(LayoutKind.Sequential)> _

Public Structure MIB_IPFORWARDTABLE

Public dwNumEntries As Int32

Public table() As MIB_IPFORWARDROW

End Structure

Declare Function GetIpForwardTable Lib "iphlpapi.DLL" (ByVal IpForwardTable
As System.IntPtr, ByRef Size As Int32, ByVal Order As Boolean) As Int32

Result = GetIpForwardTable(lpDataBuffer, ByteCount, True)

dim theTable as new MIB_IPFORWARDTABLE

theTable = Marshal.PtrToStructure(lpDataBuffer, theTable.GetType)

"theTable" gets a "dwNumEntries" value but the "table" element is zero.

Many thanks,

Sid.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top